[clang] [NFC][Clang] Use range for loops in ClangDiagnosticsEmitter (PR #115573)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 11 13:22:29 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Rahul Joshi (jurahul)
<details>
<summary>Changes</summary>
Use range based for loops in Clang diagnostics emitter.
---
Full diff: https://github.com/llvm/llvm-project/pull/115573.diff
1 Files Affected:
- (modified) clang/utils/TableGen/ClangDiagnosticsEmitter.cpp (+71-108)
``````````diff
diff --git a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
index 34e2e8f47ae71a..889c8d5ac5fbcd 100644
--- a/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
+++ b/clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
@@ -44,17 +44,12 @@ class DiagGroupParentMap {
public:
DiagGroupParentMap(const RecordKeeper &records) : Records(records) {
- ArrayRef<const Record *> DiagGroups =
- Records.getAllDerivedDefinitions("DiagGroup");
- for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
- std::vector<const Record *> SubGroups =
- DiagGroups[i]->getValueAsListOfDefs("SubGroups");
- for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
- Mapping[SubGroups[j]].push_back(DiagGroups[i]);
- }
+ for (const Record *Group : Records.getAllDerivedDefinitions("DiagGroup"))
+ for (const Record *SubGroup : Group->getValueAsListOfDefs("SubGroups"))
+ Mapping[SubGroup].push_back(Group);
}
- const std::vector<const Record *> &getParents(const Record *Group) {
+ ArrayRef<const Record *> getParents(const Record *Group) {
return Mapping[Group];
}
};
@@ -69,10 +64,8 @@ getCategoryFromDiagGroup(const Record *Group,
// The diag group may the subgroup of one or more other diagnostic groups,
// check these for a category as well.
- const std::vector<const Record *> &Parents =
- DiagGroupParents.getParents(Group);
- for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
- CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
+ for (const Record *Parent : DiagGroupParents.getParents(Group)) {
+ CatName = getCategoryFromDiagGroup(Parent, DiagGroupParents);
if (!CatName.empty()) return CatName;
}
return "";
@@ -107,10 +100,9 @@ namespace {
CategoryStrings.push_back("");
CategoryIDs[""] = 0;
- ArrayRef<const Record *> Diags =
- Records.getAllDerivedDefinitions("Diagnostic");
- for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
- std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
+ for (const Record *Diag :
+ Records.getAllDerivedDefinitions("Diagnostic")) {
+ std::string Category = getDiagnosticCategory(Diag, ParentInfo);
if (Category.empty()) continue; // Skip diags with no category.
unsigned &ID = CategoryIDs[Category];
@@ -158,9 +150,7 @@ static bool diagGroupBeforeByName(const Record *LHS, const Record *RHS) {
static void groupDiagnostics(ArrayRef<const Record *> Diags,
ArrayRef<const Record *> DiagGroups,
std::map<std::string, GroupInfo> &DiagsInGroup) {
-
- for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
- const Record *R = Diags[i];
+ for (const Record *R : Diags) {
const auto *DI = dyn_cast<DefInit>(R->getValueInit("Group"));
if (!DI)
continue;
@@ -173,8 +163,7 @@ static void groupDiagnostics(ArrayRef<const Record *> Diags,
// Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
// groups (these are warnings that GCC supports that clang never produces).
- for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
- const Record *Group = DiagGroups[i];
+ for (const Record *Group : DiagGroups) {
GroupInfo &GI =
DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
GI.GroupName = Group->getName();
@@ -185,10 +174,8 @@ static void groupDiagnostics(ArrayRef<const Record *> Diags,
}
// Assign unique ID numbers to the groups.
- unsigned IDNo = 0;
- for (std::map<std::string, GroupInfo>::iterator
- I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
- I->second.IDNo = IDNo;
+ for (auto [IdNo, Iter] : enumerate(DiagsInGroup))
+ Iter.second.IDNo = IdNo;
// Warn if the same group is defined more than once (including implicitly).
for (auto &Group : DiagsInGroup) {
@@ -297,10 +284,8 @@ bool InferPedantic::isSubGroupOfGroup(const Record *Group, StringRef GName) {
if (GName == GroupName)
return true;
- const std::vector<const Record *> &Parents =
- DiagGroupParents.getParents(Group);
- for (unsigned i = 0, e = Parents.size(); i != e; ++i)
- if (isSubGroupOfGroup(Parents[i], GName))
+ for (const Record *Parent : DiagGroupParents.getParents(Group))
+ if (isSubGroupOfGroup(Parent, GName))
return true;
return false;
@@ -308,15 +293,12 @@ bool InferPedantic::isSubGroupOfGroup(const Record *Group, StringRef GName) {
/// Determine if the diagnostic is an extension.
bool InferPedantic::isExtension(const Record *Diag) {
- const std::string &ClsName =
- std::string(Diag->getValueAsDef("Class")->getName());
- return ClsName == "CLASS_EXTENSION";
+ return Diag->getValueAsDef("Class")->getName() == "CLASS_EXTENSION";
}
bool InferPedantic::isOffByDefault(const Record *Diag) {
- const std::string &DefSeverity = std::string(
- Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name"));
- return DefSeverity == "Ignored";
+ return Diag->getValueAsDef("DefaultSeverity")->getValueAsString("Name") ==
+ "Ignored";
}
bool InferPedantic::groupInPedantic(const Record *Group, bool increment) {
@@ -342,12 +324,9 @@ void InferPedantic::markGroup(const Record *Group) {
// covered by -Wpedantic, increment the count of parent groups. Once the
// group's count is equal to the number of subgroups and diagnostics in
// that group, we can safely add this group to -Wpedantic.
- if (groupInPedantic(Group, /* increment */ true)) {
- const std::vector<const Record *> &Parents =
- DiagGroupParents.getParents(Group);
- for (unsigned i = 0, e = Parents.size(); i != e; ++i)
- markGroup(Parents[i]);
- }
+ if (groupInPedantic(Group, /* increment */ true))
+ for (const Record *Parent : DiagGroupParents.getParents(Group))
+ markGroup(Parent);
}
void InferPedantic::compute(VecOrSet DiagsInPedantic,
@@ -355,15 +334,14 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
// All extensions that are not on by default are implicitly in the
// "pedantic" group. For those that aren't explicitly included in -Wpedantic,
// mark them for consideration to be included in -Wpedantic directly.
- for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
- const Record *R = Diags[i];
- if (isExtension(R) && isOffByDefault(R)) {
- DiagsSet.insert(R);
- if (const auto *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
- const Record *GroupRec = Group->getDef();
- if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
- markGroup(GroupRec);
- }
+ for (const Record *R : Diags) {
+ if (!isExtension(R) || !isOffByDefault(R))
+ continue;
+ DiagsSet.insert(R);
+ if (const auto *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
+ const Record *GroupRec = Group->getDef();
+ if (!isSubGroupOfGroup(GroupRec, "pedantic")) {
+ markGroup(GroupRec);
}
}
}
@@ -371,8 +349,7 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
// Compute the set of diagnostics that are directly in -Wpedantic. We
// march through Diags a second time to ensure the results are emitted
// in deterministic order.
- for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
- const Record *R = Diags[i];
+ for (const Record *R : Diags) {
if (!DiagsSet.count(R))
continue;
// Check if the group is implicitly in -Wpedantic. If so,
@@ -386,9 +363,8 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
// -Wpedantic. Include it in -Wpedantic directly.
if (auto *V = DiagsInPedantic.dyn_cast<RecordVec *>())
V->push_back(R);
- else {
- DiagsInPedantic.get<RecordSet*>()->insert(R);
- }
+ else
+ DiagsInPedantic.get<RecordSet *>()->insert(R);
}
if (!GroupsInPedantic)
@@ -397,8 +373,7 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
// Compute the set of groups that are directly in -Wpedantic. We
// march through the groups to ensure the results are emitted
/// in a deterministc order.
- for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
- const Record *Group = DiagGroups[i];
+ for (const Record *Group : DiagGroups) {
if (!groupInPedantic(Group))
continue;
@@ -415,9 +390,8 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
if (auto *V = GroupsInPedantic.dyn_cast<RecordVec *>())
V->push_back(Group);
- else {
- GroupsInPedantic.get<RecordSet*>()->insert(Group);
- }
+ else
+ GroupsInPedantic.get<RecordSet *>()->insert(Group);
}
}
@@ -468,11 +442,9 @@ static StringRef getModifierName(ModifierType MT) {
return "objcclass";
case MT_ObjCInstance:
return "objcinstance";
- case MT_Unknown:
+ default:
llvm_unreachable("invalid modifier type");
}
- // Unhandled case
- llvm_unreachable("invalid modifier type");
}
struct Piece {
@@ -970,10 +942,11 @@ struct DiagTextPrinter : DiagTextVisitor<DiagTextPrinter> {
void VisitPlural(PluralPiece *P) {
Result += "%plural{";
assert(P->Options.size() == P->OptionPrefixes.size());
- for (unsigned I = 0, End = P->Options.size(); I < End; ++I) {
- if (P->OptionPrefixes[I])
- Visit(P->OptionPrefixes[I]);
- Visit(P->Options[I]);
+ for (const auto &[Prefix, Option] :
+ zip_equal(P->OptionPrefixes, P->Options)) {
+ if (Prefix)
+ Visit(Prefix);
+ Visit(Option);
Result += "|";
}
if (!P->Options.empty())
@@ -1553,15 +1526,13 @@ static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
RecordVec &GroupsInPedantic, raw_ostream &OS) {
OS << "static const int16_t DiagSubGroups[] = {\n"
<< " /* Empty */ -1,\n";
- for (auto const &I : DiagsInGroup) {
- const bool IsPedantic = I.first == "pedantic";
-
- const std::vector<std::string> &SubGroups = I.second.SubGroups;
+ for (auto const &[Name, Group] : DiagsInGroup) {
+ const bool IsPedantic = Name == "pedantic";
+ const std::vector<std::string> &SubGroups = Group.SubGroups;
if (!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty())) {
- OS << " /* DiagSubGroup" << I.second.IDNo << " */ ";
+ OS << " /* DiagSubGroup" << Group.IDNo << " */ ";
for (auto const &SubGroup : SubGroups) {
- std::map<std::string, GroupInfo>::const_iterator RI =
- DiagsInGroup.find(SubGroup);
+ auto RI = DiagsInGroup.find(SubGroup);
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
OS << RI->second.IDNo << ", ";
}
@@ -1570,8 +1541,7 @@ static void emitDiagSubGroups(std::map<std::string, GroupInfo> &DiagsInGroup,
for (auto const &Group : GroupsInPedantic) {
const std::string &GroupName =
std::string(Group->getValueAsString("GroupName"));
- std::map<std::string, GroupInfo>::const_iterator RI =
- DiagsInGroup.find(GroupName);
+ auto RI = DiagsInGroup.find(GroupName);
assert(RI != DiagsInGroup.end() && "Referenced without existing?");
OS << RI->second.IDNo << ", ";
}
@@ -1605,12 +1575,12 @@ static void emitDiagArrays(std::map<std::string, GroupInfo> &DiagsInGroup,
RecordVec &DiagsInPedantic, raw_ostream &OS) {
OS << "static const int16_t DiagArrays[] = {\n"
<< " /* Empty */ -1,\n";
- for (auto const &I : DiagsInGroup) {
- const bool IsPedantic = I.first == "pedantic";
+ for (auto const &[Name, Group] : DiagsInGroup) {
+ const bool IsPedantic = Name == "pedantic";
- const std::vector<const Record *> &V = I.second.DiagsInGroup;
+ const std::vector<const Record *> &V = Group.DiagsInGroup;
if (!V.empty() || (IsPedantic && !DiagsInPedantic.empty())) {
- OS << " /* DiagArray" << I.second.IDNo << " */ ";
+ OS << " /* DiagArray" << Group.IDNo << " */ ";
for (auto *Record : V)
OS << "diag::" << Record->getName() << ", ";
// Emit the diagnostics implicitly in "pedantic".
@@ -1692,31 +1662,29 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
OS << "\n#ifdef DIAG_ENTRY\n";
unsigned SubGroupIndex = 1, DiagArrayIndex = 1;
- for (auto const &I: DiagsInGroup) {
+ for (auto const &[Name, GroupInfo] : DiagsInGroup) {
// Group option string.
OS << "DIAG_ENTRY(";
- OS << I.second.GroupName << " /* ";
-
- if (I.first.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "0123456789!@#$%^*-+=:?") !=
- std::string::npos)
- PrintFatalError("Invalid character in diagnostic group '" + I.first +
- "'");
- OS << I.first << " */, ";
+ OS << GroupInfo.GroupName << " /* ";
+
+ if (Name.find_first_not_of("abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "0123456789!@#$%^*-+=:?") != std::string::npos)
+ PrintFatalError("Invalid character in diagnostic group '" + Name + "'");
+ OS << Name << " */, ";
// Store a pascal-style length byte at the beginning of the string.
- std::string Name = char(I.first.size()) + I.first;
- OS << *GroupNames.GetStringOffset(Name) << ", ";
+ std::string PascalName = char(Name.size()) + Name;
+ OS << *GroupNames.GetStringOffset(PascalName) << ", ";
// Special handling for 'pedantic'.
- const bool IsPedantic = I.first == "pedantic";
+ const bool IsPedantic = Name == "pedantic";
// Diagnostics in the group.
- const std::vector<const Record *> &V = I.second.DiagsInGroup;
+ const std::vector<const Record *> &V = GroupInfo.DiagsInGroup;
const bool hasDiags =
!V.empty() || (IsPedantic && !DiagsInPedantic.empty());
if (hasDiags) {
- OS << "/* DiagArray" << I.second.IDNo << " */ " << DiagArrayIndex
+ OS << "/* DiagArray" << GroupInfo.IDNo << " */ " << DiagArrayIndex
<< ", ";
if (IsPedantic)
DiagArrayIndex += DiagsInPedantic.size();
@@ -1726,11 +1694,11 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
}
// Subgroups.
- const std::vector<std::string> &SubGroups = I.second.SubGroups;
+ const std::vector<std::string> &SubGroups = GroupInfo.SubGroups;
const bool hasSubGroups =
!SubGroups.empty() || (IsPedantic && !GroupsInPedantic.empty());
if (hasSubGroups) {
- OS << "/* DiagSubGroup" << I.second.IDNo << " */ " << SubGroupIndex
+ OS << "/* DiagSubGroup" << GroupInfo.IDNo << " */ " << SubGroupIndex
<< ", ";
if (IsPedantic)
SubGroupIndex += GroupsInPedantic.size();
@@ -1739,7 +1707,7 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
OS << "0, ";
}
- std::string Documentation = I.second.Defs.back()
+ std::string Documentation = GroupInfo.Defs.back()
->getValue("Documentation")
->getValue()
->getAsUnquotedString();
@@ -1832,20 +1800,15 @@ void clang::EmitClangDiagsIndexName(const RecordKeeper &Records,
std::vector<RecordIndexElement> Index;
Index.reserve(Diags.size());
- for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
- const Record &R = *(Diags[i]);
- Index.push_back(RecordIndexElement(R));
- }
+ for (const Record *R : Diags)
+ Index.push_back(RecordIndexElement(*R));
sort(Index, [](const RecordIndexElement &Lhs, const RecordIndexElement &Rhs) {
return Lhs.Name < Rhs.Name;
});
- for (unsigned i = 0, e = Index.size(); i != e; ++i) {
- const RecordIndexElement &R = Index[i];
-
- OS << "DIAG_NAME_INDEX(" << R.Name << ")\n";
- }
+ for (const auto &Elem : Index)
+ OS << "DIAG_NAME_INDEX(" << Elem.Name << ")\n";
}
//===----------------------------------------------------------------------===//
``````````
</details>
https://github.com/llvm/llvm-project/pull/115573
More information about the cfe-commits
mailing list