Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 84 additions & 3 deletions include/StackUsageAnalyzer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string>
#include <vector>

#include "helpers.hpp"

namespace llvm
{
class Module;
Expand Down Expand Up @@ -45,19 +47,98 @@ struct FunctionResult
bool exceedsLimit = false; // maxStack > config.stackLimit
};

/*
DiagnosticSeverity EnumTraits specialization
*/

enum class LanguageType
{
Unknown = 0,
LLVM_IR = 1,
C = 2,
CXX = 3
};

template<>
struct EnumTraits<LanguageType>
{
static constexpr std::array<std::string_view, 4> names = {
"UNKNOWN",
"LLVM_IR",
"C",
"CXX"
};
};

/*
DiagnosticSeverity EnumTraits specialization
*/

enum class DiagnosticSeverity
{
Info = 0,
Warning = 1,
Error = 2
};

template<>
struct EnumTraits<DiagnosticSeverity>
{
static constexpr std::array<std::string_view, 3> names = {
"INFO",
"WARNING",
"ERROR"
};
};

/*
DescriptiveErrorCode EnumTraits specialization
*/

enum class DescriptiveErrorCode
{
None = 0,
StackBufferOverflow = 1,
NegativeStackIndex = 2,
VLAUsage = 3,
StackPointerEscape = 4,
MemcpyWithStackDest = 5,
MultipleStoresToStackBuffer = 6
};

template<>
struct EnumTraits<DescriptiveErrorCode>
{
static constexpr std::array<std::string_view, 7> names = {
"None",
"StackBufferOverflow",
"NegativeStackIndex",
"VLAUsage",
"StackPointerEscape",
"MemcpyWithStackDest",
"MultipleStoresToStackBuffer"
};
};

/*
Diagnostic struct
*/

struct Diagnostic
{
std::string funcName;
unsigned line = 0;
unsigned column = 0;
DiagnosticSeverity severity = DiagnosticSeverity::Warning;
unsigned line = 0;
unsigned column = 0;

// for SARIF / structured reporting
unsigned startLine = 0;
unsigned startColumn = 0;
unsigned endLine = 0;
unsigned endColumn = 0;

DiagnosticSeverity severity = DiagnosticSeverity::Warning;
DescriptiveErrorCode errCode = DescriptiveErrorCode::None;
std::vector<std::string> variableAliasingVec;
std::string message;
};

Expand Down
33 changes: 33 additions & 0 deletions include/helpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <array>
#include <string_view>
#include <type_traits>

namespace ctrace::stack
{

template<typename E>
struct EnumTraits; // pas de définition générique -> erreur si non spécialisé

template<typename E>
concept EnumWithTraits = std::is_enum_v<E> && requires {
EnumTraits<E>::names;
};

template<EnumWithTraits E>
constexpr std::string_view enumToString(E e) noexcept
{
using Traits = EnumTraits<E>;
using U = std::underlying_type_t<E>;

constexpr auto size = Traits::names.size();
const auto idx = static_cast<U>(e);

if (idx < 0 || static_cast<std::size_t>(idx) >= size)
return "Unknown";

return Traits::names[static_cast<std::size_t>(idx)];
}

} // namespace ctrace::stack
Loading
Loading