Skip to content

Commit e18ee79

Browse files
authored
Merge pull request #15322 from github/calumgrant/swift-diagnostics
Swift extractor: Generalise SwiftDiagnostics
2 parents 696a72a + 7cadb0a commit e18ee79

File tree

9 files changed

+44
-43
lines changed

9 files changed

+44
-43
lines changed

swift/extractor/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ using namespace std::string_literals;
2525
using namespace codeql::main_logger;
2626

2727
const std::string_view codeql::programName = "extractor";
28+
const std::string_view codeql::extractorName = "swift";
2829

2930
// must be called before processFrontendOptions modifies output paths
3031
static void lockOutputSwiftModuleTraps(codeql::SwiftExtractorState& state,

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: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
namespace codeql {
2020

2121
extern const std::string_view programName;
22+
extern const std::string_view extractorName;
2223

23-
struct SwiftDiagnosticsLocation {
24+
struct DiagnosticsLocation {
2425
std::string_view file;
2526
unsigned startLine;
2627
unsigned startColumn;
@@ -34,7 +35,8 @@ struct SwiftDiagnosticsLocation {
3435
// Models a diagnostic source for Swift, holding static information that goes out into a diagnostic
3536
// These are internally stored into a map on id's. A specific error log can use binlog's category
3637
// as id, which will then be used to recover the diagnostic source while dumping.
37-
struct SwiftDiagnostic {
38+
class Diagnostic {
39+
public:
3840
enum class Visibility : unsigned char {
3941
none = 0b000,
4042
statusPage = 0b001,
@@ -51,16 +53,14 @@ struct SwiftDiagnostic {
5153
error,
5254
};
5355

54-
static constexpr std::string_view extractorName = "swift";
55-
5656
std::string_view id;
5757
std::string_view name;
5858
std::string_view action;
5959

6060
Visibility visibility{Visibility::all};
6161
Severity severity{Severity::error};
6262

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

6565
// create a JSON diagnostics for this source with the given `timestamp` and Markdown `message`
6666
// A markdownMessage is emitted that includes both the message and the action to take. The id is
@@ -71,33 +71,33 @@ struct SwiftDiagnostic {
7171
// returns <id> or <id>@<location> if a location is present
7272
std::string abbreviation() const;
7373

74-
SwiftDiagnostic withLocation(std::string_view file,
75-
unsigned startLine = 0,
76-
unsigned startColumn = 0,
77-
unsigned endLine = 0,
78-
unsigned endColumn = 0) const {
74+
Diagnostic withLocation(std::string_view file,
75+
unsigned startLine = 0,
76+
unsigned startColumn = 0,
77+
unsigned endLine = 0,
78+
unsigned endColumn = 0) const {
7979
auto ret = *this;
80-
ret.location = SwiftDiagnosticsLocation{file, startLine, startColumn, endLine, endColumn};
80+
ret.location = DiagnosticsLocation{file, startLine, startColumn, endLine, endColumn};
8181
return ret;
8282
}
8383

8484
private:
8585
bool has(Visibility v) const;
8686
};
8787

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));
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));
9292
}
9393

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));
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));
9898
}
9999

100-
constexpr SwiftDiagnostic internalError{
100+
constexpr Diagnostic internalError{
101101
.id = "internal-error",
102102
.name = "Internal error",
103103
.action =
@@ -107,6 +107,5 @@ constexpr SwiftDiagnostic internalError{
107107
"happened, or [open an issue in our open source repository][1].\n"
108108
"\n"
109109
"[1]: https://github.com/github/codeql/issues/new?labels=bug&template=ql---general.md",
110-
.severity = SwiftDiagnostic::Severity::warning,
111-
};
110+
.severity = Diagnostic::Severity::warning};
112111
} // 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/logging/tests/assertion-diagnostics/AssertFalse.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "swift/logging/SwiftAssert.h"
22

33
const std::string_view codeql::programName = "test";
4+
const std::string_view codeql::extractorName = "swift";
45

56
static codeql::Logger& logger() {
67
static codeql::Logger ret{"main"};

swift/swift-autobuilder/BuildRunner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "swift/logging/SwiftLogging.h"
99
#include "swift/swift-autobuilder/CustomizingBuildLink.h"
1010

11-
constexpr codeql::SwiftDiagnostic buildCommandFailed{
11+
constexpr codeql::Diagnostic buildCommandFailed{
1212
.id = "build-command-failed",
1313
.name = "Detected build command failed",
1414
.action = "Set up a [manual build command][1] or [check the logs of the autobuild step][2].\n"

swift/swift-autobuilder/swift-autobuilder.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ static constexpr std::string_view unitTest = "com.apple.product-type.bundle.unit
1212
static constexpr std::string_view unknownType = "<unknown_target_type>";
1313

1414
const std::string_view codeql::programName = "autobuilder";
15+
const std::string_view codeql::extractorName = "swift";
1516

16-
constexpr codeql::SwiftDiagnostic noProjectFound{
17+
constexpr codeql::Diagnostic noProjectFound{
1718
.id = "no-project-found",
1819
.name = "No Xcode project or workspace found",
1920
.action = "Set up a [manual build command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK};
2021

21-
constexpr codeql::SwiftDiagnostic noSwiftTarget{
22+
constexpr codeql::Diagnostic noSwiftTarget{
2223
.id = "no-swift-target",
2324
.name = "No Swift compilation target found",
2425
.action = "To analyze a custom set of source files, set up a [manual build "

swift/tools/diagnostics/AutobuilderIncompatibleOs.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#include "swift/logging/SwiftLogging.h"
88

99
const std::string_view codeql::programName = "autobuilder";
10+
const std::string_view codeql::extractorName = "swift";
1011

11-
constexpr codeql::SwiftDiagnostic incompatibleOs{
12+
constexpr codeql::Diagnostic incompatibleOs{
1213
.id = "incompatible-os",
1314
.name = "Incompatible operating system (expected macOS)",
1415
.action =
@@ -22,9 +23,9 @@ constexpr codeql::SwiftDiagnostic incompatibleOs{
2223
"[2]: "
2324
"https://docs.github.com/en/enterprise-server/code-security/code-scanning/"
2425
"automatically-scanning-your-code-for-vulnerabilities-and-errors/"
25-
"configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-"
26-
"language",
27-
};
26+
"configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-"
27+
"compiled-"
28+
"language"};
2829

2930
static codeql::Logger& logger() {
3031
static codeql::Logger ret{"main"};

0 commit comments

Comments
 (0)