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