163345e4e65f436cc3a75a53f37c054f10097968
[oota-llvm.git] / lib / Support / GraphWriter.cpp
1 //===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements misc. GraphWriter support routines.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/GraphWriter.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/PathV1.h"
20 #include "llvm/Support/Program.h"
21 using namespace llvm;
22
23 static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
24   cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
25
26 std::string llvm::DOT::EscapeString(const std::string &Label) {
27   std::string Str(Label);
28   for (unsigned i = 0; i != Str.length(); ++i)
29   switch (Str[i]) {
30     case '\n':
31       Str.insert(Str.begin()+i, '\\');  // Escape character...
32       ++i;
33       Str[i] = 'n';
34       break;
35     case '\t':
36       Str.insert(Str.begin()+i, ' ');  // Convert to two spaces
37       ++i;
38       Str[i] = ' ';
39       break;
40     case '\\':
41       if (i+1 != Str.length())
42         switch (Str[i+1]) {
43           case 'l': continue; // don't disturb \l
44           case '|': case '{': case '}':
45             Str.erase(Str.begin()+i); continue;
46           default: break;
47         }
48     case '{': case '}':
49     case '<': case '>':
50     case '|': case '"':
51       Str.insert(Str.begin()+i, '\\');  // Escape character...
52       ++i;  // don't infinite loop
53       break;
54   }
55   return Str;
56 }
57
58 /// \brief Get a color string for this node number. Simply round-robin selects
59 /// from a reasonable number of colors.
60 StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
61   static const int NumColors = 20;
62   static const char* Colors[NumColors] = {
63     "aaaaaa", "aa0000", "00aa00", "aa5500", "0055ff", "aa00aa", "00aaaa",
64     "555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff",
65     "ffaaaa", "aaffaa", "ffffaa", "aaaaff", "ffaaff", "aaffff"};
66   return Colors[ColorNumber % NumColors];
67 }
68
69 std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
70   FD = -1;
71   SmallString<128> Filename;
72   error_code EC = sys::fs::unique_file(Twine(Name) + "-%%%%%%%.dot",
73                                        FD, Filename);
74   if (EC) {
75     errs() << "Error: " << EC.message() << "\n";
76     return "";
77   }
78
79   errs() << "Writing '" << Filename << "'... ";
80   return Filename.str();
81 }
82
83 // Execute the graph viewer. Return true if successful.
84 static bool LLVM_ATTRIBUTE_UNUSED
85 ExecGraphViewer(StringRef ExecPath, std::vector<const char*> &args,
86                 StringRef Filename, bool wait, std::string &ErrMsg) {
87   if (wait) {
88     if (sys::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
89       errs() << "Error: " << ErrMsg << "\n";
90       return false;
91     }
92     bool Existed;
93     sys::fs::remove(Filename, Existed);
94     errs() << " done. \n";
95   }
96   else {
97     sys::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
98     errs() << "Remember to erase graph file: " << Filename.str() << "\n";
99   }
100   return true;
101 }
102
103 void llvm::DisplayGraph(StringRef FilenameRef, bool wait,
104                         GraphProgram::Name program) {
105   std::string Filename = FilenameRef;
106   wait &= !ViewBackground;
107   std::string ErrMsg;
108 #if HAVE_GRAPHVIZ
109   std::string Graphviz(LLVM_PATH_GRAPHVIZ);
110
111   std::vector<const char*> args;
112   args.push_back(Graphviz.c_str());
113   args.push_back(Filename.c_str());
114   args.push_back(0);
115
116   errs() << "Running 'Graphviz' program... ";
117   if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
118     return;
119
120 #elif HAVE_XDOT_PY
121   std::vector<const char*> args;
122   args.push_back(LLVM_PATH_XDOT_PY);
123   args.push_back(Filename.c_str());
124
125   switch (program) {
126   case GraphProgram::DOT:   args.push_back("-f"); args.push_back("dot"); break;
127   case GraphProgram::FDP:   args.push_back("-f"); args.push_back("fdp"); break;
128   case GraphProgram::NEATO: args.push_back("-f"); args.push_back("neato");break;
129   case GraphProgram::TWOPI: args.push_back("-f"); args.push_back("twopi");break;
130   case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break;
131   }
132
133   args.push_back(0);
134
135   errs() << "Running 'xdot.py' program... ";
136   if (!ExecGraphViewer(LLVM_PATH_XDOT_PY, args, Filename, wait, ErrMsg))
137     return;
138
139 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
140                    HAVE_TWOPI || HAVE_CIRCO))
141   sys::Path PSFilename = sys::Path(Filename);
142   PSFilename.appendSuffix("ps");
143
144   std::string prog;
145
146   // Set default grapher
147 #if HAVE_CIRCO
148   prog = LLVM_PATH_CIRCO;
149 #endif
150 #if HAVE_TWOPI
151   prog = LLVM_PATH_TWOPI;
152 #endif
153 #if HAVE_NEATO
154   prog = LLVM_PATH_NEATO;
155 #endif
156 #if HAVE_FDP
157   prog = LLVM_PATH_FDP;
158 #endif
159 #if HAVE_DOT
160   prog = LLVM_PATH_DOT;
161 #endif
162
163   // Find which program the user wants
164 #if HAVE_DOT
165   if (program == GraphProgram::DOT)
166     prog = LLVM_PATH_DOT;
167 #endif
168 #if (HAVE_FDP)
169   if (program == GraphProgram::FDP)
170     prog = LLVM_PATH_FDP;
171 #endif
172 #if (HAVE_NEATO)
173   if (program == GraphProgram::NEATO)
174     prog = LLVM_PATH_NEATO;
175 #endif
176 #if (HAVE_TWOPI)
177   if (program == GraphProgram::TWOPI)
178     prog = LLVM_PATH_TWOPI;
179 #endif
180 #if (HAVE_CIRCO)
181   if (program == GraphProgram::CIRCO)
182     prog = LLVM_PATH_CIRCO;
183 #endif
184
185   std::vector<const char*> args;
186   args.push_back(prog.c_str());
187   args.push_back("-Tps");
188   args.push_back("-Nfontname=Courier");
189   args.push_back("-Gsize=7.5,10");
190   args.push_back(Filename.c_str());
191   args.push_back("-o");
192   args.push_back(PSFilename.c_str());
193   args.push_back(0);
194
195   errs() << "Running '" << prog << "' program... ";
196
197   if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
198     return;
199
200   std::string gv(LLVM_PATH_GV);
201   args.clear();
202   args.push_back(gv.c_str());
203   args.push_back(PSFilename.c_str());
204   args.push_back("--spartan");
205   args.push_back(0);
206
207   ErrMsg.clear();
208   if (!ExecGraphViewer(gv, args, PSFilename.str(), wait, ErrMsg))
209     return;
210
211 #elif HAVE_DOTTY
212   std::string dotty(LLVM_PATH_DOTTY);
213
214   std::vector<const char*> args;
215   args.push_back(dotty.c_str());
216   args.push_back(Filename.c_str());
217   args.push_back(0);
218
219 // Dotty spawns another app and doesn't wait until it returns
220 #if defined (__MINGW32__) || defined (_WINDOWS)
221   wait = false;
222 #endif
223   errs() << "Running 'dotty' program... ";
224   if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))
225     return;
226 #endif
227 }