Skip to content

Commit 8291b22

Browse files
committed
Swift: turn internal error into a TSP warning
1 parent e8423f8 commit 8291b22

File tree

5 files changed

+65
-34
lines changed

5 files changed

+65
-34
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: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,43 @@ 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+
static constexpr std::string_view extractorName = "swift";
58+
4959
std::string_view id;
5060
std::string_view name;
51-
static constexpr std::string_view extractorName = "swift";
52-
Format format;
5361
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
5862

63+
Format format{Format::markdown};
5964
Visibility visibility{Visibility::all};
65+
Severity severity{Severity::error};
66+
67+
// space separated if more than 1. Not a vector to allow constexpr
68+
// TODO(C++20) with vector going constexpr this can be turned to `std::vector<std::string_view>`
69+
std::string_view helpLinks{""};
6070

6171
std::optional<SwiftDiagnosticsLocation> location{};
6272

6373
// notice help links are really required only for plaintext messages, otherwise they should be
6474
// directly embedded in the markdown message
75+
// optional arguments can be any of
76+
// * std::string_view for setting helpLinks
77+
// * Severity, Visibility or Format to set the corresponding field
78+
template <typename... OptionalArgs>
6579
constexpr SwiftDiagnostic(std::string_view id,
6680
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} {}
81+
std::string_view action,
82+
OptionalArgs... optionalArgs)
83+
: id{id}, name{name}, action{action} {
84+
(setOptionalArg(optionalArgs), ...);
85+
}
7786

7887
// create a JSON diagnostics for this source with the given `timestamp` and `message`
7988
// Depending on format, either a plaintextMessage or markdownMessage is used that includes both
@@ -97,6 +106,11 @@ struct SwiftDiagnostic {
97106

98107
private:
99108
bool has(Visibility v) const;
109+
110+
constexpr void setOptionalArg(std::string_view h) { helpLinks = h; }
111+
constexpr void setOptionalArg(Format f) { format = f; }
112+
constexpr void setOptionalArg(Visibility v) { visibility = v; }
113+
constexpr void setOptionalArg(Severity s) { severity = s; }
100114
};
101115

102116
inline constexpr SwiftDiagnostic::Visibility operator|(SwiftDiagnostic::Visibility lhs,
@@ -114,9 +128,12 @@ inline constexpr SwiftDiagnostic::Visibility operator&(SwiftDiagnostic::Visibili
114128
constexpr SwiftDiagnostic internalError{
115129
"internal-error",
116130
"Internal error",
117-
SwiftDiagnostic::Format::plaintext,
118-
/* action=*/"",
119-
/* helpLinks=*/"",
120-
SwiftDiagnostic::Visibility::telemetry,
131+
"Some or all of the Swift analysis may have failed.\n"
132+
"\n"
133+
"If the error persists, contact support, quoting the error message and describing what "
134+
"happened, or [open an issue in our open source repository][1].\n"
135+
"\n"
136+
"[1]: https://github.com/github/codeql/issues/new?labels=bug&template=ql---general.md",
137+
SwiftDiagnostic::Severity::warning,
121138
};
122139
} // 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"};

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)