[clang] [clang][TableGen] Migrate clang-tblgen to use const RecordKeeper (PR #107533)

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 6 09:01:57 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Rahul Joshi (jurahul)

<details>
<summary>Changes</summary>

Migrate clang tablegen backends to use const RecordKeeper.

---

Patch is 135.31 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/107533.diff


22 Files Affected:

- (modified) clang/utils/TableGen/ASTTableGen.cpp (+6-6) 
- (modified) clang/utils/TableGen/ASTTableGen.h (+19-18) 
- (modified) clang/utils/TableGen/ClangASTNodesEmitter.cpp (+13-22) 
- (modified) clang/utils/TableGen/ClangASTPropertiesEmitter.cpp (+98-95) 
- (modified) clang/utils/TableGen/ClangAttrEmitter.cpp (+106-122) 
- (modified) clang/utils/TableGen/ClangBuiltinsEmitter.cpp (+5-4) 
- (modified) clang/utils/TableGen/ClangCommentCommandInfoEmitter.cpp (+9-12) 
- (modified) clang/utils/TableGen/ClangCommentHTMLNamedCharacterReferenceEmitter.cpp (+6-10) 
- (modified) clang/utils/TableGen/ClangCommentHTMLTagsEmitter.cpp (+5-6) 
- (modified) clang/utils/TableGen/ClangDataCollectorsEmitter.cpp (+1-1) 
- (modified) clang/utils/TableGen/ClangDiagnosticsEmitter.cpp (+66-67) 
- (modified) clang/utils/TableGen/ClangOpcodesEmitter.cpp (+4-4) 
- (modified) clang/utils/TableGen/ClangOpenCLBuiltinEmitter.cpp (+31-34) 
- (modified) clang/utils/TableGen/ClangOptionDocEmitter.cpp (+23-23) 
- (modified) clang/utils/TableGen/ClangSACheckersEmitter.cpp (+5-4) 
- (modified) clang/utils/TableGen/ClangSyntaxEmitter.cpp (+6-5) 
- (modified) clang/utils/TableGen/ClangTypeNodesEmitter.cpp (+6-7) 
- (modified) clang/utils/TableGen/MveEmitter.cpp (+21-20) 
- (modified) clang/utils/TableGen/NeonEmitter.cpp (+18-23) 
- (modified) clang/utils/TableGen/RISCVVEmitter.cpp (+9-14) 
- (modified) clang/utils/TableGen/SveEmitter.cpp (+31-40) 
- (modified) clang/utils/TableGen/TableGenBackends.h (+119-90) 


``````````diff
diff --git a/clang/utils/TableGen/ASTTableGen.cpp b/clang/utils/TableGen/ASTTableGen.cpp
index 54288ff6a03be3..482c9e318a12f3 100644
--- a/clang/utils/TableGen/ASTTableGen.cpp
+++ b/clang/utils/TableGen/ASTTableGen.cpp
@@ -31,7 +31,8 @@ llvm::StringRef clang::tblgen::HasProperties::getName() const {
   }
 }
 
