From 02a13f7337230481e2e972d6a8d87b0a31536d30 Mon Sep 17 00:00:00 2001 From: Alp Toker Date: Fri, 20 Jun 2014 00:06:42 +0000 Subject: [PATCH] diagtool: refactor TreeView to resemble C++ 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 | 204 ++++++++++++++++++------------------ 1 file changed, 101 insertions(+), 103 deletions(-) diff --git a/tools/diagtool/TreeView.cpp b/tools/diagtool/TreeView.cpp index 6cd7f2e548..3647e393b8 100644 --- a/tools/diagtool/TreeView.cpp +++ b/tools/diagtool/TreeView.cpp @@ -6,10 +6,6 @@ // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// -// -// This diagnostic tool -// -//===----------------------------------------------------------------------===// #include "DiagTool.h" #include "DiagnosticNames.h" @@ -22,118 +18,129 @@ #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] []\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 AllGroups = getDiagnosticGroups(); + int showGroup(StringRef RootGroup) { + ArrayRef 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 AllGroups = getDiagnosticGroups(); - llvm::DenseSet NonRootGroupIDs; - - for (ArrayRef::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 AllGroups = getDiagnosticGroups(); + llvm::DenseSet NonRootGroupIDs; + + for (ArrayRef::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] []\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); } - -- 2.40.0