Skip to content

Commit fa8a133

Browse files
authored
Merge pull request #23264 from github/jketema/fmt
Swift/Shared: Use C++23 `format` instead of `fmt`
2 parents 7d4054b + 7c3ca69 commit fa8a133

6 files changed

Lines changed: 37 additions & 13 deletions

File tree

‎MODULE.bazel‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ bazel_dep(name = "rules_shell", version = "0.10.0")
2525
bazel_dep(name = "bazel_skylib", version = "1.9.2")
2626
bazel_dep(name = "abseil-cpp", version = "20260817.0", repo_name = "absl")
2727
bazel_dep(name = "nlohmann_json", version = "3.12.0.bcr.2", repo_name = "json")
28-
bazel_dep(name = "fmt", version = "12.2.0.bcr.1-codeql.1")
2928
bazel_dep(name = "rules_kotlin", version = "2.4.21-codeql.1")
3029
bazel_dep(name = "gazelle", version = "0.54.0")
3130
bazel_dep(name = "rules_dotnet")

‎shared/cpp/BUILD.bazel‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ cc_library(
77
visibility = ["//visibility:public"],
88
deps = [
99
"@absl//absl/strings",
10-
"@fmt",
1110
"@json",
1211
],
1312
)

‎shared/cpp/Diagnostics.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "Diagnostics.h"
22

3-
#include <fmt/format.h>
4-
#include <fmt/chrono.h>
3+
#include <format>
54

65
#include "absl/strings/str_join.h"
76
#include "absl/strings/str_cat.h"
@@ -41,7 +40,7 @@ nlohmann::json Diagnostic::json(const std::chrono::system_clock::time_point& tim
4140
}},
4241
{"severity", severityToString(severity)},
4342
{"markdownMessage", absl::StrCat(message, "\n\n", action)},
44-
{"timestamp", fmt::format("{:%FT%T%z}", timestamp)},
43+
{"timestamp", std::format("{:%FT%T%z}", timestamp)},
4544
};
4645
if (location) {
4746
ret["location"] = location->json();

‎shared/cpp/Diagnostics.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <chrono>
34
#include <optional>
45
#include <string_view>
56

‎swift/logging/BUILD.bazel‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ cc_library(
99
"//shared/cpp:extractor_shared",
1010
"@absl//absl/strings",
1111
"@binlog",
12-
"@fmt",
1312
"@json",
1413
],
1514
)

‎swift/logging/SwiftLogging.h‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#pragma once
22

33
#include <chrono>
4+
#include <filesystem>
5+
#include <format>
46
#include <fstream>
57
#include <iostream>
68
#include <regex>
9+
#include <string>
10+
#include <system_error>
11+
#include <utility>
712
#include <vector>
813

9-
#include <fmt/format.h>
10-
#include <fmt/chrono.h>
11-
#include <fmt/std.h>
12-
1314
#include <binlog/binlog.hpp>
1415
#include <binlog/TextOutputStream.hpp>
1516
#include <binlog/EventFilter.hpp>
@@ -20,6 +21,31 @@
2021

2122
#include "swift/logging/SwiftDiagnostics.h"
2223

24+
namespace codeql::detail {
25+
26+
template <typename T>
27+
const T& diagnosticFormatArg(const T& value) {
28+
return value;
29+
}
30+
31+
// C++23 has no formatters for paths or error codes.
32+
inline std::string diagnosticFormatArg(const std::filesystem::path& value) {
33+
return value.string();
34+
}
35+
36+
inline std::string diagnosticFormatArg(const std::error_code& value) {
37+
return std::format("{}:{}", value.category().name(), value.value());
38+
}
39+
40+
template <typename... Args>
41+
std::string formatDiagnostic(
42+
std::format_string<decltype(diagnosticFormatArg(std::declval<const Args&>()))...> format,
43+
const Args&... args) {
44+
return std::format(format, diagnosticFormatArg(args)...);
45+
}
46+
47+
} // namespace codeql::detail
48+
2349
// Logging macros. These will call `logger()` to get a Logger instance, picking up any `logger`
2450
// defined in the current scope. Domain-specific loggers can be added or used by either:
2551
// * providing a class field called `logger` (as `Logger::operator()()` returns itself)
@@ -52,7 +78,7 @@
5278

5379
// Emit errors with a specified SwiftDiagnostic object. These will be both logged and outputted as
5480
// JSON DB diagnostics. The format must be appliable to the following arguments both as binlog and
55-
// as fmt::format formatting.
81+
// as std::format formatting.
5682
// Beware that contrary to LOG_* macros, arguments right of the format will be evaluated twice. ID
5783
// is evaluated once though.
5884
#define DIAGNOSE_ERROR(ID, ...) DIAGNOSE_WITH_LEVEL(error, ID, __VA_ARGS__)
@@ -63,8 +89,9 @@
6389
do { \
6490
auto _now = ::binlog::clockNow(); \
6591
const ::codeql::Diagnostic& _id = ID; \
66-
::codeql::Log::diagnose(_id, std::chrono::nanoseconds{_now}, \
67-
fmt::format(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \
92+
::codeql::Log::diagnose( \
93+
_id, std::chrono::nanoseconds{_now}, \
94+
::codeql::detail::formatDiagnostic(FORMAT __VA_OPT__(, ) __VA_ARGS__)); \
6895
LOG_WITH_LEVEL_AND_TIME(LEVEL, _now, CODEQL_DIAGNOSTIC_LOG_FORMAT_PREFIX FORMAT, \
6996
_id.abbreviation() __VA_OPT__(, ) __VA_ARGS__); \
7097
} while (false)

0 commit comments

Comments
 (0)