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/Program.h"
21 static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
22 cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
24 std::string llvm::DOT::EscapeString(const std::string &Label) {
25 std::string Str(Label);
26 for (unsigned i = 0; i != Str.length(); ++i)
29 Str.insert(Str.begin()+i, '\\'); // Escape character...
34 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
39 if (i+1 != Str.length())
41 case 'l': continue; // don't disturb \l
42 case '|': case '{': case '}':
43 Str.erase(Str.begin()+i); continue;
49 Str.insert(Str.begin()+i, '\\'); // Escape character...
50 ++i; // don't infinite loop
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];
67 std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
69 SmallString<128> Filename;
70 std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename);
72 errs() << "Error: " << EC.message() << "\n";
76 errs() << "Writing '" << Filename << "'... ";
77 return Filename.str();
80 // Execute the graph viewer. Return true if there were errors.
81 static bool ExecGraphViewer(StringRef ExecPath, std::vector<const char *> &args,
82 StringRef Filename, bool wait,
83 std::string &ErrMsg) {
84 assert(args.back() == nullptr);
86 if (sys::ExecuteAndWait(ExecPath, args.data(), nullptr, nullptr, 0, 0,
88 errs() << "Error: " << ErrMsg << "\n";
91 sys::fs::remove(Filename);
92 errs() << " done. \n";
94 sys::ExecuteNoWait(ExecPath, args.data(), nullptr, nullptr, 0, &ErrMsg);
95 errs() << "Remember to erase graph file: " << Filename << "\n";
101 struct GraphSession {
102 std::string LogBuffer;
103 bool TryFindProgram(StringRef Names, std::string &ProgramPath) {
104 raw_string_ostream Log(LogBuffer);
105 SmallVector<StringRef, 8> parts;
106 Names.split(parts, "|");
107 for (auto Name : parts) {
108 if (ErrorOr<std::string> P = sys::findProgramByName(Name)) {
112 Log << " Tried '" << Name << "'\n";
119 static const char *getProgramName(GraphProgram::Name program) {
121 case GraphProgram::DOT:
123 case GraphProgram::FDP:
125 case GraphProgram::NEATO:
127 case GraphProgram::TWOPI:
129 case GraphProgram::CIRCO:
132 llvm_unreachable("bad kind");
135 bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
136 GraphProgram::Name program) {
137 std::string Filename = FilenameRef;
138 wait &= !ViewBackground;
140 std::string ViewerPath;
144 if (S.TryFindProgram("open", ViewerPath)) {
145 std::vector<const char *> args;
146 args.push_back(ViewerPath.c_str());
148 args.push_back("-W");
149 args.push_back(Filename.c_str());
150 args.push_back(nullptr);
151 errs() << "Trying 'open' program... ";
152 if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
156 if (S.TryFindProgram("xdg-open", ViewerPath)) {
157 std::vector<const char *> args;
158 args.push_back(ViewerPath.c_str());
159 args.push_back(Filename.c_str());
160 args.push_back(nullptr);
161 errs() << "Trying 'xdg-open' program... ";
162 if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
167 if (S.TryFindProgram("Graphviz", ViewerPath)) {
168 std::vector<const char *> args;
169 args.push_back(ViewerPath.c_str());
170 args.push_back(Filename.c_str());
171 args.push_back(nullptr);
173 errs() << "Running 'Graphviz' program... ";
174 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
178 if (S.TryFindProgram("xdot|xdot.py", ViewerPath)) {
179 std::vector<const char *> args;
180 args.push_back(ViewerPath.c_str());
181 args.push_back(Filename.c_str());
183 args.push_back("-f");
184 args.push_back(getProgramName(program));
186 args.push_back(nullptr);
188 errs() << "Running 'xdot.py' program... ";
189 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
192 enum PSViewerKind { PSV_None, PSV_OSXOpen, PSV_XDGOpen, PSV_Ghostview };
193 PSViewerKind PSViewer = PSV_None;
195 if (!PSViewer && S.TryFindProgram("open", ViewerPath))
196 PSViewer = PSV_OSXOpen;
198 if (!PSViewer && S.TryFindProgram("gv", ViewerPath))
199 PSViewer = PSV_Ghostview;
200 if (!PSViewer && S.TryFindProgram("xdg-open", ViewerPath))
201 PSViewer = PSV_XDGOpen;
203 // PostScript graph generator + PostScript viewer
204 std::string GeneratorPath;
206 (S.TryFindProgram(getProgramName(program), GeneratorPath) ||
207 S.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath))) {
208 std::string PSFilename = Filename + ".ps";
210 std::vector<const char *> args;
211 args.push_back(GeneratorPath.c_str());
212 args.push_back("-Tps");
213 args.push_back("-Nfontname=Courier");
214 args.push_back("-Gsize=7.5,10");
215 args.push_back(Filename.c_str());
216 args.push_back("-o");
217 args.push_back(PSFilename.c_str());
218 args.push_back(nullptr);
220 errs() << "Running '" << GeneratorPath << "' program... ";
222 if (ExecGraphViewer(GeneratorPath, args, Filename, wait, ErrMsg))
226 args.push_back(ViewerPath.c_str());
229 args.push_back("-W");
230 args.push_back(PSFilename.c_str());
234 args.push_back(PSFilename.c_str());
237 args.push_back("--spartan");
238 args.push_back(PSFilename.c_str());
241 llvm_unreachable("Invalid viewer");
243 args.push_back(nullptr);
246 return ExecGraphViewer(ViewerPath, args, PSFilename, wait, ErrMsg);
250 if (S.TryFindProgram("dotty", ViewerPath)) {
251 std::vector<const char *> args;
252 args.push_back(ViewerPath.c_str());
253 args.push_back(Filename.c_str());
254 args.push_back(nullptr);
256 // Dotty spawns another app and doesn't wait until it returns
260 errs() << "Running 'dotty' program... ";
261 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
264 errs() << "Error: Couldn't find a usable graph viewer program:\n";
265 errs() << S.LogBuffer << "\n";