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 /// \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];
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<const char *> &args,
95 StringRef Filename, bool wait,
96 std::string &ErrMsg) {
97 assert(args.back() == nullptr);
99 if (sys::ExecuteAndWait(ExecPath, args.data(), nullptr, {}, 0, 0,
101 errs() << "Error: " << ErrMsg << "\n";
104 sys::fs::remove(Filename);
105 errs() << " done. \n";
107 sys::ExecuteNoWait(ExecPath, args.data(), nullptr, {}, 0, &ErrMsg);
108 errs() << "Remember to erase graph file: " << Filename << "\n";
115 struct GraphSession {
116 std::string LogBuffer;
118 bool TryFindProgram(StringRef Names, std::string &ProgramPath) {
119 raw_string_ostream Log(LogBuffer);
120 SmallVector<StringRef, 8> parts;
121 Names.split(parts, '|');
122 for (auto Name : parts) {
123 if (ErrorOr<std::string> P = sys::findProgramByName(Name)) {
127 Log << " Tried '" << Name << "'\n";
133 } // end anonymous namespace
135 static const char *getProgramName(GraphProgram::Name program) {
137 case GraphProgram::DOT:
139 case GraphProgram::FDP:
141 case GraphProgram::NEATO:
143 case GraphProgram::TWOPI:
145 case GraphProgram::CIRCO:
148 llvm_unreachable("bad kind");
151 bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
152 GraphProgram::Name program) {
153 std::string Filename = FilenameRef;
155 std::string ViewerPath;
159 wait &= !ViewBackground;
160 if (S.TryFindProgram("open", ViewerPath)) {
161 std::vector<const char *> args;
162 args.push_back(ViewerPath.c_str());
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))
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))
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);
189 errs() << "Running 'Graphviz' program... ";
190 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
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());
199 args.push_back("-f");
200 args.push_back(getProgramName(program));
202 args.push_back(nullptr);
204 errs() << "Running 'xdot.py' program... ";
205 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
215 ViewerKind Viewer = VK_None;
217 if (!Viewer && S.TryFindProgram("open", ViewerPath))
220 if (!Viewer && S.TryFindProgram("gv", ViewerPath))
221 Viewer = VK_Ghostview;
222 if (!Viewer && S.TryFindProgram("xdg-open", ViewerPath))
225 if (!Viewer && S.TryFindProgram("cmd", ViewerPath)) {
226 Viewer = VK_CmdStart;
230 // PostScript or PDF graph generator + PostScript/PDF viewer
231 std::string GeneratorPath;
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");
238 std::vector<const char *> args;
239 args.push_back(GeneratorPath.c_str());
240 if (Viewer == VK_CmdStart)
241 args.push_back("-Tpdf");
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);
251 errs() << "Running '" << GeneratorPath << "' program... ";
253 if (ExecGraphViewer(GeneratorPath, args, Filename, true, ErrMsg))
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;
261 args.push_back(ViewerPath.c_str());
264 args.push_back("-W");
265 args.push_back(OutputFilename.c_str());
269 args.push_back(OutputFilename.c_str());
272 args.push_back("--spartan");
273 args.push_back(OutputFilename.c_str());
276 args.push_back("/S");
277 args.push_back("/C");
279 (StringRef("start ") + (wait ? "/WAIT " : "") + OutputFilename).str();
280 args.push_back(StartArg.c_str());
283 llvm_unreachable("Invalid viewer");
285 args.push_back(nullptr);
288 return ExecGraphViewer(ViewerPath, args, OutputFilename, wait, ErrMsg);
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);
298 // Dotty spawns another app and doesn't wait until it returns
302 errs() << "Running 'dotty' program... ";
303 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
306 errs() << "Error: Couldn't find a usable graph viewer program:\n";
307 errs() << S.LogBuffer << "\n";