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/Basic/Diagnostic.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "clang/AST/ASTDiagnostic.h"
21 #include "clang/Basic/AllDiagnostics.h"
24 "Show warning flags in a tree view",
27 using namespace clang;
28 using namespace diagtool;
30 static void printUsage() {
31 llvm::errs() << "Usage: diagtool tree [--flags-only] [<diagnostic-group>]\n";
34 static void printGroup(llvm::raw_ostream &out, const GroupRecord &Group,
35 bool FlagsOnly, unsigned Indent = 0) {
36 out.indent(Indent * 2);
37 out << "-W" << Group.getName() << "\n";
40 for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
41 E = Group.subgroup_end();
43 printGroup(out, *I, FlagsOnly, Indent);
47 for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
48 E = Group.diagnostics_end();
50 out.indent(Indent * 2);
51 out << I->getName() << "\n";
56 static int showGroup(llvm::raw_ostream &out, StringRef RootGroup,
58 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
60 GroupRecord Key = { RootGroup.size(), RootGroup.data(), 0, 0 };
61 const GroupRecord *Found =
62 std::lower_bound(AllGroups.begin(), AllGroups.end(), Key);
64 if (Found == AllGroups.end() || Found->getName() != RootGroup) {
65 llvm::errs() << "No such diagnostic group exists\n";
69 printGroup(out, *Found, FlagsOnly);
74 static int showAll(llvm::raw_ostream &out, bool FlagsOnly) {
75 ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
76 llvm::DenseSet<unsigned> NonRootGroupIDs;
78 for (ArrayRef<GroupRecord>::iterator I = AllGroups.begin(),
81 for (GroupRecord::subgroup_iterator SI = I->subgroup_begin(),
82 SE = I->subgroup_end();
84 NonRootGroupIDs.insert((unsigned)SI.getID());
88 assert(NonRootGroupIDs.size() < AllGroups.size());
90 for (unsigned i = 0, e = AllGroups.size(); i != e; ++i) {
91 if (!NonRootGroupIDs.count(i))
92 printGroup(out, AllGroups[i], FlagsOnly);
98 int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
99 // First check our one flag (--flags-only).
100 bool FlagsOnly = false;
102 StringRef FirstArg(*argv);
103 if (FirstArg.equals("--flags-only")) {
110 bool ShowAll = false;
119 if (RootGroup.startswith("-W"))
120 RootGroup = RootGroup.substr(2);
121 if (RootGroup == "everything")
123 // FIXME: Handle other special warning flags, like -pedantic.
131 return showAll(out, FlagsOnly);
133 return showGroup(out, RootGroup, FlagsOnly);