-static StringRef removeExpectedNodeNameSuffix(Record *node, StringRef suffix) {
+static StringRef removeExpectedNodeNameSuffix(const Record *node,
+                                              StringRef suffix) {
   StringRef nodeName = node->getName();
   if (!nodeName.ends_with(suffix)) {
     PrintFatalError(node->getLoc(),
@@ -105,8 +106,7 @@ static void visitASTNodeRecursive(ASTNode node, ASTNode base,
   }
 }
 
-static void visitHierarchy(RecordKeeper &records,
-                           StringRef nodeClassName,
+static void visitHierarchy(const RecordKeeper &records, StringRef nodeClassName,
                            ASTNodeHierarchyVisitor<ASTNode> visit) {
   // Check for the node class, just as a basic correctness check.
   if (!records.getClass(nodeClassName)) {
@@ -136,8 +136,8 @@ static void visitHierarchy(RecordKeeper &records,
   visitASTNodeRecursive(root, ASTNode(), hierarchy, visit);
 }
 
-void clang::tblgen::visitASTNodeHierarchyImpl(RecordKeeper &records,
-                                              StringRef nodeClassName,
-                                      ASTNodeHierarchyVisitor<ASTNode> visit) {
+void clang::tblgen::visitASTNodeHierarchyImpl(
+    const RecordKeeper &records, StringRef nodeClassName,
+    ASTNodeHierarchyVisitor<ASTNode> visit) {
   visitHierarchy(records, nodeClassName, visit);
 }
diff --git a/clang/utils/TableGen/ASTTableGen.h b/clang/utils/TableGen/ASTTableGen.h
index 41f78a6a3bbcdd..143d779a8a64f8 100644
--- a/clang/utils/TableGen/ASTTableGen.h
+++ b/clang/utils/TableGen/ASTTableGen.h
@@ -87,18 +87,18 @@ namespace clang {
 namespace tblgen {
 
 class WrappedRecord {
-  llvm::Record *Record;
+  const llvm::Record *Record;
 
 protected:
-  WrappedRecord(llvm::Record *record = nullptr) : Record(record) {}
+  WrappedRecord(const llvm::Record *record = nullptr) : Record(record) {}
 
-  llvm::Record *get() const {
+  const llvm::Record *get() const {
     assert(Record && "accessing null record");
     return Record;
   }
 
 public:
-  llvm::Record *getRecord() const { return Record; }
+  const llvm::Record *getRecord() const { return Record; }
 
   explicit operator bool() const { return Record != nullptr; }
 
@@ -144,7 +144,7 @@ class HasProperties : public WrappedRecord {
 public:
   static constexpr llvm::StringRef ClassName = HasPropertiesClassName;
 
-  HasProperties(llvm::Record *record = nullptr) : WrappedRecord(record) {}
+  HasProperties(const llvm::Record *record = nullptr) : WrappedRecord(record) {}
 
   llvm::StringRef getName() const;
 
@@ -157,7 +157,7 @@ class HasProperties : public WrappedRecord {
 /// in one of Clang's AST hierarchies.
 class ASTNode : public HasProperties {
 public:
-  ASTNode(llvm::Record *record = nullptr) : HasProperties(record) {}
+  ASTNode(const llvm::Record *record = nullptr) : HasProperties(record) {}
 
   llvm::StringRef getName() const {
     return get()->getName();
@@ -180,7 +180,7 @@ class ASTNode : public HasProperties {
 
 class DeclNode : public ASTNode {
 public:
-  DeclNode(llvm::Record *record = nullptr) : ASTNode(record) {}
+  DeclNode(const llvm::Record *record = nullptr) : ASTNode(record) {}
 
   llvm::StringRef getId() const;
   std::string getClassName() const;
@@ -202,7 +202,7 @@ class DeclNode : public ASTNode {
 
 class TypeNode : public ASTNode {
 public:
-  TypeNode(llvm::Record *record = nullptr) : ASTNode(record) {}
+  TypeNode(const llvm::Record *record = nullptr) : ASTNode(record) {}
 
   llvm::StringRef getId() const;
   llvm::StringRef getClassName() const;
@@ -224,7 +224,7 @@ class TypeNode : public ASTNode {
 
 class StmtNode : public ASTNode {
 public:
-  StmtNode(llvm::Record *record = nullptr) : ASTNode(record) {}
+  StmtNode(const llvm::Record *record = nullptr) : ASTNode(record) {}
 
   std::string getId() const;
   llvm::StringRef getClassName() const;
@@ -247,7 +247,7 @@ class StmtNode : public ASTNode {
 /// The type of a property.
 class PropertyType : public WrappedRecord {
 public:
-  PropertyType(llvm::Record *record = nullptr) : WrappedRecord(record) {}
+  PropertyType(const llvm::Record *record = nullptr) : WrappedRecord(record) {}
 
   /// Is this a generic specialization (i.e. `Array<T>` or `Optional<T>`)?
   bool isGenericSpecialization() const {
@@ -331,7 +331,7 @@ class PropertyType : public WrappedRecord {
 /// A rule for returning the kind of a type.
 class TypeKindRule : public WrappedRecord {
 public:
-  TypeKindRule(llvm::Record *record = nullptr) : WrappedRecord(record) {}
+  TypeKindRule(const llvm::Record *record = nullptr) : WrappedRecord(record) {}
 
   /// Return the type to which this applies.
   PropertyType getParentType() const {
@@ -361,7 +361,7 @@ class TypeKindRule : public WrappedRecord {
 /// An implementation case of a property type.
 class TypeCase : public HasProperties {
 public:
-  TypeCase(llvm::Record *record = nullptr) : HasProperties(record) {}
+  TypeCase(const llvm::Record *record = nullptr) : HasProperties(record) {}
 
   /// Return the name of this case.
   llvm::StringRef getCaseName() const {
@@ -381,7 +381,7 @@ class TypeCase : public HasProperties {
 /// A property of an AST node.
 class Property : public WrappedRecord {
 public:
-  Property(llvm::Record *record = nullptr) : WrappedRecord(record) {}
+  Property(const llvm::Record *record = nullptr) : WrappedRecord(record) {}
 
   /// Return the name of this property.
   llvm::StringRef getName() const {
@@ -417,7 +417,8 @@ class Property : public WrappedRecord {
 /// a value (which is actually done when writing the value out).
 class ReadHelperRule : public WrappedRecord {
 public:
-  ReadHelperRule(llvm::Record *record = nullptr) : WrappedRecord(record) {}
+  ReadHelperRule(const llvm::Record *record = nullptr)
+      : WrappedRecord(record) {}
 
   /// Return the class for which this is a creation rule.
   /// Should never be abstract.
@@ -437,7 +438,7 @@ class ReadHelperRule : public WrappedRecord {
 /// A rule for how to create an AST node from its properties.
 class CreationRule : public WrappedRecord {
 public:
-  CreationRule(llvm::Record *record = nullptr) : WrappedRecord(record) {}
+  CreationRule(const llvm::Record *record = nullptr) : WrappedRecord(record) {}
 
   /// Return the class for which this is a creation rule.
   /// Should never be abstract.
@@ -457,7 +458,7 @@ class CreationRule : public WrappedRecord {
 /// A rule which overrides the standard rules for serializing an AST node.
 class OverrideRule : public WrappedRecord {
 public:
-  OverrideRule(llvm::Record *record = nullptr) : WrappedRecord(record) {}
+  OverrideRule(const llvm::Record *record = nullptr) : WrappedRecord(record) {}
 
   /// Return the class for which this is an override rule.
   /// Should never be abstract.
@@ -483,12 +484,12 @@ template <class NodeClass>
 using ASTNodeHierarchyVisitor =
   llvm::function_ref<void(NodeClass node, NodeClass base)>;
 
-void visitASTNodeHierarchyImpl(llvm::RecordKeeper &records,
+void visitASTNodeHierarchyImpl(const llvm::RecordKeeper &records,
                                llvm::StringRef nodeClassName,
                                ASTNodeHierarchyVisitor<ASTNode> visit);
 
 template <class NodeClass>
-void visitASTNodeHierarchy(llvm::RecordKeeper &records,
+void visitASTNodeHierarchy(const llvm::RecordKeeper &records,
                            ASTNodeHierarchyVisitor<NodeClass> visit) {
   visitASTNodeHierarchyImpl(records, NodeClass::getTableGenNodeClassName(),
                             [visit](ASTNode node, ASTNode base) {
diff --git a/clang/utils/TableGen/ClangASTNodesEmitter.cpp b/clang/utils/TableGen/ClangASTNodesEmitter.cpp
index 07ddafce329163..39cc8b87e6301f 100644
--- a/clang/utils/TableGen/ClangASTNodesEmitter.cpp
+++ b/clang/utils/TableGen/ClangASTNodesEmitter.cpp
@@ -34,7 +34,7 @@ class ClangASTNodesEmitter {
   typedef ChildMap::const_iterator ChildIterator;
 
   std::set<ASTNode> PrioritizedClasses;
-  RecordKeeper &Records;
+  const RecordKeeper &Records;
   ASTNode Root;
   const std::string &NodeClassName;
   const std::string &BaseSuffix;
@@ -70,14 +70,12 @@ class ClangASTNodesEmitter {
 
   std::pair<ASTNode, ASTNode> EmitNode(raw_ostream& OS, ASTNode Base);
 public:
-  explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
+  explicit ClangASTNodesEmitter(const RecordKeeper &R, const std::string &N,
                                 const std::string &S,
                                 std::string_view PriorizeIfSubclassOf)
       : Records(R), NodeClassName(N), BaseSuffix(S) {
     auto vecPrioritized =
-        PriorizeIfSubclassOf.empty()
-            ? std::vector<Record *>{}
-            : R.getAllDerivedDefinitions(PriorizeIfSubclassOf);
+        R.getAllDerivedDefinitionsIfDefined(PriorizeIfSubclassOf);
     PrioritizedClasses =
         std::set<ASTNode>(vecPrioritized.begin(), vecPrioritized.end());
   }
@@ -169,8 +167,8 @@ void ClangASTNodesEmitter::deriveChildTree() {
   assert(!Root && "already computed tree");
 
   // Emit statements
-  const std::vector<Record*> Stmts
-    = Records.getAllDerivedDefinitions(NodeClassName);
+  const std::vector<const Record *> &Stmts =
+      Records.getAllDerivedDefinitions(NodeClassName);
 
   for (auto *R : Stmts) {
     if (auto B = R->getValueAsOptionalDef(BaseFieldName))
@@ -217,14 +215,14 @@ void ClangASTNodesEmitter::run(raw_ostream &OS) {
   OS << "#undef ABSTRACT_" << macroHierarchyName() << "\n";
 }
 
-void clang::EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
+void clang::EmitClangASTNodes(const RecordKeeper &RK, raw_ostream &OS,
                               const std::string &N, const std::string &S,
                               std::string_view PriorizeIfSubclassOf) {
   ClangASTNodesEmitter(RK, N, S, PriorizeIfSubclassOf).run(OS);
 }
 
-void printDeclContext(const std::multimap<Record *, Record *> &Tree,
-                      Record *DeclContext, raw_ostream &OS) {
+void printDeclContext(const std::multimap<const Record *, const Record *> &Tree,
+                      const Record *DeclContext, raw_ostream &OS) {
   if (!DeclContext->getValueAsBit(AbstractFieldName))
     OS << "DECL_CONTEXT(" << DeclContext->getName() << ")\n";
   auto i = Tree.lower_bound(DeclContext);
@@ -236,7 +234,7 @@ void printDeclContext(const std::multimap<Record *, Record *> &Tree,
 
 // Emits and addendum to a .inc file to enumerate the clang declaration
 // contexts.
-void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
+void clang::EmitClangDeclContext(const RecordKeeper &Records, raw_ostream &OS) {
   // FIXME: Find a .td file format to allow for this to be represented better.
 
   emitSourceFileHeader("List of AST Decl nodes", OS, Records);
@@ -245,22 +243,15 @@ void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
   OS << "#  define DECL_CONTEXT(DECL)\n";
   OS << "#endif\n";
 
-  std::vector<Record *> DeclContextsVector =
-      Records.getAllDerivedDefinitions(DeclContextNodeClassName);
-  std::vector<Record *> Decls =
-      Records.getAllDerivedDefinitions(DeclNodeClassName);
+  std::multimap<const Record *, const Record *> Tree;
 
-  std::multimap<Record *, Record *> Tree;
-
-  const std::vector<Record *> Stmts =
-      Records.getAllDerivedDefinitions(DeclNodeClassName);
-
-  for (auto *R : Stmts) {
+  for (auto *R : Records.getAllDerivedDefinitions(DeclNodeClassName)) {
     if (auto *B = R->getValueAsOptionalDef(BaseFieldName))
       Tree.insert(std::make_pair(B, R));
   }
 
-  for (auto *DeclContext : DeclContextsVector) {
+  for (const Record *DeclContext :
+       Records.getAllDerivedDefinitions(DeclContextNodeClassName)) {
     printDeclContext(Tree, DeclContext, OS);
   }
 
diff --git a/clang/utils/TableGen/ClangASTPropertiesEmitter.cpp b/clang/utils/TableGen/ClangASTPropertiesEmitter.cpp
index de8dda60681ff8..a967ecb6523252 100644
--- a/clang/utils/TableGen/ClangASTPropertiesEmitter.cpp
+++ b/clang/utils/TableGen/ClangASTPropertiesEmitter.cpp
@@ -89,95 +89,96 @@ struct CasedTypeInfo {
 
 class ASTPropsEmitter {
 	raw_ostream &Out;
-	RecordKeeper &Records;
-	std::map<HasProperties, NodeInfo> NodeInfos;
-  std::vector<PropertyType> AllPropertyTypes;
-  std::map<PropertyType, CasedTypeInfo> CasedTypeInfos;
-
-public:
-	ASTPropsEmitter(RecordKeeper &records, raw_ostream &out)
-		: Out(out), Records(records) {
-
-		// Find all the properties.
-		for (Property property :
-           records.getAllDerivedDefinitions(PropertyClassName)) {
-			HasProperties node = property.getClass();
-			NodeInfos[node].Properties.push_back(property);
-		}
-
-    // Find all the creation rules.
-    for (CreationRule creationRule :
-           records.getAllDerivedDefinitions(CreationRuleClassName)) {
-      HasProperties node = creationRule.getClass();
-
-      auto &info = NodeInfos[node];
-      if (info.Creator) {
-        PrintFatalError(creationRule.getLoc(),
-                        "multiple creator rules for \"" + node.getName()
-                          + "\"");
-      }
-      info.Creator = creationRule;
-    }
-
-    // Find all the override rules.
-    for (OverrideRule overrideRule :
-           records.getAllDerivedDefinitions(OverrideRuleClassName)) {
-      HasProperties node = overrideRule.getClass();
-
-      auto &info = NodeInfos[node];
-      if (info.Override) {
-        PrintFatalError(overrideRule.getLoc(),
-                        "multiple override rules for \"" + node.getName()
-                          + "\"");
-      }
-      info.Override = overrideRule;
-    }
-
-    // Find all the write helper rules.
-    for (ReadHelperRule helperRule :
-           records.getAllDerivedDefinitions(ReadHelperRuleClassName)) {
-      HasProperties node = helperRule.getClass();
-
-      auto &info = NodeInfos[node];
-      if (info.ReadHelper) {
-        PrintFatalError(helperRule.getLoc(),
-                        "multiple write helper rules for \"" + node.getName()
-                          + "\"");
-      }
-      info.ReadHelper = helperRule;
-    }
-
-    // Find all the concrete property types.
-    for (PropertyType type :
-           records.getAllDerivedDefinitions(PropertyTypeClassName)) {
-      // Ignore generic specializations; they're generally not useful when
-      // emitting basic emitters etc.
-      if (type.isGenericSpecialization()) continue;
-
-      AllPropertyTypes.push_back(type);
-    }
-
-    // Find all the type kind rules.
-    for (TypeKindRule kindRule :
-           records.getAllDerivedDefinitions(TypeKindClassName)) {
-      PropertyType type = kindRule.getParentType();
-      auto &info = CasedTypeInfos[type];
-      if (info.KindRule) {
-        PrintFatalError(kindRule.getLoc(),
-                        "multiple kind rules for \""
-                           + type.getCXXTypeName() + "\"");
-      }
-      info.KindRule = kindRule;
-    }
-
-    // Find all the type cases.
-    for (TypeCase typeCase :
-           records.getAllDerivedDefinitions(TypeCaseClassName)) {
-      CasedTypeInfos[typeCase.getParentType()].Cases.push_back(typeCase);
-    }
-
-    Validator(*this).validate();
-	}
+        const RecordKeeper &Records;
+        std::map<HasProperties, NodeInfo> NodeInfos;
+        std::vector<PropertyType> AllPropertyTypes;
+        std::map<PropertyType, CasedTypeInfo> CasedTypeInfos;
+
+      public:
+        ASTPropsEmitter(const RecordKeeper &records, raw_ostream &out)
+            : Out(out), Records(records) {
+
+          // Find all the properties.
+          for (Property property :
+               records.getAllDerivedDefinitions(PropertyClassName)) {
+            HasProperties node = property.getClass();
+            NodeInfos[node].Properties.push_back(property);
+          }
+
+          // Find all the creation rules.
+          for (CreationRule creationRule :
+               records.getAllDerivedDefinitions(CreationRuleClassName)) {
+            HasProperties node = creationRule.getClass();
+
+            auto &info = NodeInfos[node];
+            if (info.Creator) {
+              PrintFatalError(creationRule.getLoc(),
+                              "multiple creator rules for \"" + node.getName() +
+                                  "\"");
+            }
+            info.Creator = creationRule;
+          }
+
+          // Find all the override rules.
+          for (OverrideRule overrideRule :
+               records.getAllDerivedDefinitions(OverrideRuleClassName)) {
+            HasProperties node = overrideRule.getClass();
+
+            auto &info = NodeInfos[node];
+            if (info.Override) {
+              PrintFatalError(overrideRule.getLoc(),
+                              "multiple override rules for \"" +
+                                  node.getName() + "\"");
+            }
+            info.Override = overrideRule;
+          }
+
+          // Find all the write helper rules.
+          for (ReadHelperRule helperRule :
+               records.getAllDerivedDefinitions(ReadHelperRuleClassName)) {
+            HasProperties node = helperRule.getClass();
+
+            auto &info = NodeInfos[node];
+            if (info.ReadHelper) {
+              PrintFatalError(helperRule.getLoc(),
+                              "multiple write helper rules for \"" +
+                                  node.getName() + "\"");
+            }
+            info.ReadHelper = helperRule;
+          }
+
+          // Find all the concrete property types.
+          for (PropertyType type :
+               records.getAllDerivedDefinitions(PropertyTypeClassName)) {
+            // Ignore generic specializations; they're generally not useful when
+            // emitting basic emitters etc.
+            if (type.isGenericSpecialization())
+              continue;
+
+            AllPropertyTypes.push_back(type);
+          }
+
+          // Find all the type kind rules.
+          for (TypeKindRule kindRule :
+               records.getAllDerivedDefinitions(TypeKindClassName)) {
+            PropertyType type = kindRule.getParentType();
+            auto &info = CasedTypeInfos[type];
+            if (info.KindRule) {
+              PrintFatalError(kindRule.getLoc(), "multiple kind rules for \"" +
+                                                     type.getCXXTypeName() +
+                                                     "\"");
+            }
+            info.KindRule = kindRule;
+          }
+
+          // Find all the type cases.
+          for (TypeCase typeCase :
+               records.getAllDerivedDefinitions(TypeCaseClassName)) {
+            CasedTypeInfos[typeCase.getParentType()].Cases.push_back(typeCase);
+          }
+
+          Validator(*this).validate();
+        }
 
   void visitAllProperties(HasProperties derived, const NodeInfo &derivedInfo,
                           function_ref<void (Property)> visit) {
@@ -591,28 +592,28 @@ void ASTPropsEmitter::emitWriteOfProperty(StringRef writerName,
 /// Emit an .inc file that defines the AbstractFooReader class
 /// for the given AST class hierarchy.
 template <class NodeClass>
-static void emitASTReader(RecordKeeper &records, raw_ostream &out,
+static void emitASTReader(const RecordKeeper &records, raw_ostream &out,
                           StringRef description) {
   emitSourceFileHeader(description, out, records);
 
   ASTPropsEmitter(records, out).emitNodeReaderClass<NodeClass>();
 }
 
-void clang::EmitClangTypeReader(RecordKeeper &records, raw_ostream &out) {
+void clang::EmitClangTypeReader(const RecordKeeper &records, raw_ostream &out) {
   emitASTReader<TypeNode>(records, out, "A CRTP reader for Clang Type nodes");
 }
 
 /// Emit an .inc file that defines the AbstractFooWriter class
 /// for the given AST class hierarchy.
 template <class NodeClass>
-static void emitASTWriter(RecordKeeper &records, raw_ostream &out,
+static void emitASTWriter(const RecordKeeper &records, raw_ostream &out,
                           StringRef description) {
   emitSourceFileHeader(description, out, records);
 
   ASTPropsEmitter(records, out).emitNodeWriterClass<NodeClass>();
 }
 
-void clang::EmitClangTypeWriter(RecordKeeper &records, raw_ostream &out) {
+void cla...
[truncated]

``````````

</details>


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


More information about the cfe-commits mailing list