Skip to content

Commit 7b5e285

Browse files
authored
[NFC][Clang] Use range for loops in ClangDiagnosticsEmitter (#115573)
Use range based for loops in Clang diagnostics emitter.
1 parent 1331750 commit 7b5e285

File tree

1 file changed

+70
-105
lines changed

1 file changed

+70
-105
lines changed

clang/utils/TableGen/ClangDiagnosticsEmitter.cpp

Lines changed: 70 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];
@@ -160,9 +152,7 @@ using DiagsInGroupTy = std::map<std::string, GroupInfo, std::less<>>;
160152
static void groupDiagnostics(ArrayRef<const Record *> Diags,
161153
ArrayRef<const Record *> DiagGroups,
162154
DiagsInGroupTy &DiagsInGroup) {
163-
164-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
165-
const Record *R = Diags[i];
155+
for (const Record *R : Diags) {
166156
const auto *DI = dyn_cast<DefInit>(R->getValueInit("Group"));
167157
if (!DI)
168158
continue;
@@ -175,8 +165,7 @@ static void groupDiagnostics(ArrayRef<const Record *> Diags,
175165

176166
// Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
177167
// groups (these are warnings that GCC supports that clang never produces).
178-
for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
179-
const Record *Group = DiagGroups[i];
168+
for (const Record *Group : DiagGroups) {
180169
GroupInfo &GI =
181170
DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
182171
GI.GroupName = Group->getName();
@@ -187,10 +176,8 @@ static void groupDiagnostics(ArrayRef<const Record *> Diags,
187176
}
188177

189178
// Assign unique ID numbers to the groups.
190-
unsigned IDNo = 0;
191-
for (std::map<std::string, GroupInfo>::iterator
192-
I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
193-
I->second.IDNo = IDNo;
179+
for (auto [IdNo, Iter] : enumerate(DiagsInGroup))
180+
Iter.second.IDNo = IdNo;
194181

195182
// Warn if the same group is defined more than once (including implicitly).
196183
for (auto &Group : DiagsInGroup) {
@@ -299,26 +286,21 @@ bool InferPedantic::isSubGroupOfGroup(const Record *Group, StringRef GName) {
299286
if (GName == GroupName)
300287
return true;
301288

302-
const std::vector<const Record *> &Parents =
303-
DiagGroupParents.getParents(Group);
304-
for (unsigned i = 0, e = Parents.size(); i != e; ++i)
305-
if (isSubGroupOfGroup(Parents[i], GName))
289+
for (const Record *Parent : DiagGroupParents.getParents(Group))
290+
if (isSubGroupOfGroup(Parent, GName))
306291
return true;
307292

308293
return false;
309294
}
310295

311296
/// Determine if the diagnostic is an extension.
312297
bool InferPedantic::isExtension(const Record *Diag) {
313-
const std::string &ClsName =
314-
std::string(Diag->getValueAsDef("Class")->getName());
315-
return ClsName == "CLASS_EXTENSION";
298+
return Diag->getValueAsDef("Class")->getName() == "CLASS_EXTENSION";
316299
}
317300

318301
bool InferPedantic::isOffByDefault(const Record *Diag) {
319-
const std::string &DefSeverity = std::string(
320-
Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name"));
321-
return DefSeverity == "Ignored";
302+
return Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name") ==
303+
"Ignored";
322304
}
323305

324306
bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
@@ -344,37 +326,32 @@ void InferPedantic::markGroup(const Record *Group) {
344326
// covered by -Wpedantic, increment the count of parent groups. Once the
345327
// group's count is equal to the number of subgroups and diagnostics in
346328
// that group, we can safely add this group to -Wpedantic.
347-
if (groupInPedantic(Group, /* increment */ true)) {
348-
const std::vector<const Record *> &Parents =
349-
DiagGroupParents.getParents(Group);
350-
for (unsigned i = 0, e = Parents.size(); i != e; ++i)
351-
markGroup(Parents[i]);
352-
}
329+
if (groupInPedantic(Group, /* increment */ true))
330+
for (const Record *Parent : DiagGroupParents.getParents(Group))
331+
markGroup(Parent);
353332
}
354333

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

373351
// Compute the set of diagnostics that are directly in -Wpedantic. We
374352
// march through Diags a second time to ensure the results are emitted
375353
// in deterministic order.
376-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
377-
const Record *R = Diags[i];
354+
for (const Record *R : Diags) {
378355
if (!DiagsSet.count(R))
379356
continue;
380357
// Check if the group is implicitly in -Wpedantic. If so,
@@ -388,9 +365,8 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
388365
// -Wpedantic. Include it in -Wpedantic directly.
389366
if (auto *V = DiagsInPedantic.dyn_cast<RecordVec *>())
390367
V->push_back(R);
391-
else {
392-
DiagsInPedantic.get<RecordSet*>()->insert(R);
393-
}
368+
else
369+
DiagsInPedantic.get<RecordSet *>()->insert(R);
394370
}
395371

396372
if (!GroupsInPedantic)
@@ -399,8 +375,7 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
399375
// Compute the set of groups that are directly in -Wpedantic. We
400376
// march through the groups to ensure the results are emitted
401377
/// in a deterministc order.
402-
for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
403-
const Record *Group = DiagGroups[i];
378+
for (const Record *Group : DiagGroups) {
404379
if (!groupInPedantic(Group))
405380
continue;
406381

@@ -417,9 +392,8 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
417392

418393
if (auto *V = GroupsInPedantic.dyn_cast<RecordVec *>())
419394
V->push_back(Group);
420-
else {
421-
GroupsInPedantic.get<RecordSet*>()->insert(Group);
422-
}
395+
else
396+
GroupsInPedantic.get<RecordSet *>()->insert(Group);
423397
}
424398
}
425399

@@ -972,10 +946,11 @@ struct DiagTextPrinter : DiagTextVisitor<DiagTextPrinter> {
972946
void VisitPlural(PluralPiece *P) {
973947
Result += "%plural{";
974948
assert(P->Options.size() == P->OptionPrefixes.size());
975-
for (unsigned I = 0, End = P->Options.size(); I < End; ++I) {
976-
if (P->OptionPrefixes[I])
977-
Visit(P->OptionPrefixes[I]);
978-
Visit(P->Options[I]);
949+
for (const auto [Prefix, Option] :
950+
zip_equal(P->OptionPrefixes, P->Options)) {
951+
if (Prefix)
952+
Visit(Prefix);
953+
Visit(Option);
979954
Result += "|";
980955
}
981956
if (!P->Options.empty())
@@ -1555,15 +1530,13 @@ static void emitDiagSubGroups(DiagsInGroupTy &DiagsInGroup,
15551530
RecordVec &GroupsInPedantic, raw_ostream &OS) {
15561531
OS << "static const int16_t DiagSubGroups[] = {\n"
15571532
<< " /* Empty */ -1,\n";
1558-
for (auto const &I : DiagsInGroup) {
1559-
const bool IsPedantic = I.first == "pedantic";
1560-
1561-
const std::vector<std::string> &SubGroups = I.second.SubGroups;
1533+
for (auto const &[Name, Group] : DiagsInGroup) {
1534+
const bool IsPedantic = Name == "pedantic";
1535+
const std::vector<std::string> &SubGroups = Group.SubGroups;
15621536
if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
1563-
OS << " /* DiagSubGroup" << I.second.IDNo << " */ ";
1537+
OS << " /* DiagSubGroup" << Group.IDNo << " */ ";
15641538
for (auto const &SubGroup : SubGroups) {
1565-
std::map<std::string, GroupInfo>::const_iterator RI =
1566-
DiagsInGroup.find(SubGroup);
1539+
auto RI = DiagsInGroup.find(SubGroup);
15671540
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15681541
OS << RI->second.IDNo << ", ";
15691542
}
@@ -1572,8 +1545,7 @@ static void emitDiagSubGroups(DiagsInGroupTy &DiagsInGroup,
15721545
for (auto const &Group : GroupsInPedantic) {
15731546
const std::string &GroupName =
15741547
std::string(Group->getValueAsString("GroupName"));
1575-
std::map<std::string, GroupInfo>::const_iterator RI =
1576-
DiagsInGroup.find(GroupName);
1548+
auto RI = DiagsInGroup.find(GroupName);
15771549
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15781550
OS << RI->second.IDNo << ", ";
15791551
}
@@ -1607,12 +1579,12 @@ static void emitDiagArrays(DiagsInGroupTy &DiagsInGroup,
16071579
RecordVec &DiagsInPedantic, raw_ostream &OS) {
16081580
OS << "static const int16_t DiagArrays[] = {\n"
16091581
<< " /* Empty */ -1,\n";
1610-
for (auto const &I : DiagsInGroup) {
1611-
const bool IsPedantic = I.first == "pedantic";
1582+
for (const auto &[Name, Group] : DiagsInGroup) {
1583+
const bool IsPedantic = Name == "pedantic";
16121584

1613-
const std::vector<const Record *> &V = I.second.DiagsInGroup;
1585+
const std::vector<const Record *> &V = Group.DiagsInGroup;
16141586
if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
1615-
OS << " /* DiagArray" << I.second.IDNo << " */ ";
1587+
OS << " /* DiagArray" << Group.IDNo << " */ ";
16161588
for (auto *Record : V)
16171589
OS << "diag::" << Record->getName() << ", ";
16181590
// Emit the diagnostics implicitly in "pedantic".
@@ -1694,31 +1666,29 @@ static void emitDiagTable(DiagsInGroupTy &DiagsInGroup,
16941666

16951667
OS << "\n#ifdef DIAG_ENTRY\n";
16961668
unsigned SubGroupIndex = 1, DiagArrayIndex = 1;
1697-
for (auto const &I: DiagsInGroup) {
1669+
for (auto const &[Name, GroupInfo] : DiagsInGroup) {
16981670
// Group option string.
16991671
OS << "DIAG_ENTRY(";
1700-
OS << I.second.GroupName << " /* ";
1701-
1702-
if (I.first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
1703-
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1704-
"0123456789!@#$%^*-+=:?") !=
1705-
std::string::npos)
1706-
PrintFatalError("Invalid character in diagnostic group '" + I.first +
1707-
"'");
1708-
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 << " */, ";
17091679
// Store a pascal-style length byte at the beginning of the string.
1710-
std::string Name = char(I.first.size()) + I.first;
1711-
OS << *GroupNames.GetStringOffset(Name) << ", ";
1680+
std::string PascalName = char(Name.size()) + Name;
1681+
OS << *GroupNames.GetStringOffset(PascalName) << ", ";
17121682

17131683
// Special handling for 'pedantic'.
1714-
const bool IsPedantic = I.first == "pedantic";
1684+
const bool IsPedantic = Name == "pedantic";
17151685

17161686
// Diagnostics in the group.
1717-
const std::vector<const Record *> &V = I.second.DiagsInGroup;
1687+
const std::vector<const Record *> &V = GroupInfo.DiagsInGroup;
17181688
const bool hasDiags =
17191689
!V.empty() || (IsPedantic && !DiagsInPedantic.empty());
17201690
if (hasDiags) {
1721-
OS << "/* DiagArray" << I.second.IDNo << " */ " << DiagArrayIndex
1691+
OS << "/* DiagArray" << GroupInfo.IDNo << " */ " << DiagArrayIndex
17221692
<< ", ";
17231693
if (IsPedantic)
17241694
DiagArrayIndex += DiagsInPedantic.size();
@@ -1728,11 +1698,11 @@ static void emitDiagTable(DiagsInGroupTy &DiagsInGroup,
17281698
}
17291699

17301700
// Subgroups.
1731-
const std::vector<std::string> &SubGroups = I.second.SubGroups;
1701+
const std::vector<std::string> &SubGroups = GroupInfo.SubGroups;
17321702
const bool hasSubGroups =
17331703
!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty());
17341704
if (hasSubGroups) {
1735-
OS << "/* DiagSubGroup" << I.second.IDNo << " */ " << SubGroupIndex
1705+
OS << "/* DiagSubGroup" << GroupInfo.IDNo << " */ " << SubGroupIndex
17361706
<< ", ";
17371707
if (IsPedantic)
17381708
SubGroupIndex += GroupsInPedantic.size();
@@ -1741,7 +1711,7 @@ static void emitDiagTable(DiagsInGroupTy &DiagsInGroup,
17411711
OS << "0, ";
17421712
}
17431713

1744-
std::string Documentation = I.second.Defs.back()
1714+
std::string Documentation = GroupInfo.Defs.back()
17451715
->getValue("Documentation")
17461716
->getValue()
17471717
->getAsUnquotedString();
@@ -1834,20 +1804,15 @@ void clang::EmitClangDiagsIndexName(const RecordKeeper &Records,
18341804

18351805
std::vector<RecordIndexElement> Index;
18361806
Index.reserve(Diags.size());
1837-
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
1838-
const Record &R = *(Diags[i]);
1839-
Index.push_back(RecordIndexElement(R));
1840-
}
1807+
for (const Record *R : Diags)
1808+
Index.push_back(RecordIndexElement(*R));
18411809

18421810
sort(Index, [](const RecordIndexElement &Lhs, const RecordIndexElement &Rhs) {
18431811
return Lhs.Name < Rhs.Name;
18441812
});
18451813

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

18531818
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)