Skip to content

Commit b15ac0d

Browse files
authored
Merge pull request #5 from CoreTrace/feat/update-json-api
Feat/update json api
2 parents b978cb8 + 2340fb2 commit b15ac0d

File tree

4 files changed

+236
-61
lines changed

4 files changed

+236
-61
lines changed

include/StackUsageAnalyzer.hpp

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <string>
77
#include <vector>
88

9+
#include "helpers.hpp"
10+
911
namespace llvm
1012
{
1113
class Module;
@@ -45,19 +47,98 @@ struct FunctionResult
4547
bool exceedsLimit = false; // maxStack > config.stackLimit
4648
};
4749

50+
/*
51+
DiagnosticSeverity EnumTraits specialization
52+
*/
53+
54+
enum class LanguageType
55+
{
56+
Unknown = 0,
57+
LLVM_IR = 1,
58+
C = 2,
59+
CXX = 3
60+
};
61+
62+
template<>
63+
struct EnumTraits<LanguageType>
64+
{
65+
static constexpr std::array<std::string_view, 4> names = {
66+
"UNKNOWN",
67+
"LLVM_IR",
68+
"C",
69+
"CXX"
70+
};
71+
};
72+
73+
/*
74+
DiagnosticSeverity EnumTraits specialization
75+
*/
76+
4877
enum class DiagnosticSeverity
4978
{
5079
Info = 0,
5180
Warning = 1,
5281
Error = 2
5382
};
5483

84+
template<>
85+
struct EnumTraits<DiagnosticSeverity>
86+
{
87+
static constexpr std::array<std::string_view, 3> names = {
88+
"INFO",
89+
"WARNING",
90+
"ERROR"
91+
};
92+
};
93+
94+
/*
95+
DescriptiveErrorCode EnumTraits specialization
96+
*/
97+
98+
enum class DescriptiveErrorCode
99+
{
100+
None = 0,
101+
StackBufferOverflow = 1,
102+
NegativeStackIndex = 2,
103+
VLAUsage = 3,
104+
StackPointerEscape = 4,
105+
MemcpyWithStackDest = 5,
106+
MultipleStoresToStackBuffer = 6
107+
};
108+
109+
template<>
110+
struct EnumTraits<DescriptiveErrorCode>
111+
{
112+
static constexpr std::array<std::string_view, 7> names = {
113+
"None",
114+
"StackBufferOverflow",
115+
"NegativeStackIndex",
116+
"VLAUsage",
117+
"StackPointerEscape",
118+
"MemcpyWithStackDest",
119+
"MultipleStoresToStackBuffer"
120+
};
121+
};
122+
123+
/*
124+
Diagnostic struct
125+
*/
126+
55127
struct Diagnostic
56128
{
57129
std::string funcName;
58-
unsigned line = 0;
59-
unsigned column = 0;
60-
DiagnosticSeverity severity = DiagnosticSeverity::Warning;
130+
unsigned line = 0;
131+
unsigned column = 0;
132+
133+
// for SARIF / structured reporting
134+
unsigned startLine = 0;
135+
unsigned startColumn = 0;
136+
unsigned endLine = 0;
137+
unsigned endColumn = 0;
138+
139+
DiagnosticSeverity severity = DiagnosticSeverity::Warning;
140+
DescriptiveErrorCode errCode = DescriptiveErrorCode::None;
141+
std::vector<std::string> variableAliasingVec;
61142
std::string message;
62143
};
63144

include/helpers.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <array>
4+
#include <string_view>
5+
#include <type_traits>
6+
7+
namespace ctrace::stack
8+
{
9+
10+
template<typename E>
11+
struct EnumTraits; // pas de définition générique -> erreur si non spécialisé
12+
13+
template<typename E>
14+
concept EnumWithTraits = std::is_enum_v<E> && requires {
15+
EnumTraits<E>::names;
16+
};
17+
18+
template<EnumWithTraits E>
19+
constexpr std::string_view enumToString(E e) noexcept
20+
{
21+
using Traits = EnumTraits<E>;
22+
using U = std::underlying_type_t<E>;
23+
24+
constexpr auto size = Traits::names.size();
25+
const auto idx = static_cast<U>(e);
26+
27+
if (idx < 0 || static_cast<std::size_t>(idx) >= size)
28+
return "Unknown";
29+
30+
return Traits::names[static_cast<std::size_t>(idx)];
31+
}
32+
33+
} // namespace ctrace::stack

0 commit comments

Comments
 (0)