18 #include "llvm/Config/config.h"
27 #include <system_error>
34 cl::desc(
"Execute graph viewer in the background. Creates tmp file litter."));
37 std::string Str(Label);
38 for (
unsigned i = 0; i != Str.length(); ++i)
41 Str.insert(Str.begin()+i,
'\\');
46 Str.insert(Str.begin()+i,
' ');
51 if (i+1 != Str.length())
54 case '|':
case '{':
case '}':
55 Str.erase(Str.begin()+i);
continue;
62 Str.insert(Str.begin()+i,
'\\');
72 static const int NumColors = 20;
73 static const char* Colors[NumColors] = {
74 "aaaaaa",
"aa0000",
"00aa00",
"aa5500",
"0055ff",
"aa00aa",
"00aaaa",
75 "555555",
"ff5555",
"55ff55",
"ffff55",
"5555ff",
"ff55ff",
"55ffff",
76 "ffaaaa",
"aaffaa",
"ffffaa",
"aaaaff",
"ffaaff",
"aaffff"};
77 return Colors[ColorNumber % NumColors];
85 errs() <<
"Error: " << EC.message() <<
"\n";
89 errs() <<
"Writing '" << Filename <<
"'... ";
90 return Filename.
str();
97 assert(args.back() ==
nullptr);
101 errs() <<
"Error: " << ErrMsg <<
"\n";
105 errs() <<
" done. \n";
108 errs() <<
"Remember to erase graph file: " << Filename <<
"\n";
115 struct GraphSession {
116 std::string LogBuffer;
118 bool TryFindProgram(
StringRef Names, std::string &ProgramPath) {
121 Names.
split(parts,
'|');
122 for (
auto Name : parts) {
127 Log <<
" Tried '" <<
Name <<
"'\n";
153 std::string Filename = FilenameRef;
155 std::string ViewerPath;
160 if (S.TryFindProgram(
"open", ViewerPath)) {
161 std::vector<const char *> args;
162 args.push_back(ViewerPath.c_str());
164 args.push_back(
"-W");
165 args.push_back(Filename.c_str());
166 args.push_back(
nullptr);
167 errs() <<
"Trying 'open' program... ";
172 if (S.TryFindProgram(
"xdg-open", ViewerPath)) {
173 std::vector<const char *> args;
174 args.push_back(ViewerPath.c_str());
175 args.push_back(Filename.c_str());
176 args.push_back(
nullptr);
177 errs() <<
"Trying 'xdg-open' program... ";
183 if (S.TryFindProgram(
"Graphviz", ViewerPath)) {
184 std::vector<const char *> args;
185 args.push_back(ViewerPath.c_str());
186 args.push_back(Filename.c_str());
187 args.push_back(
nullptr);
189 errs() <<
"Running 'Graphviz' program... ";
194 if (S.TryFindProgram(
"xdot|xdot.py", ViewerPath)) {
195 std::vector<const char *> args;
196 args.push_back(ViewerPath.c_str());
197 args.push_back(Filename.c_str());
199 args.push_back(
"-f");
202 args.push_back(
nullptr);
204 errs() <<
"Running 'xdot.py' program... ";
215 ViewerKind Viewer = VK_None;
217 if (!Viewer && S.TryFindProgram(
"open", ViewerPath))
220 if (!Viewer && S.TryFindProgram(
"gv", ViewerPath))
221 Viewer = VK_Ghostview;
222 if (!Viewer && S.TryFindProgram(
"xdg-open", ViewerPath))
225 if (!Viewer && S.TryFindProgram(
"cmd", ViewerPath)) {
226 Viewer = VK_CmdStart;
231 std::string GeneratorPath;
234 S.TryFindProgram(
"dot|fdp|neato|twopi|circo", GeneratorPath))) {
236 Filename + (Viewer == VK_CmdStart ?
".pdf" :
".ps");
238 std::vector<const char *> args;
239 args.push_back(GeneratorPath.c_str());
240 if (Viewer == VK_CmdStart)
241 args.push_back(
"-Tpdf");
243 args.push_back(
"-Tps");
244 args.push_back(
"-Nfontname=Courier");
245 args.push_back(
"-Gsize=7.5,10");
246 args.push_back(Filename.c_str());
247 args.push_back(
"-o");
248 args.push_back(OutputFilename.c_str());
249 args.push_back(
nullptr);
251 errs() <<
"Running '" << GeneratorPath <<
"' program... ";
258 std::string StartArg;
261 args.push_back(ViewerPath.c_str());
264 args.push_back(
"-W");
265 args.push_back(OutputFilename.c_str());
269 args.push_back(OutputFilename.c_str());
272 args.push_back(
"--spartan");
273 args.push_back(OutputFilename.c_str());
276 args.push_back(
"/S");
277 args.push_back(
"/C");
279 (
StringRef(
"start ") + (wait ?
"/WAIT " :
"") + OutputFilename).str();
280 args.push_back(StartArg.c_str());
285 args.push_back(
nullptr);
288 return ExecGraphViewer(ViewerPath, args, OutputFilename, wait, ErrMsg);
292 if (S.TryFindProgram(
"dotty", ViewerPath)) {
293 std::vector<const char *> args;
294 args.push_back(ViewerPath.c_str());
295 args.push_back(Filename.c_str());
296 args.push_back(
nullptr);
302 errs() <<
"Running 'dotty' program... ";
306 errs() <<
"Error: Couldn't find a usable graph viewer program:\n";
307 errs() << S.LogBuffer <<
"\n";
Represents either an error or a value T.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static cl::opt< bool > ViewBackground("view-background", cl::Hidden, cl::desc("Execute graph viewer in the background. Creates tmp file litter."))
static const char * getProgramName(GraphProgram::Name program)
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
ProcessInfo ExecuteNoWait(StringRef Program, const char **args, const char **env=nullptr, const StringRef **redirects=nullptr, unsigned memoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
Similar to ExecuteAndWait, but returns immediately.
static bool ExecGraphViewer(StringRef ExecPath, std::vector< const char * > &args, StringRef Filename, bool wait, std::string &ErrMsg)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
int ExecuteAndWait(StringRef Program, const char **args, const char **env=nullptr, const StringRef **redirects=nullptr, unsigned secondsToWait=0, unsigned memoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
This function executes the program using the arguments provided.
std::string EscapeString(const std::string &Label)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
std::string createGraphFilename(const Twine &Name, int &FD)
StringRef getColorString(unsigned NodeNumber)
Get a color string for this node number.
StringRef str() const
Explicit conversion to StringRef.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
llvm_unreachable("Unknown RegBank!")
bool DisplayGraph(StringRef Filename, bool wait=true, GraphProgram::Name program=GraphProgram::DOT)
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath)
Create a file in the system temporary directory.
ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths=None)
Find the first executable file Name in Paths.
Provides ErrorOr<T> smart pointer.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
StringRef - Represent a constant reference to a string, i.e.