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 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | void Stmt::viewAST() const { |
| 22 | #ifndef NDEBUG |
| 23 | llvm::ViewGraph(this,"AST"); |
| 24 | #else |
Daniel Dunbar | 68eaf00 | 2009-08-23 08:52:09 +0000 | [diff] [blame] | 25 | llvm::errs() << "Stmt::viewAST is only available in debug builds on " |
| 26 | << "systems with Graphviz or gv!\n"; |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 27 | #endif |
| 28 | } |
| 29 | |
| 30 | namespace llvm { |
| 31 | template<> |
| 32 | struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits { |
Tobias Grosser | 006b0eb | 2009-11-30 14:16:05 +0000 | [diff] [blame] | 33 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 34 | |
| 35 | static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 37 | #ifndef NDEBUG |
Ted Kremenek | a95d375 | 2008-09-13 05:16:45 +0000 | [diff] [blame] | 38 | std::string OutSStr; |
| 39 | llvm::raw_string_ostream Out(OutSStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 41 | if (Node) |
| 42 | Out << Node->getStmtClassName(); |
| 43 | else |
| 44 | Out << "<NULL>"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
| 46 | std::string OutStr = Out.str(); |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 47 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 49 | // Process string output to make it nicer... |
| 50 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 51 | if (OutStr[i] == '\n') { // Left justify |
| 52 | OutStr[i] = '\\'; |
| 53 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 54 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | 80de08f | 2007-09-19 21:29:43 +0000 | [diff] [blame] | 56 | return OutStr; |
| 57 | #else |
| 58 | return ""; |
| 59 | #endif |
| 60 | } |
| 61 | }; |
| 62 | } // end namespace llvm |