blob: 8be287e7cb2178b8827c7f1ca17a14b4dfa02194 [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"
Ted Kremenek8ffb1592008-10-07 23:09:49 +000016#include "clang/AST/Decl.h"
Ted Kremenek80de08f2007-09-19 21:29:43 +000017#include "llvm/Support/GraphWriter.h"
Ted Kremenek80de08f2007-09-19 21:29:43 +000018
19using namespace clang;
20
21void Stmt::viewAST() const {
22#ifndef NDEBUG
23 llvm::ViewGraph(this,"AST");
24#else
Daniel Dunbar68eaf002009-08-23 08:52:09 +000025 llvm::errs() << "Stmt::viewAST is only available in debug builds on "
26 << "systems with Graphviz or gv!\n";
Ted Kremenek80de08f2007-09-19 21:29:43 +000027#endif
28}
29
30namespace llvm {
31template<>
32struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits {
Tobias Grosser006b0eb2009-11-30 14:16:05 +000033 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
34
35 static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) {
Mike Stump1eb44332009-09-09 15:08:12 +000036
Ted Kremenek80de08f2007-09-19 21:29:43 +000037#ifndef NDEBUG
Ted Kremeneka95d3752008-09-13 05:16:45 +000038 std::string OutSStr;
39 llvm::raw_string_ostream Out(OutSStr);
Mike Stump1eb44332009-09-09 15:08:12 +000040
Ted Kremenek80de08f2007-09-19 21:29:43 +000041 if (Node)
42 Out << Node->getStmtClassName();
43 else
44 Out << "<NULL>";
Mike Stump1eb44332009-09-09 15:08:12 +000045
46 std::string OutStr = Out.str();
Ted Kremenek80de08f2007-09-19 21:29:43 +000047 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
Mike Stump1eb44332009-09-09 15:08:12 +000048
Ted Kremenek80de08f2007-09-19 21:29:43 +000049 // 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 Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenek80de08f2007-09-19 21:29:43 +000056 return OutStr;
57#else
58 return "";
59#endif
60 }
61};
62} // end namespace llvm