diff options
author | Ted Kremenek <kremenek@apple.com> | 2011-12-22 23:33:52 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2011-12-22 23:33:52 +0000 |
commit | 72be32af88cdaf2a22a4e5d631b53347bdefcd10 (patch) | |
tree | a9833ac6ff1f3ecd6e93134bb43ce8a741ac3882 /clang/lib/Analysis/CFG.cpp | |
parent | 3cef10814aedc310500d7f52e8358ae6777047dc (diff) | |
download | bcm5719-llvm-72be32af88cdaf2a22a4e5d631b53347bdefcd10.tar.gz bcm5719-llvm-72be32af88cdaf2a22a4e5d631b53347bdefcd10.zip |
Colorize and condense CFG pretty-printing.
llvm-svn: 147203
Diffstat (limited to 'clang/lib/Analysis/CFG.cpp')
-rw-r--r-- | clang/lib/Analysis/CFG.cpp | 137 |
1 files changed, 90 insertions, 47 deletions
diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp index 8d7aff19ca3..5bbccf53c54 100644 --- a/clang/lib/Analysis/CFG.cpp +++ b/clang/lib/Analysis/CFG.cpp @@ -3554,27 +3554,35 @@ static void print_elem(raw_ostream &OS, StmtPrinterHelper* Helper, static void print_block(raw_ostream &OS, const CFG* cfg, const CFGBlock &B, - StmtPrinterHelper* Helper, bool print_edges) { + StmtPrinterHelper* Helper, bool print_edges, + bool ShowColors) { - if (Helper) Helper->setBlockID(B.getBlockID()); + if (Helper) + Helper->setBlockID(B.getBlockID()); // Print the header. - OS << "\n [ B" << B.getBlockID(); + if (ShowColors) + OS.changeColor(raw_ostream::YELLOW, true); + + OS << "\n [B" << B.getBlockID(); if (&B == &cfg->getEntry()) - OS << " (ENTRY) ]\n"; + OS << " (ENTRY)]\n"; else if (&B == &cfg->getExit()) - OS << " (EXIT) ]\n"; + OS << " (EXIT)]\n"; else if (&B == cfg->getIndirectGotoBlock()) - OS << " (INDIRECT GOTO DISPATCH) ]\n"; + OS << " (INDIRECT GOTO DISPATCH)]\n"; else - OS << " ]\n"; + OS << "]\n"; + + if (ShowColors) + OS.resetColor(); // Print the label of this block. if (Stmt *Label = const_cast<Stmt*>(B.getLabel())) { if (print_edges) - OS << " "; + OS << " "; if (LabelStmt *L = dyn_cast<LabelStmt>(Label)) OS << L->getName(); @@ -3612,22 +3620,22 @@ static void print_block(raw_ostream &OS, const CFG* cfg, // Print the statement # in the basic block and the statement itself. if (print_edges) - OS << " "; + OS << " "; OS << llvm::format("%3d", j) << ": "; if (Helper) Helper->setStmtID(j); - print_elem(OS,Helper,*I); + print_elem(OS, Helper, *I); } // Print the terminator of this block. if (B.getTerminator()) { - if (print_edges) - OS << " "; + if (ShowColors) + OS.changeColor(raw_ostream::GREEN); - OS << " T: "; + OS << " T: "; if (Helper) Helper->setBlockID(-1); @@ -3635,54 +3643,86 @@ static void print_block(raw_ostream &OS, const CFG* cfg, PrintingPolicy(Helper->getLangOpts())); TPrinter.Visit(const_cast<Stmt*>(B.getTerminator().getStmt())); OS << '\n'; + + if (ShowColors) + OS.resetColor(); } if (print_edges) { // Print the predecessors of this block. - OS << " Predecessors (" << B.pred_size() << "):"; - unsigned i = 0; + if (!B.pred_empty()) { + const raw_ostream::Colors Color = raw_ostream::BLUE; + if (ShowColors) + OS.changeColor(Color); + OS << " Preds " ; + if (ShowColors) + OS.resetColor(); + OS << '(' << B.pred_size() << "):"; + unsigned i = 0; + + if (ShowColors) + OS.changeColor(Color); + + for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); + I != E; ++I, ++i) { - for (CFGBlock::const_pred_iterator I = B.pred_begin(), E = B.pred_end(); - I != E; ++I, ++i) { + if (i == 8 || (i-8) == 0) + OS << "\n "; - if (i == 8 || (i-8) == 0) - OS << "\n "; + OS << " B" << (*I)->getBlockID(); + } + + if (ShowColors) + OS.resetColor(); - OS << " B" << (*I)->getBlockID(); + OS << '\n'; } - OS << '\n'; - // Print the successors of this block. - OS << " Successors (" << B.succ_size() << "):"; - i = 0; - - for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); - I != E; ++I, ++i) { - - if (i == 8 || (i-8) % 10 == 0) - OS << "\n "; - - if (*I) - OS << " B" << (*I)->getBlockID(); - else - OS << " NULL"; + if (!B.succ_empty()) { + const raw_ostream::Colors Color = raw_ostream::MAGENTA; + if (ShowColors) + OS.changeColor(Color); + OS << " Succs "; + if (ShowColors) + OS.resetColor(); + OS << '(' << B.succ_size() << "):"; + unsigned i = 0; + + if (ShowColors) + OS.changeColor(Color); + + for (CFGBlock::const_succ_iterator I = B.succ_begin(), E = B.succ_end(); + I != E; ++I, ++i) { + + if (i == 8 || (i-8) % 10 == 0) + OS << "\n "; + + if (*I) + OS << " B" << (*I)->getBlockID(); + else + OS << " NULL"; + } + + if (ShowColors) + OS.resetColor(); + OS << '\n'; } - - OS << '\n'; } } /// dump - A simple pretty printer of a CFG that outputs to stderr. -void CFG::dump(const LangOptions &LO) const { print(llvm::errs(), LO); } +void CFG::dump(const LangOptions &LO, bool ShowColors) const { + print(llvm::errs(), LO, ShowColors); +} /// print - A simple pretty printer of a CFG that outputs to an ostream. -void CFG::print(raw_ostream &OS, const LangOptions &LO) const { +void CFG::print(raw_ostream &OS, const LangOptions &LO, bool ShowColors) const { StmtPrinterHelper Helper(this, LO); // Print the entry block. - print_block(OS, this, getEntry(), &Helper, true); + print_block(OS, this, getEntry(), &Helper, true, ShowColors); // Iterate through the CFGBlocks and print them one by one. for (const_iterator I = Blocks.begin(), E = Blocks.end() ; I != E ; ++I) { @@ -3690,25 +3730,28 @@ void CFG::print(raw_ostream &OS, const LangOptions &LO) const { if (&(**I) == &getEntry() || &(**I) == &getExit()) continue; - print_block(OS, this, **I, &Helper, true); + print_block(OS, this, **I, &Helper, true, ShowColors); } // Print the exit block. - print_block(OS, this, getExit(), &Helper, true); + print_block(OS, this, getExit(), &Helper, true, ShowColors); + OS << '\n'; OS.flush(); } /// dump - A simply pretty printer of a CFGBlock that outputs to stderr. -void CFGBlock::dump(const CFG* cfg, const LangOptions &LO) const { - print(llvm::errs(), cfg, LO); +void CFGBlock::dump(const CFG* cfg, const LangOptions &LO, + bool ShowColors) const { + print(llvm::errs(), cfg, LO, ShowColors); } /// print - A simple pretty printer of a CFGBlock that outputs to an ostream. /// Generally this will only be called from CFG::print. void CFGBlock::print(raw_ostream &OS, const CFG* cfg, - const LangOptions &LO) const { + const LangOptions &LO, bool ShowColors) const { StmtPrinterHelper Helper(cfg, LO); - print_block(OS, cfg, *this, &Helper, true); + print_block(OS, cfg, *this, &Helper, true, ShowColors); + OS << '\n'; } /// printTerminator - A simple pretty printer of the terminator of a CFGBlock. @@ -3805,7 +3848,7 @@ struct DOTGraphTraits<const CFG*> : public DefaultDOTGraphTraits { #ifndef NDEBUG std::string OutSStr; llvm::raw_string_ostream Out(OutSStr); - print_block(Out,Graph, *Node, GraphHelper, false); + print_block(Out,Graph, *Node, GraphHelper, false, false); std::string& OutStr = Out.str(); if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |