StmtViz.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //===--- StmtViz.cpp - Graphviz visualization for Stmt ASTs -----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  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. #include "clang/AST/StmtGraphTraits.h"
  15. #include "clang/AST/Decl.h"
  16. #include "llvm/Support/GraphWriter.h"
  17. using namespace clang;
  18. void Stmt::viewAST() const {
  19. #ifndef NDEBUG
  20. llvm::ViewGraph(this,"AST");
  21. #else
  22. llvm::errs() << "Stmt::viewAST is only available in debug builds on "
  23. << "systems with Graphviz or gv!\n";
  24. #endif
  25. }
  26. namespace llvm {
  27. template<>
  28. struct DOTGraphTraits<const Stmt*> : public DefaultDOTGraphTraits {
  29. DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
  30. static std::string getNodeLabel(const Stmt* Node, const Stmt* Graph) {
  31. #ifndef NDEBUG
  32. std::string OutSStr;
  33. llvm::raw_string_ostream Out(OutSStr);
  34. if (Node)
  35. Out << Node->getStmtClassName();
  36. else
  37. Out << "<NULL>";
  38. std::string OutStr = Out.str();
  39. if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
  40. // Process string output to make it nicer...
  41. for (unsigned i = 0; i != OutStr.length(); ++i)
  42. if (OutStr[i] == '\n') { // Left justify
  43. OutStr[i] = '\\';
  44. OutStr.insert(OutStr.begin()+i+1, 'l');
  45. }
  46. return OutStr;
  47. #else
  48. return "";
  49. #endif
  50. }
  51. };
  52. } // end namespace llvm