diff options
Diffstat (limited to 'tools/diagtool')
-rw-r--r-- | tools/diagtool/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tools/diagtool/DiagnosticNames.cpp | 12 | ||||
-rw-r--r-- | tools/diagtool/DiagnosticNames.h | 6 | ||||
-rw-r--r-- | tools/diagtool/FindDiagnosticID.cpp | 18 | ||||
-rw-r--r-- | tools/diagtool/ListWarnings.cpp | 56 | ||||
-rw-r--r-- | tools/diagtool/ShowEnabledWarnings.cpp | 24 | ||||
-rw-r--r-- | tools/diagtool/TreeView.cpp | 59 |
7 files changed, 104 insertions, 72 deletions
diff --git a/tools/diagtool/CMakeLists.txt b/tools/diagtool/CMakeLists.txt index 3f7d80385a82..beb6c35457c4 100644 --- a/tools/diagtool/CMakeLists.txt +++ b/tools/diagtool/CMakeLists.txt @@ -13,6 +13,7 @@ add_clang_executable(diagtool ) target_link_libraries(diagtool + PRIVATE clangBasic clangFrontend ) diff --git a/tools/diagtool/DiagnosticNames.cpp b/tools/diagtool/DiagnosticNames.cpp index a08da89577f1..b0ca7f980639 100644 --- a/tools/diagtool/DiagnosticNames.cpp +++ b/tools/diagtool/DiagnosticNames.cpp @@ -32,6 +32,7 @@ static const DiagnosticRecord BuiltinDiagnosticsByID[] = { SFINAE,NOWERROR,SHOWINSYSHEADER,CATEGORY) \ { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) }, #include "clang/Basic/DiagnosticCommonKinds.inc" +#include "clang/Basic/DiagnosticCrossTUKinds.inc" #include "clang/Basic/DiagnosticDriverKinds.inc" #include "clang/Basic/DiagnosticFrontendKinds.inc" #include "clang/Basic/DiagnosticSerializationKinds.inc" @@ -41,6 +42,7 @@ static const DiagnosticRecord BuiltinDiagnosticsByID[] = { #include "clang/Basic/DiagnosticCommentKinds.inc" #include "clang/Basic/DiagnosticSemaKinds.inc" #include "clang/Basic/DiagnosticAnalysisKinds.inc" +#include "clang/Basic/DiagnosticRefactoringKinds.inc" #undef DIAG }; @@ -84,6 +86,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; } @@ -92,6 +99,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); } diff --git a/tools/diagtool/DiagnosticNames.h b/tools/diagtool/DiagnosticNames.h index ac1a09857952..598ae6a0ba65 100644 --- a/tools/diagtool/DiagnosticNames.h +++ b/tools/diagtool/DiagnosticNames.h @@ -20,7 +20,7 @@ namespace diagtool { const char *NameStr; short DiagID; uint8_t NameLen; - + llvm::StringRef getName() const { return llvm::StringRef(NameStr, NameLen); } @@ -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; } @@ -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; diff --git a/tools/diagtool/FindDiagnosticID.cpp b/tools/diagtool/FindDiagnosticID.cpp index 167b9925eedc..db6fe5e472a9 100644 --- a/tools/diagtool/FindDiagnosticID.cpp +++ b/tools/diagtool/FindDiagnosticID.cpp @@ -18,6 +18,15 @@ DEF_DIAGTOOL("find-diagnostic-id", "Print the id of the given diagnostic", using namespace clang; using namespace diagtool; +static StringRef getNameFromID(StringRef Name) { + int DiagID; + if(!Name.getAsInteger(0, DiagID)) { + const DiagnosticRecord &Diag = getDiagnosticForID(DiagID); + return Diag.getName(); + } + return StringRef(); +} + static Optional<DiagnosticRecord> findDiagnostic(ArrayRef<DiagnosticRecord> Diagnostics, StringRef Name) { for (const auto &Diag : Diagnostics) { @@ -38,7 +47,7 @@ int FindDiagnosticID::run(unsigned int argc, char **argv, llvm::cl::Required, llvm::cl::cat(FindDiagnosticIDOptions)); std::vector<const char *> Args; - Args.push_back("find-diagnostic-id"); + Args.push_back("diagtool find-diagnostic-id"); for (const char *A : llvm::makeArrayRef(argv, argc)) Args.push_back(A); @@ -50,6 +59,13 @@ int FindDiagnosticID::run(unsigned int argc, char **argv, Optional<DiagnosticRecord> Diag = findDiagnostic(AllDiagnostics, DiagnosticName); if (!Diag) { + // Name to id failed, so try id to name. + auto Name = getNameFromID(DiagnosticName); + if (!Name.empty()) { + OS << Name << '\n'; + return 0; + } + llvm::errs() << "error: invalid diagnostic '" << DiagnosticName << "'\n"; return 1; } diff --git a/tools/diagtool/ListWarnings.cpp b/tools/diagtool/ListWarnings.cpp index 3e6e88306e24..8bf9df94011c 100644 --- a/tools/diagtool/ListWarnings.cpp +++ b/tools/diagtool/ListWarnings.cpp @@ -23,7 +23,7 @@ DEF_DIAGTOOL("list-warnings", "List warnings and their corresponding flags", ListWarnings) - + using namespace clang; using namespace diagtool; @@ -31,20 +31,19 @@ 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'; } } @@ -52,23 +51,18 @@ static void printEntries(std::vector<Entry> &entries, llvm::raw_ostream &out) { 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 { @@ -76,24 +70,24 @@ int ListWarnings::run(unsigned int argc, char **argv, llvm::raw_ostream &out) { 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'; @@ -102,7 +96,7 @@ int ListWarnings::run(unsigned int argc, char **argv, llvm::raw_ostream &out) { << flagHistogram["pedantic"].size() << '\n'; out << '\n'; - + return 0; } diff --git a/tools/diagtool/ShowEnabledWarnings.cpp b/tools/diagtool/ShowEnabledWarnings.cpp index e6ea786a9ade..513abc15b2d7 100644 --- a/tools/diagtool/ShowEnabledWarnings.cpp +++ b/tools/diagtool/ShowEnabledWarnings.cpp @@ -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; @@ -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'; } diff --git a/tools/diagtool/TreeView.cpp b/tools/diagtool/TreeView.cpp index 07af944ffc4e..b4846b557444 100644 --- a/tools/diagtool/TreeView.cpp +++ b/tools/diagtool/TreeView.cpp @@ -32,10 +32,10 @@ class TreePrinter { public: llvm::raw_ostream &out; const bool ShowColors; - bool FlagsOnly; + bool Internal; TreePrinter(llvm::raw_ostream &out) - : out(out), ShowColors(hasColors(out)), FlagsOnly(false) {} + : out(out), ShowColors(hasColors(out)), Internal(false) {} void setColor(llvm::raw_ostream::Colors Color) { if (ShowColors) @@ -54,28 +54,42 @@ public: return Diags.isIgnored(DiagID, SourceLocation()); } + static bool enabledByDefault(const GroupRecord &Group) { + for (const DiagnosticRecord &DR : Group.diagnostics()) { + if (isIgnored(DR.DiagID)) + return false; + } + + for (const GroupRecord &GR : Group.subgroups()) { + if (!enabledByDefault(GR)) + return false; + } + + return true; + } + void printGroup(const GroupRecord &Group, unsigned Indent = 0) { out.indent(Indent * 2); - setColor(llvm::raw_ostream::YELLOW); + if (enabledByDefault(Group)) + setColor(llvm::raw_ostream::GREEN); + else + setColor(llvm::raw_ostream::YELLOW); + out << "-W" << Group.getName() << "\n"; 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)) + if (Internal) { + 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"; } @@ -107,12 +121,9 @@ public: 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()); } } @@ -139,16 +150,16 @@ public: }; static void printUsage() { - llvm::errs() << "Usage: diagtool tree [--flags-only] [<diagnostic-group>]\n"; + llvm::errs() << "Usage: diagtool tree [--internal] [<diagnostic-group>]\n"; } int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) { // First check our one flag (--flags-only). - bool FlagsOnly = false; + bool Internal = false; if (argc > 0) { StringRef FirstArg(*argv); - if (FirstArg.equals("--flags-only")) { - FlagsOnly = true; + if (FirstArg.equals("--internal")) { + Internal = true; --argc; ++argv; } @@ -175,7 +186,7 @@ int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) { } TreePrinter TP(out); - TP.FlagsOnly = FlagsOnly; + TP.Internal = Internal; TP.showKey(); return ShowAll ? TP.showAll() : TP.showGroup(RootGroup); } |