Skip to content

Commit fc9fe13

Browse files
authored
Merge pull request github#13181 from github/redsun82/swift-diagnostics-enable-warnings
Swift: turn internal error into a TSP warning
2 parents ed79113 + 7e61e99 commit fc9fe13

File tree

6 files changed

+77
-36
lines changed

6 files changed

+77
-36
lines changed

swift/logging/SwiftDiagnostics.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@
88

99
namespace codeql {
1010

11+
namespace {
12+
std::string_view severityToString(SwiftDiagnostic::Severity severity) {
13+
using S = SwiftDiagnostic::Severity;
14+
switch (severity) {
15+
case S::note:
16+
return "note";
17+
case S::warning:
18+
return "warning";
19+
case S::error:
20+
return "error";
21+
default:
22+
return "unknown";
23+
}
24+
}
25+
} // namespace
26+
1127
nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point& timestamp,
1228
std::string_view message) const {
1329
nlohmann::json ret{
@@ -23,7 +39,7 @@ nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point
2339
{"cliSummaryTable", has(Visibility::cliSummaryTable)},
2440
{"telemetry", has(Visibility::telemetry)},
2541
}},
26-
{"severity", "error"},
42+
{"severity", severityToString(severity)},
2743
{"helpLinks", std::vector<std::string_view>(absl::StrSplit(helpLinks, ' '))},
2844
{format == Format::markdown ? "markdownMessage" : "plaintextMessage",
2945
absl::StrCat(message, ".\n\n", action)},

swift/logging/SwiftDiagnostics.h

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,48 @@ struct SwiftDiagnostic {
4646
all = 0b111,
4747
};
4848

49+
// Notice that Tool Status Page severity is not necessarily the same as log severity, as the
50+
// scope is different: TSP's scope is the whole analysis, log's scope is a single run
51+
enum class Severity {
52+
note,
53+
warning,
54+
error,
55+
};
56+
57+
// wrapper for passing optional help links to constructor
58+
struct HelpLinks {
59+
std::string_view value;
60+
};
61+
62+
static constexpr std::string_view extractorName = "swift";
63+
4964
std::string_view id;
5065
std::string_view name;
51-
static constexpr std::string_view extractorName = "swift";
52-
Format format;
5366
std::string_view action;
54-
// space separated if more than 1. Not a vector to allow constexpr
55-
// TODO(C++20) with vector going constexpr this can be turned to `std::vector<std::string_view>`
56-
std::string_view helpLinks;
57-
// for the moment, we only output errors, so no need to store the severity
5867

68+
Format format{Format::markdown};
5969
Visibility visibility{Visibility::all};
70+
Severity severity{Severity::error};
71+
72+
// space separated if more than 1. Not a vector to allow constexpr
73+
// TODO(C++20) with vector going constexpr this can be turned to `std::vector<std::string_view>`
74+
std::string_view helpLinks{""};
6075

6176
std::optional<SwiftDiagnosticsLocation> location{};
6277

6378
// notice help links are really required only for plaintext messages, otherwise they should be
6479
// directly embedded in the markdown message
80+
// optional arguments can be any of HelpLinks, Severity, Visibility or Format to set the
81+
// corresponding field
82+
// TODO(C++20) this constructor won't really be necessary anymore with designated initializers
83+
template <typename... OptionalArgs>
6584
constexpr SwiftDiagnostic(std::string_view id,
6685
std::string_view name,
67-
Format format,
68-
std::string_view action = "",
69-
std::string_view helpLinks = "",
70-
Visibility visibility = Visibility::all)
71-
: id{id},
72-
name{name},
73-
format{format},
74-
action{action},
75-
helpLinks{helpLinks},
76-
visibility{visibility} {}
86+
std::string_view action,
87+
OptionalArgs... optionalArgs)
88+
: id{id}, name{name}, action{action} {
89+
(setOptionalArg(optionalArgs), ...);
90+
}
7791

7892
// create a JSON diagnostics for this source with the given `timestamp` and `message`
7993
// Depending on format, either a plaintextMessage or markdownMessage is used that includes both
@@ -97,6 +111,15 @@ struct SwiftDiagnostic {
97111

98112
private:
99113
bool has(Visibility v) const;
114+
115+
constexpr void setOptionalArg(HelpLinks h) { helpLinks = h.value; }
116+
constexpr void setOptionalArg(Format f) { format = f; }
117+
constexpr void setOptionalArg(Visibility v) { visibility = v; }
118+
constexpr void setOptionalArg(Severity s) { severity = s; }
119+
120+
// intentionally left undefined
121+
template <typename T>
122+
constexpr void setOptionalArg(T);
100123
};
101124

