blob: 16837a1584953e168e80ef2aad408174d73b3c6e [file] [log] [blame]
Ted Kremenek2dc651d2011-08-09 03:39:19 +00001//===- 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 Rose0832f822012-06-04 16:57:50 +000016#include "DiagnosticNames.h"
David Blaikieceb15652012-02-15 19:45:34 +000017#include "clang/AST/ASTDiagnostic.h"
David Blaikie6c448862012-02-15 21:58:34 +000018#include "clang/Basic/AllDiagnostics.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000019#include "clang/Basic/Diagnostic.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/Support/Format.h"
Ted Kremenek2dc651d2011-08-09 03:39:19 +000022
23DEF_DIAGTOOL("list-warnings",
24 "List warnings and their corresponding flags",
25 ListWarnings)
26
27using namespace clang;
Jordan Rosee7427632012-06-24 00:07:45 +000028using namespace diagtool;
Ted Kremenek2dc651d2011-08-09 03:39:19 +000029
David Blaikieceb15652012-02-15 19:45:34 +000030namespace {
Ted Kremenek2dc651d2011-08-09 03:39:19 +000031struct 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
42static 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
52int ListWarnings::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
Nick Lewycky83f06e82011-08-12 01:14:22 +000053 std::vector<Entry> Flagged, Unflagged;
Ted Kremenek2dc651d2011-08-09 03:39:19 +000054 llvm::StringMap<std::vector<unsigned> > flagHistogram;
55
Jordan Rosee7427632012-06-24 00:07:45 +000056 ArrayRef<DiagnosticRecord> AllDiagnostics = getBuiltinDiagnosticsByName();
57
58 for (ArrayRef<DiagnosticRecord>::iterator di = AllDiagnostics.begin(),
59 de = AllDiagnostics.end();
60 di != de; ++di) {
David Blaikieceb15652012-02-15 19:45:34 +000061 unsigned diagID = di->DiagID;
Ted Kremenek2dc651d2011-08-09 03:39:19 +000062
63 if (DiagnosticIDs::isBuiltinNote(diagID))
64 continue;
65
66 if (!DiagnosticIDs::isBuiltinWarningOrExtension(diagID))
67 continue;
68
David Blaikieceb15652012-02-15 19:45:34 +000069 Entry entry(di->getName(),
Ted Kremenek2dc651d2011-08-09 03:39:19 +000070 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 Kremenek2dc651d2011-08-09 03:39:19 +000080 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 Kremeneka437b842012-07-07 06:30:31 +0000101 out << " Number in -Wpedantic (not covered by other -W flags): "
102 << flagHistogram.GetOrCreateValue("pedantic").getValue().size()
103 << '\n';
104
Ted Kremenek2dc651d2011-08-09 03:39:19 +0000105 out << '\n';
106
107 return 0;
108}
109