[clang] b6ff8ed - [clang][TableGen] Change AST Nodes Emitter to use const RecordKeeper (#108270)

via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 12 06:51:54 PDT 2024


Author: Rahul Joshi
Date: 2024-09-12T06:51:51-07:00
New Revision: b6ff8ed5d4d03b28e41b73ef8e76ce94a8fa76f0

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

LOG: [clang][TableGen] Change AST Nodes Emitter to use const RecordKeeper (#108270)

Change AST Nodes Emitter to use const RecordKeeper.

This is a part of effort to have better const correctness in TableGen
backends:


https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089

Added: 
    

Modified: 
    clang/utils/TableGen/ClangASTNodesEmitter.cpp
    clang/utils/TableGen/TableGenBackends.h

Removed: 
    


################################################################################
diff  --git a/clang/utils/TableGen/ClangASTNodesEmitter.cpp b/clang/utils/TableGen/ClangASTNodesEmitter.cpp
index 07ddafce329163..512af830e57c99 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);
+    ArrayRef<const Record *> vecPrioritized =
+        R.getAllDerivedDefinitionsIfDefined(PriorizeIfSubclassOf);
     PrioritizedClasses =
         std::set<ASTNode>(vecPrioritized.begin(), vecPrioritized.end());
   }
@@ -169,10 +167,7 @@ void ClangASTNodesEmitter::deriveChildTree() {
   assert(!Root && "already computed tree");
 
   // Emit statements
-  const std::vector<Record*> Stmts
-    = Records.getAllDerivedDefinitions(NodeClassName);
-
-  for (auto *R : Stmts) {
+  for (const Record *R : Records.getAllDerivedDefinitions(NodeClassName)) {
     if (auto B = R->getValueAsOptionalDef(BaseFieldName))
       Tree.insert(std::make_pair(B, R));
     else if (Root)
@@ -217,14 +212,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 +231,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 +240,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<Record *, Record *> Tree;
-
-  const std::vector<Record *> Stmts =
-      Records.getAllDerivedDefinitions(DeclNodeClassName);
+  std::multimap<const Record *, const Record *> Tree;
 
-  for (auto *R : Stmts) {
+  for (const Record *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/TableGenBackends.h b/clang/utils/TableGen/TableGenBackends.h
index d1eebd33d5e15a..c0582e341fc87a 100644
--- a/clang/utils/TableGen/TableGenBackends.h
+++ b/clang/utils/TableGen/TableGenBackends.h
@@ -24,7 +24,7 @@ class RecordKeeper;
 
 namespace clang {
 
-void EmitClangDeclContext(llvm::RecordKeeper &RK, llvm::raw_ostream &OS);
+void EmitClangDeclContext(const llvm::RecordKeeper &RK, llvm::raw_ostream &OS);
 /**
   @param PriorizeIfSubclassOf These classes should be prioritized in the output.
   This is useful to force enum generation/jump tables/lookup tables to be more
@@ -32,7 +32,7 @@ void EmitClangDeclContext(llvm::RecordKeeper &RK, llvm::raw_ostream &OS);
   in Decl for classes that inherit from DeclContext, for functions like
   castFromDeclContext.
   */
-void EmitClangASTNodes(llvm::RecordKeeper &RK, llvm::raw_ostream &OS,
+void EmitClangASTNodes(const llvm::RecordKeeper &RK, llvm::raw_ostream &OS,
                        const std::string &N, const std::string &S,
                        std::string_view PriorizeIfSubclassOf = "");
 void EmitClangBasicReader(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);


        


More information about the cfe-commits mailing list