-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[clang][TableGen] Change AST Nodes Emitter to use const RecordKeeper #108270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jurahul
merged 1 commit into
llvm:main
from
jurahul:clang_astnodes_emitter_const_record
Sep 12, 2024
Merged
[clang][TableGen] Change AST Nodes Emitter to use const RecordKeeper #108270
jurahul
merged 1 commit into
llvm:main
from
jurahul:clang_astnodes_emitter_const_record
Sep 12, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang Author: Rahul Joshi (jurahul) ChangesChange AST Nodes Emitter to use const RecordKeeper. This is a part of effort to have better const correctness in TableGen backends: Full diff: https://github.com/llvm/llvm-project/pull/108270.diff 2 Files Affected:
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 fe55ef2f423afe..8124564d2fd56e 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);
|
AaronBallman
approved these changes
Sep 12, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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