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