]> granicus.if.org Git - clang/commitdiff
diagtool: refactor TreeView to resemble C++
authorAlp Toker <alp@nuanti.com>
Fri, 20 Jun 2014 00:06:42 +0000 (00:06 +0000)
committerAlp Toker <alp@nuanti.com>
Fri, 20 Jun 2014 00:06:42 +0000 (00:06 +0000)
Replace lots of old-school parameter passing with neat class members.
No attempt made yet to modernize loops, but it's a start.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@211327 91177308-0d34-0410-b5e6-96231b3b80d8

tools/diagtool/TreeView.cpp

index 6cd7f2e54855d1029e60648adeaad85000af8b89..3647e393b869cad2e1f54ff7da2e8631cdab4bb3 100644 (file)
@@ -6,10 +6,6 @@
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
-//
-// This diagnostic tool 
-//
-//===----------------------------------------------------------------------===//
 
 #include "DiagTool.h"
 #include "DiagnosticNames.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/Process.h"
 
-DEF_DIAGTOOL("tree",
-             "Show warning flags in a tree view",
-             TreeView)
-  
+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 bool showColors(llvm::raw_ostream &out) {
+static bool hasColors(const llvm::raw_ostream &out) {
   if (&out != &llvm::errs() && &out != &llvm::outs())
     return false;
   return llvm::errs().is_displayed() && llvm::outs().is_displayed();
 }
 
-static void setColor(bool ShowColors, llvm::raw_ostream &out,
-                     llvm::raw_ostream::Colors Color) {
-  if (ShowColors)
-    out << llvm::sys::Process::OutputColor(Color, false, false);
-}
+class TreePrinter {
+public:
+  llvm::raw_ostream &out;
+  const bool ShowColors;
+  bool FlagsOnly;
 
-static void resetColor(bool ShowColors, llvm::raw_ostream &out) {
-  if (ShowColors)
-    out << llvm::sys::Process::ResetColor();
-}
+  TreePrinter(llvm::raw_ostream &out)
+      : out(out), ShowColors(hasColors(out)), FlagsOnly(false) {}
 
-static bool isIgnored(unsigned DiagID) {
-  // FIXME: This feels like a hack.
-  static clang::DiagnosticsEngine Diags(new DiagnosticIDs,
-                                        new DiagnosticOptions);
-  return Diags.isIgnored(DiagID, SourceLocation());
-}
+  void setColor(llvm::raw_ostream::Colors Color) {
+    if (ShowColors)
+      out << llvm::sys::Process::OutputColor(Color, false, false);
+  }
+
+  void resetColor() {
+    if (ShowColors)
+      out << llvm::sys::Process::ResetColor();
+  }
 
-static void printGroup(llvm::raw_ostream &out, const GroupRecord &Group,
-                       bool FlagsOnly, unsigned Indent = 0) {
-  out.indent(Indent * 2);
-  
-  bool ShowColors = showColors(out);
-  setColor(ShowColors, out, llvm::raw_ostream::YELLOW);
-  out << "-W" << Group.getName() << "\n";
-  resetColor(ShowColors, out);
-  
-  ++Indent;
-  for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
-                                      E = Group.subgroup_end();
-       I != E; ++I) {
-    printGroup(out, *I, FlagsOnly, Indent);
+  static bool isIgnored(unsigned DiagID) {
+    // FIXME: This feels like a hack.
+    static clang::DiagnosticsEngine Diags(new DiagnosticIDs,
+                                          new DiagnosticOptions);
+    return Diags.isIgnored(DiagID, SourceLocation());
   }
 
-  if (!FlagsOnly) {
-    for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
-                                           E = Group.diagnostics_end();
+  void printGroup(const GroupRecord &Group, unsigned Indent = 0) {
+    out.indent(Indent * 2);
+
+    setColor(llvm::raw_ostream::YELLOW);
+    out << "-W" << Group.getName() << "\n";
+    resetColor();
+
+    ++Indent;
+    for (GroupRecord::subgroup_iterator I = Group.subgroup_begin(),
+                                        E = Group.subgroup_end();
          I != E; ++I) {
-      if (ShowColors) {
-        if (!isIgnored(I->DiagID)) {
-          setColor(ShowColors, out, llvm::raw_ostream::GREEN);
-        }
+      printGroup(*I, Indent);
+    }
+
+    if (!FlagsOnly) {
+      for (GroupRecord::diagnostics_iterator I = Group.diagnostics_begin(),
+                                             E = Group.diagnostics_end();
+           I != E; ++I) {
+        if (ShowColors && !isIgnored(I->DiagID))
+          setColor(llvm::raw_ostream::GREEN);
+        out.indent(Indent * 2);
+        out << I->getName();
+        resetColor();
+        out << "\n";
       }
-      out.indent(Indent * 2);
-      out << I->getName();
-      resetColor(ShowColors, out);
-      out << "\n";
     }
   }
-}
 
-static int showGroup(llvm::raw_ostream &out, StringRef RootGroup,
-                     bool FlagsOnly) {
-  ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
+  int showGroup(StringRef RootGroup) {
+    ArrayRef<GroupRecord> AllGroups = getDiagnosticGroups();
 
-  if (RootGroup.size() > UINT16_MAX) {
-    llvm::errs() << "No such diagnostic group exists\n";
-    return 1;
-  }
+    if (RootGroup.size() > UINT16_MAX) {
+      llvm::errs() << "No such diagnostic group exists\n";
+      return 1;
+    }
 
-  const GroupRecord *Found =
-    std::lower_bound(AllGroups.begin(), AllGroups.end(), RootGroup);
-  
-  if (Found == AllGroups.end() || Found->getName() != RootGroup) {
-    llvm::errs() << "No such diagnostic group exists\n";
-    return 1;
-  }
-  
-  printGroup(out, *Found, FlagsOnly);
-  
-  return 0;
-}
+    const GroupRecord *Found =
+        std::lower_bound(AllGroups.begin(), AllGroups.end(), RootGroup);
 
-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());
+    if (Found == AllGroups.end() || Found->getName() != RootGroup) {
+      llvm::errs() << "No such diagnostic group exists\n";
+      return 1;
     }
+
+    printGroup(*Found);
+
+    return 0;
   }
 
-  assert(NonRootGroupIDs.size() < AllGroups.size());
+  int showAll() {
+    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(AllGroups[i]);
+    }
 
-  for (unsigned i = 0, e = AllGroups.size(); i != e; ++i) {
-    if (!NonRootGroupIDs.count(i))
-      printGroup(out, AllGroups[i], FlagsOnly);
+    return 0;
   }
 
-  return 0;
+  void showKey() {
+    if (ShowColors) {
+      out << '\n';
+      setColor(llvm::raw_ostream::GREEN);
+      out << "GREEN";
+      resetColor();
+      out << " = enabled by default\n\n";
+    }
+  }
+};
+
+static void printUsage() {
+  llvm::errs() << "Usage: diagtool tree [--flags-only] [<diagnostic-group>]\n";
 }
 
 int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
@@ -147,7 +154,7 @@ int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
       ++argv;
     }
   }
-  
+
   bool ShowAll = false;
   StringRef RootGroup;
 
@@ -168,17 +175,8 @@ int TreeView::run(unsigned int argc, char **argv, llvm::raw_ostream &out) {
     return -1;
   }
 
-  if (showColors(out)) {
-    out << '\n';
-    setColor(true, out, llvm::raw_ostream::GREEN);
-    out << "GREEN";
-    resetColor(true, out);
-    out << " = enabled by default\n\n";
-  }
-  
-  if (ShowAll)
-    return showAll(out, FlagsOnly);
-
-  return showGroup(out, RootGroup, FlagsOnly);
+  TreePrinter TP(out);
+  TP.FlagsOnly = FlagsOnly;
+  TP.showKey();
+  return ShowAll ? TP.showAll() : TP.showGroup(RootGroup);
 }
-