blob: e68ee434dd592557d0d44ca84191ee80cedf04ea [file] [log] [blame]
Reid Spencer9d5b5322006-06-27 16:49:46 +00001//===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer9d5b5322006-06-27 16:49:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements misc. GraphWriter support routines.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner65a392e2006-07-28 22:21:01 +000014#include "llvm/Support/GraphWriter.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/Config/config.h"
16#include "llvm/Support/CommandLine.h"
Rafael Espindola4d397272013-06-13 17:20:48 +000017#include "llvm/Support/FileSystem.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000018#include "llvm/Support/Path.h"
19#include "llvm/Support/Program.h"
Reid Spencer9d5b5322006-06-27 16:49:46 +000020using namespace llvm;
21
Andrew Trick255cd512012-03-07 00:18:27 +000022static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
23 cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
24
Chris Lattner103289e2009-08-23 07:19:13 +000025std::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
Andrew Trickc6ada8e2013-01-25 07:45:25 +000057/// \brief Get a color string for this node number. Simply round-robin selects
58/// from a reasonable number of colors.
59StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
60 static const int NumColors = 20;
61 static const char* Colors[NumColors] = {
62 "aaaaaa", "aa0000", "00aa00", "aa5500", "0055ff", "aa00aa", "00aaaa",
63 "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff",
64 "ffaaaa", "aaffaa", "ffffaa", "aaaaff", "ffaaff", "aaffff"};
65 return Colors[ColorNumber % NumColors];
66}
67
Rafael Espindolaa54ba122013-06-14 16:43:15 +000068std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
69 FD = -1;
70 SmallString<128> Filename;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070071 std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
Rafael Espindolaa54ba122013-06-14 16:43:15 +000072 if (EC) {
73 errs() << "Error: " << EC.message() << "\n";
Rafael Espindola4d397272013-06-13 17:20:48 +000074 return "";
75 }
Rafael Espindolaa54ba122013-06-14 16:43:15 +000076
77 errs() << "Writing '" << Filename << "'... ";
Rafael Espindola4d397272013-06-13 17:20:48 +000078 return Filename.str();
79}
80
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070081// Execute the graph viewer. Return true if there were errors.
82static bool ExecGraphViewer(StringRef ExecPath, std::vector<const char *> &args,
83 StringRef Filename, bool wait,
84 std::string &ErrMsg) {
85 assert(args.back() == nullptr);
Andrew Trick255cd512012-03-07 00:18:27 +000086 if (wait) {
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070087 if (sys::ExecuteAndWait(ExecPath, args.data(), nullptr, nullptr, 0, 0,
88 &ErrMsg)) {
Andrew Trick255cd512012-03-07 00:18:27 +000089 errs() << "Error: " << ErrMsg << "\n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070090 return true;
Andrew Trick255cd512012-03-07 00:18:27 +000091 }
Stephen Hines36b56882014-04-23 16:57:46 -070092 sys::fs::remove(Filename);
Andrew Trick255cd512012-03-07 00:18:27 +000093 errs() << " done. \n";
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070094 } else {
95 sys::ExecuteNoWait(ExecPath, args.data(), nullptr, nullptr, 0, &ErrMsg);
Andrew Trick255cd512012-03-07 00:18:27 +000096 errs() << "Remember to erase graph file: " << Filename.str() << "\n";
97 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070098 return false;
Andrew Trick255cd512012-03-07 00:18:27 +000099}
Chris Lattner103289e2009-08-23 07:19:13 +0000100
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700101struct 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 ProgramPath = sys::FindProgramByName(Name);
109 if (!ProgramPath.empty())
110 return true;
111 Log << " Tried '" << Name << "'\n";
112 }
113 return false;
114 }
115};
116
117static const char *getProgramName(GraphProgram::Name program) {
118 switch (program) {
119 case GraphProgram::DOT:
120 return "dot";
121 case GraphProgram::FDP:
122 return "fdp";
123 case GraphProgram::NEATO:
124 return "neato";
125 case GraphProgram::TWOPI:
126 return "twopi";
127 case GraphProgram::CIRCO:
128 return "circo";
129 }
130 llvm_unreachable("bad kind");
131}
132
133bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
David Greene00ad26f2009-07-09 17:06:18 +0000134 GraphProgram::Name program) {
Rafael Espindolaaaf4c1e2013-06-13 17:27:45 +0000135 std::string Filename = FilenameRef;
Andrew Trick255cd512012-03-07 00:18:27 +0000136 wait &= !ViewBackground;
Reid Spencer8ea5ecb2006-08-21 06:04:45 +0000137 std::string ErrMsg;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700138 std::string ViewerPath;
139 GraphSession S;
Reid Spencer9d5b5322006-06-27 16:49:46 +0000140
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700141 // Graphviz
142 if (S.TryFindProgram("Graphviz", ViewerPath)) {
143 std::vector<const char *> args;
144 args.push_back(ViewerPath.c_str());
145 args.push_back(Filename.c_str());
146 args.push_back(nullptr);
Andrew Trick255cd512012-03-07 00:18:27 +0000147
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700148 errs() << "Running 'Graphviz' program... ";
149 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
Dan Gohman48fd5a72010-09-27 16:28:34 +0000150 }
Andrew Trick255cd512012-03-07 00:18:27 +0000151
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700152 // xdot
153 if (S.TryFindProgram("xdot|xdot.py", ViewerPath)) {
154 std::vector<const char *> args;
155 args.push_back(ViewerPath.c_str());
156 args.push_back(Filename.c_str());
Dan Gohman48fd5a72010-09-27 16:28:34 +0000157
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700158 args.push_back("-f");
159 args.push_back(getProgramName(program));
Dan Gohman48fd5a72010-09-27 16:28:34 +0000160
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700161 args.push_back(nullptr);
David Greene00ad26f2009-07-09 17:06:18 +0000162
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700163 errs() << "Running 'xdot.py' program... ";
164 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
165 }
166
167 enum PSViewerKind { PSV_None, PSV_OSXOpen, PSV_XDGOpen, PSV_Ghostview };
168 PSViewerKind PSViewer = PSV_None;
169#ifdef __APPLE__
170 if (!PSViewer && S.TryFindProgram("open", ViewerPath))
171 PSViewer = PSV_OSXOpen;
David Greene00ad26f2009-07-09 17:06:18 +0000172#endif
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700173 if (!PSViewer && S.TryFindProgram("gv", ViewerPath))
174 PSViewer = PSV_Ghostview;
175 if (!PSViewer && S.TryFindProgram("xdg-open", ViewerPath))
176 PSViewer = PSV_XDGOpen;
David Greene00ad26f2009-07-09 17:06:18 +0000177
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700178 // PostScript graph generator + PostScript viewer
179 std::string GeneratorPath;
180 if (PSViewer &&
181 (S.TryFindProgram(getProgramName(program), GeneratorPath) ||
182 S.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath))) {
183 std::string PSFilename = Filename + ".ps";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000184
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700185 std::vector<const char *> args;
186 args.push_back(GeneratorPath.c_str());
187 args.push_back("-Tps");
188 args.push_back("-Nfontname=Courier");
189 args.push_back("-Gsize=7.5,10");
190 args.push_back(Filename.c_str());
191 args.push_back("-o");
192 args.push_back(PSFilename.c_str());
193 args.push_back(nullptr);
Andrew Trick255cd512012-03-07 00:18:27 +0000194
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700195 errs() << "Running '" << GeneratorPath << "' program... ";
David Greene229509a2009-07-08 21:53:41 +0000196
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700197 if (ExecGraphViewer(GeneratorPath, args, Filename, wait, ErrMsg))
198 return true;
Reid Spencer9d5b5322006-06-27 16:49:46 +0000199
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700200 args.clear();
201 args.push_back(ViewerPath.c_str());
202 switch (PSViewer) {
203 case PSV_OSXOpen:
204 args.push_back("-W");
205 args.push_back(PSFilename.c_str());
206 break;
207 case PSV_XDGOpen:
208 wait = false;
209 args.push_back(PSFilename.c_str());
210 break;
211 case PSV_Ghostview:
212 args.push_back("--spartan");
213 args.push_back(PSFilename.c_str());
214 break;
215 case PSV_None:
216 llvm_unreachable("Invalid viewer");
217 }
218 args.push_back(nullptr);
Andrew Trick255cd512012-03-07 00:18:27 +0000219
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700220 ErrMsg.clear();
221 return ExecGraphViewer(ViewerPath, args, PSFilename, wait, ErrMsg);
222 }
Andrew Trick255cd512012-03-07 00:18:27 +0000223
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700224 // dotty
225 if (S.TryFindProgram("dotty", ViewerPath)) {
226 std::vector<const char *> args;
227 args.push_back(ViewerPath.c_str());
228 args.push_back(Filename.c_str());
229 args.push_back(nullptr);
Andrew Trick255cd512012-03-07 00:18:27 +0000230
Chris Lattner7c57b7b2010-04-13 04:35:39 +0000231// Dotty spawns another app and doesn't wait until it returns
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700232#ifdef LLVM_ON_WIN32
233 wait = false;
Reid Spencer9d5b5322006-06-27 16:49:46 +0000234#endif
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700235 errs() << "Running 'dotty' program... ";
236 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
237 }
238
239 errs() << "Error: Couldn't find a usable graph viewer program:\n";
240 errs() << S.LogBuffer << "\n";
241 return true;
Reid Spencer9d5b5322006-06-27 16:49:46 +0000242}