[clang] [Clang] speed up -Wassign-enum via enumerator caching (PR #176560)

Oleksandr Tarasiuk via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 27 14:47:02 PST 2026


================
@@ -1761,30 +1761,30 @@ Sema::DiagnoseAssignmentEnum(QualType DstType, QualType SrcType,
     return;
   }
 
-  typedef SmallVector<std::pair<llvm::APSInt, EnumConstantDecl *>, 64>
-      EnumValsTy;
-  EnumValsTy EnumVals;
-
-  // Gather all enum values, set their type and sort them,
-  // allowing easier comparison with rhs constant.
-  for (auto *EDI : ED->enumerators()) {
-    llvm::APSInt Val = EDI->getInitVal();
-    AdjustAPSInt(Val, DstWidth, DstIsSigned);
-    EnumVals.emplace_back(Val, EDI);
+  const EnumDecl *Key = ED->getCanonicalDecl();
+  auto [It, Inserted] = AssignEnumCache.try_emplace(Key);
+  auto &Values = It->second;
+
+  if (Inserted) {
+    Values.reserve(std::distance(ED->enumerator_begin(), ED->enumerator_end()));
----------------
a-tarasyuk wrote:

https://github.com/llvm/llvm-project/blob/3837db12e76c783f95a82804f74762453d6b4475/llvm/include/llvm/ADT/STLExtras.h#L1664-L1674

It seems `enumerators` range has to be `random_access_iterator`, however, enums use https://github.com/llvm/llvm-project/blob/3837db12e76c783f95a82804f74762453d6b4475/clang/include/clang/AST/Decl.h#L4153-L4154





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


More information about the cfe-commits mailing list