1193326Sed//===--- StmtViz.cpp - Graphviz visualization for Stmt ASTs -----*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed//  This file implements Stmt::viewAST, which generates a Graphviz DOT file
11193326Sed//  that depicts the AST and then calls Graphviz/dot+gv on it.
12193326Sed//
13193326Sed//===----------------------------------------------------------------------===//
14193326Sed
15193326Sed#include "clang/AST/StmtGraphTraits.h"
16193326Sed#include "clang/AST/Decl.h"
17193326Sed#include "llvm/Support/GraphWriter.h"
18193326Sed
19193326Sedusing namespace clang;
20193326Sed
21193326Sedvoid Stmt::viewAST() const {
22193326Sed#ifndef NDEBUG
23193326Sed  llvm::ViewGraph(this,"AST");
24193326Sed#else
25198092Srdivacky  llvm::errs() << "Stmt::viewAST is only available in debug builds on "
26198092Srdivacky               << "systems with Graphviz or gv!\n";
27193326Sed#endif
28193326Sed}
29193326Sed
30193326Sednamespace llvm {
31193326Sedtemplate<>
32193326Sedstruct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits {
33199990Srdivacky  DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
34198092Srdivacky
35199990Srdivacky  static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) {
36199990Srdivacky
37193326Sed#ifndef NDEBUG
38193326Sed    std::string OutSStr;
39193326Sed    llvm::raw_string_ostream Out(OutSStr);
40198092Srdivacky
41193326Sed    if (Node)
42193326Sed      Out << Node->getStmtClassName();
43193326Sed    else
44193326Sed      Out << "<NULL>";
45198092Srdivacky
46198092Srdivacky    std::string OutStr = Out.str();
47193326Sed    if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
48198092Srdivacky
49193326Sed    // Process string output to make it nicer...
50193326Sed    for (unsigned i = 0; i != OutStr.length(); ++i)
51193326Sed      if (OutStr[i] == '\n') {                            // Left justify
52193326Sed        OutStr[i] = '\\';
53193326Sed        OutStr.insert(OutStr.begin()+i+1, 'l');
54193326Sed      }
55198092Srdivacky
56193326Sed    return OutStr;
57193326Sed#else
58193326Sed    return "";
59193326Sed#endif
60193326Sed  }
61193326Sed};
62193326Sed} // end namespace llvm
63