summaryrefslogtreecommitdiffstats
path: root/tools/diagtool/TreeView.cpp
blob: 9db2c26dd92dc89e1bf1ae3b062a90470c30b767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//===- TreeView.cpp - diagtool tool for printing warning flags ------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This diagnostic tool 
//
//===----------------------------------------------------------------------===//

#include "DiagTool.h"
#include "DiagnosticNames.h"
#include "clang/Basic/Diagnostic.h"
#include "llvm/Support/Format.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/DenseSet.h"
#include "clang/AST/ASTDiagnostic.h"
#include "clang/Basic/AllDiagnostics.h"

DEF_DIAGTOOL("tree",
             "Show warning flags in a tree view",
             TreeView)
  
using namespace clang;
using namespace diagtool;

static void printUsage() {
  llvm::errs() << "Usage: diagtool tree [--flags-only] [<diagnostic-group>]\n";
}

static void printGroup(llvm::raw_ostream &out, const GroupRecord &Group,
                       bool FlagsOnly, unsigned Indent = 0) {
  out.indent(Indent * 2);
  out << "-W" << Group.getName() << "\n";

  ++Indent;
  for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
                                      E = Group.subgroup_end();
       I != E; ++I) {
    printGroup(out, *I, FlagsOnly, Indent);
  }

  if (!FlagsOnly) {
    for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
                                           E = Group.diagnostics_end();
         I != E; ++I) {
      out.indent(Indent * 2);
      out << I->getName() << "\n";
    }
  }
}

static int showGroup(llvm::raw_ostream &out, StringRef RootGroup,
                     bool FlagsOnly) {
  ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();

  GroupRecord Key = { RootGroup.size(), RootGroup.data(), 0, 0 };
  const GroupRecord *Found =
    std::lower_bound(AllGroups.begin(), AllGroups.end(), Key);
  
  if (Found == AllGroups.end() || Found->getName() != RootGroup) {
    llvm::errs() << "No such diagnostic group exists\n";
    return 1;
  }
  
  printGroup(out, *Found, FlagsOnly);
  
  return 0;
}

static int showAll(llvm::raw_ostream &out, bool FlagsOnly) {
  ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
  llvm::DenseSet<unsigned> NonRootGroupIDs;

  for (ArrayRef<GroupRecord>::iterator I = AllGroups.begin(),
                                       E = AllGroups.end();
       I != E; ++I) {
    for (GroupRecord::subgroup_iterator SI = I->subgroup_begin(),
                                        SE = I->subgroup_end();
         SI != SE; ++SI) {
      NonRootGroupIDs.insert((unsigned)SI.getID());
    }
  }

  assert(NonRootGroupIDs.size() < AllGroups.size());

  for (unsigned i = 0, e = AllGroups.size(); i != e; ++i) {
    if (!NonRootGroupIDs.count(i))
      printGroup(out, AllGroups[i], FlagsOnly);
  }

  return 0;
}

int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
  // First check our one flag (--flags-only).
  bool FlagsOnly = false;
  if (argc > 0) {
    StringRef FirstArg(*argv);
    if (FirstArg.equals("--flags-only")) {
      FlagsOnly = true;
      --argc;
      ++argv;
    }
  }

  bool ShowAll = false;
  StringRef RootGroup;

  switch (argc) {
  case 0:
    ShowAll = true;
    break;
  case 1:
    RootGroup = argv[0];
    if (RootGroup.startswith("-W"))
      RootGroup = RootGroup.substr(2);
    if (RootGroup == "everything")
      ShowAll = true;
    // FIXME: Handle other special warning flags, like -pedantic.
    break;
  default:
    printUsage();
    return -1;
  }

  if (ShowAll)
    return showAll(out, FlagsOnly);

  return showGroup(out, RootGroup, FlagsOnly);
}

OpenPOWER on IntegriCloud