mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Swift: turn internal error into a TSP warning
This commit is contained in:
@@ -8,6 +8,22 @@
|
||||
|
||||
namespace codeql {
|
||||
|
||||
namespace {
|
||||
std::string_view severityToString(SwiftDiagnostic::Severity severity) {
|
||||
using S = SwiftDiagnostic::Severity;
|
||||
switch (severity) {
|
||||
case S::note:
|
||||
return "note";
|
||||
case S::warning:
|
||||
return "warning";
|
||||
case S::error:
|
||||
return "error";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point& timestamp,
|
||||
std::string_view message) const {
|
||||
nlohmann::json ret{
|
||||
@@ -23,7 +39,7 @@ nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point
|
||||
{"cliSummaryTable", has(Visibility::cliSummaryTable)},
|
||||
{"telemetry", has(Visibility::telemetry)},
|
||||
}},
|
||||
{"severity", "error"},
|
||||
{"severity", severityToString(severity)},
|
||||
{"helpLinks", std::vector<std::string_view>(absl::StrSplit(helpLinks, ' '))},
|
||||
{format == Format::markdown ? "markdownMessage" : "plaintextMessage",
|
||||
absl::StrCat(message, ".\n\n", action)},
|
||||
|
||||
@@ -46,34 +46,43 @@ struct SwiftDiagnostic {
|
||||
all = 0b111,
|
||||
};
|
||||
|
||||
// Notice that Tool Status Page severity is not necessarily the same as log severity, as the
|
||||
// scope is different: TSP's scope is the whole analysis, log's scope is a single run
|
||||
enum class Severity {
|
||||
note,
|
||||
warning,
|
||||
error,
|
||||
};
|
||||
|
||||
static constexpr std::string_view extractorName = "swift";
|
||||
|
||||
std::string_view id;
|
||||
std::string_view name;
|
||||
static constexpr std::string_view extractorName = "swift";
|
||||
Format format;
|
||||
std::string_view action;
|
||||
|
||||
Format format{Format::markdown};
|
||||
Visibility visibility{Visibility::all};
|
||||
Severity severity{Severity::error};
|
||||
|
||||
// space separated if more than 1. Not a vector to allow constexpr
|
||||
// TODO(C++20) with vector going constexpr this can be turned to `std::vector<std::string_view>`
|
||||
std::string_view helpLinks;
|
||||
// for the moment, we only output errors, so no need to store the severity
|
||||
|
||||
Visibility visibility{Visibility::all};
|
||||
std::string_view helpLinks{""};
|
||||
|
||||
std::optional<SwiftDiagnosticsLocation> location{};
|
||||
|
||||
// notice help links are really required only for plaintext messages, otherwise they should be
|
||||
// directly embedded in the markdown message
|
||||
// optional arguments can be any of
|
||||
// * std::string_view for setting helpLinks
|
||||
// * Severity, Visibility or Format to set the corresponding field
|
||||
template <typename... OptionalArgs>
|
||||
constexpr SwiftDiagnostic(std::string_view id,
|
||||
std::string_view name,
|
||||
Format format,
|
||||
std::string_view action = "",
|
||||
std::string_view helpLinks = "",
|
||||
Visibility visibility = Visibility::all)
|
||||
: id{id},
|
||||
name{name},
|
||||
format{format},
|
||||
action{action},
|
||||
helpLinks{helpLinks},
|
||||
visibility{visibility} {}
|
||||
std::string_view action,
|
||||
OptionalArgs... optionalArgs)
|
||||
: id{id}, name{name}, action{action} {
|
||||
(setOptionalArg(optionalArgs), ...);
|
||||
}
|
||||
|
||||
// create a JSON diagnostics for this source with the given `timestamp` and `message`
|
||||
// Depending on format, either a plaintextMessage or markdownMessage is used that includes both
|
||||
@@ -97,6 +106,11 @@ struct SwiftDiagnostic {
|
||||
|
||||
private:
|
||||
bool has(Visibility v) const;
|
||||
|
||||
constexpr void setOptionalArg(std::string_view h) { helpLinks = h; }
|
||||
constexpr void setOptionalArg(Format f) { format = f; }
|
||||
constexpr void setOptionalArg(Visibility v) { visibility = v; }
|
||||
constexpr void setOptionalArg(Severity s) { severity = s; }
|
||||
};
|
||||
|
||||
inline constexpr SwiftDiagnostic::Visibility operator|(SwiftDiagnostic::Visibility lhs,
|
||||
@@ -114,9 +128,12 @@ inline constexpr SwiftDiagnostic::Visibility operator&(SwiftDiagnostic::Visibili
|
||||
constexpr SwiftDiagnostic internalError{
|
||||
"internal-error",
|
||||
"Internal error",
|
||||
SwiftDiagnostic::Format::plaintext,
|
||||
/* action=*/"",
|
||||
/* helpLinks=*/"",
|
||||
SwiftDiagnostic::Visibility::telemetry,
|
||||
"Some or all of the Swift analysis may have failed.\n"
|
||||
"\n"
|
||||
"If the error persists, contact support, quoting the error message and describing what "
|
||||
"happened, or [open an issue in our open source repository][1].\n"
|
||||
"\n"
|
||||
"[1]: https://github.com/github/codeql/issues/new?labels=bug&template=ql---general.md",
|
||||
SwiftDiagnostic::Severity::warning,
|
||||
};
|
||||
} // namespace codeql
|
||||
|
||||
Reference in New Issue
Block a user