Skip to content

Commit

Permalink
[NFC] Loop modernization in diagtool
Browse files Browse the repository at this point in the history
Precommit for https://reviews.llvm.org/D37390

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312545 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
JDevlieghere committed Sep 5, 2017
1 parent 5de618f commit 33dbcd3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 62 deletions.
10 changes: 10 additions & 0 deletions tools/diagtool/DiagnosticNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ GroupRecord::subgroup_iterator GroupRecord::subgroup_end() const {
return nullptr;
}

llvm::iterator_range<diagtool::GroupRecord::subgroup_iterator>
GroupRecord::subgroups() const {
return llvm::make_range(subgroup_begin(), subgroup_end());
}

GroupRecord::diagnostics_iterator GroupRecord::diagnostics_begin() const {
return DiagArrays + Members;
}
Expand All @@ -92,6 +97,11 @@ GroupRecord::diagnostics_iterator GroupRecord::diagnostics_end() const {
return nullptr;
}

llvm::iterator_range<diagtool::GroupRecord::diagnostics_iterator>
GroupRecord::diagnostics() const {
return llvm::make_range(diagnostics_begin(), diagnostics_end());
}

llvm::ArrayRef<GroupRecord> diagtool::getDiagnosticGroups() {
return llvm::makeArrayRef(OptionTable);
}
6 changes: 4 additions & 2 deletions tools/diagtool/DiagnosticNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace diagtool {
const char *NameStr;
short DiagID;
uint8_t NameLen;

llvm::StringRef getName() const {
return llvm::StringRef(NameStr, NameLen);
}
Expand Down Expand Up @@ -80,7 +80,7 @@ namespace diagtool {
bool operator==(group_iterator &Other) const {
return CurrentID == Other.CurrentID;
}

bool operator!=(group_iterator &Other) const {
return CurrentID != Other.CurrentID;
}
Expand All @@ -89,10 +89,12 @@ namespace diagtool {
typedef group_iterator<GroupRecord> subgroup_iterator;
subgroup_iterator subgroup_begin() const;
subgroup_iterator subgroup_end() const;
llvm::iterator_range<subgroup_iterator> subgroups() const;

typedef group_iterator<DiagnosticRecord> diagnostics_iterator;
diagnostics_iterator diagnostics_begin() const;
diagnostics_iterator diagnostics_end() const;
llvm::iterator_range<diagnostics_iterator> diagnostics() const;

bool operator<(llvm::StringRef Other) const {
return getName() < Other;
Expand Down
56 changes: 25 additions & 31 deletions tools/diagtool/ListWarnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,77 +23,71 @@
DEF_DIAGTOOL("list-warnings",
"List warnings and their corresponding flags",
ListWarnings)

using namespace clang;
using namespace diagtool;

namespace {
struct Entry {
llvm::StringRef DiagName;
llvm::StringRef Flag;

Entry(llvm::StringRef diagN, llvm::StringRef flag)
: DiagName(diagN), Flag(flag) {}

bool operator<(const Entry &x) const { return DiagName < x.DiagName; }
};
}

static void printEntries(std::vector<Entry> &entries, llvm::raw_ostream &out) {
for (std::vector<Entry>::iterator it = entries.begin(), ei = entries.end();
it != ei; ++it) {
out << " " << it->DiagName;
if (!it->Flag.empty())
out << " [-W" << it->Flag << "]";
for (const Entry &E : entries) {
out << " " << E.DiagName;
if (!E.Flag.empty())
out << " [-W" << E.Flag << "]";
out << '\n';
}
}

int ListWarnings::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
std::vector<Entry> Flagged, Unflagged;
llvm::StringMap<std::vector<unsigned> > flagHistogram;

ArrayRef<DiagnosticRecord> AllDiagnostics = getBuiltinDiagnosticsByName();

for (ArrayRef<DiagnosticRecord>::iterator di = AllDiagnostics.begin(),
de = AllDiagnostics.end();
di != de; ++di) {
unsigned diagID = di->DiagID;


for (const DiagnosticRecord &DR : getBuiltinDiagnosticsByName()) {
const unsigned diagID = DR.DiagID;

if (DiagnosticIDs::isBuiltinNote(diagID))
continue;

if (!DiagnosticIDs::isBuiltinWarningOrExtension(diagID))
continue;

Entry entry(di->getName(),
DiagnosticIDs::getWarningOptionForDiag(diagID));


Entry entry(DR.getName(), DiagnosticIDs::getWarningOptionForDiag(diagID));

if (entry.Flag.empty())
Unflagged.push_back(entry);
else {
Flagged.push_back(entry);
flagHistogram[entry.Flag].push_back(diagID);
}
}

out << "Warnings with flags (" << Flagged.size() << "):\n";
printEntries(Flagged, out);

out << "Warnings without flags (" << Unflagged.size() << "):\n";
printEntries(Unflagged, out);

out << "\nSTATISTICS:\n\n";

double percentFlagged = ((double) Flagged.size())
/ (Flagged.size() + Unflagged.size()) * 100.0;
out << " Percentage of warnings with flags: "
<< llvm::format("%.4g",percentFlagged) << "%\n";
double percentFlagged =
((double)Flagged.size()) / (Flagged.size() + Unflagged.size()) * 100.0;

out << " Percentage of warnings with flags: "
<< llvm::format("%.4g", percentFlagged) << "%\n";

out << " Number of unique flags: "
<< flagHistogram.size() << '\n';

double avgDiagsPerFlag = (double) Flagged.size() / flagHistogram.size();
out << " Average number of diagnostics per flag: "
<< llvm::format("%.4g", avgDiagsPerFlag) << '\n';
Expand All @@ -102,7 +96,7 @@ int ListWarnings::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
<< flagHistogram["pedantic"].size() << '\n';

out << '\n';

return 0;
}

24 changes: 10 additions & 14 deletions tools/diagtool/ShowEnabledWarnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,14 @@ int ShowEnabledWarnings::run(unsigned int argc, char **argv, raw_ostream &Out) {
// which ones are turned on.
// FIXME: It would be very nice to print which flags are turning on which
// diagnostics, but this can be done with a diff.
ArrayRef<DiagnosticRecord> AllDiagnostics = getBuiltinDiagnosticsByName();
std::vector<PrettyDiag> Active;

for (ArrayRef<DiagnosticRecord>::iterator I = AllDiagnostics.begin(),
E = AllDiagnostics.end();
I != E; ++I) {
unsigned DiagID = I->DiagID;

for (const DiagnosticRecord &DR : getBuiltinDiagnosticsByName()) {
unsigned DiagID = DR.DiagID;

if (DiagnosticIDs::isBuiltinNote(DiagID))
continue;

if (!DiagnosticIDs::isBuiltinWarningOrExtension(DiagID))
continue;

Expand All @@ -132,17 +129,16 @@ int ShowEnabledWarnings::run(unsigned int argc, char **argv, raw_ostream &Out) {
continue;

StringRef WarningOpt = DiagnosticIDs::getWarningOptionForDiag(DiagID);
Active.push_back(PrettyDiag(I->getName(), WarningOpt, DiagLevel));
Active.push_back(PrettyDiag(DR.getName(), WarningOpt, DiagLevel));
}

// Print them all out.
for (std::vector<PrettyDiag>::const_iterator I = Active.begin(),
E = Active.end(); I != E; ++I) {
for (const PrettyDiag &PD : Active) {
if (ShouldShowLevels)
Out << getCharForLevel(I->Level) << " ";
Out << I->Name;
if (!I->Flag.empty())
Out << " [-W" << I->Flag << "]";
Out << getCharForLevel(PD.Level) << " ";
Out << PD.Name;
if (!PD.Flag.empty())
Out << " [-W" << PD.Flag << "]";
Out << '\n';
}

Expand Down
23 changes: 8 additions & 15 deletions tools/diagtool/TreeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,16 @@ class TreePrinter {
resetColor();

++Indent;
for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
E = Group.subgroup_end();
I != E; ++I) {
printGroup(*I, Indent);
for (const GroupRecord &GR : Group.subgroups()) {
printGroup(GR, Indent);
}

if (!FlagsOnly) {
for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
E = Group.diagnostics_end();
I != E; ++I) {
if (ShowColors && !isIgnored(I->DiagID))
for (const DiagnosticRecord &DR : Group.diagnostics()) {
if (ShowColors && !isIgnored(DR.DiagID))
setColor(llvm::raw_ostream::GREEN);
out.indent(Indent * 2);
out << I->getName();
out << DR.getName();
resetColor();
out << "\n";
}
Expand Down Expand Up @@ -107,12 +103,9 @@ class TreePrinter {
ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
llvm::DenseSet<unsigned> NonRootGroupIDs;

for (ArrayRef<GroupRecord>::iterator I = AllGroups.begin(),
E = AllGroups.end();
I != E; ++I) {
for (GroupRecord::subgroup_iterator SI = I->subgroup_begin(),
SE = I->subgroup_end();
SI != SE; ++SI) {
for (const GroupRecord &GR : AllGroups) {
for (auto SI = GR.subgroup_begin(), SE = GR.subgroup_end(); SI != SE;
++SI) {
NonRootGroupIDs.insert((unsigned)SI.getID());
}
}
Expand Down

0 comments on commit 33dbcd3

Please sign in to comment.