Skip to content

Commit a858836

Browse files
authored
[clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper (#108209)
Change Diagnostic 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 695cb55 commit a858836

File tree

2 files changed

+59
-51
lines changed

2 files changed

+59
-51
lines changed

clang/utils/TableGen/ClangDiagnosticsEmitter.cpp

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ using namespace llvm;
3939

4040
namespace {
4141
class DiagGroupParentMap {
42-
RecordKeeper &Records;
43-
std::map<const Record*, std::vector<Record*> > Mapping;
42+
const RecordKeeper &Records;
43+
std::map<const Record *, std::vector<const Record *>> Mapping;
44+
4445
public:
45-
DiagGroupParentMap(RecordKeeper &records) : Records(records) {
46-
std::vector<Record*> DiagGroups
47-
= Records.getAllDerivedDefinitions("DiagGroup");
46+
DiagGroupParentMap(const RecordKeeper &records) : Records(records) {
47+
ArrayRef<const Record *> DiagGroups =
48+
Records.getAllDerivedDefinitions("DiagGroup");
4849
for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
4950
std::vector<Record*> SubGroups =
5051
DiagGroups[i]->getValueAsListOfDefs("SubGroups");
@@ -53,7 +54,7 @@ class DiagGroupParentMap {
5354
}
5455
}
5556

56-
const std::vector<Record*> &getParents(const Record *Group) {
57+
const std::vector<const Record *> &getParents(const Record *Group) {
5758
return Mapping[Group];
5859
}
5960
};
@@ -68,7 +69,8 @@ getCategoryFromDiagGroup(const Record *Group,
6869

6970
// The diag group may the subgroup of one or more other diagnostic groups,
7071
// check these for a category as well.
71-
const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
72+
const std::vector<const Record *> &Parents =
73+
DiagGroupParents.getParents(Group);
7274
for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
7375
CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
7476
if (!CatName.empty()) return CatName;
@@ -94,19 +96,19 @@ static std::string getDiagnosticCategory(const Record *R,
9496

9597
namespace {
9698
class DiagCategoryIDMap {
97-
RecordKeeper &Records;
99+
const RecordKeeper &Records;
98100
StringMap<unsigned> CategoryIDs;
99101
std::vector<std::string> CategoryStrings;
100102
public:
101-
DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
103+
DiagCategoryIDMap(const RecordKeeper &records) : Records(records) {
102104
DiagGroupParentMap ParentInfo(Records);
103105

104106
// The zero'th category is "".
105107
CategoryStrings.push_back("");
106108
CategoryIDs[""] = 0;
107109

108-
std::vector<Record*> Diags =
109-
Records.getAllDerivedDefinitions("Diagnostic");
110+
ArrayRef<const Record *> Diags =
111+
Records.getAllDerivedDefinitions("Diagnostic");
110112
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
111113
std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
112114
if (Category.empty()) continue; // Skip diags with no category.
@@ -153,8 +155,8 @@ static bool diagGroupBeforeByName(const Record *LHS, const Record *RHS) {
153155

154156
/// Invert the 1-[0/1] mapping of diags to group into a one to many
155157
/// mapping of groups to diags in the group.
156-
static void groupDiagnostics(const std::vector<Record*> &Diags,
157-
const std::vector<Record*> &DiagGroups,
158+
static void groupDiagnostics(ArrayRef<const Record *> Diags,
159+
ArrayRef<const Record *> DiagGroups,
158160
std::map<std::string, GroupInfo> &DiagsInGroup) {
159161

160162
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
@@ -172,7 +174,7 @@ static void groupDiagnostics(const std::vector<Record*> &Diags,
172174
// Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
173175
// groups (these are warnings that GCC supports that clang never produces).
174176
for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
175-
Record *Group = DiagGroups[i];
177+
const Record *Group = DiagGroups[i];
176178
GroupInfo &GI =
177179
DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
178180
GI.GroupName = Group->getName();
@@ -255,20 +257,18 @@ class InferPedantic {
255257
GMap;
256258

257259
DiagGroupParentMap &DiagGroupParents;
258-
const std::vector<Record*> &Diags;
259-
const std::vector<Record*> DiagGroups;
260+
ArrayRef<const Record *> Diags;
261+
const std::vector<const Record *> DiagGroups;
260262
std::map<std::string, GroupInfo> &DiagsInGroup;
261263
llvm::DenseSet<const Record*> DiagsSet;
262264
GMap GroupCount;
263265
public:
264266
InferPedantic(DiagGroupParentMap &DiagGroupParents,
265-
const std::vector<Record*> &Diags,
266-
const std::vector<Record*> &DiagGroups,
267+
ArrayRef<const Record *> Diags,
268+
ArrayRef<const Record *> DiagGroups,
267269
std::map<std::string, GroupInfo> &DiagsInGroup)
268-
: DiagGroupParents(DiagGroupParents),
269-
Diags(Diags),
270-
DiagGroups(DiagGroups),
271-
DiagsInGroup(DiagsInGroup) {}
270+
: DiagGroupParents(DiagGroupParents), Diags(Diags),
271+
DiagGroups(DiagGroups), DiagsInGroup(DiagsInGroup) {}
272272

273273
/// Compute the set of diagnostics and groups that are immediately
274274
/// in -Wpedantic.
@@ -302,7 +302,8 @@ bool InferPedantic::isSubGroupOfGroup(const Record *Group,
302302
if (GName == GroupName)
303303
return true;
304304

305-
const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
305+
const std::vector<const Record *> &Parents =
306+
DiagGroupParents.getParents(Group);
306307
for (unsigned i = 0, e = Parents.size(); i != e; ++i)
307308
if (isSubGroupOfGroup(Parents[i], GName))
308309
return true;
@@ -347,7 +348,8 @@ void InferPedantic::markGroup(const Record *Group) {
347348
// group's count is equal to the number of subgroups and diagnostics in
348349
// that group, we can safely add this group to -Wpedantic.
349350
if (groupInPedantic(Group, /* increment */ true)) {
350-
const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
351+
const std::vector<const Record *> &Parents =
352+
DiagGroupParents.getParents(Group);
351353
for (unsigned i = 0, e = Parents.size(); i != e; ++i)
352354
markGroup(Parents[i]);
353355
}
@@ -359,7 +361,7 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
359361
// "pedantic" group. For those that aren't explicitly included in -Wpedantic,
360362
// mark them for consideration to be included in -Wpedantic directly.
361363
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
362-
Record *R = Diags[i];
364+
const Record *R = Diags[i];
363365
if (isExtension(R) && isOffByDefault(R)) {
364366
DiagsSet.insert(R);
365367
if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
@@ -375,7 +377,7 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
375377
// march through Diags a second time to ensure the results are emitted
376378
// in deterministic order.
377379
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
378-
Record *R = Diags[i];
380+
const Record *R = Diags[i];
379381
if (!DiagsSet.count(R))
380382
continue;
381383
// Check if the group is implicitly in -Wpedantic. If so,
@@ -401,13 +403,14 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
401403
// march through the groups to ensure the results are emitted
402404
/// in a deterministc order.
403405
for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
404-
Record *Group = DiagGroups[i];
406+
const Record *Group = DiagGroups[i];
405407
if (!groupInPedantic(Group))
406408
continue;
407409

408-
const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
409-
bool AllParentsInPedantic =
410-
llvm::all_of(Parents, [&](Record *R) { return groupInPedantic(R); });
410+
const std::vector<const Record *> &Parents =
411+
DiagGroupParents.getParents(Group);
412+
bool AllParentsInPedantic = llvm::all_of(
413+
Parents, [&](const Record *R) { return groupInPedantic(R); });
411414
// If all the parents are in -Wpedantic, this means that this diagnostic
412415
// group will be indirectly included by -Wpedantic already. In that
413416
// case, do not add it directly to -Wpedantic. If the group has no
@@ -583,7 +586,7 @@ struct DiagnosticTextBuilder {
583586
DiagnosticTextBuilder(DiagnosticTextBuilder const &) = delete;
584587
DiagnosticTextBuilder &operator=(DiagnosticTextBuilder const &) = delete;
585588

586-
DiagnosticTextBuilder(RecordKeeper &Records) {
589+
DiagnosticTextBuilder(const RecordKeeper &Records) {
587590
// Build up the list of substitution records.
588591
for (auto *S : Records.getAllDerivedDefinitions("TextSubstitution")) {
589592
EvaluatingRecordGuard Guard(&EvaluatingRecord, S);
@@ -593,7 +596,7 @@ struct DiagnosticTextBuilder {
593596

594597
// Check that no diagnostic definitions have the same name as a
595598
// substitution.
596-
for (Record *Diag : Records.getAllDerivedDefinitions("Diagnostic")) {
599+
for (const Record *Diag : Records.getAllDerivedDefinitions("Diagnostic")) {
597600
StringRef Name = Diag->getName();
598601
if (Substitutions.count(Name))
599602
llvm::PrintFatalError(
@@ -1407,7 +1410,7 @@ static void verifyDiagnosticWording(const Record &Diag) {
14071410

14081411
/// ClangDiagsDefsEmitter - The top-level class emits .def files containing
14091412
/// declarations of Clang diagnostics.
1410-
void clang::EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
1413+
void clang::EmitClangDiagsDefs(const RecordKeeper &Records, raw_ostream &OS,
14111414
const std::string &Component) {
14121415
// Write the #if guard
14131416
if (!Component.empty()) {
@@ -1421,10 +1424,11 @@ void clang::EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
14211424

14221425
DiagnosticTextBuilder DiagTextBuilder(Records);
14231426

1424-
std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic");
1427+
ArrayRef<const Record *> Diags =
1428+
Records.getAllDerivedDefinitions("Diagnostic");
14251429

1426-
std::vector<Record*> DiagGroups
1427-
= Records.getAllDerivedDefinitions("DiagGroup");
1430+
ArrayRef<const Record *> DiagGroups =
1431+
Records.getAllDerivedDefinitions("DiagGroup");
14281432

14291433
std::map<std::string, GroupInfo> DiagsInGroup;
14301434
groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
@@ -1764,21 +1768,22 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
17641768
/// CATEGORY("Lambda Issue", DiagCat_Lambda_Issue)
17651769
/// #endif
17661770
/// \endcode
1767-
static void emitCategoryTable(RecordKeeper &Records, raw_ostream &OS) {
1771+
static void emitCategoryTable(const RecordKeeper &Records, raw_ostream &OS) {
17681772
DiagCategoryIDMap CategoriesByID(Records);
17691773
OS << "\n#ifdef GET_CATEGORY_TABLE\n";
17701774
for (auto const &C : CategoriesByID)
17711775
OS << "CATEGORY(\"" << C << "\", " << getDiagCategoryEnum(C) << ")\n";
17721776
OS << "#endif // GET_CATEGORY_TABLE\n\n";
17731777
}
17741778

1775-
void clang::EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
1779+
void clang::EmitClangDiagGroups(const RecordKeeper &Records, raw_ostream &OS) {
17761780
// Compute a mapping from a DiagGroup to all of its parents.
17771781
DiagGroupParentMap DGParentMap(Records);
17781782

1779-
std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic");
1783+
ArrayRef<const Record *> Diags =
1784+
Records.getAllDerivedDefinitions("Diagnostic");
17801785

1781-
std::vector<Record *> DiagGroups =
1786+
ArrayRef<const Record *> DiagGroups =
17821787
Records.getAllDerivedDefinitions("DiagGroup");
17831788

17841789
std::map<std::string, GroupInfo> DiagsInGroup;
@@ -1824,9 +1829,10 @@ struct RecordIndexElement
18241829
};
18251830
} // end anonymous namespace.
18261831

1827-
void clang::EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
1828-
const std::vector<Record*> &Diags =
1829-
Records.getAllDerivedDefinitions("Diagnostic");
1832+
void clang::EmitClangDiagsIndexName(const RecordKeeper &Records,
1833+
raw_ostream &OS) {
1834+
ArrayRef<const Record *> Diags =
1835+
Records.getAllDerivedDefinitions("Diagnostic");
18301836

18311837
std::vector<RecordIndexElement> Index;
18321838
Index.reserve(Diags.size());
@@ -1915,7 +1921,7 @@ void writeDiagnosticText(DiagnosticTextBuilder &Builder, const Record *R,
19151921
} // namespace
19161922
} // namespace docs
19171923

1918-
void clang::EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) {
1924+
void clang::EmitClangDiagDocs(const RecordKeeper &Records, raw_ostream &OS) {
19191925
using namespace docs;
19201926

19211927
// Get the documentation introduction paragraph.
@@ -1930,10 +1936,10 @@ void clang::EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) {
19301936

19311937
DiagnosticTextBuilder Builder(Records);
19321938

1933-
std::vector<Record*> Diags =
1939+
ArrayRef<const Record *> Diags =
19341940
Records.getAllDerivedDefinitions("Diagnostic");
19351941

1936-
std::vector<Record*> DiagGroups =
1942+
std::vector<const Record *> DiagGroups =
19371943
Records.getAllDerivedDefinitions("DiagGroup");
19381944
llvm::sort(DiagGroups, diagGroupBeforeByName);
19391945

clang/utils/TableGen/TableGenBackends.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ void EmitClangAttrDocTable(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
7676
void EmitClangBuiltins(const llvm::RecordKeeper &Records,
7777
llvm::raw_ostream &OS);
7878

79-
void EmitClangDiagsDefs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS,
80-
const std::string &Component);
81-
void EmitClangDiagGroups(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
82-
void EmitClangDiagsIndexName(llvm::RecordKeeper &Records,
79+
void EmitClangDiagsDefs(const llvm::RecordKeeper &Records,
80+
llvm::raw_ostream &OS, const std::string &Component);
81+
void EmitClangDiagGroups(const llvm::RecordKeeper &Records,
82+
llvm::raw_ostream &OS);
83+
void EmitClangDiagsIndexName(const llvm::RecordKeeper &Records,
8384
llvm::raw_ostream &OS);
8485

8586
void EmitClangSACheckers(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
@@ -142,7 +143,8 @@ void EmitCdeBuiltinCG(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
142143
void EmitCdeBuiltinAliases(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
143144

144145
void EmitClangAttrDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
145-
void EmitClangDiagDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
146+
void EmitClangDiagDocs(const llvm::RecordKeeper &Records,
147+
llvm::raw_ostream &OS);
146148
void EmitClangOptDocs(const llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
147149

148150
void EmitClangOpenCLBuiltins(const llvm::RecordKeeper &Records,

0 commit comments

Comments
 (0)