Index: tools/diagtool/CMakeLists.txt =================================================================== --- tools/diagtool/CMakeLists.txt +++ tools/diagtool/CMakeLists.txt @@ -8,6 +8,7 @@ DiagnosticNames.cpp FindDiagnosticID.cpp ListWarnings.cpp + ListWarningFlags.cpp ShowEnabledWarnings.cpp TreeView.cpp ) Index: tools/diagtool/ListWarningFlags.cpp =================================================================== --- /dev/null +++ tools/diagtool/ListWarningFlags.cpp @@ -0,0 +1,55 @@ +//===- ListWarningFlags.h - diagtool tool for printing warning flags ------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file provides a diagtool tool that displays only warnings that have a +// corresponding flags for diagnostics. +// +//===----------------------------------------------------------------------===// + +#include "DiagTool.h" +#include "DiagnosticNames.h" +#include "clang/AST/ASTDiagnostic.h" +#include "clang/Basic/AllDiagnostics.h" +#include "clang/Basic/Diagnostic.h" +#include "llvm/Support/Format.h" +#include +#include + +DEF_DIAGTOOL("list-warning-flags", "List only warnings with flags", + ListWarningFlags) + +using namespace clang; +using namespace diagtool; + +int ListWarningFlags::run(unsigned int argc, char **argv, + llvm::raw_ostream &out) { + std::vector flags; + + ArrayRef AllDiagnostics = getBuiltinDiagnosticsByName(); + for (ArrayRef::iterator di = AllDiagnostics.begin(), + de = AllDiagnostics.end(); + di != de; ++di) { + unsigned diagID = di->DiagID; + llvm::StringRef flag = DiagnosticIDs::getWarningOptionForDiag(diagID); + + if (flag.empty()) + continue; + + flags.push_back(flag); + } + + std::sort(flags.begin(), flags.end()); + flags.erase(std::unique(flags.begin(), flags.end()), flags.end()); + + for (auto &flag : flags) { + out << "-W" << flag << '\n'; + } + + return 0; +} Index: tools/diagtool/ListWarnings.cpp =================================================================== --- tools/diagtool/ListWarnings.cpp +++ tools/diagtool/ListWarnings.cpp @@ -1,4 +1,4 @@ -//===- ListWarnings.h - diagtool tool for printing warning flags ----------===// +//===- ListWarnings.h - diagtool tool for printing warnings ---------------===// // // The LLVM Compiler Infrastructure // @@ -7,7 +7,7 @@ // //===----------------------------------------------------------------------===// // -// This file provides a diagtool tool that displays warning flags for +// This file provides a diagtool tool that displays all warning for // diagnostics. // //===----------------------------------------------------------------------===// @@ -21,9 +21,9 @@ #include "llvm/Support/Format.h" DEF_DIAGTOOL("list-warnings", - "List warnings and their corresponding flags", + "List all (internal) warnings", ListWarnings) - + using namespace clang; using namespace diagtool; @@ -31,10 +31,10 @@ 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; } }; } @@ -52,23 +52,23 @@ int ListWarnings::run(unsigned int argc, char **argv, llvm::raw_ostream &out) { std::vector Flagged, Unflagged; llvm::StringMap > flagHistogram; - + ArrayRef AllDiagnostics = getBuiltinDiagnosticsByName(); for (ArrayRef::iterator di = AllDiagnostics.begin(), de = AllDiagnostics.end(); di != de; ++di) { unsigned diagID = di->DiagID; - + if (DiagnosticIDs::isBuiltinNote(diagID)) continue; - + if (!DiagnosticIDs::isBuiltinWarningOrExtension(diagID)) continue; - + Entry entry(di->getName(), DiagnosticIDs::getWarningOptionForDiag(diagID)); - + if (entry.Flag.empty()) Unflagged.push_back(entry); else { @@ -76,24 +76,24 @@ 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()) + double percentFlagged = ((double) Flagged.size()) / (Flagged.size() + Unflagged.size()) * 100.0; - - out << " Percentage of warnings with flags: " + + 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 +102,7 @@ << flagHistogram["pedantic"].size() << '\n'; out << '\n'; - + return 0; }