GraphWriter.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
  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 misc. GraphWriter support routines.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/GraphWriter.h"
  14. #include "llvm/Config/config.h"
  15. #include "llvm/Support/CommandLine.h"
  16. #include "llvm/Support/FileSystem.h"
  17. #include "llvm/Support/Program.h"
  18. using namespace llvm;
  19. static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
  20. cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
  21. std::string llvm::DOT::EscapeString(const std::string &Label) {
  22. std::string Str(Label);
  23. for (unsigned i = 0; i != Str.length(); ++i)
  24. switch (Str[i]) {
  25. case '\n':
  26. Str.insert(Str.begin()+i, '\\'); // Escape character...
  27. ++i;
  28. Str[i] = 'n';
  29. break;
  30. case '\t':
  31. Str.insert(Str.begin()+i, ' '); // Convert to two spaces
  32. ++i;
  33. Str[i] = ' ';
  34. break;
  35. case '\\':
  36. if (i+1 != Str.length())
  37. switch (Str[i+1]) {
  38. case 'l': continue; // don't disturb \l
  39. case '|': case '{': case '}':
  40. Str.erase(Str.begin()+i); continue;
  41. default: break;
  42. }
  43. case '{': case '}':
  44. case '<': case '>':
  45. case '|': case '"':
  46. Str.insert(Str.begin()+i, '\\'); // Escape character...
  47. ++i; // don't infinite loop
  48. break;
  49. }
  50. return Str;
  51. }
  52. /// \brief Get a color string for this node number. Simply round-robin selects
  53. /// from a reasonable number of colors.
  54. StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
  55. static const int NumColors = 20;
  56. static const char* Colors[NumColors] = {
  57. "aaaaaa", "aa0000", "00aa00", "aa5500", "0055ff", "aa00aa", "00aaaa",
  58. "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff",
  59. "ffaaaa", "aaffaa", "ffffaa", "aaaaff", "ffaaff", "aaffff"};
  60. return Colors[ColorNumber % NumColors];
  61. }
  62. std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
  63. FD = -1;
  64. SmallString<128> Filename;
  65. std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
  66. if (EC) {
  67. errs() << "Error: " << EC.message() << "\n";
  68. return "";
  69. }
  70. errs() << "Writing '" << Filename << "'... ";
  71. return Filename.str();
  72. }
  73. // Execute the graph viewer. Return true if there were errors.
  74. static bool ExecGraphViewer(StringRef ExecPath, std::vector<const char *> &args,
  75. StringRef Filename, bool wait,
  76. std::string &ErrMsg) {
  77. assert(args.back() == nullptr);
  78. #if 0 // HLSL Change Starts
  79. if (wait) {
  80. if (sys::ExecuteAndWait(ExecPath, args.data(), nullptr, nullptr, 0, 0,
  81. &ErrMsg)) {
  82. errs() << "Error: " << ErrMsg << "\n";
  83. return true;
  84. }
  85. sys::fs::remove(Filename);
  86. errs() << " done. \n";
  87. } else {
  88. sys::ExecuteNoWait(ExecPath, args.data(), nullptr, nullptr, 0, &ErrMsg);
  89. errs() << "Remember to erase graph file: " << Filename << "\n";
  90. }
  91. #else
  92. errs() << "Support for graph creation disabled.\n";
  93. #endif // HLSL Change Ends
  94. return false;
  95. }
  96. namespace {
  97. struct GraphSession {
  98. std::string LogBuffer;
  99. bool TryFindProgram(StringRef Names, std::string &ProgramPath) {
  100. raw_string_ostream Log(LogBuffer);
  101. SmallVector<StringRef, 8> parts;
  102. Names.split(parts, "|");
  103. for (auto Name : parts) {
  104. if (ErrorOr<std::string> P = sys::findProgramByName(Name)) {
  105. ProgramPath = *P;
  106. return true;
  107. }
  108. Log << " Tried '" << Name << "'\n";
  109. }
  110. return false;
  111. }
  112. };
  113. } // namespace
  114. static const char *getProgramName(GraphProgram::Name program) {
  115. switch (program) {
  116. case GraphProgram::DOT:
  117. return "dot";
  118. case GraphProgram::FDP:
  119. return "fdp";
  120. case GraphProgram::NEATO:
  121. return "neato";
  122. case GraphProgram::TWOPI:
  123. return "twopi";
  124. case GraphProgram::CIRCO:
  125. return "circo";
  126. }
  127. llvm_unreachable("bad kind");
  128. }
  129. bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
  130. GraphProgram::Name program) {
  131. std::string Filename = FilenameRef;
  132. std::string ErrMsg;
  133. std::string ViewerPath;
  134. GraphSession S;
  135. #ifdef __APPLE__
  136. wait &= !ViewBackground;
  137. if (S.TryFindProgram("open", ViewerPath)) {
  138. std::vector<const char *> args;
  139. args.push_back(ViewerPath.c_str());
  140. if (wait)
  141. args.push_back("-W");
  142. args.push_back(Filename.c_str());
  143. args.push_back(nullptr);
  144. errs() << "Trying 'open' program... ";
  145. if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
  146. return false;
  147. }
  148. #endif
  149. if (S.TryFindProgram("xdg-open", ViewerPath)) {
  150. std::vector<const char *> args;
  151. args.push_back(ViewerPath.c_str());
  152. args.push_back(Filename.c_str());
  153. args.push_back(nullptr);
  154. errs() << "Trying 'xdg-open' program... ";
  155. if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
  156. return false;
  157. }
  158. // Graphviz
  159. if (S.TryFindProgram("Graphviz", ViewerPath)) {
  160. std::vector<const char *> args;
  161. args.push_back(ViewerPath.c_str());
  162. args.push_back(Filename.c_str());
  163. args.push_back(nullptr);
  164. errs() << "Running 'Graphviz' program... ";
  165. return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
  166. }
  167. // xdot
  168. if (S.TryFindProgram("xdot|xdot.py", ViewerPath)) {
  169. std::vector<const char *> args;
  170. args.push_back(ViewerPath.c_str());
  171. args.push_back(Filename.c_str());
  172. args.push_back("-f");
  173. args.push_back(getProgramName(program));
  174. args.push_back(nullptr);
  175. errs() << "Running 'xdot.py' program... ";
  176. return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
  177. }
  178. enum PSViewerKind { PSV_None, PSV_OSXOpen, PSV_XDGOpen, PSV_Ghostview };
  179. PSViewerKind PSViewer = PSV_None;
  180. #ifdef __APPLE__
  181. if (!PSViewer && S.TryFindProgram("open", ViewerPath))
  182. PSViewer = PSV_OSXOpen;
  183. #endif
  184. if (!PSViewer && S.TryFindProgram("gv", ViewerPath))
  185. PSViewer = PSV_Ghostview;
  186. if (!PSViewer && S.TryFindProgram("xdg-open", ViewerPath))
  187. PSViewer = PSV_XDGOpen;
  188. // PostScript graph generator + PostScript viewer
  189. std::string GeneratorPath;
  190. if (PSViewer &&
  191. (S.TryFindProgram(getProgramName(program), GeneratorPath) ||
  192. S.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath))) {
  193. std::string PSFilename = Filename + ".ps";
  194. std::vector<const char *> args;
  195. args.push_back(GeneratorPath.c_str());
  196. args.push_back("-Tps");
  197. args.push_back("-Nfontname=Courier");
  198. args.push_back("-Gsize=7.5,10");
  199. args.push_back(Filename.c_str());
  200. args.push_back("-o");
  201. args.push_back(PSFilename.c_str());
  202. args.push_back(nullptr);
  203. errs() << "Running '" << GeneratorPath << "' program... ";
  204. if (ExecGraphViewer(GeneratorPath, args, Filename, wait, ErrMsg))
  205. return true;
  206. args.clear();
  207. args.push_back(ViewerPath.c_str());
  208. switch (PSViewer) {
  209. case PSV_OSXOpen:
  210. args.push_back("-W");
  211. args.push_back(PSFilename.c_str());
  212. break;
  213. case PSV_XDGOpen:
  214. wait = false;
  215. args.push_back(PSFilename.c_str());
  216. break;
  217. case PSV_Ghostview:
  218. args.push_back("--spartan");
  219. args.push_back(PSFilename.c_str());
  220. break;
  221. case PSV_None:
  222. llvm_unreachable("Invalid viewer");
  223. }
  224. args.push_back(nullptr);
  225. ErrMsg.clear();
  226. return ExecGraphViewer(ViewerPath, args, PSFilename, wait, ErrMsg);
  227. }
  228. // dotty
  229. if (S.TryFindProgram("dotty", ViewerPath)) {
  230. std::vector<const char *> args;
  231. args.push_back(ViewerPath.c_str());
  232. args.push_back(Filename.c_str());
  233. args.push_back(nullptr);
  234. // Dotty spawns another app and doesn't wait until it returns
  235. #ifdef LLVM_ON_WIN32
  236. wait = false;
  237. #endif
  238. errs() << "Running 'dotty' program... ";
  239. return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
  240. }
  241. errs() << "Error: Couldn't find a usable graph viewer program:\n";
  242. errs() << S.LogBuffer << "\n";
  243. return true;
  244. }