From 838130ca3a50f4d6a321fb4566b44d63c1e6e8f6 Mon Sep 17 00:00:00 2001 From: Alexandre Boulgakov Date: Thu, 8 Jun 2023 13:11:14 +0100 Subject: [PATCH 1/3] Swift: Fix some C++20 todos. --- swift/extractor/trap/TrapLabel.h | 4 ++-- swift/logging/SwiftLogging.h | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/swift/extractor/trap/TrapLabel.h b/swift/extractor/trap/TrapLabel.h index fe8b18e127ec..e8b374faff0e 100644 --- a/swift/extractor/trap/TrapLabel.h +++ b/swift/extractor/trap/TrapLabel.h @@ -1,11 +1,11 @@ #pragma once +#include #include #include #include #include #include -#include "absl/numeric/bits.h" #include #include #include @@ -52,7 +52,7 @@ class UntypedTrapLabel { size_t strSize() const { if (id_ == 0) return 2; // #0 // Number of hex digits is ceil(bit_width(id) / 4), but C++ integer division can only do floor. - return /* # */ 1 + /* hex digits */ 1 + (absl::bit_width(id_) - 1) / 4; + return /* # */ 1 + /* hex digits */ 1 + (std::bit_width(id_) - 1) / 4; } friend bool operator!=(UntypedTrapLabel lhs, UntypedTrapLabel rhs) { return lhs.id_ != rhs.id_; } diff --git a/swift/logging/SwiftLogging.h b/swift/logging/SwiftLogging.h index ce0d4cd0c9e1..a577863aced7 100644 --- a/swift/logging/SwiftLogging.h +++ b/swift/logging/SwiftLogging.h @@ -55,15 +55,14 @@ #define DIAGNOSE_CRITICAL(ID, ...) DIAGNOSE_WITH_LEVEL(critical, ID, __VA_ARGS__) #define CODEQL_DIAGNOSTIC_LOG_FORMAT_PREFIX "[{}] " -// TODO(C++20) replace non-standard , ##__VA_ARGS__ with __VA_OPT__(,) __VA_ARGS__ #define DIAGNOSE_WITH_LEVEL(LEVEL, ID, FORMAT, ...) \ do { \ auto _now = ::binlog::clockNow(); \ const ::codeql::SwiftDiagnostic& _id = ID; \ ::codeql::Log::diagnose(_id, std::chrono::nanoseconds{_now}, \ - fmt::format(FORMAT, ##__VA_ARGS__)); \ + fmt::format(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \ LOG_WITH_LEVEL_AND_TIME(LEVEL, _now, CODEQL_DIAGNOSTIC_LOG_FORMAT_PREFIX FORMAT, \ - _id.abbreviation(), ##__VA_ARGS__); \ + _id.abbreviation() __VA_OPT__(, ) __VA_ARGS__); \ } while (false) // avoid calling into binlog's original macros From 5450585c1c3d8f151fe408d43db8c5e806d79fe3 Mon Sep 17 00:00:00 2001 From: Alexandre Boulgakov Date: Thu, 8 Jun 2023 13:11:14 +0100 Subject: [PATCH 2/3] Swift: Remove SwiftDiagnostic constructor (C++20 todo). --- swift/logging/SwiftDiagnostics.h | 30 +++++++------------ .../IncompatibleOs.cpp | 29 +++++++++--------- swift/xcode-autobuilder/XcodeBuildRunner.cpp | 7 +++-- swift/xcode-autobuilder/xcode-autobuilder.cpp | 23 +++++++------- 4 files changed, 41 insertions(+), 48 deletions(-) diff --git a/swift/logging/SwiftDiagnostics.h b/swift/logging/SwiftDiagnostics.h index eb54421fd2be..e945e11b8e6d 100644 --- a/swift/logging/SwiftDiagnostics.h +++ b/swift/logging/SwiftDiagnostics.h @@ -62,17 +62,6 @@ struct SwiftDiagnostic { std::optional location{}; - // optional arguments can be either Severity or Visibility to set the corresponding field. - // TODO(C++20) this constructor won't really be necessary anymore with designated initializers - template - constexpr SwiftDiagnostic(std::string_view id, - std::string_view name, - 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 Markdown `message` // A markdownMessage is emitted that includes both the message and the action to take. The id is // used to construct the source id in the form `swift//` @@ -116,14 +105,15 @@ inline constexpr SwiftDiagnostic::Visibility operator&(SwiftDiagnostic::Visibili } constexpr SwiftDiagnostic internalError{ - "internal-error", - "Internal error", - "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, + .id = "internal-error", + .name = "Internal error", + .action = + "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", + .severity = SwiftDiagnostic::Severity::warning, }; } // namespace codeql diff --git a/swift/tools/autobuilder-diagnostics/IncompatibleOs.cpp b/swift/tools/autobuilder-diagnostics/IncompatibleOs.cpp index 2920a00f9559..0bab7afbf69a 100644 --- a/swift/tools/autobuilder-diagnostics/IncompatibleOs.cpp +++ b/swift/tools/autobuilder-diagnostics/IncompatibleOs.cpp @@ -9,20 +9,21 @@ const std::string_view codeql::programName = "autobuilder"; constexpr codeql::SwiftDiagnostic incompatibleOs{ - "incompatible-os", - "Incompatible operating system (expected macOS)", - "[Change the Actions runner][1] to run on macOS.\n" - "\n" - "You may be able to run analysis on Linux by setting up a [manual build command][2].\n" - "\n" - "[1]: " - "https://docs.github.com/en/actions/using-workflows/" - "workflow-syntax-for-github-actions#jobsjob_idruns-on\n" - "[2]: " - "https://docs.github.com/en/enterprise-server/code-security/code-scanning/" - "automatically-scanning-your-code-for-vulnerabilities-and-errors/" - "configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-" - "language", + .id = "incompatible-os", + .name = "Incompatible operating system (expected macOS)", + .action = + "[Change the Actions runner][1] to run on macOS.\n" + "\n" + "You may be able to run analysis on Linux by setting up a [manual build command][2].\n" + "\n" + "[1]: " + "https://docs.github.com/en/actions/using-workflows/" + "workflow-syntax-for-github-actions#jobsjob_idruns-on\n" + "[2]: " + "https://docs.github.com/en/enterprise-server/code-security/code-scanning/" + "automatically-scanning-your-code-for-vulnerabilities-and-errors/" + "configuring-the-codeql-workflow-for-compiled-languages#adding-build-steps-for-a-compiled-" + "language", }; static codeql::Logger& logger() { diff --git a/swift/xcode-autobuilder/XcodeBuildRunner.cpp b/swift/xcode-autobuilder/XcodeBuildRunner.cpp index eaf5e7c03551..567f726ad896 100644 --- a/swift/xcode-autobuilder/XcodeBuildRunner.cpp +++ b/swift/xcode-autobuilder/XcodeBuildRunner.cpp @@ -9,9 +9,10 @@ #include "swift/xcode-autobuilder/CustomizingBuildLink.h" constexpr codeql::SwiftDiagnostic buildCommandFailed{ - "build-command-failed", "Detected build command failed", - "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}; + .id = "build-command-failed", + .name = "Detected build command failed", + .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}; static codeql::Logger& logger() { static codeql::Logger ret{"build"}; diff --git a/swift/xcode-autobuilder/xcode-autobuilder.cpp b/swift/xcode-autobuilder/xcode-autobuilder.cpp index 4cc234de66b7..fa10a674edd2 100644 --- a/swift/xcode-autobuilder/xcode-autobuilder.cpp +++ b/swift/xcode-autobuilder/xcode-autobuilder.cpp @@ -13,21 +13,22 @@ static constexpr std::string_view unknownType = ""; const std::string_view codeql::programName = "autobuilder"; -constexpr codeql::SwiftDiagnostic noProjectFound{"no-project-found", - "No Xcode project or workspace found", - "Set up a [manual build command][1].\n" - "\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK}; +constexpr codeql::SwiftDiagnostic noProjectFound{ + .id = "no-project-found", + .name = "No Xcode project or workspace found", + .action = "Set up a [manual build command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK}; constexpr codeql::SwiftDiagnostic noSwiftTarget{ - "no-swift-target", "No Swift compilation target found", - "To analyze a custom set of source files, set up a [manual build command][1].\n" - "\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK}; + .id = "no-swift-target", + .name = "No Swift compilation target found", + .action = "To analyze a custom set of source files, set up a [manual build " + "command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK}; constexpr codeql::SwiftDiagnostic spmNotSupported{ - "spm-not-supported", "Swift Package Manager is not supported", - "Swift Package Manager builds are not currently supported by `autobuild`. Set up a [manual " - "build command][1].\n" - "\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK}; + .id = "spm-not-supported", + .name = "Swift Package Manager is not supported", + .action = "Swift Package Manager builds are not currently supported by `autobuild`. Set up a " + "[manual build command][1].\n\n[1]: " MANUAL_BUILD_COMMAND_HELP_LINK}; static codeql::Logger& logger() { static codeql::Logger ret{"main"}; From 9ec09000e5026d790d27381ad98032d05331a4c6 Mon Sep 17 00:00:00 2001 From: Alexandre Boulgakov Date: Thu, 8 Jun 2023 13:11:14 +0100 Subject: [PATCH 3/3] Swift: Remove no longer needed code. --- swift/extractor/trap/BUILD.bazel | 1 - swift/logging/SwiftDiagnostics.h | 7 ------- 2 files changed, 8 deletions(-) diff --git a/swift/extractor/trap/BUILD.bazel b/swift/extractor/trap/BUILD.bazel index f156aa9c9845..f68e0551d647 100644 --- a/swift/extractor/trap/BUILD.bazel +++ b/swift/extractor/trap/BUILD.bazel @@ -53,6 +53,5 @@ swift_cc_library( deps = [ "//swift/extractor/infra/file", "//swift/logging", - "@absl//absl/numeric:bits", ], ) diff --git a/swift/logging/SwiftDiagnostics.h b/swift/logging/SwiftDiagnostics.h index e945e11b8e6d..ecec68793aa9 100644 --- a/swift/logging/SwiftDiagnostics.h +++ b/swift/logging/SwiftDiagnostics.h @@ -83,13 +83,6 @@ struct SwiftDiagnostic { private: bool has(Visibility v) const; - - constexpr void setOptionalArg(Visibility v) { visibility = v; } - constexpr void setOptionalArg(Severity s) { severity = s; } - - // intentionally left undefined - template - constexpr void setOptionalArg(T); }; inline constexpr SwiftDiagnostic::Visibility operator|(SwiftDiagnostic::Visibility lhs,