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