clang API Documentation

StmtViz.cpp
Go to the documentation of this file.
00001 //===--- StmtViz.cpp - Graphviz visualization for Stmt ASTs -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements Stmt::viewAST, which generates a Graphviz DOT file
00011 //  that depicts the AST and then calls Graphviz/dot+gv on it.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "clang/AST/StmtGraphTraits.h"
00016 #include "clang/AST/Decl.h"
00017 #include "llvm/Support/GraphWriter.h"
00018 
00019 using namespace clang;
00020 
00021 void Stmt::viewAST() const {
00022 #ifndef NDEBUG
00023   llvm::ViewGraph(this,"AST");
00024 #else
00025   llvm::errs() << "Stmt::viewAST is only available in debug builds on "
00026                << "systems with Graphviz or gv!\n";
00027 #endif
00028 }
00029 
00030 namespace llvm {
00031 template<>
00032 struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits {
00033   DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
00034 
00035   static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) {
00036 
00037 #ifndef NDEBUG
00038     std::string OutSStr;
00039     llvm::raw_string_ostream Out(OutSStr);
00040 
00041     if (Node)
00042       Out << Node->getStmtClassName();
00043     else
00044       Out << "<NULL>";
00045 
00046     std::string OutStr = Out.str();
00047     if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
00048 
00049     // Process string output to make it nicer...
00050     for (unsigned i = 0; i != OutStr.length(); ++i)
00051       if (OutStr[i] == '\n') {                            // Left justify
00052         OutStr[i] = '\\';
00053         OutStr.insert(OutStr.begin()+i+1, 'l');
00054       }
00055 
00056     return OutStr;
00057 #else
00058     return "";
00059 #endif
00060   }
00061 };
00062 } // end namespace llvm