LLVM  6.0.0svn
GraphWriter.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Config/config.h"
20 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/Program.h"
26 #include <cassert>
27 #include <system_error>
28 #include <string>
29 #include <vector>
30 
31 using namespace llvm;
32 
33 static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
34  cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
35 
36 std::string llvm::DOT::EscapeString(const std::string &Label) {
37  std::string Str(Label);
38  for (unsigned i = 0; i != Str.length(); ++i)
39  switch (Str[i]) {
40  case '\n':
41  Str.insert(Str.begin()+i, '\\'); // Escape character...
42  ++i;
43  Str[i] = 'n';
44  break;
45  case '\t':
46  Str.insert(Str.begin()+i, ' '); // Convert to two spaces
47  ++i;
48  Str[i] = ' ';
49  break;
50  case '\\':
51  if (i+1 != Str.length())
52  switch (Str[i+1]) {
53  case 'l': continue; // don't disturb \l
54  case '|': case '{': case '}':
55  Str.erase(Str.begin()+i); continue;
56  default: break;
57  }
59  case '{': case '}':
60  case '<': case '>':
61  case '|': case '"':
62  Str.insert(Str.begin()+i, '\\'); // Escape character...
63  ++i; // don't infinite loop
64  break;
65  }
66  return Str;
67 }
68 
69 /// \brief Get a color string for this node number. Simply round-robin selects
70 /// from a reasonable number of colors.
71 StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
72  static const int NumColors = 20;
73  static const char* Colors[NumColors] = {
74  "aaaaaa", "aa0000", "00aa00", "aa5500", "0055ff", "aa00aa", "00aaaa",
75  "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff",
76  "ffaaaa", "aaffaa", "ffffaa", "aaaaff", "ffaaff", "aaffff"};
77  return Colors[ColorNumber % NumColors];
78 }
79 
80 std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
81  FD = -1;
82  SmallString<128> Filename;
83  std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
84  if (EC) {
85  errs() << "Error: " << EC.message() << "\n";
86  return "";
87  }
88 
89  errs() << "Writing '" << Filename << "'... ";
90  return Filename.str();
91 }
92 
93 // Execute the graph viewer. Return true if there were errors.
94 static bool ExecGraphViewer(StringRef ExecPath, std::vector<const char *> &args,
95  StringRef Filename, bool wait,
96  std::string &ErrMsg) {
97  assert(args.back() == nullptr);
98  if (wait) {
99  if (sys::ExecuteAndWait(ExecPath, args.data(), nullptr, {}, 0, 0,
100  &ErrMsg)) {
101  errs() << "Error: " << ErrMsg << "\n";
102  return true;
103  }
104  sys::fs::remove(Filename);
105  errs() << " done. \n";
106  } else {
107  sys::ExecuteNoWait(ExecPath, args.data(), nullptr, {}, 0, &ErrMsg);
108  errs() << "Remember to erase graph file: " << Filename << "\n";
109  }
110  return false;
111 }
112 
113 namespace {
114 
115 struct GraphSession {
116  std::string LogBuffer;
117 
118  bool TryFindProgram(StringRef Names, std::string &ProgramPath) {
119  raw_string_ostream Log(LogBuffer);
121  Names.split(parts, '|');
122  for (auto Name : parts) {
124  ProgramPath = *P;
125  return true;
126  }
127  Log << " Tried '" << Name << "'\n";
128  }
129  return false;
130  }
131 };
132 
133 } // end anonymous namespace
134 
135 static const char *getProgramName(GraphProgram::Name program) {
136  switch (program) {
137  case GraphProgram::DOT:
138  return "dot";
139  case GraphProgram::FDP:
140  return "fdp";
141  case GraphProgram::NEATO:
142  return "neato";
143  case GraphProgram::TWOPI:
144  return "twopi";
145  case GraphProgram::CIRCO:
146  return "circo";
147  }
148  llvm_unreachable("bad kind");
149 }
150 
151 bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
152  GraphProgram::Name program) {
153  std::string Filename = FilenameRef;
154  std::string ErrMsg;
155  std::string ViewerPath;
156  GraphSession S;
157 
158 #ifdef __APPLE__
159  wait &= !ViewBackground;
160  if (S.TryFindProgram("open", ViewerPath)) {
161  std::vector<const char *> args;
162  args.push_back(ViewerPath.c_str());
163  if (wait)
164  args.push_back("-W");
165  args.push_back(Filename.c_str());
166  args.push_back(nullptr);
167  errs() << "Trying 'open' program... ";
168  if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
169  return false;
170  }
171 #endif
172  if (S.TryFindProgram("xdg-open", 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(nullptr);
177  errs() << "Trying 'xdg-open' program... ";
178  if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
179  return false;
180  }
181 
182  // Graphviz
183  if (S.TryFindProgram("Graphviz", ViewerPath)) {
184  std::vector<const char *> args;
185  args.push_back(ViewerPath.c_str());
186  args.push_back(Filename.c_str());
187  args.push_back(nullptr);
188 
189  errs() << "Running 'Graphviz' program... ";
190  return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
191  }
192 
193  // xdot
194  if (S.TryFindProgram("xdot|xdot.py", ViewerPath)) {
195  std::vector<const char *> args;
196  args.push_back(ViewerPath.c_str());
197  args.push_back(Filename.c_str());
198 
199  args.push_back("-f");
200  args.push_back(getProgramName(program));
201 
202  args.push_back(nullptr);
203 
204  errs() << "Running 'xdot.py' program... ";
205  return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
206  }
207 
208  enum ViewerKind {
209  VK_None,
210  VK_OSXOpen,
211  VK_XDGOpen,
212  VK_Ghostview,
213  VK_CmdStart
214  };
215  ViewerKind Viewer = VK_None;
216 #ifdef __APPLE__
217  if (!Viewer && S.TryFindProgram("open", ViewerPath))
218  Viewer = VK_OSXOpen;
219 #endif
220  if (!Viewer && S.TryFindProgram("gv", ViewerPath))
221  Viewer = VK_Ghostview;
222  if (!Viewer && S.TryFindProgram("xdg-open", ViewerPath))
223  Viewer = VK_XDGOpen;
224 #ifdef LLVM_ON_WIN32
225  if (!Viewer && S.TryFindProgram("cmd", ViewerPath)) {
226  Viewer = VK_CmdStart;
227  }
228 #endif
229 
230  // PostScript or PDF graph generator + PostScript/PDF viewer
231  std::string GeneratorPath;
232  if (Viewer &&
233  (S.TryFindProgram(getProgramName(program), GeneratorPath) ||
234  S.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath))) {
235  std::string OutputFilename =
236  Filename + (Viewer == VK_CmdStart ? ".pdf" : ".ps");
237 
238  std::vector<const char *> args;
239  args.push_back(GeneratorPath.c_str());
240  if (Viewer == VK_CmdStart)
241  args.push_back("-Tpdf");
242  else
243  args.push_back("-Tps");
244  args.push_back("-Nfontname=Courier");
245  args.push_back("-Gsize=7.5,10");
246  args.push_back(Filename.c_str());
247  args.push_back("-o");
248  args.push_back(OutputFilename.c_str());
249  args.push_back(nullptr);
250 
251  errs() << "Running '" << GeneratorPath << "' program... ";
252 
253  if (ExecGraphViewer(GeneratorPath, args, Filename, true, ErrMsg))
254  return true;
255 
256  // The lifetime of StartArg must include the call of ExecGraphViewer
257  // because the args are passed as vector of char*.
258  std::string StartArg;
259 
260  args.clear();
261  args.push_back(ViewerPath.c_str());
262  switch (Viewer) {
263  case VK_OSXOpen:
264  args.push_back("-W");
265  args.push_back(OutputFilename.c_str());
266  break;
267  case VK_XDGOpen:
268  wait = false;
269  args.push_back(OutputFilename.c_str());
270  break;
271  case VK_Ghostview:
272  args.push_back("--spartan");
273  args.push_back(OutputFilename.c_str());
274  break;
275  case VK_CmdStart:
276  args.push_back("/S");
277  args.push_back("/C");
278  StartArg =
279  (StringRef("start ") + (wait ? "/WAIT " : "") + OutputFilename).str();
280  args.push_back(StartArg.c_str());
281  break;
282  case VK_None:
283  llvm_unreachable("Invalid viewer");
284  }
285  args.push_back(nullptr);
286 
287  ErrMsg.clear();
288  return ExecGraphViewer(ViewerPath, args, OutputFilename, wait, ErrMsg);
289  }
290 
291  // dotty
292  if (S.TryFindProgram("dotty", ViewerPath)) {
293  std::vector<const char *> args;
294  args.push_back(ViewerPath.c_str());
295  args.push_back(Filename.c_str());
296  args.push_back(nullptr);
297 
298 // Dotty spawns another app and doesn't wait until it returns
299 #ifdef LLVM_ON_WIN32
300  wait = false;
301 #endif
302  errs() << "Running 'dotty' program... ";
303  return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
304  }
305 
306  errs() << "Error: Couldn't find a usable graph viewer program:\n";
307  errs() << S.LogBuffer << "\n";
308  return true;
309 }
Represents either an error or a value T.
Definition: ErrorOr.h:69
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static cl::opt< bool > ViewBackground("view-background", cl::Hidden, cl::desc("Execute graph viewer in the background. Creates tmp file litter."))
static const char * getProgramName(GraphProgram::Name program)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths={})
Find the first executable file Name in Paths.
int ExecuteAndWait(StringRef Program, const char **Args, const char **Env=nullptr, ArrayRef< Optional< StringRef >> Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
This function executes the program using the arguments provided.
Definition: Program.cpp:30
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
#define P(N)
std::string EscapeString(const std::string &Label)
Definition: GraphWriter.cpp:36
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
std::string createGraphFilename(const Twine &Name, int &FD)
Definition: GraphWriter.cpp:80
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
StringRef getColorString(unsigned NodeNumber)
Get a color string for this node number.
Definition: GraphWriter.cpp:71
ProcessInfo ExecuteNoWait(StringRef Program, const char **Args, const char **Env=nullptr, ArrayRef< Optional< StringRef >> Redirects={}, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
Similar to ExecuteAndWait, but returns immediately.
Definition: Program.cpp:50
bool DisplayGraph(StringRef Filename, bool wait=true, GraphProgram::Name program=GraphProgram::DOT)
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath)
Create a file in the system temporary directory.
Definition: Path.cpp:860
Provides ErrorOr<T> smart pointer.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:462
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:235
static bool ExecGraphViewer(StringRef ExecPath, std::vector< const char *> &args, StringRef Filename, bool wait, std::string &ErrMsg)
Definition: GraphWriter.cpp:94
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49