20#include "llvm/Config/config.h"
35#include <system_error>
42struct CreateViewBackground {
45 cl::desc(
"Execute graph viewer in the background. "
46 "Creates tmp file litter."));
57 std::string Str(Label);
58 for (
unsigned i = 0; i != Str.length(); ++i)
61 Str.insert(Str.begin()+i,
'\\');
66 Str.insert(Str.begin()+i,
' ');
71 if (i+1 != Str.length())
74 case '|':
case '{':
case '}':
75 Str.erase(Str.begin()+i);
continue;
82 Str.insert(Str.begin()+i,
'\\');
92 static const int NumColors = 20;
93 static const char* Colors[NumColors] = {
94 "aaaaaa",
"aa0000",
"00aa00",
"aa5500",
"0055ff",
"aa00aa",
"00aaaa",
95 "555555",
"ff5555",
"55ff55",
"ffff55",
"5555ff",
"ff55ff",
"55ffff",
96 "ffaaaa",
"aaffaa",
"ffffaa",
"aaaaff",
"ffaaff",
"aaffff"};
97 return Colors[ColorNumber % NumColors];
101 const char ReplacementChar) {
102 std::string IllegalChars =
103 is_style_windows(sys::path::Style::native) ?
"\\/:?\"<>|" :
"/";
105 for (
char IllegalChar : IllegalChars) {
106 std::replace(Filename.begin(), Filename.end(), IllegalChar,
118 std::string
N =
Name.str();
128 errs() <<
"Error: " << EC.message() <<
"\n";
132 errs() <<
"Writing '" << Filename <<
"'... ";
133 return std::string(Filename);
139 std::string &ErrMsg) {
142 errs() <<
"Error: " << ErrMsg <<
"\n";
146 errs() <<
" done. \n";
149 errs() <<
"Remember to erase graph file: " << Filename <<
"\n";
157 std::string LogBuffer;
159 bool TryFindProgram(
StringRef Names, std::string &ProgramPath) {
162 Names.
split(parts,
'|');
163 for (
auto Name : parts) {
168 Log <<
" Tried '" <<
Name <<
"'\n";
194 std::string Filename = std::string(FilenameRef);
196 std::string ViewerPath;
200 wait &= !*ViewBackground;
201 if (S.TryFindProgram(
"open", ViewerPath)) {
202 std::vector<StringRef>
args;
203 args.push_back(ViewerPath);
205 args.push_back(
"-W");
206 args.push_back(Filename);
207 errs() <<
"Trying 'open' program... ";
212 if (S.TryFindProgram(
"xdg-open", ViewerPath)) {
213 std::vector<StringRef>
args;
214 args.push_back(ViewerPath);
215 args.push_back(Filename);
216 errs() <<
"Trying 'xdg-open' program... ";
222 if (S.TryFindProgram(
"Graphviz", ViewerPath)) {
223 std::vector<StringRef>
args;
224 args.push_back(ViewerPath);
225 args.push_back(Filename);
227 errs() <<
"Running 'Graphviz' program... ";
232 if (S.TryFindProgram(
"xdot|xdot.py", ViewerPath)) {
233 std::vector<StringRef>
args;
234 args.push_back(ViewerPath);
235 args.push_back(Filename);
237 args.push_back(
"-f");
240 errs() <<
"Running 'xdot.py' program... ";
251 ViewerKind Viewer = VK_None;
253 if (!Viewer && S.TryFindProgram(
"open", ViewerPath))
256 if (!Viewer && S.TryFindProgram(
"gv", ViewerPath))
257 Viewer = VK_Ghostview;
258 if (!Viewer && S.TryFindProgram(
"xdg-open", ViewerPath))
261 if (!Viewer && S.TryFindProgram(
"cmd", ViewerPath)) {
262 Viewer = VK_CmdStart;
267 std::string GeneratorPath;
270 S.TryFindProgram(
"dot|fdp|neato|twopi|circo", GeneratorPath))) {
272 Filename + (Viewer == VK_CmdStart ?
".pdf" :
".ps");
274 std::vector<StringRef>
args;
275 args.push_back(GeneratorPath);
276 if (Viewer == VK_CmdStart)
277 args.push_back(
"-Tpdf");
279 args.push_back(
"-Tps");
280 args.push_back(
"-Nfontname=Courier");
281 args.push_back(
"-Gsize=7.5,10");
282 args.push_back(Filename);
283 args.push_back(
"-o");
286 errs() <<
"Running '" << GeneratorPath <<
"' program... ";
293 std::string StartArg;
296 args.push_back(ViewerPath);
299 args.push_back(
"-W");
307 args.push_back(
"--spartan");
311 args.push_back(
"/S");
312 args.push_back(
"/C");
315 args.push_back(StartArg);
326 if (S.TryFindProgram(
"dotty", ViewerPath)) {
327 std::vector<StringRef>
args;
328 args.push_back(ViewerPath);
329 args.push_back(Filename);
335 errs() <<
"Running 'dotty' program... ";
339 errs() <<
"Error: Couldn't find a usable graph viewer program:\n";
340 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()