blob: 6790efbd5682ac0e2980a591c417f7df406477f6 [file] [log] [blame]
Ted Kremenek80de08f2007-09-19 21:29:43 +00001//===--- StmtViz.cpp - Graphviz visualization for Stmt ASTs -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremenek80de08f2007-09-19 21:29:43 +00007//
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"
Ted Kremenek80de08f2007-09-19 21:29:43 +000017#include <sstream>
18
19using namespace clang;
20
21void Stmt::viewAST() const {
22#ifndef NDEBUG
23 llvm::ViewGraph(this,"AST");
24#else
Ted Kremenekbdd30c22008-01-14 16:44:48 +000025 llvm::cerr << "Stmt::viewAST is only available in debug builds on "
Ted Kremenek80de08f2007-09-19 21:29:43 +000026 << "systems with Graphviz or gv!\n";
27#endif
28}
29
30namespace llvm {
31template<>
32struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits {
33 static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) {
34
35#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +000036 std::string OutSStr;
37 llvm::raw_string_ostream Out(OutSStr);
Ted Kremenek80de08f2007-09-19 21:29:43 +000038
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