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 //===----------------------------------------------------------------------===//
10 // This diagnostic tool
12 //===----------------------------------------------------------------------===//
15 #include "DiagnosticNames.h"
16 #include "clang/AST/ASTDiagnostic.h"
17 #include "clang/Basic/AllDiagnostics.h"
18 #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"
23 #include "llvm/Support/Process.h"
26 "Show warning flags in a tree view",
29 using namespace clang;
30 using namespace diagtool;
32 static void printUsage() {
33 llvm::errs() << "Usage: diagtool tree [--flags-only] [<diagnostic-group>]\n";
36 static bool showColors(llvm::raw_ostream &out) {
37 if (&out != &llvm::errs() && &out != &llvm::outs())
39 return llvm::errs().is_displayed() && llvm::outs().is_displayed();
42 static void setColor(bool ShowColors, llvm::raw_ostream &out,
43 llvm::raw_ostream::Colors Color) {
45 out << llvm::sys::Process::OutputColor(Color, false, false);
48 static void resetColor(bool ShowColors, llvm::raw_ostream &out) {
50 out << llvm::sys::Process::ResetColor();
53 static clang::DiagnosticsEngine::Level getLevel(unsigned DiagID) {
54 // FIXME: This feels like a hack.
55 static clang::DiagnosticsEngine Diags(new DiagnosticIDs,
56 new DiagnosticOptions);
57 return Diags.getDiagnosticLevel(DiagID, SourceLocation());
60 static void printGroup(llvm::raw_ostream &out, const GroupRecord &Group,
61 bool FlagsOnly, unsigned Indent = 0) {
62 out.indent(Indent * 2);
64 bool ShowColors = showColors(out);
65 setColor(ShowColors, out, llvm::raw_ostream::YELLOW);
66 out << "-W" << Group.getName() << "\n";
67 resetColor(ShowColors, out);
70 for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
71 E = Group.subgroup_end();
73 printGroup(out, *I, FlagsOnly, Indent);
77 for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
78 E = Group.diagnostics_end();
81 if (getLevel(I->DiagID) != DiagnosticsEngine::Ignored) {
82 setColor(ShowColors, out, llvm::raw_ostream::GREEN);
85 out.indent(Indent * 2);
87 resetColor(ShowColors, out);
93 static int showGroup(llvm::raw_ostream &out, StringRef RootGroup,
95 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
97 GroupRecord Key = { RootGroup.size(), RootGroup.data(), 0, 0 };
98 const GroupRecord *Found =
99 std::lower_bound(AllGroups.begin(), AllGroups.end(), Key);
101 if (Found == AllGroups.end() || Found->getName() != RootGroup) {
102 llvm::errs() << "No such diagnostic group exists\n";
106 printGroup(out, *Found, FlagsOnly);
111 static int showAll(llvm::raw_ostream &out, bool FlagsOnly) {
112 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
113 llvm::DenseSet<unsigned> NonRootGroupIDs;
115 for (ArrayRef<GroupRecord>::iterator I = AllGroups.begin(),
118 for (GroupRecord::subgroup_iterator SI = I->subgroup_begin(),
119 SE = I->subgroup_end();
121 NonRootGroupIDs.insert((unsigned)SI.getID());
125 assert(NonRootGroupIDs.size() < AllGroups.size());
127 for (unsigned i = 0, e = AllGroups.size(); i != e; ++i) {
128 if (!NonRootGroupIDs.count(i))
129 printGroup(out, AllGroups[i], FlagsOnly);
135 int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
136 // First check our one flag (--flags-only).
137 bool FlagsOnly = false;
139 StringRef FirstArg(*argv);
140 if (FirstArg.equals("--flags-only")) {
147 bool ShowAll = false;
156 if (RootGroup.startswith("-W"))
157 RootGroup = RootGroup.substr(2);
158 if (RootGroup == "everything")
160 // FIXME: Handle other special warning flags, like -pedantic.
167 if (showColors(out)) {
169 setColor(true, out, llvm::raw_ostream::GREEN);
171 resetColor(true, out);
172 out << " = enabled by default\n\n";
176 return showAll(out, FlagsOnly);
178 return showGroup(out, RootGroup, FlagsOnly);