[clang-tools-extra] [clang-tidy][NFC] Construct map at compile time (PR #158166)

Victor Chernyakin via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 12 08:24:41 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)];
----------------
localspook wrote:

The `to_underlying`s are localized to just the functions you see in this PR, but there are a lot more functions that work with `Scale`, so I think there's still value for those other functions in it being an `enum class`.

https://github.com/llvm/llvm-project/pull/158166


More information about the cfe-commits mailing list