GraphWriter.cpp 8.2 KB

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