ListWarnings.cpp revision f11b0f9a4e8d79f1bf59f4271f96d28b55bcc067
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"
16#include "DiagnosticNames.h"
17#include "clang/Basic/Diagnostic.h"
18#include "llvm/Support/Format.h"
19#include "llvm/ADT/StringMap.h"
20#include "clang/AST/ASTDiagnostic.h"
21#include "clang/Basic/AllDiagnostics.h"
22
23DEF_DIAGTOOL("list-warnings",
24             "List warnings and their corresponding flags",
25             ListWarnings)
26
27using namespace clang;
28
29namespace {
30struct Entry {
31  llvm::StringRef DiagName;
32  llvm::StringRef Flag;
33
34  Entry(llvm::StringRef diagN, llvm::StringRef flag)
35    : DiagName(diagN), Flag(flag) {}
36
37  bool operator<(const Entry &x) const { return DiagName < x.DiagName; }
38};
39}
40
41static void printEntries(std::vector<Entry> &entries, llvm::raw_ostream &out) {
42  for (std::vector<Entry>::iterator it = entries.begin(), ei = entries.end();
43       it != ei; ++it) {
44    out << "  " << it->DiagName;
45    if (!it->Flag.empty())
46      out << " [-W" << it->Flag << "]";
47    out << '\n';
48  }
49}
50
51int ListWarnings::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
52  std::vector<Entry> Flagged, Unflagged;
53  llvm::StringMap<std::vector<unsigned> > flagHistogram;
54
55  for (const diagtool::DiagnosticRecord *di = diagtool::BuiltinDiagnostics,
56       *de = di + diagtool::BuiltinDiagnosticsCount; di != de; ++di) {
57
58    unsigned diagID = di->DiagID;
59
60    if (DiagnosticIDs::isBuiltinNote(diagID))
61      continue;
62
63    if (!DiagnosticIDs::isBuiltinWarningOrExtension(diagID))
64      continue;
65
66    Entry entry(di->getName(),
67                DiagnosticIDs::getWarningOptionForDiag(diagID));
68
69    if (entry.Flag.empty())
70      Unflagged.push_back(entry);
71    else {
72      Flagged.push_back(entry);
73      flagHistogram.GetOrCreateValue(entry.Flag).getValue().push_back(diagID);
74    }
75  }
76
77  std::sort(Flagged.begin(), Flagged.end());
78  std::sort(Unflagged.begin(), Unflagged.end());
79
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
101  out << '\n';
102
103  return 0;
104}
105
106