Add a function for profiling to run at shutdown. Unlike the existing API, this
[llvm/stm8.git] / lib / Support / GraphWriter.cpp
blob0dba28a2530c9f8bf0bd9c03c2ca974e68fa4c4a
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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/GraphWriter.h"
15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/Program.h"
17 #include "llvm/Config/config.h"
18 using namespace llvm;
20 std::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;
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;
49 return Str;
54 void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
55 GraphProgram::Name program) {
56 std::string ErrMsg;
57 #if HAVE_GRAPHVIZ
58 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
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);
65 errs() << "Running 'Graphviz' program... ";
66 if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,0,&ErrMsg)) {
67 errs() << "Error: " << ErrMsg << "\n";
68 return;
70 Filename.eraseFromDisk();
71 errs() << " done. \n";
73 #elif HAVE_XDOT_PY
74 std::vector<const char*> args;
75 args.push_back(LLVM_PATH_XDOT_PY);
76 args.push_back(Filename.c_str());
78 switch (program) {
79 case GraphProgram::DOT: args.push_back("-f"); args.push_back("dot"); break;
80 case GraphProgram::FDP: args.push_back("-f"); args.push_back("fdp"); break;
81 case GraphProgram::NEATO: args.push_back("-f"); args.push_back("neato");break;
82 case GraphProgram::TWOPI: args.push_back("-f"); args.push_back("twopi");break;
83 case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break;
84 default: errs() << "Unknown graph layout name; using default.\n";
87 args.push_back(0);
89 errs() << "Running 'xdot.py' program... ";
90 if (sys::Program::ExecuteAndWait(sys::Path(LLVM_PATH_XDOT_PY),
91 &args[0],0,0,0,0,&ErrMsg)) {
92 errs() << "Error: " << ErrMsg << "\n";
93 return;
95 Filename.eraseFromDisk();
96 errs() << " done. \n";
98 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
99 HAVE_TWOPI || HAVE_CIRCO))
100 sys::Path PSFilename = Filename;
101 PSFilename.appendSuffix("ps");
103 sys::Path prog;
105 // Set default grapher
106 #if HAVE_CIRCO
107 prog = sys::Path(LLVM_PATH_CIRCO);
108 #endif
109 #if HAVE_TWOPI
110 prog = sys::Path(LLVM_PATH_TWOPI);
111 #endif
112 #if HAVE_NEATO
113 prog = sys::Path(LLVM_PATH_NEATO);
114 #endif
115 #if HAVE_FDP
116 prog = sys::Path(LLVM_PATH_FDP);
117 #endif
118 #if HAVE_DOT
119 prog = sys::Path(LLVM_PATH_DOT);
120 #endif
122 // Find which program the user wants
123 #if HAVE_DOT
124 if (program == GraphProgram::DOT)
125 prog = sys::Path(LLVM_PATH_DOT);
126 #endif
127 #if (HAVE_FDP)
128 if (program == GraphProgram::FDP)
129 prog = sys::Path(LLVM_PATH_FDP);
130 #endif
131 #if (HAVE_NEATO)
132 if (program == GraphProgram::NEATO)
133 prog = sys::Path(LLVM_PATH_NEATO);
134 #endif
135 #if (HAVE_TWOPI)
136 if (program == GraphProgram::TWOPI)
137 prog = sys::Path(LLVM_PATH_TWOPI);
138 #endif
139 #if (HAVE_CIRCO)
140 if (program == GraphProgram::CIRCO)
141 prog = sys::Path(LLVM_PATH_CIRCO);
142 #endif
144 std::vector<const char*> args;
145 args.push_back(prog.c_str());
146 args.push_back("-Tps");
147 args.push_back("-Nfontname=Courier");
148 args.push_back("-Gsize=7.5,10");
149 args.push_back(Filename.c_str());
150 args.push_back("-o");
151 args.push_back(PSFilename.c_str());
152 args.push_back(0);
154 errs() << "Running '" << prog.str() << "' program... ";
156 if (sys::Program::ExecuteAndWait(prog, &args[0], 0, 0, 0, 0, &ErrMsg)) {
157 errs() << "Error: " << ErrMsg << "\n";
158 return;
160 errs() << " done. \n";
162 sys::Path gv(LLVM_PATH_GV);
163 args.clear();
164 args.push_back(gv.c_str());
165 args.push_back(PSFilename.c_str());
166 args.push_back("--spartan");
167 args.push_back(0);
169 ErrMsg.clear();
170 if (wait) {
171 if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg))
172 errs() << "Error: " << ErrMsg << "\n";
173 Filename.eraseFromDisk();
174 PSFilename.eraseFromDisk();
176 else {
177 sys::Program::ExecuteNoWait(gv, &args[0],0,0,0,&ErrMsg);
178 errs() << "Remember to erase graph files: " << Filename.str() << " "
179 << PSFilename.str() << "\n";
181 #elif HAVE_DOTTY
182 sys::Path dotty(LLVM_PATH_DOTTY);
184 std::vector<const char*> args;
185 args.push_back(dotty.c_str());
186 args.push_back(Filename.c_str());
187 args.push_back(0);
189 errs() << "Running 'dotty' program... ";
190 if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,0,&ErrMsg)) {
191 errs() << "Error: " << ErrMsg << "\n";
192 } else {
193 // Dotty spawns another app and doesn't wait until it returns
194 #if defined (__MINGW32__) || defined (_WINDOWS)
195 return;
196 #endif
197 Filename.eraseFromDisk();
199 #endif