1 //===- GraphWriter.cpp - Implements GraphWriter support routines ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements misc. GraphWriter support routines.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/GraphWriter.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Config/config.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/ErrorOr.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Program.h"
25 #include "llvm/Support/raw_ostream.h"
27 #include <system_error>
33 static cl::opt
<bool> ViewBackground("view-background", cl::Hidden
,
34 cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
36 std::string
llvm::DOT::EscapeString(const std::string
&Label
) {
37 std::string
Str(Label
);
38 for (unsigned i
= 0; i
!= Str
.length(); ++i
)
41 Str
.insert(Str
.begin()+i
, '\\'); // Escape character...
46 Str
.insert(Str
.begin()+i
, ' '); // Convert to two spaces
51 if (i
+1 != Str
.length())
53 case 'l': continue; // don't disturb \l
54 case '|': case '{': case '}':
55 Str
.erase(Str
.begin()+i
); continue;
62 Str
.insert(Str
.begin()+i
, '\\'); // Escape character...
63 ++i
; // don't infinite loop
69 /// 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
];
80 std::string
llvm::createGraphFilename(const Twine
&Name
, int &FD
) {
82 SmallString
<128> Filename
;
83 std::error_code EC
= sys::fs::createTemporaryFile(Name
, "dot", FD
, Filename
);
85 errs() << "Error: " << EC
.message() << "\n";
89 errs() << "Writing '" << Filename
<< "'... ";
90 return Filename
.str();
93 // Execute the graph viewer. Return true if there were errors.
94 static bool ExecGraphViewer(StringRef ExecPath
, std::vector
<StringRef
> &args
,
95 StringRef Filename
, bool wait
,
96 std::string
&ErrMsg
) {
98 if (sys::ExecuteAndWait(ExecPath
, args
, None
, {}, 0, 0, &ErrMsg
)) {
99 errs() << "Error: " << ErrMsg
<< "\n";
102 sys::fs::remove(Filename
);
103 errs() << " done. \n";
105 sys::ExecuteNoWait(ExecPath
, args
, None
, {}, 0, &ErrMsg
);
106 errs() << "Remember to erase graph file: " << Filename
<< "\n";
113 struct GraphSession
{
114 std::string LogBuffer
;
116 bool TryFindProgram(StringRef Names
, std::string
&ProgramPath
) {
117 raw_string_ostream
Log(LogBuffer
);
118 SmallVector
<StringRef
, 8> parts
;
119 Names
.split(parts
, '|');
120 for (auto Name
: parts
) {
121 if (ErrorOr
<std::string
> P
= sys::findProgramByName(Name
)) {
125 Log
<< " Tried '" << Name
<< "'\n";
131 } // end anonymous namespace
133 static const char *getProgramName(GraphProgram::Name program
) {
135 case GraphProgram::DOT
:
137 case GraphProgram::FDP
:
139 case GraphProgram::NEATO
:
141 case GraphProgram::TWOPI
:
143 case GraphProgram::CIRCO
:
146 llvm_unreachable("bad kind");
149 bool llvm::DisplayGraph(StringRef FilenameRef
, bool wait
,
150 GraphProgram::Name program
) {
151 std::string Filename
= FilenameRef
;
153 std::string ViewerPath
;
157 wait
&= !ViewBackground
;
158 if (S
.TryFindProgram("open", ViewerPath
)) {
159 std::vector
<StringRef
> args
;
160 args
.push_back(ViewerPath
);
162 args
.push_back("-W");
163 args
.push_back(Filename
);
164 errs() << "Trying 'open' program... ";
165 if (!ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
))
169 if (S
.TryFindProgram("xdg-open", ViewerPath
)) {
170 std::vector
<StringRef
> args
;
171 args
.push_back(ViewerPath
);
172 args
.push_back(Filename
);
173 errs() << "Trying 'xdg-open' program... ";
174 if (!ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
))
179 if (S
.TryFindProgram("Graphviz", ViewerPath
)) {
180 std::vector
<StringRef
> args
;
181 args
.push_back(ViewerPath
);
182 args
.push_back(Filename
);
184 errs() << "Running 'Graphviz' program... ";
185 return ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
);
189 if (S
.TryFindProgram("xdot|xdot.py", ViewerPath
)) {
190 std::vector
<StringRef
> args
;
191 args
.push_back(ViewerPath
);
192 args
.push_back(Filename
);
194 args
.push_back("-f");
195 args
.push_back(getProgramName(program
));
197 errs() << "Running 'xdot.py' program... ";
198 return ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
);
208 ViewerKind Viewer
= VK_None
;
210 if (!Viewer
&& S
.TryFindProgram("open", ViewerPath
))
213 if (!Viewer
&& S
.TryFindProgram("gv", ViewerPath
))
214 Viewer
= VK_Ghostview
;
215 if (!Viewer
&& S
.TryFindProgram("xdg-open", ViewerPath
))
218 if (!Viewer
&& S
.TryFindProgram("cmd", ViewerPath
)) {
219 Viewer
= VK_CmdStart
;
223 // PostScript or PDF graph generator + PostScript/PDF viewer
224 std::string GeneratorPath
;
226 (S
.TryFindProgram(getProgramName(program
), GeneratorPath
) ||
227 S
.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath
))) {
228 std::string OutputFilename
=
229 Filename
+ (Viewer
== VK_CmdStart
? ".pdf" : ".ps");
231 std::vector
<StringRef
> args
;
232 args
.push_back(GeneratorPath
);
233 if (Viewer
== VK_CmdStart
)
234 args
.push_back("-Tpdf");
236 args
.push_back("-Tps");
237 args
.push_back("-Nfontname=Courier");
238 args
.push_back("-Gsize=7.5,10");
239 args
.push_back(Filename
);
240 args
.push_back("-o");
241 args
.push_back(OutputFilename
);
243 errs() << "Running '" << GeneratorPath
<< "' program... ";
245 if (ExecGraphViewer(GeneratorPath
, args
, Filename
, true, ErrMsg
))
248 // The lifetime of StartArg must include the call of ExecGraphViewer
249 // because the args are passed as vector of char*.
250 std::string StartArg
;
253 args
.push_back(ViewerPath
);
256 args
.push_back("-W");
257 args
.push_back(OutputFilename
);
261 args
.push_back(OutputFilename
);
264 args
.push_back("--spartan");
265 args
.push_back(OutputFilename
);
268 args
.push_back("/S");
269 args
.push_back("/C");
271 (StringRef("start ") + (wait
? "/WAIT " : "") + OutputFilename
).str();
272 args
.push_back(StartArg
);
275 llvm_unreachable("Invalid viewer");
279 return ExecGraphViewer(ViewerPath
, args
, OutputFilename
, wait
, ErrMsg
);
283 if (S
.TryFindProgram("dotty", ViewerPath
)) {
284 std::vector
<StringRef
> args
;
285 args
.push_back(ViewerPath
);
286 args
.push_back(Filename
);
288 // Dotty spawns another app and doesn't wait until it returns
292 errs() << "Running 'dotty' program... ";
293 return ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
);
296 errs() << "Error: Couldn't find a usable graph viewer program:\n";
297 errs() << S
.LogBuffer
<< "\n";