clang 20.0.0git
StmtViz.cpp
Go to the documentation of this file.
1//===--- StmtViz.cpp - Graphviz visualization for Stmt ASTs -----*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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.
11//
12//===----------------------------------------------------------------------===//
13
15#include "clang/AST/Decl.h"
16#include "llvm/Support/GraphWriter.h"
17
18using namespace clang;
19
20void Stmt::viewAST() const {
21#ifndef NDEBUG
22 llvm::ViewGraph(this,"AST");
23#else
24 llvm::errs() << "Stmt::viewAST is only available in debug builds on "
25 << "systems with Graphviz or gv!\n";
26#endif
27}
28
29namespace llvm {
30template<>
31struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits {
32 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
33
34 static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) {
35
36#ifndef NDEBUG
37 std::string OutStr;
38 llvm::raw_string_ostream Out(OutStr);
39
40 if (Node)
41 Out << Node->getStmtClassName();
42 else
43 Out << "<NULL>";
44
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
DynTypedNode Node
Stmt - This represents one statement.
Definition: Stmt.h:84
void viewAST() const
viewAST - Visualize an AST rooted at this Stmt* using GraphViz.
Definition: StmtViz.cpp:20
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
DOTGraphTraits(bool isSimple=false)
Definition: StmtViz.cpp:32
static std::string getNodeLabel(const Stmt *Node, const Stmt *Graph)
Definition: StmtViz.cpp:34