Ted Kremenek | 2dc651d | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 1 | //===- ListWarnings.h - diagtool tool for printing warning flags ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides a diagtool tool that displays warning flags for |
| 11 | // diagnostics. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "DiagTool.h" |
Jordan Rose | 0832f82 | 2012-06-04 16:57:50 +0000 | [diff] [blame] | 16 | #include "DiagnosticNames.h" |
David Blaikie | ceb1565 | 2012-02-15 19:45:34 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTDiagnostic.h" |
David Blaikie | 6c44886 | 2012-02-15 21:58:34 +0000 | [diff] [blame] | 18 | #include "clang/Basic/AllDiagnostics.h" |
Chandler Carruth | f59edb9 | 2012-12-04 09:25:21 +0000 | [diff] [blame] | 19 | #include "clang/Basic/Diagnostic.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
| 21 | #include "llvm/Support/Format.h" |
Ted Kremenek | 2dc651d | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 22 | |
| 23 | DEF_DIAGTOOL("list-warnings", |
| 24 | "List warnings and their corresponding flags", |
| 25 | ListWarnings) |
| 26 | |
| 27 | using namespace clang; |
Jordan Rose | e742763 | 2012-06-24 00:07:45 +0000 | [diff] [blame] | 28 | using namespace diagtool; |
Ted Kremenek | 2dc651d | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 29 | |
David Blaikie | ceb1565 | 2012-02-15 19:45:34 +0000 | [diff] [blame] | 30 | namespace { |
Ted Kremenek | 2dc651d | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 31 | struct Entry { |
| 32 | llvm::StringRef DiagName; |
| 33 | llvm::StringRef Flag; |
| 34 | |
| 35 | Entry(llvm::StringRef diagN, llvm::StringRef flag) |
| 36 | : DiagName(diagN), Flag(flag) {} |
| 37 | |
| 38 | bool operator<(const Entry &x) const { return DiagName < x.DiagName; } |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | static void printEntries(std::vector<Entry> &entries, llvm::raw_ostream &out) { |
| 43 | for (std::vector<Entry>::iterator it = entries.begin(), ei = entries.end(); |
| 44 | it != ei; ++it) { |
| 45 | out << " " << it->DiagName; |
| 46 | if (!it->Flag.empty()) |
| 47 | out << " [-W" << it->Flag << "]"; |
| 48 | out << '\n'; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | int ListWarnings::run(unsigned int argc, char **argv, llvm::raw_ostream &out) { |
Nick Lewycky | 83f06e8 | 2011-08-12 01:14:22 +0000 | [diff] [blame] | 53 | std::vector<Entry> Flagged, Unflagged; |
Ted Kremenek | 2dc651d | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 54 | llvm::StringMap<std::vector<unsigned> > flagHistogram; |
| 55 | |
Jordan Rose | e742763 | 2012-06-24 00:07:45 +0000 | [diff] [blame] | 56 | ArrayRef<DiagnosticRecord> AllDiagnostics = getBuiltinDiagnosticsByName(); |
| 57 | |
| 58 | for (ArrayRef<DiagnosticRecord>::iterator di = AllDiagnostics.begin(), |
| 59 | de = AllDiagnostics.end(); |
| 60 | di != de; ++di) { |
David Blaikie | ceb1565 | 2012-02-15 19:45:34 +0000 | [diff] [blame] | 61 | unsigned diagID = di->DiagID; |
Ted Kremenek | 2dc651d | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 62 | |
| 63 | if (DiagnosticIDs::isBuiltinNote(diagID)) |
| 64 | continue; |
| 65 | |
| 66 | if (!DiagnosticIDs::isBuiltinWarningOrExtension(diagID)) |
| 67 | continue; |
| 68 | |
David Blaikie | ceb1565 | 2012-02-15 19:45:34 +0000 | [diff] [blame] | 69 | Entry entry(di->getName(), |
Ted Kremenek | 2dc651d | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 70 | DiagnosticIDs::getWarningOptionForDiag(diagID)); |
| 71 | |
| 72 | if (entry.Flag.empty()) |
| 73 | Unflagged.push_back(entry); |
| 74 | else { |
| 75 | Flagged.push_back(entry); |
| 76 | flagHistogram.GetOrCreateValue(entry.Flag).getValue().push_back(diagID); |
| 77 | } |
| 78 | } |
| 79 | |
Ted Kremenek | 2dc651d | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 80 | out << "Warnings with flags (" << Flagged.size() << "):\n"; |
| 81 | printEntries(Flagged, out); |
| 82 | |
| 83 | out << "Warnings without flags (" << Unflagged.size() << "):\n"; |
| 84 | printEntries(Unflagged, out); |
| 85 | |
| 86 | out << "\nSTATISTICS:\n\n"; |
| 87 | |
| 88 | double percentFlagged = ((double) Flagged.size()) |
| 89 | / (Flagged.size() + Unflagged.size()) * 100.0; |
| 90 | |
| 91 | out << " Percentage of warnings with flags: " |
| 92 | << llvm::format("%.4g",percentFlagged) << "%\n"; |
| 93 | |
| 94 | out << " Number of unique flags: " |
| 95 | << flagHistogram.size() << '\n'; |
| 96 | |
| 97 | double avgDiagsPerFlag = (double) Flagged.size() / flagHistogram.size(); |
| 98 | out << " Average number of diagnostics per flag: " |
| 99 | << llvm::format("%.4g", avgDiagsPerFlag) << '\n'; |
| 100 | |
Ted Kremenek | a437b84 | 2012-07-07 06:30:31 +0000 | [diff] [blame] | 101 | out << " Number in -Wpedantic (not covered by other -W flags): " |
| 102 | << flagHistogram.GetOrCreateValue("pedantic").getValue().size() |
| 103 | << '\n'; |
| 104 | |
Ted Kremenek | 2dc651d | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 105 | out << '\n'; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |