Ted Kremenek | f88d335 | 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 | 2fe20dc | 2012-06-04 16:57:50 +0000 | [diff] [blame] | 16 | #include "DiagnosticNames.h" |
David Blaikie | 040a3a2 | 2012-02-15 19:45:34 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTDiagnostic.h" |
David Blaikie | af90ec1 | 2012-02-15 21:58:34 +0000 | [diff] [blame] | 18 | #include "clang/Basic/AllDiagnostics.h" |
Chandler Carruth | cc0694c | 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 | f88d335 | 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 | 473e877 | 2012-06-24 00:07:45 +0000 | [diff] [blame] | 28 | using namespace diagtool; |
Ted Kremenek | f88d335 | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 29 | |
David Blaikie | 040a3a2 | 2012-02-15 19:45:34 +0000 | [diff] [blame] | 30 | namespace { |
Ted Kremenek | f88d335 | 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 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 40 | } |
Ted Kremenek | f88d335 | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 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 | 1c5f3fa | 2011-08-12 01:14:22 +0000 | [diff] [blame] | 53 | std::vector<Entry> Flagged, Unflagged; |
Ted Kremenek | f88d335 | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 54 | llvm::StringMap<std::vector<unsigned> > flagHistogram; |
| 55 | |
Jordan Rose | 473e877 | 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 | 040a3a2 | 2012-02-15 19:45:34 +0000 | [diff] [blame] | 61 | unsigned diagID = di->DiagID; |
Ted Kremenek | f88d335 | 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 | 040a3a2 | 2012-02-15 19:45:34 +0000 | [diff] [blame] | 69 | Entry entry(di->getName(), |
Ted Kremenek | f88d335 | 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); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 76 | flagHistogram[entry.Flag].push_back(diagID); |
Ted Kremenek | f88d335 | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Ted Kremenek | f88d335 | 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'; |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | 30105f6 | 2012-07-07 06:30:31 +0000 | [diff] [blame] | 101 | out << " Number in -Wpedantic (not covered by other -W flags): " |
David Blaikie | 3c8c46e | 2014-11-19 05:48:40 +0000 | [diff] [blame] | 102 | << flagHistogram["pedantic"].size() << '\n'; |
| 103 | |
Ted Kremenek | f88d335 | 2011-08-09 03:39:19 +0000 | [diff] [blame] | 104 | out << '\n'; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |