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