Skip to content

Commit f82c29e

Browse files
committed
Swift extractor: Generalize SwiftDiagnostics
1 parent 8b464fb commit f82c29e

File tree

6 files changed

+67
-58
lines changed

6 files changed

+67
-58
lines changed

swift/logging/SwiftDiagnostics.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
#include <binlog/Entries.hpp>
44
#include "absl/strings/str_join.h"
55
#include "absl/strings/str_cat.h"
6-
#include "absl/strings/str_split.h"
7-
#include "swift/logging/SwiftAssert.h"
86

97
namespace codeql {
108

119
namespace {
12-
std::string_view severityToString(SwiftDiagnostic::Severity severity) {
13-
using S = SwiftDiagnostic::Severity;
10+
std::string_view severityToString(Diagnostic::Severity severity) {
11+
using S = Diagnostic::Severity;
1412
switch (severity) {
1513
case S::note:
1614
return "note";
@@ -24,8 +22,8 @@ std::string_view severityToString(SwiftDiagnostic::Severity severity) {
2422
}
2523
} // namespace
2624

27-
nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point& timestamp,
28-
std::string_view message) const {
25+
nlohmann::json Diagnostic::json(const std::chrono::system_clock::time_point& timestamp,
26+
std::string_view message) const {
2927
nlohmann::json ret{
3028
{"source",
3129
{
@@ -49,18 +47,18 @@ nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point
4947
return ret;
5048
}
5149

52-
std::string SwiftDiagnostic::abbreviation() const {
50+
std::string Diagnostic::abbreviation() const {
5351
if (location) {
5452
return absl::StrCat(id, "@", location->str());
5553
}
5654
return std::string{id};
5755
}
5856

59-
bool SwiftDiagnostic::has(SwiftDiagnostic::Visibility v) const {
57+
bool Diagnostic::has(Visibility v) const {
6058
return (visibility & v) != Visibility::none;
6159
}
6260

63-
nlohmann::json SwiftDiagnosticsLocation::json() const {
61+
nlohmann::json DiagnosticsLocation::json() const {
6462
nlohmann::json ret{{"file", file}};
6563
if (startLine) ret["startLine"] = startLine;
6664
if (startColumn) ret["startColumn"] = startColumn;
@@ -69,7 +67,7 @@ nlohmann::json SwiftDiagnosticsLocation::json() const {
6967
return ret;
7068
}
7169

72-
std::string SwiftDiagnosticsLocation::str() const {
70+
std::string DiagnosticsLocation::str() const {
7371
return absl::StrJoin(std::tuple{file, startLine, startColumn, endLine, endColumn}, ":");
7472
}
7573
} // namespace codeql

swift/logging/SwiftDiagnostics.h

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace codeql {
2020

2121
extern const std::string_view programName;
2222

23-
struct SwiftDiagnosticsLocation {
23+
struct DiagnosticsLocation {
2424
std::string_view file;
2525
unsigned startLine;
2626
unsigned startColumn;
@@ -34,7 +34,8 @@ struct SwiftDiagnosticsLocation {
3434
// Models a diagnostic source for Swift, holding static information that goes out into a diagnostic
3535
// These are internally stored into a map on id's. A specific error log can use binlog's category
3636
// as id, which will then be used to recover the diagnostic source while dumping.
37-
struct SwiftDiagnostic {
37+
class Diagnostic {
38+
public:
3839
enum class Visibility : unsigned char {
3940
none = 0b000,
4041
statusPage = 0b001,
@@ -51,7 +52,14 @@ struct SwiftDiagnostic {
5152
error,
5253
};
5354

54-
static constexpr std::string_view extractorName = "swift";
55+
constexpr Diagnostic(std::string_view extractorName,
56+
std::string_view id,
57+
std::string_view name,
58+
std::string_view action,
59+
Severity severity)
60+
: extractorName(extractorName), id(id), name(name), action(action), severity(severity) {}
61+
62+
std::string_view extractorName;
5563

5664
std::string_view id;
5765
std::string_view name;
@@ -60,7 +68,7 @@ struct SwiftDiagnostic {
6068
Visibility visibility{Visibility::all};
6169
Severity severity{Severity::error};
6270

63-
std::optional<SwiftDiagnosticsLocation> location{};
71+
std::optional<DiagnosticsLocation> location{};
6472

6573
// create a JSON diagnostics for this source with the given `timestamp` and Markdown `message`
6674
// A markdownMessage is emitted that includes both the message and the action to take. The id is
@@ -71,42 +79,46 @@ struct SwiftDiagnostic {
7179
// returns <id> or <id>@<location> if a location is present
7280
std::string abbreviation() const;
7381

74-
SwiftDiagnostic withLocation(std::string_view file,
75-
unsigned startLine = 0,
76-
unsigned startColumn = 0,
77-
unsigned endLine = 0,
78-
unsigned endColumn = 0) const {
82+
Diagnostic withLocation(std::string_view file,
83+
unsigned startLine = 0,
84+
unsigned startColumn = 0,
85+
unsigned endLine = 0,
86+
unsigned endColumn = 0) const {
7987
auto ret = *this;
80-
ret.location = SwiftDiagnosticsLocation{file, startLine, startColumn, endLine, endColumn};
88+
ret.location = DiagnosticsLocation{file, startLine, startColumn, endLine, endColumn};
8189
return ret;
8290
}
8391

8492
private:
8593
bool has(Visibility v) const;
8694
};
8795

88-
inline constexpr SwiftDiagnostic::Visibility operator|(SwiftDiagnostic::Visibility lhs,
89-
SwiftDiagnostic::Visibility rhs) {
90-
return static_cast<SwiftDiagnostic::Visibility>(static_cast<unsigned char>(lhs) |
91-
static_cast<unsigned char>(rhs));
96+
inline constexpr Diagnostic::Visibility operator|(Diagnostic::Visibility lhs,
97+
Diagnostic::Visibility rhs) {
98+
return static_cast<Diagnostic::Visibility>(static_cast<unsigned char>(lhs) |
99+
static_cast<unsigned char>(rhs));
92100
}
93101

94-
inline constexpr SwiftDiagnostic::Visibility operator&(SwiftDiagnostic::Visibility lhs,
95-
SwiftDiagnostic::Visibility rhs) {
96-
return static_cast<SwiftDiagnostic::Visibility>(static_cast<unsigned char>(lhs) &
97-
static_cast<unsigned char>(rhs));
102+
inline constexpr Diagnostic::Visibility operator&(Diagnostic::Visibility lhs,
103+
Diagnostic::Visibility rhs) {
104+
return static_cast<Diagnostic::Visibility>(static_cast<unsigned char>(lhs) &
105+
static_cast<unsigned char>(rhs));
98106
}
99107

100-
constexpr SwiftDiagnostic internalError{
101-
.id = "internal-error",
102-
.name = "Internal error",
103-
.action =
104-
"Some or all of the Swift analysis may have failed.\n"
105-
"\n"
106-
"If the error persists, contact support, quoting the error message and describing what "
107-
"happened, or [open an issue in our open source repository][1].\n"
108-
"\n"
109-
"[1]: https://github.com/github/codeql/issues/new?labels=bug&template=ql---general.md",
110-
.severity = SwiftDiagnostic::Severity::warning,
111-
};
108+
constexpr Diagnostic swiftDiagnostic(std::string_view id,
109+
std::string_view name,
110+
std::string_view action,
111+
Diagnostic::Severity severity = Diagnostic::Severity::error) {
112+
return Diagnostic("swift", id, name, action, severity);
113+
}
114+
115+
constexpr Diagnostic internalError = swiftDiagnostic(
116+
"internal-error", "Internal error",
117+
"Some or all of the Swift analysis may have failed.\n"
118+
"\n"
119+
"If the error persists, contact support, quoting the error message and describing what "
120+
"happened, or [open an issue in our open source repository][1].\n"
121+
"\n"
122+
"[1]: https://github.com/github/codeql/issues/new?labels=bug&template=ql---general.md",
123+
Diagnostic::Severity::warning);
112124
} // namespace codeql

swift/logging/SwiftLogging.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void Log::flushImpl() {
173173
}
174174
}
175175

176-
void Log::diagnoseImpl(const SwiftDiagnostic& source,
176+
void Log::diagnoseImpl(const Diagnostic& source,
177177
const std::chrono::nanoseconds& elapsed,
178178
std::string_view message) {
179179
using Clock = std::chrono::system_clock;

swift/logging/SwiftLogging.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#define DIAGNOSE_WITH_LEVEL(LEVEL, ID, FORMAT, ...) \
5959
do { \
6060
auto _now = ::binlog::clockNow(); \
61-
const ::codeql::SwiftDiagnostic& _id = ID; \
61+
const ::codeql::Diagnostic& _id = ID; \
6262
::codeql::Log::diagnose(_id, std::chrono::nanoseconds{_now}, \
6363
fmt::format(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \
6464
LOG_WITH_LEVEL_AND_TIME(LEVEL, _now, CODEQL_DIAGNOSTIC_LOG_FORMAT_PREFIX FORMAT, \
@@ -132,7 +132,7 @@ class Log {
132132
return instance().getLoggerConfigurationImpl(name);
133133
}
134134

135-
static void diagnose(const SwiftDiagnostic& source,
135+
static void diagnose(const Diagnostic& source,
136136
const std::chrono::nanoseconds& elapsed,
137137
std::string_view message) {
138138
instance().diagnoseImpl(source, elapsed, message);
@@ -153,7 +153,7 @@ class Log {
153153

154154
void configure();
155155
void flushImpl();
156-
void diagnoseImpl(const SwiftDiagnostic& source,
156+
void diagnoseImpl(const Diagnostic& source,
157157
const std::chrono::nanoseconds& elapsed,
158158
std::string_view message);
159159

swift/swift-autobuilder/BuildRunner.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
#include "swift/logging/SwiftLogging.h"
99
#include "swift/swift-autobuilder/CustomizingBuildLink.h"
1010

11-
constexpr codeql::SwiftDiagnostic buildCommandFailed{
12-
.id = "build-command-failed",
13-
.name = "Detected build command failed",
14-
.action = "Set up a [manual build command][1] or [check the logs of the autobuild step][2].\n"
15-
"\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK "\n[2]: " CHECK_LOGS_HELP_LINK};
11+
constexpr codeql::Diagnostic buildCommandFailed = codeql::swiftDiagnostic(
12+
"build-command-failed", "Detected build command failed",
13+
"Set up a [manual build command][1] or [check the logs of the autobuild step][2].\n"
14+
"\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK "\n[2]: " CHECK_LOGS_HELP_LINK);
1615

1716
static codeql::Logger& logger() {
1817
static codeql::Logger ret{"build"};

swift/swift-autobuilder/swift-autobuilder.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ static constexpr std::string_view unknownType = "<unknown_target_type>";
1313

1414
const std::string_view codeql::programName = "autobuilder";
1515

16-
constexpr codeql::SwiftDiagnostic noProjectFound{
17-
.id = "no-project-found",
18-
.name = "No Xcode project or workspace found",
19-
.action = "Set up a [manual build command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK};
16+
constexpr codeql::Diagnostic noProjectFound = codeql::swiftDiagnostic(
17+
"no-project-found",
18+
"No Xcode project or workspace found",
19+
"Set up a [manual build command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK);
2020

21-
constexpr codeql::SwiftDiagnostic noSwiftTarget{
22-
.id = "no-swift-target",
23-
.name = "No Swift compilation target found",
24-
.action = "To analyze a custom set of source files, set up a [manual build "
25-
"command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK};
21+
constexpr codeql::Diagnostic noSwiftTarget = codeql::swiftDiagnostic(
22+
"no-swift-target",
23+
"No Swift compilation target found",
24+
"To analyze a custom set of source files, set up a [manual build "
25+
"command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK);
2626

2727
static codeql::Logger& logger() {
2828
static codeql::Logger ret{"main"};

0 commit comments

Comments
 (0)