StmtViz.cpp 1.7 KB

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