Skip to content

Commit 4660a25

Browse files
authored
Merge pull request #15354 from github/calumgrant/shared-diagnostics
C++/Swift: Create shared library and share Diagnostics
2 parents 1746638 + d57fc3d commit 4660a25

File tree

6 files changed

+111
-96
lines changed

6 files changed

+111
-96
lines changed

shared/cpp/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cc_library(
2+
name = "extractor_shared",
3+
srcs = glob(["*.cpp"]),
4+
hdrs = glob(["*.h"]),
5+
visibility = ["//visibility:public"],
6+
deps = [
7+
"@absl//absl/strings",
8+
"@fmt",
9+
"@json",
10+
],
11+
)

swift/logging/SwiftDiagnostics.cpp renamed to shared/cpp/Diagnostics.cpp

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: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,9 @@
11
#pragma once
22

3-
#include <binlog/binlog.hpp>
4-
#include <string>
5-
#include <vector>
6-
#include <unordered_map>
7-
#include <optional>
8-
#include <cassert>
9-
#include <fstream>
10-
#include <filesystem>
11-
#include <sstream>
12-
#include <mutex>
13-
#include <fmt/format.h>
14-
#include <fmt/chrono.h>
15-
#include <nlohmann/json.hpp>
16-
17-
#include "swift/logging/Formatters.h"
3+
#include "shared/cpp/Diagnostics.h"
184

195
namespace codeql {
206

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-
1007
constexpr Diagnostic internalError{
1018
.id = "internal-error",
1029
.name = "Internal error",
@@ -108,4 +15,5 @@ constexpr Diagnostic internalError{
10815
"\n"
10916
"[1]: https://github.com/github/codeql/issues/new?labels=bug&template=ql---general.md",
11017
.severity = Diagnostic::Severity::warning};
18+
11119
} // namespace codeql

swift/logging/SwiftLogging.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#include <regex>
77
#include <vector>
88

9+
#include <fmt/format.h>
10+
#include <fmt/chrono.h>
11+
12+
#include "swift/logging/Formatters.h"
13+
914
#include <binlog/binlog.hpp>
1015
#include <binlog/TextOutputStream.hpp>
1116
#include <binlog/EventFilter.hpp>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy