20#include "llvm/Config/config.h"
34#include <system_error>
41struct CreateViewBackground {
44 cl::desc(
"Execute graph viewer in the background. "
45 "Creates tmp file litter."));
56 std::string Str(Label);
57 for (
unsigned i = 0; i != Str.length(); ++i)
60 Str.insert(Str.begin()+i,
'\\');
65 Str.insert(Str.begin()+i,
' ');
70 if (i+1 != Str.length())
73 case '|':
case '{':
case '}':
74 Str.erase(Str.begin()+i);
continue;
81 Str.insert(Str.begin()+i,
'\\');
91 static const int NumColors = 20;
92 static const char* Colors[NumColors] = {
93 "aaaaaa",
"aa0000",
"00aa00",
"aa5500",
"0055ff",
"aa00aa",
"00aaaa",
94 "555555",
"ff5555",
"55ff55",
"ffff55",
"5555ff",
"ff55ff",
"55ffff",
95 "ffaaaa",
"aaffaa",
"ffffaa",
"aaaaff",
"ffaaff",
"aaffff"};
96 return Colors[ColorNumber % NumColors];
100 const char ReplacementChar) {
101 std::string IllegalChars =
102 is_style_windows(sys::path::Style::native) ?
"\\/:?\"<>|" :
"/";
104 for (
char IllegalChar : IllegalChars) {
105 std::replace(Filename.begin(), Filename.end(), IllegalChar,
117 std::string
N =
Name.str();
127 errs() <<
"Error: " << EC.message() <<
"\n";
131 errs() <<
"Writing '" << Filename <<
"'... ";
132 return std::string(Filename);
138 std::string &ErrMsg) {
141 errs() <<
"Error: " << ErrMsg <<
"\n";
145 errs() <<
" done. \n";
148 errs() <<
"Remember to erase graph file: " << Filename <<
"\n";
156 std::string LogBuffer;
158 bool TryFindProgram(
StringRef Names, std::string &ProgramPath) {
161 Names.
split(parts,
'|');
162 for (
auto Name : parts) {
167 Log <<
" Tried '" <<
Name <<
"'\n";
193 std::string Filename = std::string(FilenameRef);
195 std::string ViewerPath;
199 wait &= !*ViewBackground;
200 if (S.TryFindProgram(
"open", ViewerPath)) {
201 std::vector<StringRef>
args;
202 args.push_back(ViewerPath);
204 args.push_back(
"-W");
205 args.push_back(Filename);
206 errs() <<
"Trying 'open' program... ";
211 if (S.TryFindProgram(
"xdg-open", ViewerPath)) {
212 std::vector<StringRef>
args;
213 args.push_back(ViewerPath);
214 args.push_back(Filename);
215 errs() <<
"Trying 'xdg-open' program... ";
221 if (S.TryFindProgram(
"Graphviz", ViewerPath)) {
222 std::vector<StringRef>
args;
223 args.push_back(ViewerPath);
224 args.push_back(Filename);
226 errs() <<
"Running 'Graphviz' program... ";
231 if (S.TryFindProgram(
"xdot|xdot.py", ViewerPath)) {
232 std::vector<StringRef>
args;
233 args.push_back(ViewerPath);
234 args.push_back(Filename);
236 args.push_back(
"-f");
239 errs() <<
"Running 'xdot.py' program... ";
250 ViewerKind Viewer = VK_None;
252 if (!Viewer && S.TryFindProgram(
"open", ViewerPath))
255 if (!Viewer && S.TryFindProgram(
"gv", ViewerPath))
256 Viewer = VK_Ghostview;
257 if (!Viewer && S.TryFindProgram(
"xdg-open", ViewerPath))
260 if (!Viewer && S.TryFindProgram(
"cmd", ViewerPath)) {
261 Viewer = VK_CmdStart;
266 std::string GeneratorPath;
269 S.TryFindProgram(
"dot|fdp|neato|twopi|circo", GeneratorPath))) {
271 Filename + (Viewer == VK_CmdStart ?
".pdf" :
".ps");
273 std::vector<StringRef>
args;
274 args.push_back(GeneratorPath);
275 if (Viewer == VK_CmdStart)
276 args.push_back(
"-Tpdf");
278 args.push_back(
"-Tps");
279 args.push_back(
"-Nfontname=Courier");
280 args.push_back(
"-Gsize=7.5,10");
281 args.push_back(Filename);
282 args.push_back(
"-o");
285 errs() <<
"Running '" << GeneratorPath <<
"' program... ";
292 std::string StartArg;
295 args.push_back(ViewerPath);
298 args.push_back(
"-W");
306 args.push_back(
"--spartan");
310 args.push_back(
"/S");
311 args.push_back(
"/C");
314 args.push_back(StartArg);
325 if (S.TryFindProgram(
"dotty", ViewerPath)) {
326 std::vector<StringRef>
args;
327 args.push_back(ViewerPath);
328 args.push_back(Filename);
334 errs() <<
"Running 'dotty' program... ";
338 errs() <<
"Error: Couldn't find a usable graph viewer program:\n";
339 errs() << S.LogBuffer <<
"\n";
Provides ErrorOr<T> smart pointer.
static std::string replaceIllegalFilenameChars(std::string Filename, const char ReplacementChar)
static bool ExecGraphViewer(StringRef ExecPath, std::vector< StringRef > &args, StringRef Filename, bool wait, std::string &ErrMsg)
static const char * getProgramName(GraphProgram::Name program)
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
This file defines the SmallString class.
This file defines the SmallVector class.
Represents either an error or a value T.
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::string EscapeString(const std::string &Label)
StringRef getColorString(unsigned NodeNumber)
Get a color string for this node number.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath, OpenFlags Flags=OF_None)
Create a file in the system temporary directory.
ProcessInfo ExecuteNoWait(StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env, ArrayRef< std::optional< StringRef > > Redirects={}, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, BitVector *AffinityMask=nullptr, bool DetachProcess=false)
Similar to ExecuteAndWait, but returns immediately.
int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, std::optional< ArrayRef< StringRef > > Env=std::nullopt, ArrayRef< std::optional< StringRef > > Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, std::optional< ProcessStatistics > *ProcStat=nullptr, BitVector *AffinityMask=nullptr)
This function executes the program using the arguments provided.
ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths={})
Find the first executable file Name in Paths.
This is an optimization pass for GlobalISel generic memory operations.
bool DisplayGraph(StringRef Filename, bool wait=true, GraphProgram::Name program=GraphProgram::DOT)
std::string createGraphFilename(const Twine &Name, int &FD)
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void initGraphWriterOptions()