blob: 3e6e88306e24712ef7273a2ff2ccd50227b17cd1 [file] [log] [blame]
Ted Kremenekf88d3352011-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 Rose2fe20dc2012-06-04 16:57:50 +000016#include "DiagnosticNames.h"
David Blaikie040a3a22012-02-15 19:45:34 +000017#include "clang/AST/ASTDiagnostic.h"
David Blaikieaf90ec12012-02-15 21:58:34 +000018#include "clang/Basic/AllDiagnostics.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000019#include "clang/Basic/Diagnostic.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/Support/Format.h"
Ted Kremenekf88d3352011-08-09 03:39:19 +000022
23DEF_DIAGTOOL("list-warnings",
24 "List warnings and their corresponding flags",
25 ListWarnings)
26
27using namespace clang;
Jordan Rose473e8772012-06-24 00:07:45 +000028using namespace diagtool;
Ted Kremenekf88d3352011-08-09 03:39:19 +000029
David Blaikie040a3a22012-02-15 19:45:34 +000030namespace {
Ted Kremenekf88d3352011-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};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000040}
Ted Kremenekf88d3352011-08-09 03:39:19 +000041
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 Lewycky1c5f3fa2011-08-12 01:14:22 +000053 std::vector<Entry> Flagged, Unflagged;
Ted Kremenekf88d3352011-08-09 03:39:19 +000054 llvm::StringMap<std::vector<unsigned> > flagHistogram;
55
Jordan Rose473e8772012-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 Blaikie040a3a22012-02-15 19:45:34 +000061 unsigned diagID = di->DiagID;
Ted Kremenekf88d3352011-08-09 03:39:19 +000062
63 if (DiagnosticIDs::isBuiltinNote(diagID))
64 continue;
65
66 if (!DiagnosticIDs::isBuiltinWarningOrExtension(diagID))
67 continue;
68
David Blaikie040a3a22012-02-15 19:45:34 +000069 Entry entry(di->getName(),
Ted Kremenekf88d3352011-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);
David Blaikie13156b62014-11-19 03:06:06 +000076 flagHistogram[entry.Flag].push_back(diagID);
Ted Kremenekf88d3352011-08-09 03:39:19 +000077 }
78 }
79
Ted Kremenekf88d3352011-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';
David Blaikie3c8c46e2014-11-19 05:48:40 +0000100
Ted Kremenek30105f62012-07-07 06:30:31 +0000101 out << " Number in -Wpedantic (not covered by other -W flags): "
David Blaikie3c8c46e2014-11-19 05:48:40 +0000102 << flagHistogram["pedantic"].size() << '\n';
103
Ted Kremenekf88d3352011-08-09 03:39:19 +0000104 out << '\n';
105
106 return 0;
107}
108