102125
inline constexpr SwiftDiagnostic::Visibility operator|(SwiftDiagnostic::Visibility lhs,
@@ -114,9 +137,12 @@ inline constexpr SwiftDiagnostic::Visibility operator&(SwiftDiagnostic::Visibili
114137
constexpr SwiftDiagnostic internalError{
115138
"internal-error",
116139
"Internal error",
117-
SwiftDiagnostic::Format::plaintext,
118-
/* action=*/"",
119-
/* helpLinks=*/"",
120-
SwiftDiagnostic::Visibility::telemetry,
140+
"Some or all of the Swift analysis may have failed.\n"
141+
"\n"
142+
"If the error persists, contact support, quoting the error message and describing what "
143+
"happened, or [open an issue in our open source repository][1].\n"
144+
"\n"
145+
"[1]: https://github.com/github/codeql/issues/new?labels=bug&template=ql---general.md",
146+
SwiftDiagnostic::Severity::warning,
121147
};
122148
} // namespace codeql

swift/tools/autobuilder-diagnostics/IncompatibleOs.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
const std::string_view codeql::programName = "autobuilder";
1010

1111
constexpr codeql::SwiftDiagnostic incompatibleOs{
12-
"incompatible-os", "Incompatible operating system (expected macOS)",
13-
codeql::SwiftDiagnostic::Format::markdown,
12+
"incompatible-os",
13+
"Incompatible operating system (expected macOS)",
1414
"[Change the Actions runner][1] to run on macOS.\n"
1515
"\n"
1616
"You may be able to run analysis on Linux by setting up a [manual build command][2].\n"
@@ -22,7 +22,8 @@ constexpr codeql::SwiftDiagnostic incompatibleOs{
2222
"https://docs.github.com/en/enterprise-server/code-security/code-scanning/"
2323
"automatically-scanning-your-code-for-vulnerabilities-and-errors/"
2424
"configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-"
25-
"language"};
25+
"language",
26+
};
2627

2728
static codeql::Logger& logger() {
2829
static codeql::Logger ret{"main"};
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include <string_view>
2+
#include "swift/logging/SwiftDiagnostics.h"
23

34
namespace codeql {
45
constexpr std::string_view customizingBuildAction = "Set up a manual build command.";
5-
constexpr std::string_view customizingBuildHelpLinks =
6+
constexpr SwiftDiagnostic::HelpLinks customizingBuildHelpLinks{
67
"https://docs.github.com/en/enterprise-server/code-security/code-scanning/"
78
"automatically-scanning-your-code-for-vulnerabilities-and-errors/customizing-code-scanning "
89
"https://docs.github.com/en/enterprise-server/code-security/code-scanning/"
910
"automatically-scanning-your-code-for-vulnerabilities-and-errors/"
1011
"configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-"
11-
"language";
12+
"language"};
1213
} // namespace codeql

swift/xcode-autobuilder/XcodeBuildRunner.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#include "swift/xcode-autobuilder/CustomizingBuildDiagnostics.h"
1010

1111
constexpr codeql::SwiftDiagnostic buildCommandFailed{
12-
"build-command-failed", "Detected build command failed",
13-
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildAction,
14-
codeql::customizingBuildHelpLinks};
12+
"build-command-failed", "Detected build command failed", codeql::customizingBuildAction,
13+
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildHelpLinks};
1514

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

swift/xcode-autobuilder/xcode-autobuilder.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ static const char* unitTest = "com.apple.product-type.bundle.unit-test";
1313
const std::string_view codeql::programName = "autobuilder";
1414

1515
constexpr codeql::SwiftDiagnostic noProjectFound{
16-
"no-project-found", "No Xcode project or workspace detected",
17-
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildAction,
18-
codeql::customizingBuildHelpLinks};
16+
"no-project-found", "No Xcode project or workspace detected", codeql::customizingBuildAction,
17+
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildHelpLinks};
1918

2019
constexpr codeql::SwiftDiagnostic noSwiftTarget{
21-
"no-swift-target", "No Swift compilation target found",
22-
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildAction,
23-
codeql::customizingBuildHelpLinks};
20+
"no-swift-target", "No Swift compilation target found", codeql::customizingBuildAction,
21+
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildHelpLinks};
2422

2523
constexpr codeql::SwiftDiagnostic spmNotSupported{
2624
"spm-not-supported", "Swift Package Manager build unsupported by autobuild",
27-
codeql::SwiftDiagnostic::Format::plaintext, codeql::customizingBuildAction,
25+
codeql::customizingBuildAction, codeql::SwiftDiagnostic::Format::plaintext,
2826
codeql::customizingBuildHelpLinks};
2927

3028
static codeql::Logger& logger() {

0 commit comments

Comments
 (0)