blob: 9d72db1bd2d29ec40e759d0a21ff4e1ff634684d [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements misc. GraphWriter support routines.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/GraphWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/System/Path.h"
16#include "llvm/System/Program.h"
17#include "llvm/Config/config.h"
18using namespace llvm;
19
Chris Lattnerbf9d76d2009-08-23 07:19:13 +000020std::string llvm::DOT::EscapeString(const std::string &Label) {
21 std::string Str(Label);
22 for (unsigned i = 0; i != Str.length(); ++i)
23 switch (Str[i]) {
24 case '\n':
25 Str.insert(Str.begin()+i, '\\'); // Escape character...
26 ++i;
27 Str[i] = 'n';
28 break;
29 case '\t':
30 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
31 ++i;
32 Str[i] = ' ';
33 break;
34 case '\\':
35 if (i+1 != Str.length())
36 switch (Str[i+1]) {
37 case 'l': continue; // don't disturb \l
38 case '|': case '{': case '}':
39 Str.erase(Str.begin()+i); continue;
40 default: break;
41 }
42 case '{': case '}':
43 case '<': case '>':
44 case '|': case '"':
45 Str.insert(Str.begin()+i, '\\'); // Escape character...
46 ++i; // don't infinite loop
47 break;
48 }
49 return Str;
50}
51
52
53
David Greene33ef34c2009-07-09 17:06:18 +000054void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
55 GraphProgram::Name program) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 std::string ErrMsg;
57#if HAVE_GRAPHVIZ
58 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
59
60 std::vector<const char*> args;
61 args.push_back(Graphviz.c_str());
62 args.push_back(Filename.c_str());
63 args.push_back(0);
64
Chris Lattnerbf9d76d2009-08-23 07:19:13 +000065 errs() << "Running 'Graphviz' program... ";
66 if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,0,&ErrMsg))
67 errs() << "Error viewing graph " << Filename << ": " << ErrMsg << "\n";
68 else
David Greenebd915cf2009-07-08 21:53:41 +000069 Filename.eraseFromDisk();
David Greene33ef34c2009-07-09 17:06:18 +000070
71#elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
72 HAVE_TWOPI || HAVE_CIRCO))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 sys::Path PSFilename = Filename;
74 PSFilename.appendSuffix("ps");
David Greenebd915cf2009-07-08 21:53:41 +000075
David Greene33ef34c2009-07-09 17:06:18 +000076 sys::Path prog;
77
78 // Set default grapher
79#if HAVE_CIRCO
80 prog = sys::Path(LLVM_PATH_CIRCO);
81#endif
82#if HAVE_TWOPI
83 prog = sys::Path(LLVM_PATH_TWOPI);
84#endif
85#if HAVE_NEATO
86 prog = sys::Path(LLVM_PATH_NEATO);
87#endif
David Greenebd915cf2009-07-08 21:53:41 +000088#if HAVE_FDP
David Greene33ef34c2009-07-09 17:06:18 +000089 prog = sys::Path(LLVM_PATH_FDP);
90#endif
91#if HAVE_DOT
92 prog = sys::Path(LLVM_PATH_DOT);
93#endif
94
95 // Find which program the user wants
96#if HAVE_DOT
97 if (program == GraphProgram::DOT) {
98 prog = sys::Path(LLVM_PATH_DOT);
99 }
100#endif
101#if (HAVE_FDP)
102 if (program == GraphProgram::FDP) {
103 prog = sys::Path(LLVM_PATH_FDP);
104 }
105#endif
106#if (HAVE_NEATO)
107 if (program == GraphProgram::NEATO) {
108 prog = sys::Path(LLVM_PATH_NEATO);
109 }
110#endif
111#if (HAVE_TWOPI)
112 if (program == GraphProgram::TWOPI) {
113 prog = sys::Path(LLVM_PATH_TWOPI);
114 }
115#endif
116#if (HAVE_CIRCO)
117 if (program == GraphProgram::CIRCO) {
118 prog = sys::Path(LLVM_PATH_CIRCO);
119 }
David Greenebd915cf2009-07-08 21:53:41 +0000120#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
122 std::vector<const char*> args;
David Greenebd915cf2009-07-08 21:53:41 +0000123 args.push_back(prog.c_str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 args.push_back("-Tps");
125 args.push_back("-Nfontname=Courier");
126 args.push_back("-Gsize=7.5,10");
127 args.push_back(Filename.c_str());
128 args.push_back("-o");
129 args.push_back(PSFilename.c_str());
130 args.push_back(0);
131
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000132 errs() << "Running '" << prog << "' program... ";
David Greenebd915cf2009-07-08 21:53:41 +0000133
134 if (sys::Program::ExecuteAndWait(prog, &args[0],0,0,0,0,&ErrMsg)) {
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000135 errs() << "Error viewing graph " << Filename << ": '" << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 } else {
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000137 errs() << " done. \n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
139 sys::Path gv(LLVM_PATH_GV);
140 args.clear();
141 args.push_back(gv.c_str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 args.push_back(PSFilename.c_str());
Chris Lattner0c57e602009-01-20 18:25:03 +0000143 args.push_back("-spartan");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 args.push_back(0);
145
146 ErrMsg.clear();
David Greenebd915cf2009-07-08 21:53:41 +0000147 if (wait) {
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000148 if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg))
149 errs() << "Error viewing graph: " << ErrMsg << "\n";
David Greenebd915cf2009-07-08 21:53:41 +0000150 Filename.eraseFromDisk();
151 PSFilename.eraseFromDisk();
152 }
153 else {
154 sys::Program::ExecuteNoWait(gv, &args[0],0,0,0,&ErrMsg);
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000155 errs() << "Remember to erase graph files: " << Filename << " "
156 << PSFilename << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 }
158 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159#elif HAVE_DOTTY
160 sys::Path dotty(LLVM_PATH_DOTTY);
161
162 std::vector<const char*> args;
163 args.push_back(dotty.c_str());
164 args.push_back(Filename.c_str());
165 args.push_back(0);
166
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000167 errs() << "Running 'dotty' program... ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,0,&ErrMsg)) {
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000169 errs() << "Error viewing graph " << Filename << ": " << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 } else {
171#ifdef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
172 return;
173#endif
David Greenebd915cf2009-07-08 21:53:41 +0000174 Filename.eraseFromDisk();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 }
176#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177}