17 #include "llvm/Config/config.h" 26 #include <system_error> 33 cl::desc(
"Execute graph viewer in the background. Creates tmp file litter."));
36 std::string Str(Label);
37 for (
unsigned i = 0; i != Str.length(); ++i)
40 Str.insert(Str.begin()+i,
'\\');
45 Str.insert(Str.begin()+i,
' ');
50 if (i+1 != Str.length())
53 case '|':
case '{':
case '}':
54 Str.erase(Str.begin()+i);
continue;
61 Str.insert(Str.begin()+i,
'\\');
71 static const int NumColors = 20;
72 static const char* Colors[NumColors] = {
73 "aaaaaa",
"aa0000",
"00aa00",
"aa5500",
"0055ff",
"aa00aa",
"00aaaa",
74 "555555",
"ff5555",
"55ff55",
"ffff55",
"5555ff",
"ff55ff",
"55ffff",
75 "ffaaaa",
"aaffaa",
"ffffaa",
"aaaaff",
"ffaaff",
"aaffff"};
76 return Colors[ColorNumber % NumColors];
84 errs() <<
"Error: " << EC.message() <<
"\n";
88 errs() <<
"Writing '" << Filename <<
"'... ";
89 return Filename.
str();
95 std::string &ErrMsg) {
98 errs() <<
"Error: " << ErrMsg <<
"\n";
102 errs() <<
" done. \n";
105 errs() <<
"Remember to erase graph file: " << Filename <<
"\n";
112 struct GraphSession {
113 std::string LogBuffer;
115 bool TryFindProgram(
StringRef Names, std::string &ProgramPath) {
118 Names.
split(parts,
'|');
119 for (
auto Name : parts) {
124 Log <<
" Tried '" <<
Name <<
"'\n";
150 std::string Filename = FilenameRef;
152 std::string ViewerPath;
157 if (S.TryFindProgram(
"open", ViewerPath)) {
158 std::vector<StringRef> args;
159 args.push_back(ViewerPath);
161 args.push_back(
"-W");
162 args.push_back(Filename);
163 errs() <<
"Trying 'open' program... ";
168 if (S.TryFindProgram(
"xdg-open", ViewerPath)) {
169 std::vector<StringRef> args;
170 args.push_back(ViewerPath);
171 args.push_back(Filename);
172 errs() <<
"Trying 'xdg-open' program... ";
178 if (S.TryFindProgram(
"Graphviz", ViewerPath)) {
179 std::vector<StringRef> args;
180 args.push_back(ViewerPath);
181 args.push_back(Filename);
183 errs() <<
"Running 'Graphviz' program... ";
188 if (S.TryFindProgram(
"xdot|xdot.py", ViewerPath)) {
189 std::vector<StringRef> args;
190 args.push_back(ViewerPath);
191 args.push_back(Filename);
193 args.push_back(
"-f");
196 errs() <<
"Running 'xdot.py' program... ";
207 ViewerKind Viewer = VK_None;
209 if (!Viewer && S.TryFindProgram(
"open", ViewerPath))
212 if (!Viewer && S.TryFindProgram(
"gv", ViewerPath))
213 Viewer = VK_Ghostview;
214 if (!Viewer && S.TryFindProgram(
"xdg-open", ViewerPath))
217 if (!Viewer && S.TryFindProgram(
"cmd", ViewerPath)) {
218 Viewer = VK_CmdStart;
223 std::string GeneratorPath;
226 S.TryFindProgram(
"dot|fdp|neato|twopi|circo", GeneratorPath))) {
228 Filename + (Viewer == VK_CmdStart ?
".pdf" :
".ps");
230 std::vector<StringRef> args;
231 args.push_back(GeneratorPath);
232 if (Viewer == VK_CmdStart)
233 args.push_back(
"-Tpdf");
235 args.push_back(
"-Tps");
236 args.push_back(
"-Nfontname=Courier");
237 args.push_back(
"-Gsize=7.5,10");
238 args.push_back(Filename);
239 args.push_back(
"-o");
240 args.push_back(OutputFilename);
242 errs() <<
"Running '" << GeneratorPath <<
"' program... ";
249 std::string StartArg;
252 args.push_back(ViewerPath);
255 args.push_back(
"-W");
256 args.push_back(OutputFilename);
260 args.push_back(OutputFilename);
263 args.push_back(
"--spartan");
264 args.push_back(OutputFilename);
267 args.push_back(
"/S");
268 args.push_back(
"/C");
270 (
StringRef(
"start ") + (wait ?
"/WAIT " :
"") + OutputFilename).str();
271 args.push_back(StartArg);
278 return ExecGraphViewer(ViewerPath, args, OutputFilename, wait, ErrMsg);
282 if (S.TryFindProgram(
"dotty", ViewerPath)) {
283 std::vector<StringRef> args;
284 args.push_back(ViewerPath);
285 args.push_back(Filename);
291 errs() <<
"Running 'dotty' program... ";
295 errs() <<
"Error: Couldn't find a usable graph viewer program:\n";
296 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)
This class represents lattice values for constants.
ProcessInfo ExecuteNoWait(StringRef Program, ArrayRef< StringRef > Args, Optional< ArrayRef< StringRef >> Env, ArrayRef< Optional< StringRef >> Redirects={}, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
Similar to ExecuteAndWait, but returns immediately.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
ErrorOr< std::string > findProgramByName(StringRef Name, ArrayRef< StringRef > Paths={})
Find the first executable file Name in Paths.
int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, Optional< ArrayRef< StringRef >> Env=None, ArrayRef< Optional< StringRef >> Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr)
This function executes the program using the arguments provided.
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("-"))
StringRef str() const
Explicit conversion to StringRef.
std::string EscapeString(const std::string &Label)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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)
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
StringRef getColorString(unsigned NodeNumber)
Get a color string for this node number.
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.
Provides ErrorOr<T> smart pointer.
static bool ExecGraphViewer(StringRef ExecPath, std::vector< StringRef > &args, StringRef Filename, bool wait, std::string &ErrMsg)
A raw_ostream that writes to an std::string.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
StringRef - Represent a constant reference to a string, i.e.