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 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 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" |
Ted Kremenek | 8ffb159 | 2008-10-07 23:09:49 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 17 | #include "llvm/Support/GraphWriter.h" |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 18 | #include <sstream> |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | void Stmt::viewAST() const { |
| 23 | #ifndef NDEBUG |
| 24 | llvm::ViewGraph(this,"AST"); |
| 25 | #else |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 26 | llvm::cerr << "Stmt::viewAST is only available in debug builds on " |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 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 |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 37 | std::string OutSStr; |
| 38 | llvm::raw_string_ostream Out(OutSStr); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 39 | |
| 40 | if (Node) |
| 41 | Out << Node->getStmtClassName(); |
| 42 | else |
| 43 | Out << "<NULL>"; |
| 44 | |
| 45 | std::string OutStr = Out.str(); |
| 46 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 47 | |
| 48 | // Process string output to make it nicer... |
| 49 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 50 | if (OutStr[i] == '\n') { // Left justify |
| 51 | OutStr[i] = '\\'; |
| 52 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 53 | } |
| 54 | |
| 55 | return OutStr; |
| 56 | #else |
| 57 | return ""; |
| 58 | #endif |
| 59 | } |
| 60 | }; |
| 61 | } // end namespace llvm |