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 there were errors.
82 static bool ExecGraphViewer(StringRef ExecPath, std::vector<const char *> &args,
83 StringRef Filename, bool wait,
84 std::string &ErrMsg) {
85 assert(args.back() == nullptr);
87 if (sys::ExecuteAndWait(ExecPath, args.data(), nullptr, nullptr, 0, 0,
89 errs() << "Error: " << ErrMsg << "\n";
92 sys::fs::remove(Filename);
93 errs() << " done. \n";
95 sys::ExecuteNoWait(ExecPath, args.data(), nullptr, nullptr, 0, &ErrMsg);
96 errs() << "Remember to erase graph file: " << Filename.str() << "\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 ProgramPath = sys::FindProgramByName(Name);
109 if (!ProgramPath.empty())
111 Log << " Tried '" << Name << "'\n";
117 static const char *getProgramName(GraphProgram::Name program) {
119 case GraphProgram::DOT:
121 case GraphProgram::FDP:
123 case GraphProgram::NEATO:
125 case GraphProgram::TWOPI:
127 case GraphProgram::CIRCO:
130 llvm_unreachable("bad kind");
133 bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
134 GraphProgram::Name program) {
135 std::string Filename = FilenameRef;
136 wait &= !ViewBackground;
138 std::string ViewerPath;
142 if (S.TryFindProgram("Graphviz", ViewerPath)) {
143 std::vector<const char *> args;
144 args.push_back(ViewerPath.c_str());
145 args.push_back(Filename.c_str());
146 args.push_back(nullptr);
148 errs() << "Running 'Graphviz' program... ";
149 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
153 if (S.TryFindProgram("xdot|xdot.py", ViewerPath)) {
154 std::vector<const char *> args;
155 args.push_back(ViewerPath.c_str());
156 args.push_back(Filename.c_str());
158 args.push_back("-f");
159 args.push_back(getProgramName(program));
161 args.push_back(nullptr);
163 errs() << "Running 'xdot.py' program... ";
164 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
167 enum PSViewerKind { PSV_None, PSV_OSXOpen, PSV_Ghostview };
168 PSViewerKind PSViewer = PSV_None;
170 if (S.TryFindProgram("open", ViewerPath))
171 PSViewer = PSV_OSXOpen;
173 if (!PSViewer && S.TryFindProgram("gv", ViewerPath))
174 PSViewer = PSV_Ghostview;
176 // PostScript graph generator + PostScript viewer
177 std::string GeneratorPath;
179 (S.TryFindProgram(getProgramName(program), GeneratorPath) ||
180 S.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath))) {
181 std::string PSFilename = Filename + ".ps";
183 std::vector<const char *> args;
184 args.push_back(GeneratorPath.c_str());
185 args.push_back("-Tps");
186 args.push_back("-Nfontname=Courier");
187 args.push_back("-Gsize=7.5,10");
188 args.push_back(Filename.c_str());
189 args.push_back("-o");
190 args.push_back(PSFilename.c_str());
191 args.push_back(nullptr);
193 errs() << "Running '" << GeneratorPath << "' program... ";
195 if (ExecGraphViewer(GeneratorPath, args, Filename, wait, ErrMsg))
199 args.push_back(ViewerPath.c_str());
202 args.push_back("-W");
203 args.push_back(PSFilename.c_str());
206 args.push_back("--spartan");
207 args.push_back(PSFilename.c_str());
210 llvm_unreachable("Invalid viewer");
212 args.push_back(nullptr);
215 return ExecGraphViewer(ViewerPath, args, PSFilename, wait, ErrMsg);
219 if (S.TryFindProgram("dotty", ViewerPath)) {
220 std::vector<const char *> args;
221 args.push_back(ViewerPath.c_str());
222 args.push_back(Filename.c_str());
223 args.push_back(nullptr);
225 // Dotty spawns another app and doesn't wait until it returns
229 errs() << "Running 'dotty' program... ";
230 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
233 errs() << "Error: Couldn't find a usable graph viewer program:\n";
234 errs() << S.LogBuffer << "\n";