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/CommandLine.h"
15 #include "llvm/Support/GraphWriter.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/Program.h"
18 #include "llvm/Config/config.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 // Execute the graph viewer. Return true if successful.
57 static bool LLVM_ATTRIBUTE_UNUSED
58 ExecGraphViewer(const sys::Path &ExecPath, std::vector<const char*> &args,
59 const sys::Path &Filename, bool wait, std::string &ErrMsg) {
61 if (sys::Program::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
62 errs() << "Error: " << ErrMsg << "\n";
65 Filename.eraseFromDisk();
66 errs() << " done. \n";
69 sys::Program::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
70 errs() << "Remember to erase graph file: " << Filename.str() << "\n";
75 void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
76 GraphProgram::Name program) {
77 wait &= !ViewBackground;
80 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
82 std::vector<const char*> args;
83 args.push_back(Graphviz.c_str());
84 args.push_back(Filename.c_str());
87 errs() << "Running 'Graphviz' program... ";
88 if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
92 std::vector<const char*> args;
93 args.push_back(LLVM_PATH_XDOT_PY);
94 args.push_back(Filename.c_str());
97 case GraphProgram::DOT: args.push_back("-f"); args.push_back("dot"); break;
98 case GraphProgram::FDP: args.push_back("-f"); args.push_back("fdp"); break;
99 case GraphProgram::NEATO: args.push_back("-f"); args.push_back("neato");break;
100 case GraphProgram::TWOPI: args.push_back("-f"); args.push_back("twopi");break;
101 case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break;
106 errs() << "Running 'xdot.py' program... ";
107 if (!ExecGraphViewer(sys::Path(LLVM_PATH_XDOT_PY), args, Filename, wait, ErrMsg))
110 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
111 HAVE_TWOPI || HAVE_CIRCO))
112 sys::Path PSFilename = Filename;
113 PSFilename.appendSuffix("ps");
117 // Set default grapher
119 prog = sys::Path(LLVM_PATH_CIRCO);
122 prog = sys::Path(LLVM_PATH_TWOPI);
125 prog = sys::Path(LLVM_PATH_NEATO);
128 prog = sys::Path(LLVM_PATH_FDP);
131 prog = sys::Path(LLVM_PATH_DOT);
134 // Find which program the user wants
136 if (program == GraphProgram::DOT)
137 prog = sys::Path(LLVM_PATH_DOT);
140 if (program == GraphProgram::FDP)
141 prog = sys::Path(LLVM_PATH_FDP);
144 if (program == GraphProgram::NEATO)
145 prog = sys::Path(LLVM_PATH_NEATO);
148 if (program == GraphProgram::TWOPI)
149 prog = sys::Path(LLVM_PATH_TWOPI);
152 if (program == GraphProgram::CIRCO)
153 prog = sys::Path(LLVM_PATH_CIRCO);
156 std::vector<const char*> args;
157 args.push_back(prog.c_str());
158 args.push_back("-Tps");
159 args.push_back("-Nfontname=Courier");
160 args.push_back("-Gsize=7.5,10");
161 args.push_back(Filename.c_str());
162 args.push_back("-o");
163 args.push_back(PSFilename.c_str());
166 errs() << "Running '" << prog.str() << "' program... ";
168 if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
171 sys::Path gv(LLVM_PATH_GV);
173 args.push_back(gv.c_str());
174 args.push_back(PSFilename.c_str());
175 args.push_back("--spartan");
179 if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
183 sys::Path dotty(LLVM_PATH_DOTTY);
185 std::vector<const char*> args;
186 args.push_back(dotty.c_str());
187 args.push_back(Filename.c_str());
190 // Dotty spawns another app and doesn't wait until it returns
191 #if defined (__MINGW32__) || defined (_WINDOWS)
194 errs() << "Running 'dotty' program... ";
195 if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))