Skip to content

Commit edbb3b2

Browse files
committed
Added -view-background to avoid waiting for each GraphViz invocation.
GV and XDOT paths are untested but should work the same. llvm-svn: 152179
1 parent a5f1956 commit edbb3b2

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

llvm/lib/Support/GraphWriter.cpp

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "llvm/Support/CommandLine.h"
1415
#include "llvm/Support/GraphWriter.h"
1516
#include "llvm/Support/Path.h"
1617
#include "llvm/Support/Program.h"
1718
#include "llvm/Config/config.h"
1819
using namespace llvm;
1920

21+
static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
22+
cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
23+
2024
std::string llvm::DOT::EscapeString(const std::string &Label) {
2125
std::string Str(Label);
2226
for (unsigned i = 0; i != Str.length(); ++i)
@@ -49,10 +53,30 @@ std::string llvm::DOT::EscapeString(const std::string &Label) {
4953
return Str;
5054
}
5155

52-
56+
// Execute the graph viewer. Return true if successful.
57+
static bool ExecGraphViewer(const sys::Path &ExecPath,
58+
std::vector<const char*> &args,
59+
const sys::Path &Filename,
60+
bool wait,
61+
std::string &ErrMsg) {
62+
if (wait) {
63+
if (sys::Program::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
64+
errs() << "Error: " << ErrMsg << "\n";
65+
return false;
66+
}
67+
Filename.eraseFromDisk();
68+
errs() << " done. \n";
69+
}
70+
else {
71+
sys::Program::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
72+
errs() << "Remember to erase graph file: " << Filename.str() << "\n";
73+
}
74+
return true;
75+
}
5376

5477
void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
5578
GraphProgram::Name program) {
79+
wait &= !ViewBackground;
5680
std::string ErrMsg;
5781
#if HAVE_GRAPHVIZ
5882
sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
@@ -61,14 +85,10 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
6185
args.push_back(Graphviz.c_str());
6286
args.push_back(Filename.c_str());
6387
args.push_back(0);
64-
88+
6589
errs() << "Running 'Graphviz' program... ";
66-
if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,0,&ErrMsg)) {
67-
errs() << "Error: " << ErrMsg << "\n";
90+
if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
6891
return;
69-
}
70-
Filename.eraseFromDisk();
71-
errs() << " done. \n";
7292

7393
#elif HAVE_XDOT_PY
7494
std::vector<const char*> args;
@@ -83,17 +103,12 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
83103
case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break;
84104
default: errs() << "Unknown graph layout name; using default.\n";
85105
}
86-
106+
87107
args.push_back(0);
88108

89109
errs() << "Running 'xdot.py' program... ";
90-
if (sys::Program::ExecuteAndWait(sys::Path(LLVM_PATH_XDOT_PY),
91-
&args[0],0,0,0,0,&ErrMsg)) {
92-
errs() << "Error: " << ErrMsg << "\n";
110+
if (!ExecGraphViewer(sys::Path(LLVM_PATH_XDOT_PY), args, Filename, wait, ErrMsg))
93111
return;
94-
}
95-
Filename.eraseFromDisk();
96-
errs() << " done. \n";
97112

98113
#elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
99114
HAVE_TWOPI || HAVE_CIRCO))
@@ -150,51 +165,37 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
150165
args.push_back("-o");
151166
args.push_back(PSFilename.c_str());
152167
args.push_back(0);
153-
168+
154169
errs() << "Running '" << prog.str() << "' program... ";
155170

156-
if (sys::Program::ExecuteAndWait(prog, &args[0], 0, 0, 0, 0, &ErrMsg)) {
157-
errs() << "Error: " << ErrMsg << "\n";
171+
if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
158172
return;
159-
}
160-
errs() << " done. \n";
161173

162174
sys::Path gv(LLVM_PATH_GV);
163175
args.clear();
164176
args.push_back(gv.c_str());
165177
args.push_back(PSFilename.c_str());
166178
args.push_back("--spartan");
167179
args.push_back(0);
168-
180+
169181
ErrMsg.clear();
170-
if (wait) {
171-
if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg))
172-
errs() << "Error: " << ErrMsg << "\n";
173-
Filename.eraseFromDisk();
174-
PSFilename.eraseFromDisk();
175-
}
176-
else {
177-
sys::Program::ExecuteNoWait(gv, &args[0],0,0,0,&ErrMsg);
178-
errs() << "Remember to erase graph files: " << Filename.str() << " "
179-
<< PSFilename.str() << "\n";
180-
}
182+
if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
183+
return;
184+
181185
#elif HAVE_DOTTY
182186
sys::Path dotty(LLVM_PATH_DOTTY);
183187

184188
std::vector<const char*> args;
185189
args.push_back(dotty.c_str());
186190
args.push_back(Filename.c_str());
187191
args.push_back(0);
188-
189-
errs() << "Running 'dotty' program... ";
190-
if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,0,&ErrMsg)) {
191-
errs() << "Error: " << ErrMsg << "\n";
192-
} else {
192+
193193
// Dotty spawns another app and doesn't wait until it returns
194194
#if defined (__MINGW32__) || defined (_WINDOWS)
195-
return;
195+
wait = false;
196196
#endif
197-
Filename.eraseFromDisk();
198-
}
197+
errs() << "Running 'dotty' program... ";
198+
if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))
199+
return;
199200
#endif
200201
}

0 commit comments

Comments
 (0)