Swift extractor: Generalize SwiftDiagnostics

This commit is contained in:
Calum Grant
2024-01-15 09:12:26 +00:00
parent 8b464fbc4a
commit f82c29ee37
6 changed files with 67 additions and 58 deletions

View File

@@ -3,14 +3,12 @@
#include <binlog/Entries.hpp> #include <binlog/Entries.hpp>
#include "absl/strings/str_join.h" #include "absl/strings/str_join.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "swift/logging/SwiftAssert.h"
namespace codeql { namespace codeql {
namespace { namespace {
std::string_view severityToString(SwiftDiagnostic::Severity severity) { std::string_view severityToString(Diagnostic::Severity severity) {
using S = SwiftDiagnostic::Severity; using S = Diagnostic::Severity;
switch (severity) { switch (severity) {
case S::note: case S::note:
return "note"; return "note";
@@ -24,8 +22,8 @@ std::string_view severityToString(SwiftDiagnostic::Severity severity) {
} }
} // namespace } // namespace
nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point& timestamp, nlohmann::json Diagnostic::json(const std::chrono::system_clock::time_point& timestamp,
std::string_view message) const { std::string_view message) const {
nlohmann::json ret{ nlohmann::json ret{
{"source", {"source",
{ {
@@ -49,18 +47,18 @@ nlohmann::json SwiftDiagnostic::json(const std::chrono::system_clock::time_point
return ret; return ret;
} }
std::string SwiftDiagnostic::abbreviation() const { std::string Diagnostic::abbreviation() const {
if (location) { if (location) {
return absl::StrCat(id, "@", location->str()); return absl::StrCat(id, "@", location->str());
} }
return std::string{id}; return std::string{id};
} }
bool SwiftDiagnostic::has(SwiftDiagnostic::Visibility v) const { bool Diagnostic::has(Visibility v) const {
return (visibility & v) != Visibility::none; return (visibility & v) != Visibility::none;
} }
nlohmann::json SwiftDiagnosticsLocation::json() const { nlohmann::json DiagnosticsLocation::json() const {
nlohmann::json ret{{"file", file}}; nlohmann::json ret{{"file", file}};
if (startLine) ret["startLine"] = startLine; if (startLine) ret["startLine"] = startLine;
if (startColumn) ret["startColumn"] = startColumn; if (startColumn) ret["startColumn"] = startColumn;
@@ -69,7 +67,7 @@ nlohmann::json SwiftDiagnosticsLocation::json() const {
return ret; return ret;
} }
std::string SwiftDiagnosticsLocation::str() const { std::string DiagnosticsLocation::str() const {
return absl::StrJoin(std::tuple{file, startLine, startColumn, endLine, endColumn}, ":"); return absl::StrJoin(std::tuple{file, startLine, startColumn, endLine, endColumn}, ":");
} }
} // namespace codeql } // namespace codeql

View File

@@ -20,7 +20,7 @@ namespace codeql {
extern const std::string_view programName; extern const std::string_view programName;
struct SwiftDiagnosticsLocation { struct DiagnosticsLocation {
std::string_view file; std::string_view file;
unsigned startLine; unsigned startLine;
unsigned startColumn; unsigned startColumn;
@@ -34,7 +34,8 @@ struct SwiftDiagnosticsLocation {
// Models a diagnostic source for Swift, holding static information that goes out into a diagnostic // Models a diagnostic source for Swift, holding static information that goes out into a diagnostic
// These are internally stored into a map on id's. A specific error log can use binlog's category // These are internally stored into a map on id's. A specific error log can use binlog's category
// as id, which will then be used to recover the diagnostic source while dumping. // as id, which will then be used to recover the diagnostic source while dumping.
struct SwiftDiagnostic { class Diagnostic {
public:
enum class Visibility : unsigned char { enum class Visibility : unsigned char {
none = 0b000, none = 0b000,
statusPage = 0b001, statusPage = 0b001,
@@ -51,7 +52,14 @@ struct SwiftDiagnostic {
error, error,
}; };
static constexpr std::string_view extractorName = "swift"; constexpr Diagnostic(std::string_view extractorName,
std::string_view id,
std::string_view name,
std::string_view action,
Severity severity)
: extractorName(extractorName), id(id), name(name), action(action), severity(severity) {}
std::string_view extractorName;
std::string_view id; std::string_view id;
std::string_view name; std::string_view name;
@@ -60,7 +68,7 @@ struct SwiftDiagnostic {
Visibility visibility{Visibility::all}; Visibility visibility{Visibility::all};
Severity severity{Severity::error}; Severity severity{Severity::error};
std::optional<SwiftDiagnosticsLocation> location{}; std::optional<DiagnosticsLocation> location{};
// create a JSON diagnostics for this source with the given `timestamp` and Markdown `message` // create a JSON diagnostics for this source with the given `timestamp` and Markdown `message`
// A markdownMessage is emitted that includes both the message and the action to take. The id is // A markdownMessage is emitted that includes both the message and the action to take. The id is
@@ -71,13 +79,13 @@ struct SwiftDiagnostic {
// returns <id> or <id>@<location> if a location is present // returns <id> or <id>@<location> if a location is present
std::string abbreviation() const; std::string abbreviation() const;
SwiftDiagnostic withLocation(std::string_view file, Diagnostic withLocation(std::string_view file,
unsigned startLine = 0, unsigned startLine = 0,
unsigned startColumn = 0, unsigned startColumn = 0,
unsigned endLine = 0, unsigned endLine = 0,
unsigned endColumn = 0) const { unsigned endColumn = 0) const {
auto ret = *this; auto ret = *this;
ret.location = SwiftDiagnosticsLocation{file, startLine, startColumn, endLine, endColumn}; ret.location = DiagnosticsLocation{file, startLine, startColumn, endLine, endColumn};
return ret; return ret;
} }
@@ -85,28 +93,32 @@ struct SwiftDiagnostic {
bool has(Visibility v) const; bool has(Visibility v) const;
}; };
inline constexpr SwiftDiagnostic::Visibility operator|(SwiftDiagnostic::Visibility lhs, inline constexpr Diagnostic::Visibility operator|(Diagnostic::Visibility lhs,
SwiftDiagnostic::Visibility rhs) { Diagnostic::Visibility rhs) {
return static_cast<SwiftDiagnostic::Visibility>(static_cast<unsigned char>(lhs) | return static_cast<Diagnostic::Visibility>(static_cast<unsigned char>(lhs) |
static_cast<unsigned char>(rhs)); static_cast<unsigned char>(rhs));
} }
inline constexpr SwiftDiagnostic::Visibility operator&(SwiftDiagnostic::Visibility lhs, inline constexpr Diagnostic::Visibility operator&(Diagnostic::Visibility lhs,
SwiftDiagnostic::Visibility rhs) { Diagnostic::Visibility rhs) {
return static_cast<SwiftDiagnostic::Visibility>(static_cast<unsigned char>(lhs) & return static_cast<Diagnostic::Visibility>(static_cast<unsigned char>(lhs) &
static_cast<unsigned char>(rhs)); static_cast<unsigned char>(rhs));
} }
constexpr SwiftDiagnostic internalError{ constexpr Diagnostic swiftDiagnostic(std::string_view id,
.id = "internal-error", std::string_view name,
.name = "Internal error", std::string_view action,
.action = Diagnostic::Severity severity = Diagnostic::Severity::error) {
"Some or all of the Swift analysis may have failed.\n" return Diagnostic("swift", id, name, action, severity);
"\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" constexpr Diagnostic internalError = swiftDiagnostic(
"\n" "internal-error", "Internal error",
"[1]: https://github.com/github/codeql/issues/new?labels=bug&template=ql---general.md", "Some or all of the Swift analysis may have failed.\n"
.severity = SwiftDiagnostic::Severity::warning, "\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",
Diagnostic::Severity::warning);
} // namespace codeql } // namespace codeql

View File

@@ -173,7 +173,7 @@ void Log::flushImpl() {
} }
} }
void Log::diagnoseImpl(const SwiftDiagnostic& source, void Log::diagnoseImpl(const Diagnostic& source,
const std::chrono::nanoseconds& elapsed, const std::chrono::nanoseconds& elapsed,
std::string_view message) { std::string_view message) {
using Clock = std::chrono::system_clock; using Clock = std::chrono::system_clock;

View File

@@ -58,7 +58,7 @@
#define DIAGNOSE_WITH_LEVEL(LEVEL, ID, FORMAT, ...) \ #define DIAGNOSE_WITH_LEVEL(LEVEL, ID, FORMAT, ...) \
do { \ do { \
auto _now = ::binlog::clockNow(); \ auto _now = ::binlog::clockNow(); \
const ::codeql::SwiftDiagnostic& _id = ID; \ const ::codeql::Diagnostic& _id = ID; \
::codeql::Log::diagnose(_id, std::chrono::nanoseconds{_now}, \ ::codeql::Log::diagnose(_id, std::chrono::nanoseconds{_now}, \
fmt::format(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \ fmt::format(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \
LOG_WITH_LEVEL_AND_TIME(LEVEL, _now, CODEQL_DIAGNOSTIC_LOG_FORMAT_PREFIX FORMAT, \ LOG_WITH_LEVEL_AND_TIME(LEVEL, _now, CODEQL_DIAGNOSTIC_LOG_FORMAT_PREFIX FORMAT, \
@@ -132,7 +132,7 @@ class Log {
return instance().getLoggerConfigurationImpl(name); return instance().getLoggerConfigurationImpl(name);
} }
static void diagnose(const SwiftDiagnostic& source, static void diagnose(const Diagnostic& source,
const std::chrono::nanoseconds& elapsed, const std::chrono::nanoseconds& elapsed,
std::string_view message) { std::string_view message) {
instance().diagnoseImpl(source, elapsed, message); instance().diagnoseImpl(source, elapsed, message);
@@ -153,7 +153,7 @@ class Log {
void configure(); void configure();
void flushImpl(); void flushImpl();
void diagnoseImpl(const SwiftDiagnostic& source, void diagnoseImpl(const Diagnostic& source,
const std::chrono::nanoseconds& elapsed, const std::chrono::nanoseconds& elapsed,
std::string_view message); std::string_view message);

View File

@@ -8,11 +8,10 @@
#include "swift/logging/SwiftLogging.h" #include "swift/logging/SwiftLogging.h"
#include "swift/swift-autobuilder/CustomizingBuildLink.h" #include "swift/swift-autobuilder/CustomizingBuildLink.h"
constexpr codeql::SwiftDiagnostic buildCommandFailed{ constexpr codeql::Diagnostic buildCommandFailed = codeql::swiftDiagnostic(
.id = "build-command-failed", "build-command-failed", "Detected build command failed",
.name = "Detected build command failed", "Set up a [manual build command][1] or [check the logs of the autobuild step][2].\n"
.action = "Set up a [manual build command][1] or [check the logs of the autobuild step][2].\n" "\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK "\n[2]: " CHECK_LOGS_HELP_LINK);
"\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK "\n[2]: " CHECK_LOGS_HELP_LINK};
static codeql::Logger& logger() { static codeql::Logger& logger() {
static codeql::Logger ret{"build"}; static codeql::Logger ret{"build"};

View File

@@ -13,16 +13,16 @@ static constexpr std::string_view unknownType = "<unknown_target_type>";
const std::string_view codeql::programName = "autobuilder"; const std::string_view codeql::programName = "autobuilder";
constexpr codeql::SwiftDiagnostic noProjectFound{ constexpr codeql::Diagnostic noProjectFound = codeql::swiftDiagnostic(
.id = "no-project-found", "no-project-found",
.name = "No Xcode project or workspace found", "No Xcode project or workspace found",
.action = "Set up a [manual build command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK}; "Set up a [manual build command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK);
constexpr codeql::SwiftDiagnostic noSwiftTarget{ constexpr codeql::Diagnostic noSwiftTarget = codeql::swiftDiagnostic(
.id = "no-swift-target", "no-swift-target",
.name = "No Swift compilation target found", "No Swift compilation target found",
.action = "To analyze a custom set of source files, set up a [manual build " "To analyze a custom set of source files, set up a [manual build "
"command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK}; "command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK);
static codeql::Logger& logger() { static codeql::Logger& logger() {
static codeql::Logger ret{"main"}; static codeql::Logger ret{"main"};