Skip to content

Commit 8625eb0

Browse files
authored
[clang][TableGen] Change OptionDoc Emitter to use const RecordKeeper (#108216)
Change OptionDoc 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 7c53a7a commit 8625eb0

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

clang/utils/TableGen/ClangOptionDocEmitter.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ using namespace llvm;
2424

2525
namespace {
2626
struct DocumentedOption {
27-
Record *Option;
28-
std::vector<Record*> Aliases;
27+
const Record *Option;
28+
std::vector<const Record *> Aliases;
2929
};
3030
struct DocumentedGroup;
3131
struct Documentation {
@@ -37,7 +37,7 @@ struct Documentation {
3737
}
3838
};
3939
struct DocumentedGroup : Documentation {
40-
Record *Group;
40+
const Record *Group;
4141
};
4242

4343
static bool hasFlag(const Record *Option, StringRef OptionFlag,
@@ -63,25 +63,25 @@ static bool isOptionVisible(const Record *Option, const Record *DocInfo) {
6363
}
6464

6565
// Reorganize the records into a suitable form for emitting documentation.
66-
Documentation extractDocumentation(RecordKeeper &Records,
66+
Documentation extractDocumentation(const RecordKeeper &Records,
6767
const Record *DocInfo) {
6868
Documentation Result;
6969

7070
// Build the tree of groups. The root in the tree is the fake option group
7171
// (Record*)nullptr, which contains all top-level groups and options.
72-
std::map<Record*, std::vector<Record*> > OptionsInGroup;
73-
std::map<Record*, std::vector<Record*> > GroupsInGroup;
74-
std::map<Record*, std::vector<Record*> > Aliases;
72+
std::map<const Record *, std::vector<const Record *>> OptionsInGroup;
73+
std::map<const Record *, std::vector<const Record *>> GroupsInGroup;
74+
std::map<const Record *, std::vector<const Record *>> Aliases;
7575

76-
std::map<std::string, Record*> OptionsByName;
77-
for (Record *R : Records.getAllDerivedDefinitions("Option"))
76+
std::map<std::string, const Record *> OptionsByName;
77+
for (const Record *R : Records.getAllDerivedDefinitions("Option"))
7878
OptionsByName[std::string(R->getValueAsString("Name"))] = R;
7979

80-
auto Flatten = [](Record *R) {
80+
auto Flatten = [](const Record *R) {
8181
return R->getValue("DocFlatten") && R->getValueAsBit("DocFlatten");
8282
};
8383

84-
auto SkipFlattened = [&](Record *R) -> Record* {
84+
auto SkipFlattened = [&](const Record *R) -> const Record * {
8585
while (R && Flatten(R)) {
8686
auto *G = dyn_cast<DefInit>(R->getValueInit("Group"));
8787
if (!G)
@@ -91,17 +91,17 @@ Documentation extractDocumentation(RecordKeeper &Records,
9191
return R;
9292
};
9393

94-
for (Record *R : Records.getAllDerivedDefinitions("OptionGroup")) {
94+
for (const Record *R : Records.getAllDerivedDefinitions("OptionGroup")) {
9595
if (Flatten(R))
9696
continue;
9797

98-
Record *Group = nullptr;
98+
const Record *Group = nullptr;
9999
if (auto *G = dyn_cast<DefInit>(R->getValueInit("Group")))
100100
Group = SkipFlattened(G->getDef());
101101
GroupsInGroup[Group].push_back(R);
102102
}
103103

104-
for (Record *R : Records.getAllDerivedDefinitions("Option")) {
104+
for (const Record *R : Records.getAllDerivedDefinitions("Option")) {
105105
if (auto *A = dyn_cast<DefInit>(R->getValueInit("Alias"))) {
106106
Aliases[A->getDef()].push_back(R);
107107
continue;
@@ -120,33 +120,33 @@ Documentation extractDocumentation(RecordKeeper &Records,
120120
}
121121
}
122122

123-
Record *Group = nullptr;
123+
const Record *Group = nullptr;
124124
if (auto *G = dyn_cast<DefInit>(R->getValueInit("Group")))
125125
Group = SkipFlattened(G->getDef());
126126
OptionsInGroup[Group].push_back(R);
127127
}
128128

129-
auto CompareByName = [](Record *A, Record *B) {
129+
auto CompareByName = [](const Record *A, const Record *B) {
130130
return A->getValueAsString("Name") < B->getValueAsString("Name");
131131
};
132132

133-
auto CompareByLocation = [](Record *A, Record *B) {
133+
auto CompareByLocation = [](const Record *A, const Record *B) {
134134
return A->getLoc()[0].getPointer() < B->getLoc()[0].getPointer();
135135
};
136136

137-
auto DocumentationForOption = [&](Record *R) -> DocumentedOption {
137+
auto DocumentationForOption = [&](const Record *R) -> DocumentedOption {
138138
auto &A = Aliases[R];
139139
llvm::sort(A, CompareByName);
140140
return {R, std::move(A)};
141141
};
142142

143-
std::function<Documentation(Record *)> DocumentationForGroup =
144-
[&](Record *R) -> Documentation {
143+
std::function<Documentation(const Record *)> DocumentationForGroup =
144+
[&](const Record *R) -> Documentation {
145145
Documentation D;
146146

147147
auto &Groups = GroupsInGroup[R];
148148
llvm::sort(Groups, CompareByLocation);
149-
for (Record *G : Groups) {
149+
for (const Record *G : Groups) {
150150
D.Groups.emplace_back();
151151
D.Groups.back().Group = G;
152152
Documentation &Base = D.Groups.back();
@@ -157,7 +157,7 @@ Documentation extractDocumentation(RecordKeeper &Records,
157157

158158
auto &Options = OptionsInGroup[R];
159159
llvm::sort(Options, CompareByName);
160-
for (Record *O : Options)
160+
for (const Record *O : Options)
161161
if (isOptionVisible(O, DocInfo))
162162
D.Options.push_back(DocumentationForOption(O));
163163

@@ -444,7 +444,7 @@ void emitDocumentation(int Depth, const Documentation &Doc,
444444

445445
} // namespace
446446

447-
void clang::EmitClangOptDocs(RecordKeeper &Records, raw_ostream &OS) {
447+
void clang::EmitClangOptDocs(const RecordKeeper &Records, raw_ostream &OS) {
448448
const Record *DocInfo = Records.getDef("GlobalDocumentation");
449449
if (!DocInfo) {
450450
PrintFatalError("The GlobalDocumentation top-level definition is missing, "

clang/utils/TableGen/TableGenBackends.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void EmitCdeBuiltinAliases(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
143143

144144
void EmitClangAttrDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
145145
void EmitClangDiagDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
146-
void EmitClangOptDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
146+
void EmitClangOptDocs(const llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
147147

148148
void EmitClangOpenCLBuiltins(const llvm::RecordKeeper &Records,
149149
llvm::raw_ostream &OS);

0 commit comments

Comments
 (0)