Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 1 | //===--- StmtViz.cpp - Graphviz visualization for Stmt ASTs -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Ted Kremenek and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements Stmt::viewAST, which generates a Graphviz DOT file |
| 11 | // that depicts the AST and then calls Graphviz/dot+gv on it. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/AST/StmtGraphTraits.h" |
| 16 | #include "llvm/Support/GraphWriter.h" |
| 17 | #include <iostream> |
| 18 | #include <sstream> |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | void Stmt::viewAST() const { |
| 23 | #ifndef NDEBUG |
| 24 | llvm::ViewGraph(this,"AST"); |
| 25 | #else |
| 26 | std::cerr << "Stmt::viewAST is only available in debug builds on " |
| 27 | << "systems with Graphviz or gv!\n"; |
| 28 | #endif |
| 29 | } |
| 30 | |
| 31 | namespace llvm { |
| 32 | template<> |
| 33 | struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits { |
| 34 | static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) { |
| 35 | |
| 36 | #ifndef NDEBUG |
| 37 | std::ostringstream Out; |
| 38 | |
| 39 | if (Node) |
| 40 | Out << Node->getStmtClassName(); |
| 41 | else |
| 42 | Out << "<NULL>"; |
| 43 | |
| 44 | std::string OutStr = Out.str(); |
| 45 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 46 | |
| 47 | // Process string output to make it nicer... |
| 48 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 49 | if (OutStr[i] == '\n') { // Left justify |
| 50 | OutStr[i] = '\\'; |
| 51 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 52 | } |
| 53 | |
| 54 | return OutStr; |
| 55 | #else |
| 56 | return ""; |
| 57 | #endif |
| 58 | } |
| 59 | }; |
| 60 | } // end namespace llvm |