Skip to content

Commit c8a991f

Browse files
committed
[NFC][Clang] Use range for loops in ClangDiagnosticsEmitter
Use range based for loops in Clang diagnostics emitter.
1 parent 44076c9 commit c8a991f

File tree

1 file changed

+72
-105
lines changed

1 file changed

+72
-105
lines changed

clang/utils/TableGen/ClangDiagnosticsEmitter.cpp

Lines changed: 72 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,12 @@ class DiagGroupParentMap {
4444

4545
public:
4646
DiagGroupParentMap(const RecordKeeper &records) : Records(records) {
47-
ArrayRef<const Record *> DiagGroups =
48-
Records.getAllDerivedDefinitions("DiagGroup");
49-
for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
50-
std::vector<const Record *> SubGroups =
51-
DiagGroups[i]->getValueAsListOfDefs("SubGroups");
52-
for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
53-
Mapping[SubGroups[j]].push_back(DiagGroups[i]);
54-
}
47+
for (const Record *Group : Records.getAllDerivedDefinitions("DiagGroup"))
48+
for (const Record *SubGroup : Group->getValueAsListOfDefs("SubGroups"))
49+
Mapping[SubGroup].push_back(Group);
5550
}
5651

57-
const std::vector<const Record *> &getParents(const Record *Group) {
52+
ArrayRef<const Record *> getParents(const Record *Group) {
5853
return Mapping[Group];
5954
}
6055
};
@@ -69,10 +64,8 @@ getCategoryFromDiagGroup(const Record *Group,
6964

7065
// The diag group may the subgroup of one or more other diagnostic groups,
7166
// check these for a category as well.
72-
const std::vector<const Record *> &Parents =
73-
DiagGroupParents.getParents(Group);
74-
for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
75-
CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
67+
for (const Record *Parent : DiagGroupParents.getParents(Group)) {
68+
CatName = getCategoryFromDiagGroup(Parent, DiagGroupParents);
7669
if (!CatName.empty()) return CatName;
7770
}
7871
return "";
@@ -107,10 +100,9 @@ namespace {
107100
CategoryStrings.push_back("");
108101
CategoryIDs[""] = 0;
109102

110-
ArrayRef<const Record *> Diags =
111-
Records.getAllDerivedDefinitions("Diagnostic");
112-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
113-
std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
103+
for (const Record *Diag :
104+
Records.getAllDerivedDefinitions("Diagnostic")) {
105+
std::string Category = getDiagnosticCategory(Diag, ParentInfo);
114106
if (Category.empty()) continue; // Skip diags with no category.
115107

116108
unsigned &ID = CategoryIDs[Category];
@@ -158,9 +150,7 @@ static bool diagGroupBeforeByName(const Record *LHS, const Record *RHS) {
158150
static void groupDiagnostics(ArrayRef<const Record *> Diags,
159151
ArrayRef<const Record *> DiagGroups,
160152
std::map<std::string, GroupInfo> &DiagsInGroup) {
161-
162-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
163-
const Record *R = Diags[i];
153+
for (const Record *R : Diags) {
164154
const auto *DI = dyn_cast<DefInit>(R->getValueInit("Group"));
165155
if (!DI)
166156
continue;
@@ -173,8 +163,7 @@ static void groupDiagnostics(ArrayRef<const Record *> Diags,
173163

174164
// Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
175165
// groups (these are warnings that GCC supports that clang never produces).
176-
for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
177-
const Record *Group = DiagGroups[i];
166+
for (const Record *Group : DiagGroups) {
178167
GroupInfo &GI =
179168
DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
180169
GI.GroupName = Group->getName();
@@ -185,10 +174,8 @@ static void groupDiagnostics(ArrayRef<const Record *> Diags,
185174
}
186175

187176
// Assign unique ID numbers to the groups.
188-
unsigned IDNo = 0;
189-
for (std::map<std::string, GroupInfo>::iterator
190-
I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
191-
I->second.IDNo = IDNo;
177+
for (auto [IdNo, Iter] : enumerate(DiagsInGroup))
178+
Iter.second.IDNo = IdNo;
192179

193180
// Warn if the same group is defined more than once (including implicitly).
194181
for (auto &Group : DiagsInGroup) {
@@ -297,26 +284,21 @@ bool InferPedantic::isSubGroupOfGroup(const Record *Group, StringRef GName) {
297284
if (GName == GroupName)
298285
return true;
299286

300-
const std::vector<const Record *> &Parents =
301-
DiagGroupParents.getParents(Group);
302-
for (unsigned i = 0, e = Parents.size(); i != e; ++i)
303-
if (isSubGroupOfGroup(Parents[i], GName))
287+
for (const Record *Parent : DiagGroupParents.getParents(Group))
288+
if (isSubGroupOfGroup(Parent, GName))
304289
return true;
305290

306291
return false;
307292
}
308293

309294
/// Determine if the diagnostic is an extension.
310295
bool InferPedantic::isExtension(const Record *Diag) {
311-
const std::string &ClsName =
312-
std::string(Diag->getValueAsDef("Class")->getName());
313-
return ClsName == "CLASS_EXTENSION";
296+
return Diag->getValueAsDef("Class")->getName() == "CLASS_EXTENSION";
314297
}
315298

316299
bool InferPedantic::isOffByDefault(const Record *Diag) {
317-
const std::string &DefSeverity = std::string(
318-
Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name"));
319-
return DefSeverity == "Ignored";
300+
return Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name") ==
301+
"Ignored";
320302
}
321303

322304
bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
@@ -342,37 +324,32 @@ void InferPedantic::markGroup(const Record *Group) {
342324
// covered by -Wpedantic, increment the count of parent groups. Once the
343325
// group's count is equal to the number of subgroups and diagnostics in
344326
// that group, we can safely add this group to -Wpedantic.
345-
if (groupInPedantic(Group, /* increment */ true)) {
346-
const std::vector<const Record *> &Parents =
347-
DiagGroupParents.getParents(Group);
348-
for (unsigned i = 0, e = Parents.size(); i != e; ++i)
349-
markGroup(Parents[i]);
350-
}
327+
if (groupInPedantic(Group, /* increment */ true))
328+
for (const Record *Parent : DiagGroupParents.getParents(Group))
329+
markGroup(Parent);
351330
}
352331

353332
void InferPedantic::compute(VecOrSet DiagsInPedantic,
354333
VecOrSet GroupsInPedantic) {
355334
// All extensions that are not on by default are implicitly in the
356335
// "pedantic" group. For those that aren't explicitly included in -Wpedantic,
357336
// mark them for consideration to be included in -Wpedantic directly.
358-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
359-
const Record *R = Diags[i];
360-
if (isExtension(R) && isOffByDefault(R)) {
361-
DiagsSet.insert(R);
362-
if (const auto *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
363-
const Record *GroupRec = Group->getDef();
364-
if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
365-
markGroup(GroupRec);
366-
}
337+
for (const Record *R : Diags) {
338+
if (!isExtension(R) || !isOffByDefault(R))
339+
continue;
340+
DiagsSet.insert(R);
341+
if (const auto *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
342+
const Record *GroupRec = Group->getDef();
343+
if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
344+
markGroup(GroupRec);
367345
}
368346
}
369347
}
370348

371349
// Compute the set of diagnostics that are directly in -Wpedantic. We
372350
// march through Diags a second time to ensure the results are emitted
373351
// in deterministic order.
374-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
375-
const Record *R = Diags[i];
352+
for (const Record *R : Diags) {
376353
if (!DiagsSet.count(R))
377354
continue;
378355
// Check if the group is implicitly in -Wpedantic. If so,
@@ -386,9 +363,8 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
386363
// -Wpedantic. Include it in -Wpedantic directly.
387364
if (auto *V = DiagsInPedantic.dyn_cast<RecordVec *>())
388365
V->push_back(R);
389-
else {
390-
DiagsInPedantic.get<RecordSet*>()->insert(R);
391-
}
366+
else
367+
DiagsInPedantic.get<RecordSet *>()->insert(R);
392368
}
393369

394370
if (!GroupsInPedantic)
@@ -397,8 +373,7 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
397373
// Compute the set of groups that are directly in -Wpedantic. We
398374
// march through the groups to ensure the results are emitted
399375
/// in a deterministc order.
400-
for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
401-
const Record *Group = DiagGroups[i];
376+
for (const Record *Group : DiagGroups) {
402377
if (!groupInPedantic(Group))
403378
continue;
404379

@@ -415,9 +390,8 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
415390

416391
if (auto *V = GroupsInPedantic.dyn_cast<RecordVec *>())
417392
V->push_back(Group);
418-
else {
419-
GroupsInPedantic.get<RecordSet*>()->insert(Group);
420-
}
393+
else
394+
GroupsInPedantic.get<RecordSet *>()->insert(Group);
421395
}
422396
}
423397

@@ -970,10 +944,11 @@ struct DiagTextPrinter : DiagTextVisitor<DiagTextPrinter> {
970944
void VisitPlural(PluralPiece *P) {
971945
Result += "%plural{";
972946
assert(P->Options.size() == P->OptionPrefixes.size());
973-
for (unsigned I = 0, End = P->Options.size(); I < End; ++I) {
974-
if (P->OptionPrefixes[I])
975-
Visit(P->OptionPrefixes[I]);
976-
Visit(P->Options[I]);
947+
for (const auto [Prefix, Option] :
948+
zip_equal(P->OptionPrefixes, P->Options)) {
949+
if (Prefix)
950+
Visit(Prefix);
951+
Visit(Option);
977952
Result += "|";
978953
}
979954
if (!P->Options.empty())
@@ -1553,15 +1528,13 @@ static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
15531528
RecordVec &GroupsInPedantic, raw_ostream &OS) {
15541529
OS << "static const int16_t DiagSubGroups[] = {\n"
15551530
<< " /* Empty */ -1,\n";
1556-
for (auto const &I : DiagsInGroup) {
1557-
const bool IsPedantic = I.first == "pedantic";
1558-
1559-
const std::vector<std::string> &SubGroups = I.second.SubGroups;
1531+
for (auto const &[Name, Group] : DiagsInGroup) {
1532+
const bool IsPedantic = Name == "pedantic";
1533+
const std::vector<std::string> &SubGroups = Group.SubGroups;
15601534
if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
1561-
OS << " /* DiagSubGroup" << I.second.IDNo << " */ ";
1535+
OS << " /* DiagSubGroup" << Group.IDNo << " */ ";
15621536
for (auto const &SubGroup : SubGroups) {
1563-
std::map<std::string, GroupInfo>::const_iterator RI =
1564-
DiagsInGroup.find(SubGroup);
1537+
auto RI = DiagsInGroup.find(SubGroup);
15651538
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15661539
OS << RI->second.IDNo << ", ";
15671540
}
@@ -1570,8 +1543,7 @@ static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
15701543
for (auto const &Group : GroupsInPedantic) {
15711544
const std::string &GroupName =
15721545
std::string(Group->getValueAsString("GroupName"));
1573-
std::map<std::string, GroupInfo>::const_iterator RI =
1574-
DiagsInGroup.find(GroupName);
1546+
auto RI = DiagsInGroup.find(GroupName);
15751547
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15761548
OS << RI->second.IDNo << ", ";
15771549
}
@@ -1601,16 +1573,18 @@ static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
16011573
/// };
16021574
/// \endcode
16031575
///
1576+
#include <type_traits>
1577+
16041578
static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
16051579
RecordVec &DiagsInPedantic, raw_ostream &OS) {
16061580
OS << "static const int16_t DiagArrays[] = {\n"
16071581
<< " /* Empty */ -1,\n";
1608-
for (auto const &I : DiagsInGroup) {
1609-
const bool IsPedantic = I.first == "pedantic";
1582+
for (const auto &[Name, Group] : DiagsInGroup) {
1583+
const bool IsPedantic = Name == "pedantic";
16101584

1611-
const std::vector<const Record *> &V = I.second.DiagsInGroup;
1585+
const std::vector<const Record *> &V = Group.DiagsInGroup;
16121586
if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
1613-
OS << " /* DiagArray" << I.second.IDNo << " */ ";
1587+
OS << " /* DiagArray" << Group.IDNo << " */ ";
16141588
for (auto *Record : V)
16151589
OS << "diag::" << Record->getName() << ", ";
16161590
// Emit the diagnostics implicitly in "pedantic".
@@ -1692,31 +1666,29 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
16921666

16931667
OS << "\n#ifdef DIAG_ENTRY\n";
16941668
unsigned SubGroupIndex = 1, DiagArrayIndex = 1;
1695-
for (auto const &I: DiagsInGroup) {
1669+
for (auto const &[Name, GroupInfo] : DiagsInGroup) {
16961670
// Group option string.
16971671
OS << "DIAG_ENTRY(";
1698-
OS << I.second.GroupName << " /* ";
1699-
1700-
if (I.first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
1701-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1702-
"0123456789!@#$%^*-+=:?") !=
1703-
std::string::npos)
1704-
PrintFatalError("Invalid character in diagnostic group '" + I.first +
1705-
"'");
1706-
OS << I.first << " */, ";
1672+
OS << GroupInfo.GroupName << " /* ";
1673+
1674+
if (Name.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
1675+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1676+
"0123456789!@#$%^*-+=:?") != std::string::npos)
1677+
PrintFatalError("Invalid character in diagnostic group '" + Name + "'");
1678+
OS << Name << " */, ";
17071679
// Store a pascal-style length byte at the beginning of the string.
1708-
std::string Name = char(I.first.size()) + I.first;
1709-
OS << *GroupNames.GetStringOffset(Name) << ", ";
1680+
std::string PascalName = char(Name.size()) + Name;
1681+
OS << *GroupNames.GetStringOffset(PascalName) << ", ";
17101682

17111683
// Special handling for 'pedantic'.
1712-
const bool IsPedantic = I.first == "pedantic";
1684+
const bool IsPedantic = Name == "pedantic";
17131685

17141686
// Diagnostics in the group.
1715-
const std::vector<const Record *> &V = I.second.DiagsInGroup;
1687+
const std::vector<const Record *> &V = GroupInfo.DiagsInGroup;
17161688
const bool hasDiags =
17171689
!V.empty() || (IsPedantic && !DiagsInPedantic.empty());
17181690
if (hasDiags) {
1719-
OS << "/* DiagArray" << I.second.IDNo << " */ " << DiagArrayIndex
1691+
OS << "/* DiagArray" << GroupInfo.IDNo << " */ " << DiagArrayIndex
17201692
<< ", ";
17211693
if (IsPedantic)
17221694
DiagArrayIndex += DiagsInPedantic.size();
@@ -1726,11 +1698,11 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
17261698
}
17271699

17281700
// Subgroups.
1729-
const std::vector<std::string> &SubGroups = I.second.SubGroups;
1701+
const std::vector<std::string> &SubGroups = GroupInfo.SubGroups;
17301702
const bool hasSubGroups =
17311703
!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty());
17321704
if (hasSubGroups) {
1733-
OS << "/* DiagSubGroup" << I.second.IDNo << " */ " << SubGroupIndex
1705+
OS << "/* DiagSubGroup" << GroupInfo.IDNo << " */ " << SubGroupIndex
17341706
<< ", ";
17351707
if (IsPedantic)
17361708
SubGroupIndex += GroupsInPedantic.size();
@@ -1739,7 +1711,7 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
17391711
OS << "0, ";
17401712
}
17411713

1742-
std::string Documentation = I.second.Defs.back()
1714+
std::string Documentation = GroupInfo.Defs.back()
17431715
->getValue("Documentation")
17441716
->getValue()
17451717
->getAsUnquotedString();
@@ -1832,20 +1804,15 @@ void clang::EmitClangDiagsIndexName(const RecordKeeper &Records,
18321804

18331805
std::vector<RecordIndexElement> Index;
18341806
Index.reserve(Diags.size());
1835-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
1836-
const Record &R = *(Diags[i]);
1837-
Index.push_back(RecordIndexElement(R));
1838-
}
1807+
for (const Record *R : Diags)
1808+
Index.push_back(RecordIndexElement(*R));
18391809

18401810
sort(Index, [](const RecordIndexElement &Lhs, const RecordIndexElement &Rhs) {
18411811
return Lhs.Name < Rhs.Name;
18421812
});
18431813

1844-
for (unsigned i = 0, e = Index.size(); i != e; ++i) {
1845-
const RecordIndexElement &R = Index[i];
1846-
1847-
OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
1848-
}
1814+
for (const auto &Elem : Index)
1815+
OS << "DIAG_NAME_INDEX(" << Elem.Name << ")\n";
18491816
}
18501817

18511818
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)