1 //===- GraphWriter.cpp - Implements GraphWriter support routines ----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements misc. GraphWriter support routines.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/GraphWriter.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/ErrorOr.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/raw_ostream.h"
26 #include <system_error>
32 static cl::opt
<bool> ViewBackground("view-background", cl::Hidden
,
33 cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
35 std::string
llvm::DOT::EscapeString(const std::string
&Label
) {
36 std::string
Str(Label
);
37 for (unsigned i
= 0; i
!= Str
.length(); ++i
)
40 Str
.insert(Str
.begin()+i
, '\\'); // Escape character...
45 Str
.insert(Str
.begin()+i
, ' '); // Convert to two spaces
50 if (i
+1 != Str
.length())
52 case 'l': continue; // don't disturb \l
53 case '|': case '{': case '}':
54 Str
.erase(Str
.begin()+i
); continue;
61 Str
.insert(Str
.begin()+i
, '\\'); // Escape character...
62 ++i
; // don't infinite loop
68 /// Get a color string for this node number. Simply round-robin selects
69 /// from a reasonable number of colors.
70 StringRef
llvm::DOT::getColorString(unsigned ColorNumber
) {
71 static const int NumColors
= 20;
72 static const char* Colors
[NumColors
] = {
73 "aaaaaa", "aa0000", "00aa00", "aa5500", "0055ff", "aa00aa", "00aaaa",
74 "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff",
75 "ffaaaa", "aaffaa", "ffffaa", "aaaaff", "ffaaff", "aaffff"};
76 return Colors
[ColorNumber
% NumColors
];
79 std::string
llvm::createGraphFilename(const Twine
&Name
, int &FD
) {
81 SmallString
<128> Filename
;
82 std::error_code EC
= sys::fs::createTemporaryFile(Name
, "dot", FD
, Filename
);
84 errs() << "Error: " << EC
.message() << "\n";
88 errs() << "Writing '" << Filename
<< "'... ";
89 return Filename
.str();
92 // Execute the graph viewer. Return true if there were errors.
93 static bool ExecGraphViewer(StringRef ExecPath
, std::vector
<StringRef
> &args
,
94 StringRef Filename
, bool wait
,
95 std::string
&ErrMsg
) {
97 if (sys::ExecuteAndWait(ExecPath
, args
, None
, {}, 0, 0, &ErrMsg
)) {
98 errs() << "Error: " << ErrMsg
<< "\n";
101 sys::fs::remove(Filename
);
102 errs() << " done. \n";
104 sys::ExecuteNoWait(ExecPath
, args
, None
, {}, 0, &ErrMsg
);
105 errs() << "Remember to erase graph file: " << Filename
<< "\n";
112 struct GraphSession
{
113 std::string LogBuffer
;
115 bool TryFindProgram(StringRef Names
, std::string
&ProgramPath
) {
116 raw_string_ostream
Log(LogBuffer
);
117 SmallVector
<StringRef
, 8> parts
;
118 Names
.split(parts
, '|');
119 for (auto Name
: parts
) {
120 if (ErrorOr
<std::string
> P
= sys::findProgramByName(Name
)) {
124 Log
<< " Tried '" << Name
<< "'\n";
130 } // end anonymous namespace
132 static const char *getProgramName(GraphProgram::Name program
) {
134 case GraphProgram::DOT
:
136 case GraphProgram::FDP
:
138 case GraphProgram::NEATO
:
140 case GraphProgram::TWOPI
:
142 case GraphProgram::CIRCO
:
145 llvm_unreachable("bad kind");
148 bool llvm::DisplayGraph(StringRef FilenameRef
, bool wait
,
149 GraphProgram::Name program
) {
150 std::string Filename
= FilenameRef
;
152 std::string ViewerPath
;
156 wait
&= !ViewBackground
;
157 if (S
.TryFindProgram("open", ViewerPath
)) {
158 std::vector
<StringRef
> args
;
159 args
.push_back(ViewerPath
);
161 args
.push_back("-W");
162 args
.push_back(Filename
);
163 errs() << "Trying 'open' program... ";
164 if (!ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
))
168 if (S
.TryFindProgram("xdg-open", ViewerPath
)) {
169 std::vector
<StringRef
> args
;
170 args
.push_back(ViewerPath
);
171 args
.push_back(Filename
);
172 errs() << "Trying 'xdg-open' program... ";
173 if (!ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
))
178 if (S
.TryFindProgram("Graphviz", ViewerPath
)) {
179 std::vector
<StringRef
> args
;
180 args
.push_back(ViewerPath
);
181 args
.push_back(Filename
);
183 errs() << "Running 'Graphviz' program... ";
184 return ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
);
188 if (S
.TryFindProgram("xdot|xdot.py", ViewerPath
)) {
189 std::vector
<StringRef
> args
;
190 args
.push_back(ViewerPath
);
191 args
.push_back(Filename
);
193 args
.push_back("-f");
194 args
.push_back(getProgramName(program
));
196 errs() << "Running 'xdot.py' program... ";
197 return ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
);
207 ViewerKind Viewer
= VK_None
;
209 if (!Viewer
&& S
.TryFindProgram("open", ViewerPath
))
212 if (!Viewer
&& S
.TryFindProgram("gv", ViewerPath
))
213 Viewer
= VK_Ghostview
;
214 if (!Viewer
&& S
.TryFindProgram("xdg-open", ViewerPath
))
217 if (!Viewer
&& S
.TryFindProgram("cmd", ViewerPath
)) {
218 Viewer
= VK_CmdStart
;
222 // PostScript or PDF graph generator + PostScript/PDF viewer
223 std::string GeneratorPath
;
225 (S
.TryFindProgram(getProgramName(program
), GeneratorPath
) ||
226 S
.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath
))) {
227 std::string OutputFilename
=
228 Filename
+ (Viewer
== VK_CmdStart
? ".pdf" : ".ps");
230 std::vector
<StringRef
> args
;
231 args
.push_back(GeneratorPath
);
232 if (Viewer
== VK_CmdStart
)
233 args
.push_back("-Tpdf");
235 args
.push_back("-Tps");
236 args
.push_back("-Nfontname=Courier");
237 args
.push_back("-Gsize=7.5,10");
238 args
.push_back(Filename
);
239 args
.push_back("-o");
240 args
.push_back(OutputFilename
);
242 errs() << "Running '" << GeneratorPath
<< "' program... ";
244 if (ExecGraphViewer(GeneratorPath
, args
, Filename
, true, ErrMsg
))
247 // The lifetime of StartArg must include the call of ExecGraphViewer
248 // because the args are passed as vector of char*.
249 std::string StartArg
;
252 args
.push_back(ViewerPath
);
255 args
.push_back("-W");
256 args
.push_back(OutputFilename
);
260 args
.push_back(OutputFilename
);
263 args
.push_back("--spartan");
264 args
.push_back(OutputFilename
);
267 args
.push_back("/S");
268 args
.push_back("/C");
270 (StringRef("start ") + (wait
? "/WAIT " : "") + OutputFilename
).str();
271 args
.push_back(StartArg
);
274 llvm_unreachable("Invalid viewer");
278 return ExecGraphViewer(ViewerPath
, args
, OutputFilename
, wait
, ErrMsg
);
282 if (S
.TryFindProgram("dotty", ViewerPath
)) {
283 std::vector
<StringRef
> args
;
284 args
.push_back(ViewerPath
);
285 args
.push_back(Filename
);
287 // Dotty spawns another app and doesn't wait until it returns
291 errs() << "Running 'dotty' program... ";
292 return ExecGraphViewer(ViewerPath
, args
, Filename
, wait
, ErrMsg
);
295 errs() << "Error: Couldn't find a usable graph viewer program:\n";
296 errs() << S
.LogBuffer
<< "\n";