1 //===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements misc. GraphWriter support routines.
12 //===----------------------------------------------------------------------===//
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"
22 static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
23 cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
25 std::string llvm::DOT::EscapeString(const std::string &Label) {
26 std::string Str(Label);
27 for (unsigned i = 0; i != Str.length(); ++i)
30 Str.insert(Str.begin()+i, '\\'); // Escape character...
35 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
40 if (i+1 != Str.length())
42 case 'l': continue; // don't disturb \l
43 case '|': case '{': case '}':
44 Str.erase(Str.begin()+i); continue;
50 Str.insert(Str.begin()+i, '\\'); // Escape character...
51 ++i; // don't infinite loop
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];
68 std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
70 SmallString<128> Filename;
71 error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
73 errs() << "Error: " << EC.message() << "\n";
77 errs() << "Writing '" << Filename << "'... ";
78 return Filename.str();
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) {
86 if (sys::ExecuteAndWait(ExecPath, &args[0],nullptr,nullptr,0,0,&ErrMsg)) {
87 errs() << "Error: " << ErrMsg << "\n";
90 sys::fs::remove(Filename);
91 errs() << " done. \n";
94 sys::ExecuteNoWait(ExecPath, &args[0],nullptr,nullptr,0,&ErrMsg);
95 errs() << "Remember to erase graph file: " << Filename.str() << "\n";
100 void llvm::DisplayGraph(StringRef FilenameRef, bool wait,
101 GraphProgram::Name program) {
102 std::string Filename = FilenameRef;
103 wait &= !ViewBackground;
106 std::string Graphviz(LLVM_PATH_GRAPHVIZ);
108 std::vector<const char*> args;
109 args.push_back(Graphviz.c_str());
110 args.push_back(Filename.c_str());
111 args.push_back(nullptr);
113 errs() << "Running 'Graphviz' program... ";
114 if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
118 std::vector<const char*> args;
119 args.push_back(LLVM_PATH_XDOT);
120 args.push_back(Filename.c_str());
123 case GraphProgram::DOT: args.push_back("-f"); args.push_back("dot"); break;
124 case GraphProgram::FDP: args.push_back("-f"); args.push_back("fdp"); break;
125 case GraphProgram::NEATO: args.push_back("-f"); args.push_back("neato");break;
126 case GraphProgram::TWOPI: args.push_back("-f"); args.push_back("twopi");break;
127 case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break;
132 errs() << "Running 'xdot.py' program... ";
133 if (!ExecGraphViewer(LLVM_PATH_XDOT, args, Filename, wait, ErrMsg))
136 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
137 HAVE_TWOPI || HAVE_CIRCO))
138 std::string PSFilename = Filename + ".ps";
141 // Set default grapher
143 prog = LLVM_PATH_CIRCO;
146 prog = LLVM_PATH_TWOPI;
149 prog = LLVM_PATH_NEATO;
152 prog = LLVM_PATH_FDP;
155 prog = LLVM_PATH_DOT;
158 // Find which program the user wants
160 if (program == GraphProgram::DOT)
161 prog = LLVM_PATH_DOT;
164 if (program == GraphProgram::FDP)
165 prog = LLVM_PATH_FDP;
168 if (program == GraphProgram::NEATO)
169 prog = LLVM_PATH_NEATO;
172 if (program == GraphProgram::TWOPI)
173 prog = LLVM_PATH_TWOPI;
176 if (program == GraphProgram::CIRCO)
177 prog = LLVM_PATH_CIRCO;
180 std::vector<const char*> args;
181 args.push_back(prog.c_str());
182 args.push_back("-Tps");
183 args.push_back("-Nfontname=Courier");
184 args.push_back("-Gsize=7.5,10");
185 args.push_back(Filename.c_str());
186 args.push_back("-o");
187 args.push_back(PSFilename.c_str());
190 errs() << "Running '" << prog << "' program... ";
192 if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
195 std::string gv(LLVM_PATH_GV);
197 args.push_back(gv.c_str());
198 args.push_back(PSFilename.c_str());
199 args.push_back("--spartan");
203 if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
207 std::string dotty(LLVM_PATH_DOTTY);
209 std::vector<const char*> args;
210 args.push_back(dotty.c_str());
211 args.push_back(Filename.c_str());
214 // Dotty spawns another app and doesn't wait until it returns
215 #if defined (__MINGW32__) || defined (_WINDOWS)
218 errs() << "Running 'dotty' program... ";
219 if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))