Create shared/cpp library and move Diagnostics there · gfs/codeql@51c5aff · GitHub
Skip to content

Commit 51c5aff

Browse files
committed
Create shared/cpp library and move Diagnostics there
1 parent b7a7963 commit 51c5aff

5 files changed

Lines changed: 106 additions & 82 deletions

File tree

‎shared/cpp/BUILD.bazel‎

Lines changed: 12 additions & 0 deletions
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
#include "swift/logging/SwiftDiagnostics.h"
1+
#include "Diagnostics.h"
2+
3+
#include <fmt/format.h>
4+
#include <fmt/chrono.h>
25

3-
#include <binlog/Entries.hpp>
46
#include "absl/strings/str_join.h"
57
#include "absl/strings/str_cat.h"
68

‎shared/cpp/Diagnostics.h‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#pragma once
2+
3+
#include <optional>
4+
#include <string_view>
5+
6+
#include <nlohmann/json.hpp>
7+
8+
namespace codeql {
9+
10+
extern const std::string_view programName;
11+
extern const std::string_view extractorName;
12+
13+
struct DiagnosticsLocation {
14+
std::string_view file;
15+
unsigned startLine;
16+
unsigned startColumn;
17+
unsigned endLine;
18+
unsigned endColumn;
19+
20+
nlohmann::json json() const;
21+
std::string str() const;
22+
};
23+
24+
// Models a diagnostic source for Swift, holding static information that goes out into a diagnostic
25+
// These are internally stored into a map on id's. A specific error log can use binlog's category
26+
// as id, which will then be used to recover the diagnostic source while dumping.
27+
class Diagnostic {
28+
public:
29+
enum class Visibility : unsigned char {
30+
none = 0b000,
31+
statusPage = 0b001,
32+
cliSummaryTable = 0b010,
33+
telemetry = 0b100,
34+
all = 0b111,
35+
};
36+
37+
// Notice that Tool Status Page severity is not necessarily the same as log severity, as the
38+
// scope is different: TSP's scope is the whole analysis, log's scope is a single run
39+
enum class Severity {
40+
note,
41+
warning,
42+
error,
43+
};
44+
45+
std::string_view id;
46+
std::string_view name;
47+
std::string_view action;
48+
49+
Visibility visibility{Visibility::all};
50+
Severity severity{Severity::error};
51+
52+
std::optional<DiagnosticsLocation> location{};
53+
54+
// create a JSON diagnostics for this source with the given `timestamp` and Markdown `message`
55+
// A markdownMessage is emitted that includes both the message and the action to take. The id is
56+
// used to construct the source id in the form `swift/<prog name>/<id>`
57+
nlohmann::json json(const std::chrono::system_clock::time_point& timestamp,
58+
std::string_view message) const;
59+
60+
// returns <id> or <id>@<location> if a location is present
61+
std::string abbreviation() const;
62+
63+
Diagnostic withLocation(std::string_view file,
64+
unsigned startLine = 0,
65+
unsigned startColumn = 0,
66+
unsigned endLine = 0,
67+
unsigned endColumn = 0) const {
68+
auto ret = *this;
69+
ret.location = DiagnosticsLocation{file, startLine, startColumn, endLine, endColumn};
70+
return ret;
71+
}
72+
73+
private:
74+
bool has(Visibility v) const;
75+
};
76+
77+
inline constexpr Diagnostic::Visibility operator|(Diagnostic::Visibility lhs,
78+
Diagnostic::Visibility rhs) {
79+
return static_cast<Diagnostic::Visibility>(static_cast<unsigned char>(lhs) |
80+
static_cast<unsigned char>(rhs));
81+
}
82+
83+
inline constexpr Diagnostic::Visibility operator&(Diagnostic::Visibility lhs,
84+
Diagnostic::Visibility rhs) {
85+
return static_cast<Diagnostic::Visibility>(static_cast<unsigned char>(lhs) &
86+
static_cast<unsigned char>(rhs));
87+
}
88+
}

‎swift/logging/BUILD.bazel‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ cc_library(
88
"@binlog",
99
"@fmt",
1010
"@json",
11+
"//shared/cpp:extractor_shared",
1112
],
1213
)

‎swift/logging/SwiftDiagnostics.h‎

Lines changed: 1 addition & 80 deletions

0 commit comments

Comments
 (0)