Skip to content

Commit 7ada125

Browse files
committed
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: fmtlib/fmt#3170) or enabling RTTI (which will require some consideration).
1 parent 9954542 commit 7ada125

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

swift/extractor/translators/DeclTranslator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void DeclTranslator::fillFunction(const swift::AbstractFunctionDecl& decl,
253253
entry.name = !decl.hasName() ? "(unnamed function decl)" : constructName(decl.getName());
254254
entry.body = dispatcher.fetchOptionalLabel(decl.getBody());
255255
CODEQL_EXPECT_OR(return, decl.hasParameterList(), "Function {} has no parameter list",
256-
entry.name);
256+
*entry.name);
257257
entry.params = dispatcher.fetchRepeatedLabels(*decl.getParameters());
258258
auto self = const_cast<swift::ParamDecl* const>(decl.getImplicitSelfDecl());
259259
entry.self_param = dispatcher.fetchOptionalLabel(self);

swift/logging/Formatters.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
// Provides formatters for standard library types to be used with fmtlib.
4+
// TODO: Patch fmtlib to support using `fmt/std.h` without RTTI
5+
// (https://github.com/fmtlib/fmt/issues/3170).
6+
7+
#include <filesystem>
8+
#include <fmt/format.h>
9+
#include <string_view>
10+
11+
namespace fmt {
12+
FMT_FORMAT_AS(std::filesystem::path, std::string);
13+
}
14+
15+
template <>
16+
struct fmt::formatter<std::error_code> : fmt::formatter<std::string> {
17+
auto format(const std::error_code& e, format_context& ctx) const {
18+
return fmt::formatter<std::string>::format(e.message(), ctx);
19+
}
20+
};

swift/logging/SwiftDiagnostics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <fmt/chrono.h>
1515
#include <nlohmann/json.hpp>
1616

17+
#include "swift/logging/Formatters.h"
18+
1719
namespace codeql {
1820

1921
extern const std::string_view programName;

0 commit comments

Comments
 (0)