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/Config/config.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/Program.h"
21 static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
22 cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
24 std::string llvm::DOT::EscapeString(const std::string &Label) {
25 std::string Str(Label);
26 for (unsigned i = 0; i != Str.length(); ++i)
29 Str.insert(Str.begin()+i, '\\'); // Escape character...
34 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
39 if (i+1 != Str.length())
41 case 'l': continue; // don't disturb \l
42 case '|': case '{': case '}':
43 Str.erase(Str.begin()+i); continue;
49 Str.insert(Str.begin()+i, '\\'); // Escape character...
50 ++i; // don't infinite loop
56 /// \brief Get a color string for this node number. Simply round-robin selects
57 /// from a reasonable number of colors.
58 StringRef 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];
67 // Execute the graph viewer. Return true if successful.
68 static bool LLVM_ATTRIBUTE_UNUSED
69 ExecGraphViewer(const sys::Path &ExecPath, std::vector<const char*> &args,
70 const sys::Path &Filename, bool wait, std::string &ErrMsg) {
72 if (sys::Program::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
73 errs() << "Error: " << ErrMsg << "\n";
76 Filename.eraseFromDisk();
77 errs() << " done. \n";
80 sys::Program::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
81 errs() << "Remember to erase graph file: " << Filename.str() << "\n";
86 void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
87 GraphProgram::Name program) {
88 wait &= !ViewBackground;
91 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
93 std::vector<const char*> args;
94 args.push_back(Graphviz.c_str());
95 args.push_back(Filename.c_str());
98 errs() << "Running 'Graphviz' program... ";
99 if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
103 std::vector<const char*> args;
104 args.push_back(LLVM_PATH_XDOT_PY);
105 args.push_back(Filename.c_str());
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;
117 errs() << "Running 'xdot.py' program... ";
118 if (!ExecGraphViewer(sys::Path(LLVM_PATH_XDOT_PY), args, Filename, wait, ErrMsg))
121 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
122 HAVE_TWOPI || HAVE_CIRCO))
123 sys::Path PSFilename = Filename;
124 PSFilename.appendSuffix("ps");
128 // Set default grapher
130 prog = sys::Path(LLVM_PATH_CIRCO);
133 prog = sys::Path(LLVM_PATH_TWOPI);
136 prog = sys::Path(LLVM_PATH_NEATO);
139 prog = sys::Path(LLVM_PATH_FDP);
142 prog = sys::Path(LLVM_PATH_DOT);
145 // Find which program the user wants
147 if (program == GraphProgram::DOT)
148 prog = sys::Path(LLVM_PATH_DOT);
151 if (program == GraphProgram::FDP)
152 prog = sys::Path(LLVM_PATH_FDP);
155 if (program == GraphProgram::NEATO)
156 prog = sys::Path(LLVM_PATH_NEATO);
159 if (program == GraphProgram::TWOPI)
160 prog = sys::Path(LLVM_PATH_TWOPI);
163 if (program == GraphProgram::CIRCO)
164 prog = sys::Path(LLVM_PATH_CIRCO);
167 std::vector<const char*> args;
168 args.push_back(prog.c_str());
169 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());
177 errs() << "Running '" << prog.str() << "' program... ";
179 if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
182 sys::Path gv(LLVM_PATH_GV);
184 args.push_back(gv.c_str());
185 args.push_back(PSFilename.c_str());
186 args.push_back("--spartan");
190 if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
194 sys::Path dotty(LLVM_PATH_DOTTY);
196 std::vector<const char*> args;
197 args.push_back(dotty.c_str());
198 args.push_back(Filename.c_str());
201 // Dotty spawns another app and doesn't wait until it returns
202 #if defined (__MINGW32__) || defined (_WINDOWS)
205 errs() << "Running 'dotty' program... ";
206 if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))