blob: 41af06c23924750792e48bb0aba06e5683de04f2 [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"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000017#include "llvm/Support/Path.h"
18#include "llvm/Support/Program.h"
Reid Spencer9d5b5322006-06-27 16:49:46 +000019using namespace llvm;
20
Andrew Trick255cd512012-03-07 00:18:27 +000021static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
22 cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
23
Chris Lattner103289e2009-08-23 07:19:13 +000024std::string llvm::DOT::EscapeString(const std::string &Label) {
25 std::string Str(Label);
26 for (unsigned i = 0; i != Str.length(); ++i)
27 switch (Str[i]) {
28 case '\n':
29 Str.insert(Str.begin()+i, '\\'); // Escape character...
30 ++i;
31 Str[i] = 'n';
32 break;
33 case '\t':
34 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
35 ++i;
36 Str[i] = ' ';
37 break;
38 case '\\':
39 if (i+1 != Str.length())
40 switch (Str[i+1]) {
41 case 'l': continue; // don't disturb \l
42 case '|': case '{': case '}':
43 Str.erase(Str.begin()+i); continue;
44 default: break;
45 }
46 case '{': case '}':
47 case '<': case '>':
48 case '|': case '"':
49 Str.insert(Str.begin()+i, '\\'); // Escape character...
50 ++i; // don't infinite loop
51 break;
52 }
53 return Str;
54}
55
Andrew Trickc6ada8e2013-01-25 07:45:25 +000056/// \brief Get a color string for this node number. Simply round-robin selects
57/// from a reasonable number of colors.
58StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
59 static const int NumColors = 20;
60 static const char* Colors[NumColors] = {
61 "aaaaaa", "aa0000", "00aa00", "aa5500", "0055ff", "aa00aa", "00aaaa",
62 "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff",
63 "ffaaaa", "aaffaa", "ffffaa", "aaaaff", "ffaaff", "aaffff"};
64 return Colors[ColorNumber % NumColors];
65}
66
Andrew Trick255cd512012-03-07 00:18:27 +000067// Execute the graph viewer. Return true if successful.
Benjamin Kramercdaedf92012-03-08 22:15:23 +000068static bool LLVM_ATTRIBUTE_UNUSED
69ExecGraphViewer(const sys::Path &ExecPath, std::vector<const char*> &args,
70 const sys::Path &Filename, bool wait, std::string &ErrMsg) {
Andrew Trick255cd512012-03-07 00:18:27 +000071 if (wait) {
Rafael Espindola9f1d9fd2013-06-12 20:58:35 +000072 if (sys::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
Andrew Trick255cd512012-03-07 00:18:27 +000073 errs() << "Error: " << ErrMsg << "\n";
74 return false;
75 }
76 Filename.eraseFromDisk();
77 errs() << " done. \n";
78 }
79 else {
Rafael Espindola9f1d9fd2013-06-12 20:58:35 +000080 sys::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
Andrew Trick255cd512012-03-07 00:18:27 +000081 errs() << "Remember to erase graph file: " << Filename.str() << "\n";
82 }
83 return true;
84}
Chris Lattner103289e2009-08-23 07:19:13 +000085
David Greene00ad26f2009-07-09 17:06:18 +000086void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
87 GraphProgram::Name program) {
Andrew Trick255cd512012-03-07 00:18:27 +000088 wait &= !ViewBackground;
Reid Spencer8ea5ecb2006-08-21 06:04:45 +000089 std::string ErrMsg;
Reid Spencer9d5b5322006-06-27 16:49:46 +000090#if HAVE_GRAPHVIZ
91 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
92
93 std::vector<const char*> args;
94 args.push_back(Graphviz.c_str());
95 args.push_back(Filename.c_str());
96 args.push_back(0);
Andrew Trick255cd512012-03-07 00:18:27 +000097
Chris Lattner103289e2009-08-23 07:19:13 +000098 errs() << "Running 'Graphviz' program... ";
Andrew Trick255cd512012-03-07 00:18:27 +000099 if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
Dan Gohman644801a2010-10-05 15:30:27 +0000100 return;
David Greene00ad26f2009-07-09 17:06:18 +0000101
Dan Gohman48fd5a72010-09-27 16:28:34 +0000102#elif HAVE_XDOT_PY
Dan Gohman48fd5a72010-09-27 16:28:34 +0000103 std::vector<const char*> args;
104 args.push_back(LLVM_PATH_XDOT_PY);
105 args.push_back(Filename.c_str());
106
107 switch (program) {
108 case GraphProgram::DOT: args.push_back("-f"); args.push_back("dot"); break;
109 case GraphProgram::FDP: args.push_back("-f"); args.push_back("fdp"); break;
110 case GraphProgram::NEATO: args.push_back("-f"); args.push_back("neato");break;
111 case GraphProgram::TWOPI: args.push_back("-f"); args.push_back("twopi");break;
112 case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break;
Dan Gohman48fd5a72010-09-27 16:28:34 +0000113 }
Andrew Trick255cd512012-03-07 00:18:27 +0000114
Dan Gohman48fd5a72010-09-27 16:28:34 +0000115 args.push_back(0);
116
117 errs() << "Running 'xdot.py' program... ";
Andrew Trick255cd512012-03-07 00:18:27 +0000118 if (!ExecGraphViewer(sys::Path(LLVM_PATH_XDOT_PY), args, Filename, wait, ErrMsg))
Dan Gohman644801a2010-10-05 15:30:27 +0000119 return;
Dan Gohman48fd5a72010-09-27 16:28:34 +0000120
David Greene00ad26f2009-07-09 17:06:18 +0000121#elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
122 HAVE_TWOPI || HAVE_CIRCO))
Reid Spencer9d5b5322006-06-27 16:49:46 +0000123 sys::Path PSFilename = Filename;
124 PSFilename.appendSuffix("ps");
David Greene229509a2009-07-08 21:53:41 +0000125
David Greene00ad26f2009-07-09 17:06:18 +0000126 sys::Path prog;
127
128 // Set default grapher
129#if HAVE_CIRCO
130 prog = sys::Path(LLVM_PATH_CIRCO);
131#endif
132#if HAVE_TWOPI
133 prog = sys::Path(LLVM_PATH_TWOPI);
134#endif
135#if HAVE_NEATO
136 prog = sys::Path(LLVM_PATH_NEATO);
137#endif
David Greene229509a2009-07-08 21:53:41 +0000138#if HAVE_FDP
David Greene00ad26f2009-07-09 17:06:18 +0000139 prog = sys::Path(LLVM_PATH_FDP);
140#endif
141#if HAVE_DOT
142 prog = sys::Path(LLVM_PATH_DOT);
143#endif
144
145 // Find which program the user wants
146#if HAVE_DOT
Chris Lattner40ef79d2009-08-23 22:53:53 +0000147 if (program == GraphProgram::DOT)
David Greene00ad26f2009-07-09 17:06:18 +0000148 prog = sys::Path(LLVM_PATH_DOT);
David Greene00ad26f2009-07-09 17:06:18 +0000149#endif
150#if (HAVE_FDP)
Chris Lattner40ef79d2009-08-23 22:53:53 +0000151 if (program == GraphProgram::FDP)
David Greene00ad26f2009-07-09 17:06:18 +0000152 prog = sys::Path(LLVM_PATH_FDP);
David Greene00ad26f2009-07-09 17:06:18 +0000153#endif
154#if (HAVE_NEATO)
Chris Lattner40ef79d2009-08-23 22:53:53 +0000155 if (program == GraphProgram::NEATO)
David Greene00ad26f2009-07-09 17:06:18 +0000156 prog = sys::Path(LLVM_PATH_NEATO);
David Greene00ad26f2009-07-09 17:06:18 +0000157#endif
158#if (HAVE_TWOPI)
Chris Lattner40ef79d2009-08-23 22:53:53 +0000159 if (program == GraphProgram::TWOPI)
David Greene00ad26f2009-07-09 17:06:18 +0000160 prog = sys::Path(LLVM_PATH_TWOPI);
David Greene00ad26f2009-07-09 17:06:18 +0000161#endif
162#if (HAVE_CIRCO)
Chris Lattner40ef79d2009-08-23 22:53:53 +0000163 if (program == GraphProgram::CIRCO)
David Greene00ad26f2009-07-09 17:06:18 +0000164 prog = sys::Path(LLVM_PATH_CIRCO);
David Greene229509a2009-07-08 21:53:41 +0000165#endif
Reid Spencer9d5b5322006-06-27 16:49:46 +0000166
167 std::vector<const char*> args;
David Greene229509a2009-07-08 21:53:41 +0000168 args.push_back(prog.c_str());
Reid Spencer9d5b5322006-06-27 16:49:46 +0000169 args.push_back("-Tps");
170 args.push_back("-Nfontname=Courier");
171 args.push_back("-Gsize=7.5,10");
172 args.push_back(Filename.c_str());
173 args.push_back("-o");
174 args.push_back(PSFilename.c_str());
175 args.push_back(0);
Andrew Trick255cd512012-03-07 00:18:27 +0000176
Dan Gohman2d5e3ac2009-08-24 03:23:12 +0000177 errs() << "Running '" << prog.str() << "' program... ";
David Greene229509a2009-07-08 21:53:41 +0000178
Andrew Trick255cd512012-03-07 00:18:27 +0000179 if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
Chris Lattner459e13a2010-04-18 03:35:23 +0000180 return;
Reid Spencer9d5b5322006-06-27 16:49:46 +0000181
Chris Lattner459e13a2010-04-18 03:35:23 +0000182 sys::Path gv(LLVM_PATH_GV);
183 args.clear();
184 args.push_back(gv.c_str());
185 args.push_back(PSFilename.c_str());
186 args.push_back("--spartan");
187 args.push_back(0);
Andrew Trick255cd512012-03-07 00:18:27 +0000188
Chris Lattner459e13a2010-04-18 03:35:23 +0000189 ErrMsg.clear();
Andrew Trick255cd512012-03-07 00:18:27 +0000190 if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
191 return;
192
Reid Spencer9d5b5322006-06-27 16:49:46 +0000193#elif HAVE_DOTTY
194 sys::Path dotty(LLVM_PATH_DOTTY);
195
196 std::vector<const char*> args;
Chris Lattner82493282007-05-03 18:32:10 +0000197 args.push_back(dotty.c_str());
Reid Spencer9d5b5322006-06-27 16:49:46 +0000198 args.push_back(Filename.c_str());
199 args.push_back(0);
Andrew Trick255cd512012-03-07 00:18:27 +0000200
Chris Lattner7c57b7b2010-04-13 04:35:39 +0000201// Dotty spawns another app and doesn't wait until it returns
202#if defined (__MINGW32__) || defined (_WINDOWS)
Andrew Trick255cd512012-03-07 00:18:27 +0000203 wait = false;
Reid Spencer9d5b5322006-06-27 16:49:46 +0000204#endif
Andrew Trick255cd512012-03-07 00:18:27 +0000205 errs() << "Running 'dotty' program... ";
206 if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))
207 return;
Reid Spencer9d5b5322006-06-27 16:49:46 +0000208#endif
Reid Spencer9d5b5322006-06-27 16:49:46 +0000209}