Skip to content

Commit b6ff8ed

Browse files
authored
[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
1 parent dbc90b5 commit b6ff8ed

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

clang/utils/TableGen/ClangASTNodesEmitter.cpp

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ClangASTNodesEmitter {
3434
typedef ChildMap::const_iterator ChildIterator;
3535

3636
std::set<ASTNode> PrioritizedClasses;
37-
RecordKeeper &Records;
37+
const RecordKeeper &Records;
3838
ASTNode Root;
3939
const std::string &NodeClassName;
4040
const std::string &BaseSuffix;
@@ -70,14 +70,12 @@ class ClangASTNodesEmitter {
7070

7171
std::pair<ASTNode, ASTNode> EmitNode(raw_ostream& OS, ASTNode Base);
7272
public:
73-
explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
73+
explicit ClangASTNodesEmitter(const RecordKeeper &R, const std::string &N,
7474
const std::string &S,
7575
std::string_view PriorizeIfSubclassOf)
7676
: Records(R), NodeClassName(N), BaseSuffix(S) {
77-
auto vecPrioritized =
78-
PriorizeIfSubclassOf.empty()
79-
? std::vector<Record *>{}
80-
: R.getAllDerivedDefinitions(PriorizeIfSubclassOf);
77+
ArrayRef<const Record *> vecPrioritized =
78+
R.getAllDerivedDefinitionsIfDefined(PriorizeIfSubclassOf);
8179
PrioritizedClasses =
8280
std::set<ASTNode>(vecPrioritized.begin(), vecPrioritized.end());
8381
}
@@ -169,10 +167,7 @@ void ClangASTNodesEmitter::deriveChildTree() {
169167
assert(!Root && "already computed tree");
170168

171169
// Emit statements
172-
const std::vector<Record*> Stmts
173-
= Records.getAllDerivedDefinitions(NodeClassName);
174-
175-
for (auto *R : Stmts) {
170+
for (const Record *R : Records.getAllDerivedDefinitions(NodeClassName)) {
176171
if (auto B = R->getValueAsOptionalDef(BaseFieldName))
177172
Tree.insert(std::make_pair(B, R));
178173
else if (Root)
@@ -217,14 +212,14 @@ void ClangASTNodesEmitter::run(raw_ostream &OS) {
217212
OS << "#undef ABSTRACT_" << macroHierarchyName() << "\n";
218213
}
219214

220-
void clang::EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
215+
void clang::EmitClangASTNodes(const RecordKeeper &RK, raw_ostream &OS,
221216
const std::string &N, const std::string &S,
222217
std::string_view PriorizeIfSubclassOf) {
223218
ClangASTNodesEmitter(RK, N, S, PriorizeIfSubclassOf).run(OS);
224219
}
225220

226-
void printDeclContext(const std::multimap<Record *, Record *> &Tree,
227-
Record *DeclContext, raw_ostream &OS) {
221+
void printDeclContext(const std::multimap<const Record *, const Record *> &Tree,
222+
const Record *DeclContext, raw_ostream &OS) {
228223
if (!DeclContext->getValueAsBit(AbstractFieldName))
229224
OS << "DECL_CONTEXT(" << DeclContext->getName() << ")\n";
230225
auto i = Tree.lower_bound(DeclContext);
@@ -236,7 +231,7 @@ void printDeclContext(const std::multimap<Record *, Record *> &Tree,
236231

237232
// Emits and addendum to a .inc file to enumerate the clang declaration
238233
// contexts.
239-
void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
234+
void clang::EmitClangDeclContext(const RecordKeeper &Records, raw_ostream &OS) {
240235
// FIXME: Find a .td file format to allow for this to be represented better.
241236

242237
emitSourceFileHeader("List of AST Decl nodes", OS, Records);
@@ -245,22 +240,15 @@ void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
245240
OS << "# define DECL_CONTEXT(DECL)\n";
246241
OS << "#endif\n";
247242

248-
std::vector<Record *> DeclContextsVector =
249-
Records.getAllDerivedDefinitions(DeclContextNodeClassName);
250-
std::vector<Record *> Decls =
251-
Records.getAllDerivedDefinitions(DeclNodeClassName);
252-
253-
std::multimap<Record *, Record *> Tree;
254-
255-
const std::vector<Record *> Stmts =
256-
Records.getAllDerivedDefinitions(DeclNodeClassName);
243+
std::multimap<const Record *, const Record *> Tree;
257244

258-
for (auto *R : Stmts) {
245+
for (const Record *R : Records.getAllDerivedDefinitions(DeclNodeClassName)) {
259246
if (auto *B = R->getValueAsOptionalDef(BaseFieldName))
260247
Tree.insert(std::make_pair(B, R));
261248
}
262249

263-
for (auto *DeclContext : DeclContextsVector) {
250+
for (const Record *DeclContext :
251+
Records.getAllDerivedDefinitions(DeclContextNodeClassName)) {
264252
printDeclContext(Tree, DeclContext, OS);
265253
}
266254

clang/utils/TableGen/TableGenBackends.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class RecordKeeper;
2424

2525
namespace clang {
2626

27-
void EmitClangDeclContext(llvm::RecordKeeper &RK, llvm::raw_ostream &OS);
27+
void EmitClangDeclContext(const llvm::RecordKeeper &RK, llvm::raw_ostream &OS);
2828
/**
2929
@param PriorizeIfSubclassOf These classes should be prioritized in the output.
3030
This is useful to force enum generation/jump tables/lookup tables to be more
3131
compact in both size and surrounding code in hot functions. An example use is
3232
in Decl for classes that inherit from DeclContext, for functions like
3333
castFromDeclContext.
3434
*/
35-
void EmitClangASTNodes(llvm::RecordKeeper &RK, llvm::raw_ostream &OS,
35+
void EmitClangASTNodes(const llvm::RecordKeeper &RK, llvm::raw_ostream &OS,
3636
const std::string &N, const std::string &S,
3737
std::string_view PriorizeIfSubclassOf = "");
3838
void EmitClangBasicReader(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);

0 commit comments

Comments
 (0)