Skip to content

Commit 635b24a

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

File tree

1 file changed

+80
-114
lines changed

1 file changed

+80
-114
lines changed

clang/utils/TableGen/ClangDiagnosticsEmitter.cpp

Lines changed: 80 additions & 114 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

@@ -468,7 +442,7 @@ static StringRef getModifierName(ModifierType MT) {
468442
return "objcclass";
469443
case MT_ObjCInstance:
470444
return "objcinstance";
471-
case MT_Unknown:
445+
case MT_Unknown
472446
llvm_unreachable("invalid modifier type");
473447
}
474448
// Unhandled case
@@ -477,16 +451,17 @@ static StringRef getModifierName(ModifierType MT) {
477451

478452
struct Piece {
479453
// This type and its derived classes are move-only.
480-
Piece(PieceKind Kind) : ClassKind(Kind) {}
481-
Piece(Piece const &O) = delete;
482-
Piece &operator=(Piece const &) = delete;
483-
virtual ~Piece() {}
454+
Piece(PieceKind Kind) :
455+
ClassKind(Kind) {}
456+
Piece(Piece const &O) = delete;
457+
Piece &operator=(Piece const &) = delete;
458+
virtual ~Piece() {}
484459

485-
PieceKind getPieceClass() const { return ClassKind; }
486-
static bool classof(const Piece *) { return true; }
460+
PieceKind getPieceClass() const { return ClassKind; }
461+
static bool classof(const Piece *) { return true; }
487462

488-
private:
489-
PieceKind ClassKind;
463+
private:
464+
PieceKind ClassKind;
490465
};
491466

492467
struct MultiPiece : Piece {
@@ -970,10 +945,11 @@ struct DiagTextPrinter : DiagTextVisitor<DiagTextPrinter> {
970945
void VisitPlural(PluralPiece *P) {
971946
Result += "%plural{";
972947
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]);
948+
for (const auto &[Prefix, Option] :
949+
zip_equal(P->OptionPrefixes, P->Options)) {
950+
if (Prefix)
951+
Visit(Prefix);
952+
Visit(Option);
977953
Result += "|";
978954
}
979955
if (!P->Options.empty())
@@ -1553,15 +1529,13 @@ static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
15531529
RecordVec &GroupsInPedantic, raw_ostream &OS) {
15541530
OS << "static const int16_t DiagSubGroups[] = {\n"
15551531
<< " /* 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;
1532+
for (auto const &[Name, Group] : DiagsInGroup) {
1533+
const bool IsPedantic = Name == "pedantic";
1534+
const std::vector<std::string> &SubGroups = Group.SubGroups;
15601535
if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
1561-
OS << " /* DiagSubGroup" << I.second.IDNo << " */ ";
1536+
OS << " /* DiagSubGroup" << Group.IDNo << " */ ";
15621537
for (auto const &SubGroup : SubGroups) {
1563-
std::map<std::string, GroupInfo>::const_iterator RI =
1564-
DiagsInGroup.find(SubGroup);
1538+
auto RI = DiagsInGroup.find(SubGroup);
15651539
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15661540
OS << RI->second.IDNo << ", ";
15671541
}
@@ -1570,8 +1544,7 @@ static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
15701544
for (auto const &Group : GroupsInPedantic) {
15711545
const std::string &GroupName =
15721546
std::string(Group->getValueAsString("GroupName"));
1573-
std::map<std::string, GroupInfo>::const_iterator RI =
1574-
DiagsInGroup.find(GroupName);
1547+
auto RI = DiagsInGroup.find(GroupName);
15751548
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
15761549
OS << RI->second.IDNo << ", ";
15771550
}
@@ -1605,12 +1578,12 @@ static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
16051578
RecordVec &DiagsInPedantic, raw_ostream &OS) {
16061579
OS << "static const int16_t DiagArrays[] = {\n"
16071580
<< " /* Empty */ -1,\n";
1608-
for (auto const &I : DiagsInGroup) {
1609-
const bool IsPedantic = I.first == "pedantic";
1581+
for (auto const &[Name, Group] : DiagsInGroup) {
1582+
const bool IsPedantic = Name == "pedantic";
16101583

1611-
const std::vector<const Record *> &V = I.second.DiagsInGroup;
1584+
const std::vector<const Record *> &V = Group.DiagsInGroup;
16121585
if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
1613-
OS << " /* DiagArray" << I.second.IDNo << " */ ";
1586+
OS << " /* DiagArray" << Group.IDNo << " */ ";
16141587
for (auto *Record : V)
16151588
OS << "diag::" << Record->getName() << ", ";
16161589
// Emit the diagnostics implicitly in "pedantic".
@@ -1692,31 +1665,29 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
16921665

16931666
OS << "\n#ifdef DIAG_ENTRY\n";
16941667
unsigned SubGroupIndex = 1, DiagArrayIndex = 1;
1695-
for (auto const &I: DiagsInGroup) {
1668+
for (auto const &[Name, GroupInfo] : DiagsInGroup) {
16961669
// Group option string.
16971670
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 << " */, ";
1671+
OS << GroupInfo.GroupName << " /* ";
1672+
1673+
if (Name.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
1674+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1675+
"0123456789!@#$%^*-+=:?") != std::string::npos)
1676+
PrintFatalError("Invalid character in diagnostic group '" + Name + "'");
1677+
OS << Name << " */, ";
17071678
// 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) << ", ";
1679+
std::string PascalName = char(Name.size()) + Name;
1680+
OS << *GroupNames.GetStringOffset(PascalName) << ", ";
17101681

17111682
// Special handling for 'pedantic'.
1712-
const bool IsPedantic = I.first == "pedantic";
1683+
const bool IsPedantic = Name == "pedantic";
17131684

17141685
// Diagnostics in the group.
1715-
const std::vector<const Record *> &V = I.second.DiagsInGroup;
1686+
const std::vector<const Record *> &V = GroupInfo.DiagsInGroup;
17161687
const bool hasDiags =
17171688
!V.empty() || (IsPedantic && !DiagsInPedantic.empty());
17181689
if (hasDiags) {
1719-
OS << "/* DiagArray" << I.second.IDNo << " */ " << DiagArrayIndex
1690+
OS << "/* DiagArray" << GroupInfo.IDNo << " */ " << DiagArrayIndex
17201691
<< ", ";
17211692
if (IsPedantic)
17221693
DiagArrayIndex += DiagsInPedantic.size();
@@ -1726,11 +1697,11 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
17261697
}
17271698

17281699
// Subgroups.
1729-
const std::vector<std::string> &SubGroups = I.second.SubGroups;
1700+
const std::vector<std::string> &SubGroups = GroupInfo.SubGroups;
17301701
const bool hasSubGroups =
17311702
!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty());
17321703
if (hasSubGroups) {
1733-
OS << "/* DiagSubGroup" << I.second.IDNo << " */ " << SubGroupIndex
1704+
OS << "/* DiagSubGroup" << GroupInfo.IDNo << " */ " << SubGroupIndex
17341705
<< ", ";
17351706
if (IsPedantic)
17361707
SubGroupIndex += GroupsInPedantic.size();
@@ -1739,7 +1710,7 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
17391710
OS << "0, ";
17401711
}
17411712

1742-
std::string Documentation = I.second.Defs.back()
1713+
std::string Documentation = GroupInfo.Defs.back()
17431714
->getValue("Documentation")
17441715
->getValue()
17451716
->getAsUnquotedString();
@@ -1832,20 +1803,15 @@ void clang::EmitClangDiagsIndexName(const RecordKeeper &Records,
18321803

18331804
std::vector<RecordIndexElement> Index;
18341805
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-
}
1806+
for (const Record *R : Diags)
1807+
Index.push_back(RecordIndexElement(*R));
18391808

18401809
sort(Index, [](const RecordIndexElement &Lhs, const RecordIndexElement &Rhs) {
18411810
return Lhs.Name < Rhs.Name;
18421811
});
18431812

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-
}
1813+
for (const auto &Elem : Index)
1814+
OS << "DIAG_NAME_INDEX(" << Elem.Name << ")\n";
18491815
}
18501816

18511817
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)