1 //===--- StmtViz.cpp - Graphviz visualization for Stmt ASTs -----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements Stmt::viewAST, which generates a Graphviz DOT file
10 // that depicts the AST and then calls Graphviz/dot+gv on it.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/StmtGraphTraits.h"
15 #include "clang/AST/Decl.h"
16 #include "llvm/Support/GraphWriter.h"
18 using namespace clang
;
20 void Stmt::viewAST() const {
22 llvm::ViewGraph(this,"AST");
24 llvm::errs() << "Stmt::viewAST is only available in debug builds on "
25 << "systems with Graphviz or gv!\n";
31 struct DOTGraphTraits
<const Stmt
*> : public DefaultDOTGraphTraits
{
32 DOTGraphTraits (bool isSimple
=false) : DefaultDOTGraphTraits(isSimple
) {}
34 static std::string
getNodeLabel(const Stmt
* Node
, const Stmt
* Graph
) {
38 llvm::raw_string_ostream
Out(OutSStr
);
41 Out
<< Node
->getStmtClassName();
45 std::string OutStr
= Out
.str();
46 if (OutStr
[0] == '\n') OutStr
.erase(OutStr
.begin());
48 // Process string output to make it nicer...
49 for (unsigned i
= 0; i
!= OutStr
.length(); ++i
)
50 if (OutStr
[i
] == '\n') { // Left justify
52 OutStr
.insert(OutStr
.begin()+i
+1, 'l');
61 } // end namespace llvm