[clang] 40eda35 - [AST][NFC] Use struct as string table for AttrDoc (#246986)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 29 08:25:52 PST 2026


Author: Alexis Engelke
Date: 2026-11-29T17:25:46+01:00
New Revision: 40eda3583f37703bc2cb4131d16a2b578a4f552f

URL: https://github.com/llvm/llvm-project/commit/40eda3583f37703bc2cb4131d16a2b578a4f552f
DIFF: https://github.com/llvm/llvm-project/commit/40eda3583f37703bc2cb4131d16a2b578a4f552f.diff

LOG: [AST][NFC] Use struct as string table for AttrDoc (#246986)

Using a StringRef array has two problems: first, the length evaluation
uses a strlen-like loop from std::string_view at compile time (could be
fixed with StringLiteral); and second, it causes dynamic relocations.

Store all strings in one single global (445 kiB) and store a second
array with the offsets indexed by the attribute kind. This avoids 7.5kiB
of .data.rel.ro and improves compile times of AttrDocTable.cpp.

For compatibility with older MSVC, struct members cannot use an
initializer directly at the member declaration in constexpr contexts but
need to use a separate initializer for the entire struct.

Added: 
    

Modified: 
    clang/lib/AST/AttrDocTable.cpp
    clang/utils/TableGen/ClangAttrEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/AttrDocTable.cpp b/clang/lib/AST/AttrDocTable.cpp
index 56a143b9ed29be..b1491ba74fb62c 100644
--- a/clang/lib/AST/AttrDocTable.cpp
+++ b/clang/lib/AST/AttrDocTable.cpp
@@ -13,15 +13,23 @@
 #include "clang/AST/Attr.h"
 #include "llvm/ADT/StringRef.h"
 
+llvm::StringRef clang::Attr::getDocumentation(clang::attr::Kind K) {
+  static constexpr struct {
+#define ATTR_DOC_STR(ATTR, STR) char ATTR[sizeof(STR)];
+#include "AttrDocTable.inc"
+#undef ATTR_DOC_STR
+  } StrTab = {
+#define ATTR_DOC_STR(ATTR, STR) STR,
 #include "AttrDocTable.inc"
+#undef ATTR_DOC_STR
+  };
 
-static const llvm::StringRef AttrDoc[] = {
-#define ATTR(NAME) AttrDoc_##NAME,
+  static constexpr uint32_t Offsets[] = {
+#define ATTR(NAME) offsetof(decltype(StrTab), NAME),
 #include "clang/Basic/AttrList.inc"
-};
+  };
 
-llvm::StringRef clang::Attr::getDocumentation(clang::attr::Kind K) {
-  if (K < (int)std::size(AttrDoc))
-    return AttrDoc[K];
+  if (K < (int)std::size(Offsets))
+    return llvm::StringRef((const char *)&StrTab + Offsets[K]);
   return "";
 }

diff  --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp
index 060c7a403f2b17..54f1a1f1cc65a7 100644
--- a/clang/utils/TableGen/ClangAttrEmitter.cpp
+++ b/clang/utils/TableGen/ClangAttrEmitter.cpp
@@ -5496,8 +5496,8 @@ void EmitClangAttrDocTable(const RecordKeeper &Records, raw_ostream &OS) {
     // (Currently there's only one such attr, revisit if this becomes common).
     StringRef Text =
         Docs.front()->getValueAsOptionalString("Content").value_or("");
-    OS << "\nstatic const char AttrDoc_" << A->getName() << "[] = "
-       << "R\"reST(" << Text.trim() << ")reST\";\n";
+    OS << "\nATTR_DOC_STR(" << A->getName() << ", "
+       << "R\"reST(" << Text.trim() << ")reST\")\n";
   }
 }
 


        


More information about the cfe-commits mailing list