blob: a3d2d6c871f062892c59b179e02c5341bc153506 [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))
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000067 errs() << "Error viewing graph " << Filename.str() << ": " << ErrMsg
68 << "\n";
Chris Lattnerbf9d76d2009-08-23 07:19:13 +000069 else
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000070 Filename.eraseFromDisk();
David Greene33ef34c2009-07-09 17:06:18 +000071
72#elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
73 HAVE_TWOPI || HAVE_CIRCO))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 sys::Path PSFilename = Filename;
75 PSFilename.appendSuffix("ps");
David Greenebd915cf2009-07-08 21:53:41 +000076
David Greene33ef34c2009-07-09 17:06:18 +000077 sys::Path prog;
78
79 // Set default grapher
80#if HAVE_CIRCO
81 prog = sys::Path(LLVM_PATH_CIRCO);
82#endif
83#if HAVE_TWOPI
84 prog = sys::Path(LLVM_PATH_TWOPI);
85#endif
86#if HAVE_NEATO
87 prog = sys::Path(LLVM_PATH_NEATO);
88#endif
David Greenebd915cf2009-07-08 21:53:41 +000089#if HAVE_FDP
David Greene33ef34c2009-07-09 17:06:18 +000090 prog = sys::Path(LLVM_PATH_FDP);
91#endif
92#if HAVE_DOT
93 prog = sys::Path(LLVM_PATH_DOT);
94#endif
95
96 // Find which program the user wants
97#if HAVE_DOT
98 if (program == GraphProgram::DOT) {
99 prog = sys::Path(LLVM_PATH_DOT);
100 }
101#endif
102#if (HAVE_FDP)
103 if (program == GraphProgram::FDP) {
104 prog = sys::Path(LLVM_PATH_FDP);
105 }
106#endif
107#if (HAVE_NEATO)
108 if (program == GraphProgram::NEATO) {
109 prog = sys::Path(LLVM_PATH_NEATO);
110 }
111#endif
112#if (HAVE_TWOPI)
113 if (program == GraphProgram::TWOPI) {
114 prog = sys::Path(LLVM_PATH_TWOPI);
115 }
116#endif
117#if (HAVE_CIRCO)
118 if (program == GraphProgram::CIRCO) {
119 prog = sys::Path(LLVM_PATH_CIRCO);
120 }
David Greenebd915cf2009-07-08 21:53:41 +0000121#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122
123 std::vector<const char*> args;
David Greenebd915cf2009-07-08 21:53:41 +0000124 args.push_back(prog.c_str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 args.push_back("-Tps");
126 args.push_back("-Nfontname=Courier");
127 args.push_back("-Gsize=7.5,10");
128 args.push_back(Filename.c_str());
129 args.push_back("-o");
130 args.push_back(PSFilename.c_str());
131 args.push_back(0);
132
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000133 errs() << "Running '" << prog << "' program... ";
David Greenebd915cf2009-07-08 21:53:41 +0000134
135 if (sys::Program::ExecuteAndWait(prog, &args[0],0,0,0,0,&ErrMsg)) {
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000136 errs() << "Error viewing graph " << Filename << ": '" << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 } else {
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000138 errs() << " done. \n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139
140 sys::Path gv(LLVM_PATH_GV);
141 args.clear();
142 args.push_back(gv.c_str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 args.push_back(PSFilename.c_str());
Chris Lattner0c57e602009-01-20 18:25:03 +0000144 args.push_back("-spartan");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 args.push_back(0);
146
147 ErrMsg.clear();
David Greenebd915cf2009-07-08 21:53:41 +0000148 if (wait) {
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000149 if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg))
150 errs() << "Error viewing graph: " << ErrMsg << "\n";
David Greenebd915cf2009-07-08 21:53:41 +0000151 Filename.eraseFromDisk();
152 PSFilename.eraseFromDisk();
153 }
154 else {
155 sys::Program::ExecuteNoWait(gv, &args[0],0,0,0,&ErrMsg);
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000156 errs() << "Remember to erase graph files: " << Filename << " "
157 << PSFilename << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 }
159 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160#elif HAVE_DOTTY
161 sys::Path dotty(LLVM_PATH_DOTTY);
162
163 std::vector<const char*> args;
164 args.push_back(dotty.c_str());
165 args.push_back(Filename.c_str());
166 args.push_back(0);
167
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000168 errs() << "Running 'dotty' program... ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,0,&ErrMsg)) {
Chris Lattnerbf9d76d2009-08-23 07:19:13 +0000170 errs() << "Error viewing graph " << Filename << ": " << ErrMsg << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 } else {
172#ifdef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
173 return;
174#endif
David Greenebd915cf2009-07-08 21:53:41 +0000175 Filename.eraseFromDisk();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 }
177#endif
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178}