blob: 10809c1d8d99951055e3d8e42c8ad4e5428d1a33 [file] [log] [blame]
Jordan Rosee7427632012-06-24 00:07:45 +00001//===- TreeView.cpp - 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 diagnostic tool
11//
12//===----------------------------------------------------------------------===//
13
14#include "DiagTool.h"
15#include "DiagnosticNames.h"
Jordan Rosee7427632012-06-24 00:07:45 +000016#include "clang/AST/ASTDiagnostic.h"
17#include "clang/Basic/AllDiagnostics.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000018#include "clang/Basic/Diagnostic.h"
19#include "clang/Basic/DiagnosticOptions.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/Support/Format.h"
Ted Kremenekefdfc1d2012-10-05 22:07:14 +000023#include "llvm/Support/Process.h"
Jordan Rosee7427632012-06-24 00:07:45 +000024
25DEF_DIAGTOOL("tree",
26 "Show warning flags in a tree view",
27 TreeView)
28
29using namespace clang;
30using namespace diagtool;
31
32static void printUsage() {
33 llvm::errs() << "Usage: diagtool tree [--flags-only] [<diagnostic-group>]\n";
34}
35
Ted Kremenekefdfc1d2012-10-05 22:07:14 +000036static bool showColors(llvm::raw_ostream &out) {
37 if (&out != &llvm::errs() && &out != &llvm::outs())
38 return false;
39 return llvm::errs().is_displayed() && llvm::outs().is_displayed();
40}
41
42static void setColor(bool ShowColors, llvm::raw_ostream &out,
43 llvm::raw_ostream::Colors Color) {
44 if (ShowColors)
45 out << llvm::sys::Process::OutputColor(Color, false, false);
46}
47
48static void resetColor(bool ShowColors, llvm::raw_ostream &out) {
49 if (ShowColors)
50 out << llvm::sys::Process::ResetColor();
51}
52
53static clang::DiagnosticsEngine::Level getLevel(unsigned DiagID) {
Douglas Gregor02c23eb2012-10-23 22:26:28 +000054 // FIXME: This feels like a hack.
55 static clang::DiagnosticsEngine Diags(new DiagnosticIDs,
56 new DiagnosticOptions);
Ted Kremenekefdfc1d2012-10-05 22:07:14 +000057 return Diags.getDiagnosticLevel(DiagID, SourceLocation());
58}
59
Jordan Rosee7427632012-06-24 00:07:45 +000060static void printGroup(llvm::raw_ostream &out, const GroupRecord &Group,
61 bool FlagsOnly, unsigned Indent = 0) {
62 out.indent(Indent * 2);
Ted Kremenekefdfc1d2012-10-05 22:07:14 +000063
64 bool ShowColors = showColors(out);
65 setColor(ShowColors, out, llvm::raw_ostream::YELLOW);
Jordan Rosee7427632012-06-24 00:07:45 +000066 out << "-W" << Group.getName() << "\n";
Ted Kremenekefdfc1d2012-10-05 22:07:14 +000067 resetColor(ShowColors, out);
68
Jordan Rosee7427632012-06-24 00:07:45 +000069 ++Indent;
70 for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
71 E = Group.subgroup_end();
72 I != E; ++I) {
73 printGroup(out, *I, FlagsOnly, Indent);
74 }
75
76 if (!FlagsOnly) {
77 for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
78 E = Group.diagnostics_end();
79 I != E; ++I) {
Ted Kremenekefdfc1d2012-10-05 22:07:14 +000080 if (ShowColors) {
81 if (getLevel(I->DiagID) != DiagnosticsEngine::Ignored) {
82 setColor(ShowColors, out, llvm::raw_ostream::GREEN);
83 }
84 }
Jordan Rosee7427632012-06-24 00:07:45 +000085 out.indent(Indent * 2);
Ted Kremenekefdfc1d2012-10-05 22:07:14 +000086 out << I->getName();
87 resetColor(ShowColors, out);
88 out << "\n";
Jordan Rosee7427632012-06-24 00:07:45 +000089 }
90 }
91}
92
93static int showGroup(llvm::raw_ostream &out, StringRef RootGroup,
94 bool FlagsOnly) {
95 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
96
Craig Topper0e28a252013-08-28 06:01:10 +000097 if (RootGroup.size() > UINT16_MAX) {
98 llvm::errs() << "No such diagnostic group exists\n";
99 return 1;
100 }
101
102 GroupRecord Key = { RootGroup.data(), (uint16_t)RootGroup.size(), 0, 0 };
Jordan Rosee7427632012-06-24 00:07:45 +0000103 const GroupRecord *Found =
104 std::lower_bound(AllGroups.begin(), AllGroups.end(), Key);
105
106 if (Found == AllGroups.end() || Found->getName() != RootGroup) {
107 llvm::errs() << "No such diagnostic group exists\n";
108 return 1;
109 }
110
111 printGroup(out, *Found, FlagsOnly);
112
113 return 0;
114}
115
116static int showAll(llvm::raw_ostream &out, bool FlagsOnly) {
117 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
118 llvm::DenseSet<unsigned> NonRootGroupIDs;
119
120 for (ArrayRef<GroupRecord>::iterator I = AllGroups.begin(),
121 E = AllGroups.end();
122 I != E; ++I) {
123 for (GroupRecord::subgroup_iterator SI = I->subgroup_begin(),
124 SE = I->subgroup_end();
125 SI != SE; ++SI) {
126 NonRootGroupIDs.insert((unsigned)SI.getID());
127 }
128 }
129
130 assert(NonRootGroupIDs.size() < AllGroups.size());
131
132 for (unsigned i = 0, e = AllGroups.size(); i != e; ++i) {
133 if (!NonRootGroupIDs.count(i))
134 printGroup(out, AllGroups[i], FlagsOnly);
135 }
136
137 return 0;
138}
139
140int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
141 // First check our one flag (--flags-only).
142 bool FlagsOnly = false;
143 if (argc > 0) {
144 StringRef FirstArg(*argv);
145 if (FirstArg.equals("--flags-only")) {
146 FlagsOnly = true;
147 --argc;
148 ++argv;
149 }
150 }
Ted Kremenekefdfc1d2012-10-05 22:07:14 +0000151
Jordan Rosee7427632012-06-24 00:07:45 +0000152 bool ShowAll = false;
153 StringRef RootGroup;
154
155 switch (argc) {
156 case 0:
157 ShowAll = true;
158 break;
159 case 1:
160 RootGroup = argv[0];
161 if (RootGroup.startswith("-W"))
162 RootGroup = RootGroup.substr(2);
163 if (RootGroup == "everything")
164 ShowAll = true;
165 // FIXME: Handle other special warning flags, like -pedantic.
166 break;
167 default:
168 printUsage();
169 return -1;
170 }
171
Ted Kremenekefdfc1d2012-10-05 22:07:14 +0000172 if (showColors(out)) {
173 out << '\n';
174 setColor(true, out, llvm::raw_ostream::GREEN);
175 out << "GREEN";
176 resetColor(true, out);
177 out << " = enabled by default\n\n";
178 }
179
Jordan Rosee7427632012-06-24 00:07:45 +0000180 if (ShowAll)
181 return showAll(out, FlagsOnly);
182
183 return showGroup(out, RootGroup, FlagsOnly);
184}
185