1 //===- TreeView.cpp - diagtool tool for printing warning flags ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "DiagnosticNames.h"
12 #include "clang/AST/ASTDiagnostic.h"
13 #include "clang/Basic/AllDiagnostics.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/DiagnosticOptions.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/Process.h"
20 DEF_DIAGTOOL("tree", "Show warning flags in a tree view", TreeView)
22 using namespace clang;
23 using namespace diagtool;
25 static bool hasColors(const llvm::raw_ostream &out) {
26 if (&out != &llvm::errs() && &out != &llvm::outs())
28 return llvm::errs().is_displayed() && llvm::outs().is_displayed();
33 llvm::raw_ostream &out;
34 const bool ShowColors;
37 TreePrinter(llvm::raw_ostream &out)
38 : out(out), ShowColors(hasColors(out)), FlagsOnly(false) {}
40 void setColor(llvm::raw_ostream::Colors Color) {
42 out << llvm::sys::Process::OutputColor(Color, false, false);
47 out << llvm::sys::Process::ResetColor();
50 static bool isIgnored(unsigned DiagID) {
51 // FIXME: This feels like a hack.
52 static clang::DiagnosticsEngine Diags(new DiagnosticIDs,
53 new DiagnosticOptions);
54 return Diags.isIgnored(DiagID, SourceLocation());
57 void printGroup(const GroupRecord &Group, unsigned Indent = 0) {
58 out.indent(Indent * 2);
60 setColor(llvm::raw_ostream::YELLOW);
61 out << "-W" << Group.getName() << "\n";
65 for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
66 E = Group.subgroup_end();
68 printGroup(*I, Indent);
72 for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
73 E = Group.diagnostics_end();
75 if (ShowColors && !isIgnored(I->DiagID))
76 setColor(llvm::raw_ostream::GREEN);
77 out.indent(Indent * 2);
85 int showGroup(StringRef RootGroup) {
86 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
88 if (RootGroup.size() > UINT16_MAX) {
89 llvm::errs() << "No such diagnostic group exists\n";
93 const GroupRecord *Found =
94 std::lower_bound(AllGroups.begin(), AllGroups.end(), RootGroup);
96 if (Found == AllGroups.end() || Found->getName() != RootGroup) {
97 llvm::errs() << "No such diagnostic group exists\n";
107 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
108 llvm::DenseSet<unsigned> NonRootGroupIDs;
110 for (ArrayRef<GroupRecord>::iterator I = AllGroups.begin(),
113 for (GroupRecord::subgroup_iterator SI = I->subgroup_begin(),
114 SE = I->subgroup_end();
116 NonRootGroupIDs.insert((unsigned)SI.getID());
120 assert(NonRootGroupIDs.size() < AllGroups.size());
122 for (unsigned i = 0, e = AllGroups.size(); i != e; ++i) {
123 if (!NonRootGroupIDs.count(i))
124 printGroup(AllGroups[i]);
133 setColor(llvm::raw_ostream::GREEN);
136 out << " = enabled by default\n\n";
141 static void printUsage() {
142 llvm::errs() << "Usage: diagtool tree [--flags-only] [<diagnostic-group>]\n";
145 int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
146 // First check our one flag (--flags-only).
147 bool FlagsOnly = false;
149 StringRef FirstArg(*argv);
150 if (FirstArg.equals("--flags-only")) {
157 bool ShowAll = false;
166 if (RootGroup.startswith("-W"))
167 RootGroup = RootGroup.substr(2);
168 if (RootGroup == "everything")
170 // FIXME: Handle other special warning flags, like -pedantic.
178 TP.FlagsOnly = FlagsOnly;
180 return ShowAll ? TP.showAll() : TP.showGroup(RootGroup);