Skip to content

Commit a06d84a

Browse files
authored
[NFC][Clang][TableGen] Refactor ClangASTNodesEmitter (#108580)
Change macroName() to accept a StringRef to avoid extra string copy. Simplify ASTNode comparison function. Use equal_range() instead of calling lower_bound() and upper_bound() separately for std::multimap. No need to use std::make_pair.
1 parent 823eab2 commit a06d84a

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

clang/utils/TableGen/ClangASTNodesEmitter.cpp

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm/TableGen/Error.h"
1717
#include "llvm/TableGen/Record.h"
1818
#include "llvm/TableGen/TableGenBackend.h"
19-
#include <cctype>
2019
#include <map>
2120
#include <set>
2221
#include <string>
@@ -42,17 +41,12 @@ class ClangASTNodesEmitter {
4241
ChildMap Tree;
4342

4443
// Create a macro-ized version of a name
45-
static std::string macroName(std::string S) {
46-
for (unsigned i = 0; i < S.size(); ++i)
47-
S[i] = std::toupper(S[i]);
48-
49-
return S;
50-
}
44+
static std::string macroName(StringRef S) { return S.upper(); }
5145

5246
const std::string &macroHierarchyName() {
5347
assert(Root && "root node not yet derived!");
5448
if (MacroHierarchyName.empty())
55-
MacroHierarchyName = macroName(std::string(Root.getName()));
49+
MacroHierarchyName = macroName(Root.getName());
5650
return MacroHierarchyName;
5751
}
5852

@@ -93,34 +87,30 @@ class ClangASTNodesEmitter {
9387
// Called recursively to ensure that nodes remain contiguous
9488
std::pair<ASTNode, ASTNode> ClangASTNodesEmitter::EmitNode(raw_ostream &OS,
9589
ASTNode Base) {
96-
std::string BaseName = macroName(std::string(Base.getName()));
90+
std::string BaseName = macroName(Base.getName());
9791

98-
ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
99-
bool HasChildren = (i != e);
92+
auto [II, E] = Tree.equal_range(Base);
93+
bool HasChildren = II != E;
10094

10195
ASTNode First, Last;
10296
if (!Base.isAbstract())
10397
First = Last = Base;
10498

105-
auto comp = [this](ASTNode LHS, ASTNode RHS) {
106-
auto LHSPrioritized = PrioritizedClasses.count(LHS) > 0;
107-
auto RHSPrioritized = PrioritizedClasses.count(RHS) > 0;
108-
if (LHSPrioritized && !RHSPrioritized)
109-
return true;
110-
if (!LHSPrioritized && RHSPrioritized)
111-
return false;
112-
113-
return LHS.getName() > RHS.getName();
99+
auto Comp = [this](const ASTNode &LHS, const ASTNode &RHS) {
100+
bool LHSPrioritized = PrioritizedClasses.count(LHS) > 0;
101+
bool RHSPrioritized = PrioritizedClasses.count(RHS) > 0;
102+
return std::tuple(LHSPrioritized, LHS.getName()) >
103+
std::tuple(RHSPrioritized, RHS.getName());
114104
};
115-
auto SortedChildren = std::set<ASTNode, decltype(comp)>(comp);
105+
auto SortedChildren = std::set<ASTNode, decltype(Comp)>(Comp);
116106

117-
for (; i != e; ++i) {
118-
SortedChildren.insert(i->second);
107+
for (; II != E; ++II) {
108+
SortedChildren.insert(II->second);
119109
}
120110

121111
for (const auto &Child : SortedChildren) {
122112
bool Abstract = Child.isAbstract();
123-
std::string NodeName = macroName(std::string(Child.getName()));
113+
std::string NodeName = macroName(Child.getName());
124114

125115
OS << "#ifndef " << NodeName << "\n";
126116
OS << "# define " << NodeName << "(Type, Base) "
@@ -144,9 +134,8 @@ std::pair<ASTNode, ASTNode> ClangASTNodesEmitter::EmitNode(raw_ostream &OS,
144134

145135
// If there aren't first/last nodes, it must be because there were no
146136
// children and this node was abstract, which is not a sensible combination.
147-
if (!First) {
137+
if (!First)
148138
PrintFatalError(Base.getLoc(), "abstract node has no children");
149-
}
150139
assert(Last && "set First without Last");
151140

152141
if (HasChildren) {
@@ -169,7 +158,7 @@ void ClangASTNodesEmitter::deriveChildTree() {
169158
// Emit statements
170159
for (const Record *R : Records.getAllDerivedDefinitions(NodeClassName)) {
171160
if (auto B = R->getValueAsOptionalDef(BaseFieldName))
172-
Tree.insert(std::make_pair(B, R));
161+
Tree.insert({B, R});
173162
else if (Root)
174163
PrintFatalError(R->getLoc(),
175164
Twine("multiple root nodes in \"") + NodeClassName
@@ -222,10 +211,9 @@ void printDeclContext(const std::multimap<const Record *, const Record *> &Tree,
222211
const Record *DeclContext, raw_ostream &OS) {
223212
if (!DeclContext->getValueAsBit(AbstractFieldName))
224213
OS << "DECL_CONTEXT(" << DeclContext->getName() << ")\n";
225-
auto i = Tree.lower_bound(DeclContext);
226-
auto end = Tree.upper_bound(DeclContext);
227-
for (; i != end; ++i) {
228-
printDeclContext(Tree, i->second, OS);
214+
auto [II, E] = Tree.equal_range(DeclContext);
215+
for (; II != E; ++II) {
216+
printDeclContext(Tree, II->second, OS);
229217
}
230218
}
231219

@@ -244,7 +232,7 @@ void clang::EmitClangDeclContext(const RecordKeeper &Records, raw_ostream &OS) {
244232

245233
for (const Record *R : Records.getAllDerivedDefinitions(DeclNodeClassName)) {
246234
if (auto *B = R->getValueAsOptionalDef(BaseFieldName))
247-
Tree.insert(std::make_pair(B, R));
235+
Tree.insert({B, R});
248236
}
249237

250238
for (const Record *DeclContext :

0 commit comments

Comments
 (0)