11
11
//
12
12
// ===----------------------------------------------------------------------===//
13
13
14
+ #include " llvm/Support/CommandLine.h"
14
15
#include " llvm/Support/GraphWriter.h"
15
16
#include " llvm/Support/Path.h"
16
17
#include " llvm/Support/Program.h"
17
18
#include " llvm/Config/config.h"
18
19
using namespace llvm ;
19
20
21
+ static cl::opt<bool > ViewBackground (" view-background" , cl::Hidden,
22
+ cl::desc (" Execute graph viewer in the background. Creates tmp file litter." ));
23
+
20
24
std::string llvm::DOT::EscapeString (const std::string &Label) {
21
25
std::string Str (Label);
22
26
for (unsigned i = 0 ; i != Str.length (); ++i)
@@ -49,10 +53,30 @@ std::string llvm::DOT::EscapeString(const std::string &Label) {
49
53
return Str;
50
54
}
51
55
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
+ }
53
76
54
77
void llvm::DisplayGraph (const sys::Path &Filename, bool wait,
55
78
GraphProgram::Name program) {
79
+ wait &= !ViewBackground;
56
80
std::string ErrMsg;
57
81
#if HAVE_GRAPHVIZ
58
82
sys::Path Graphviz (LLVM_PATH_GRAPHVIZ);
@@ -61,14 +85,10 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
61
85
args.push_back (Graphviz.c_str ());
62
86
args.push_back (Filename.c_str ());
63
87
args.push_back (0 );
64
-
88
+
65
89
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))
68
91
return ;
69
- }
70
- Filename.eraseFromDisk ();
71
- errs () << " done. \n " ;
72
92
73
93
#elif HAVE_XDOT_PY
74
94
std::vector<const char *> args;
@@ -83,17 +103,12 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
83
103
case GraphProgram::CIRCO: args.push_back (" -f" ); args.push_back (" circo" );break ;
84
104
default : errs () << " Unknown graph layout name; using default.\n " ;
85
105
}
86
-
106
+
87
107
args.push_back (0 );
88
108
89
109
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))
93
111
return ;
94
- }
95
- Filename.eraseFromDisk ();
96
- errs () << " done. \n " ;
97
112
98
113
#elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
99
114
HAVE_TWOPI || HAVE_CIRCO))
@@ -150,51 +165,37 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
150
165
args.push_back (" -o" );
151
166
args.push_back (PSFilename.c_str ());
152
167
args.push_back (0 );
153
-
168
+
154
169
errs () << " Running '" << prog.str () << " ' program... " ;
155
170
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))
158
172
return ;
159
- }
160
- errs () << " done. \n " ;
161
173
162
174
sys::Path gv (LLVM_PATH_GV);
163
175
args.clear ();
164
176
args.push_back (gv.c_str ());
165
177
args.push_back (PSFilename.c_str ());
166
178
args.push_back (" --spartan" );
167
179
args.push_back (0 );
168
-
180
+
169
181
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
+
181
185
#elif HAVE_DOTTY
182
186
sys::Path dotty (LLVM_PATH_DOTTY);
183
187
184
188
std::vector<const char *> args;
185
189
args.push_back (dotty.c_str ());
186
190
args.push_back (Filename.c_str ());
187
191
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
+
193
193
// Dotty spawns another app and doesn't wait until it returns
194
194
#if defined (__MINGW32__) || defined (_WINDOWS)
195
- return ;
195
+ wait = false ;
196
196
#endif
197
- Filename.eraseFromDisk ();
198
- }
197
+ errs () << " Running 'dotty' program... " ;
198
+ if (!ExecGraphViewer (dotty, args, Filename, wait, ErrMsg))
199
+ return ;
199
200
#endif
200
201
}
0 commit comments