Skip to content

[NFC][Clang] Use range for loops in ClangDiagnosticsEmitter #115573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 70 additions & 105 deletions clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
};
Expand All @@ -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 "";
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -160,9 +152,7 @@ using DiagsInGroupTy = std::map<std::string, GroupInfo, std::less<>>;
static void groupDiagnostics(ArrayRef<const Record *> Diags,
ArrayRef<const Record *> DiagGroups,
DiagsInGroupTy &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;
Expand All @@ -175,8 +165,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();
Expand All @@ -187,10 +176,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) {
Expand Down Expand Up @@ -299,26 +286,21 @@ 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;
}

/// 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) {
Expand All @@ -344,37 +326,32 @@ 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,
VecOrSet GroupsInPedantic) {
// 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);
}
}
}

// 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,
Expand All @@ -388,9 +365,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)
Expand All @@ -399,8 +375,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;

Expand All @@ -417,9 +392,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);
}
}

Expand Down Expand Up @@ -972,10 +946,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())
Expand Down Expand Up @@ -1555,15 +1530,13 @@ static void emitDiagSubGroups(DiagsInGroupTy &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 << ", ";
}
Expand All @@ -1572,8 +1545,7 @@ static void emitDiagSubGroups(DiagsInGroupTy &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 << ", ";
}
Expand Down Expand Up @@ -1607,12 +1579,12 @@ static void emitDiagArrays(DiagsInGroupTy &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 (const auto &[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".
Expand Down Expand Up @@ -1694,31 +1666,29 @@ static void emitDiagTable(DiagsInGroupTy &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();
Expand All @@ -1728,11 +1698,11 @@ static void emitDiagTable(DiagsInGroupTy &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();
Expand All @@ -1741,7 +1711,7 @@ static void emitDiagTable(DiagsInGroupTy &DiagsInGroup,
OS << "0, ";
}

std::string Documentation = I.second.Defs.back()
std::string Documentation = GroupInfo.Defs.back()
->getValue("Documentation")
->getValue()
->getAsUnquotedString();
Expand Down Expand Up @@ -1834,20 +1804,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";
}

//===----------------------------------------------------------------------===//
Expand Down
Loading