mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
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).
This commit is contained in:
@@ -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<swift::ParamDecl* const>(decl.getImplicitSelfDecl());
|
||||
entry.self_param = dispatcher.fetchOptionalLabel(self);
|
||||
|
||||
20
swift/logging/Formatters.h
Normal file
20
swift/logging/Formatters.h
Normal file
@@ -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 <filesystem>
|
||||
#include <fmt/format.h>
|
||||
#include <string_view>
|
||||
|
||||
namespace fmt {
|
||||
FMT_FORMAT_AS(std::filesystem::path, std::string);
|
||||
}
|
||||
|
||||
template <>
|
||||
struct fmt::formatter<std::error_code> : fmt::formatter<std::string> {
|
||||
auto format(const std::error_code& e, format_context& ctx) const {
|
||||
return fmt::formatter<std::string>::format(e.message(), ctx);
|
||||
}
|
||||
};
|
||||
@@ -14,6 +14,8 @@
|
||||
#include <fmt/chrono.h>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "swift/logging/Formatters.h"
|
||||
|
||||
namespace codeql {
|
||||
|
||||
extern const std::string_view programName;
|
||||
|
||||
Reference in New Issue
Block a user