From 7ada125299013979bf8ffdccd3898e9b5b117ded Mon Sep 17 00:00:00 2001 From: Alexandre Boulgakov Date: Mon, 15 May 2023 21:14:17 +0100 Subject: [PATCH] Swift: Support fmtlib for assertions/expectations. Specifically, this adds custom formatters using `path::operator string()` and `error_code::message()` and dereferences a (non-empty) optional. `fmtlib` provides formatters for these standard library types in `fmt/std.h`, but that file also requires RTTI (which we disable) for `std::exception` so we can't use it without either patching `fmtlib` (which they're open to: https://github.com/fmtlib/fmt/issues/3170) or enabling RTTI (which will require some consideration). --- .../extractor/translators/DeclTranslator.cpp | 2 +- swift/logging/Formatters.h | 20 +++++++++++++++++++ swift/logging/SwiftDiagnostics.h | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 swift/logging/Formatters.h diff --git a/swift/extractor/translators/DeclTranslator.cpp b/swift/extractor/translators/DeclTranslator.cpp index 06bc086bec1..2abfa820534 100644 --- a/swift/extractor/translators/DeclTranslator.cpp +++ b/swift/extractor/translators/DeclTranslator.cpp @@ -253,7 +253,7 @@ void DeclTranslator::fillFunction(const swift::AbstractFunctionDecl& decl, entry.name = !decl.hasName() ? "(unnamed function decl)" : constructName(decl.getName()); entry.body = dispatcher.fetchOptionalLabel(decl.getBody()); CODEQL_EXPECT_OR(return, decl.hasParameterList(), "Function {} has no parameter list", - entry.name); + *entry.name); entry.params = dispatcher.fetchRepeatedLabels(*decl.getParameters()); auto self = const_cast(decl.getImplicitSelfDecl()); entry.self_param = dispatcher.fetchOptionalLabel(self); diff --git a/swift/logging/Formatters.h b/swift/logging/Formatters.h new file mode 100644 index 00000000000..9dc003b9ffe --- /dev/null +++ b/swift/logging/Formatters.h @@ -0,0 +1,20 @@ +#pragma once + +// Provides formatters for standard library types to be used with fmtlib. +// TODO: Patch fmtlib to support using `fmt/std.h` without RTTI +// (https://github.com/fmtlib/fmt/issues/3170). + +#include +#include +#include + +namespace fmt { +FMT_FORMAT_AS(std::filesystem::path, std::string); +} + +template <> +struct fmt::formatter : fmt::formatter { + auto format(const std::error_code& e, format_context& ctx) const { + return fmt::formatter::format(e.message(), ctx); + } +}; diff --git a/swift/logging/SwiftDiagnostics.h b/swift/logging/SwiftDiagnostics.h index 5a0f5617076..e930a93e2b2 100644 --- a/swift/logging/SwiftDiagnostics.h +++ b/swift/logging/SwiftDiagnostics.h @@ -14,6 +14,8 @@ #include #include +#include "swift/logging/Formatters.h" + namespace codeql { extern const std::string_view programName;