1//===- TreeView.cpp - diagtool tool for printing warning flags ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DiagTool.h"
10#include "DiagnosticNames.h"
11#include "clang/Basic/AllDiagnostics.h"
12#include "clang/Basic/Diagnostic.h"
13#include "clang/Basic/DiagnosticOptions.h"
14#include "llvm/ADT/DenseSet.h"
15#include "llvm/ADT/StringSwitch.h"
16#include "llvm/Support/Format.h"
17#include "llvm/Support/Process.h"
18#include "llvm/Support/WithColor.h"
19#include <optional>
20
21DEF_DIAGTOOL("tree", "Show warning flags in a tree view", TreeView)
22
23using namespace clang;
24using namespace diagtool;
25
26class TreePrinter {
27 using Colors = llvm::raw_ostream::Colors;
28
29public:
30 llvm::raw_ostream &out;
31 bool Internal;
32
33 TreePrinter(llvm::raw_ostream &out) : out(out), Internal(false) {}
34
35 static bool isIgnored(unsigned DiagID) {
36 // FIXME: This feels like a hack.
37 static DiagnosticOptions DiagOpts;
38 static clang::DiagnosticsEngine Diags(DiagnosticIDs::create(), DiagOpts);
39 return Diags.isIgnored(DiagID, Loc: SourceLocation());
40 }
41
42 static bool unimplemented(const GroupRecord &Group) {
43 if (!Group.diagnostics().empty())
44 return false;
45
46 return llvm::all_of(Range: Group.subgroups(), P: unimplemented);
47 }
48
49 static bool enabledByDefault(const GroupRecord &Group) {
50 for (const DiagnosticRecord &DR : Group.diagnostics()) {
51 if (isIgnored(DiagID: DR.DiagID))
52 return false;
53 }
54
55 for (const GroupRecord &GR : Group.subgroups()) {
56 if (!enabledByDefault(Group: GR))
57 return false;
58 }
59
60 return true;
61 }
62
63 void printGroup(const GroupRecord &Group, unsigned Indent = 0) {
64 out.indent(NumSpaces: Indent * 2);
65
66 if (unimplemented(Group))
67 out << Colors::RED;
68 else if (enabledByDefault(Group))
69 out << Colors::GREEN;
70 else
71 out << Colors::YELLOW;
72
73 out << "-W" << Group.getName() << "\n" << Colors::RESET;
74
75 ++Indent;
76 for (const GroupRecord &GR : Group.subgroups()) {
77 printGroup(Group: GR, Indent);
78 }
79
80 if (Internal) {
81 for (const DiagnosticRecord &DR : Group.diagnostics()) {
82 if (!isIgnored(DiagID: DR.DiagID))
83 out << Colors::GREEN;
84 out.indent(NumSpaces: Indent * 2);
85 out << DR.getName() << Colors::RESET << "\n";
86 }
87 }
88 }
89
90 int showGroup(StringRef RootGroup) {
91 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
92
93 if (RootGroup.size() > UINT16_MAX) {
94 llvm::errs() << "No such diagnostic group exists\n";
95 return 1;
96 }
97
98 const GroupRecord *Found = llvm::lower_bound(Range&: AllGroups, Value&: RootGroup);
99 if (Found == AllGroups.end() || Found->getName() != RootGroup) {
100 llvm::errs() << "No such diagnostic group exists\n";
101 return 1;
102 }
103
104 printGroup(Group: *Found);
105
106 return 0;
107 }
108
109 int showAll() {
110 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
111 llvm::DenseSet<unsigned> NonRootGroupIDs;
112
113 for (const GroupRecord &GR : AllGroups) {
114 for (auto SI = GR.subgroup_begin(), SE = GR.subgroup_end(); SI != SE;
115 ++SI) {
116 NonRootGroupIDs.insert(V: (unsigned)SI.getID());
117 }
118 }
119
120 assert(NonRootGroupIDs.size() < AllGroups.size());
121
122 for (unsigned i = 0, e = AllGroups.size(); i != e; ++i) {
123 if (!NonRootGroupIDs.count(V: i))
124 printGroup(Group: AllGroups[i]);
125 }
126
127 return 0;
128 }
129
130 void showKey() {
131 out << '\n' << Colors::GREEN << "GREEN" << Colors::RESET
132 << " = enabled by default";
133 out << '\n'
134 << Colors::YELLOW << "YELLOW" << Colors::RESET
135 << " = disabled by default";
136 out << '\n' << Colors::RED << "RED" << Colors::RESET
137 << " = unimplemented (accepted for GCC compatibility)\n\n";
138 }
139};
140
141static void printUsage() {
142 llvm::errs()
143 << "Usage: diagtool tree [--internal] [--color[=<always|auto|never>]] "
144 "[<diagnostic-group>]\n";
145}
146
147int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
148 bool Internal = false;
149 llvm::ColorMode Color = llvm::ColorMode::Auto;
150
151 while (argc > 0) {
152 StringRef Arg(*argv);
153 if (Arg == "--internal") {
154 Internal = true;
155 } else if (Arg == "--color") {
156 Color = llvm::ColorMode::Enable;
157 } else if (Arg.consume_front(Prefix: "--color=")) {
158 std::optional<llvm::ColorMode> ParsedColor =
159 llvm::StringSwitch<std::optional<llvm::ColorMode>>(Arg)
160 .Case(S: "always", Value: llvm::ColorMode::Enable)
161 .Case(S: "auto", Value: llvm::ColorMode::Auto)
162 .Case(S: "never", Value: llvm::ColorMode::Disable)
163 .Default(Value: std::nullopt);
164 if (!ParsedColor) {
165 llvm::errs() << "Invalid value for --color: '" << Arg
166 << "'; expected 'always', 'auto', or 'never'\n";
167 printUsage();
168 return -1;
169 }
170 Color = *ParsedColor;
171 } else {
172 break;
173 }
174 --argc;
175 ++argv;
176 }
177
178 bool ShowAll = false;
179 StringRef RootGroup;
180
181 switch (argc) {
182 case 0:
183 ShowAll = true;
184 break;
185 case 1:
186 RootGroup = argv[0];
187 RootGroup.consume_front(Prefix: "-W");
188 if (RootGroup == "everything")
189 ShowAll = true;
190 // FIXME: Handle other special warning flags, like -pedantic.
191 break;
192 default:
193 printUsage();
194 return -1;
195 }
196
197 switch (Color) {
198 case llvm::ColorMode::Enable:
199 out.enable_colors(enable: true);
200 break;
201 case llvm::ColorMode::Disable:
202 out.enable_colors(enable: false);
203 break;
204 case llvm::ColorMode::Auto:
205 out.enable_colors(enable: out.has_colors());
206 break;
207 }
208
209 TreePrinter TP(out);
210 TP.Internal = Internal;
211 TP.showKey();
212 return ShowAll ? TP.showAll() : TP.showGroup(RootGroup);
213}
214