2007-09-20 05:29:43 +08:00
|
|
|
//===--- StmtViz.cpp - Graphviz visualization for Stmt ASTs -----*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-09-20 05:29:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements Stmt::viewAST, which generates a Graphviz DOT file
|
|
|
|
// that depicts the AST and then calls Graphviz/dot+gv on it.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/StmtGraphTraits.h"
|
2008-10-08 07:09:49 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2007-09-20 05:29:43 +08:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
void Stmt::viewAST() const {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
llvm::ViewGraph(this,"AST");
|
|
|
|
#else
|
2009-08-23 16:52:09 +08:00
|
|
|
llvm::errs() << "Stmt::viewAST is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
2007-09-20 05:29:43 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits {
|
2009-11-30 22:16:05 +08:00
|
|
|
DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
|
|
|
|
|
|
|
|
static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-20 05:29:43 +08:00
|
|
|
#ifndef NDEBUG
|
2008-09-13 13:16:45 +08:00
|
|
|
std::string OutSStr;
|
|
|
|
llvm::raw_string_ostream Out(OutSStr);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-20 05:29:43 +08:00
|
|
|
if (Node)
|
|
|
|
Out << Node->getStmtClassName();
|
|
|
|
else
|
|
|
|
Out << "<NULL>";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
std::string OutStr = Out.str();
|
2007-09-20 05:29:43 +08:00
|
|
|
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-20 05:29:43 +08:00
|
|
|
// Process string output to make it nicer...
|
|
|
|
for (unsigned i = 0; i != OutStr.length(); ++i)
|
|
|
|
if (OutStr[i] == '\n') { // Left justify
|
|
|
|
OutStr[i] = '\\';
|
|
|
|
OutStr.insert(OutStr.begin()+i+1, 'l');
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-09-20 05:29:43 +08:00
|
|
|
return OutStr;
|
|
|
|
#else
|
|
|
|
return "";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end namespace llvm
|