[clang-tools-extra] [clang-tidy][NFC] Construct map at compile time (PR #158166)
Nicolas van Kempen via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 12 07:43:18 PDT 2025
================
@@ -39,31 +32,17 @@ truncateIfIntegral(const FloatingLiteral &FloatLiteral) {
const std::pair<llvm::StringRef, llvm::StringRef> &
getDurationInverseForScale(DurationScale Scale) {
- static const llvm::IndexedMap<std::pair<llvm::StringRef, llvm::StringRef>,
- DurationScale2IndexFunctor>
- InverseMap = []() {
- // TODO: Revisit the immediately invoked lambda technique when
- // IndexedMap gets an initializer list constructor.
- llvm::IndexedMap<std::pair<llvm::StringRef, llvm::StringRef>,
- DurationScale2IndexFunctor>
- InverseMap;
- InverseMap.resize(6);
- InverseMap[DurationScale::Hours] =
- std::make_pair("::absl::ToDoubleHours", "::absl::ToInt64Hours");
- InverseMap[DurationScale::Minutes] =
- std::make_pair("::absl::ToDoubleMinutes", "::absl::ToInt64Minutes");
- InverseMap[DurationScale::Seconds] =
- std::make_pair("::absl::ToDoubleSeconds", "::absl::ToInt64Seconds");
- InverseMap[DurationScale::Milliseconds] = std::make_pair(
- "::absl::ToDoubleMilliseconds", "::absl::ToInt64Milliseconds");
- InverseMap[DurationScale::Microseconds] = std::make_pair(
- "::absl::ToDoubleMicroseconds", "::absl::ToInt64Microseconds");
- InverseMap[DurationScale::Nanoseconds] = std::make_pair(
- "::absl::ToDoubleNanoseconds", "::absl::ToInt64Nanoseconds");
- return InverseMap;
- }();
-
- return InverseMap[Scale];
+ static constexpr std::array<std::pair<llvm::StringRef, llvm::StringRef>, 6>
+ InverseMap = {{
+ {"::absl::ToDoubleHours", "::absl::ToInt64Hours"},
+ {"::absl::ToDoubleMinutes", "::absl::ToInt64Minutes"},
+ {"::absl::ToDoubleSeconds", "::absl::ToInt64Seconds"},
+ {"::absl::ToDoubleMilliseconds", "::absl::ToInt64Milliseconds"},
+ {"::absl::ToDoubleMicroseconds", "::absl::ToInt64Microseconds"},
+ {"::absl::ToDoubleNanoseconds", "::absl::ToInt64Nanoseconds"},
+ }};
+
+ return InverseMap[llvm::to_underlying(Scale)];
----------------
nicovank wrote:
I guess I don't care that much about the runtime cost, I think it's negligible here. But keeping the way it is now with `operator[]` is also just fine.
https://github.com/llvm/llvm-project/pull/158166
More information about the cfe-commits
mailing list