1 //===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements misc. GraphWriter support routines. 11 // 12 //===----------------------------------------------------------------------===// 13 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" 20 using namespace llvm; 21 22 static cl::opt<bool> ViewBackground("view-background", cl::Hidden, 23 cl::desc("Execute graph viewer in the background. Creates tmp file litter.")); 24 25 std::string llvm::DOT::EscapeString(const std::string &Label) { 26 std::string Str(Label); 27 for (unsigned i = 0; i != Str.length(); ++i) 28 switch (Str[i]) { 29 case '\n': 30 Str.insert(Str.begin()+i, '\\'); // Escape character... 31 ++i; 32 Str[i] = 'n'; 33 break; 34 case '\t': 35 Str.insert(Str.begin()+i, ' '); // Convert to two spaces 36 ++i; 37 Str[i] = ' '; 38 break; 39 case '\\': 40 if (i+1 != Str.length()) 41 switch (Str[i+1]) { 42 case 'l': continue; // don't disturb \l 43 case '|': case '{': case '}': 44 Str.erase(Str.begin()+i); continue; 45 default: break; 46 } 47 case '{': case '}': 48 case '<': case '>': 49 case '|': case '"': 50 Str.insert(Str.begin()+i, '\\'); // Escape character... 51 ++i; // don't infinite loop 52 break; 53 } 54 return Str; 55 } 56 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]; 66 } 67 68 std::string llvm::createGraphFilename(const Twine &Name, int &FD) { 69 FD = -1; 70 SmallString<128> Filename; 71 std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename); 72 if (EC) { 73 errs() << "Error: " << EC.message() << "\n"; 74 return ""; 75 } 76 77 errs() << "Writing '" << Filename << "'... "; 78 return Filename.str(); 79 } 80 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); 86 if (wait) { 87 if (sys::ExecuteAndWait(ExecPath, args.data(), nullptr, nullptr, 0, 0, 88 &ErrMsg)) { 89 errs() << "Error: " << ErrMsg << "\n"; 90 return true; 91 } 92 sys::fs::remove(Filename); 93 errs() << " done. \n"; 94 } else { 95 sys::ExecuteNoWait(ExecPath, args.data(), nullptr, nullptr, 0, &ErrMsg); 96 errs() << "Remember to erase graph file: " << Filename.str() << "\n"; 97 } 98 return false; 99 } 100 101 namespace { 102 struct GraphSession { 103 std::string LogBuffer; 104 bool TryFindProgram(StringRef Names, std::string &ProgramPath) { 105 raw_string_ostream Log(LogBuffer); 106 SmallVector<StringRef, 8> parts; 107 Names.split(parts, "|"); 108 for (auto Name : parts) { 109 if (ErrorOr<std::string> P = sys::findProgramByName(Name)) { 110 ProgramPath = *P; 111 return true; 112 } 113 Log << " Tried '" << Name << "'\n"; 114 } 115 return false; 116 } 117 }; 118 } // namespace 119 120 static const char *getProgramName(GraphProgram::Name program) { 121 switch (program) { 122 case GraphProgram::DOT: 123 return "dot"; 124 case GraphProgram::FDP: 125 return "fdp"; 126 case GraphProgram::NEATO: 127 return "neato"; 128 case GraphProgram::TWOPI: 129 return "twopi"; 130 case GraphProgram::CIRCO: 131 return "circo"; 132 } 133 llvm_unreachable("bad kind"); 134 } 135 136 bool llvm::DisplayGraph(StringRef FilenameRef, bool wait, 137 GraphProgram::Name program) { 138 std::string Filename = FilenameRef; 139 wait &= !ViewBackground; 140 std::string ErrMsg; 141 std::string ViewerPath; 142 GraphSession S; 143 144 // Graphviz 145 if (S.TryFindProgram("Graphviz", ViewerPath)) { 146 std::vector<const char *> args; 147 args.push_back(ViewerPath.c_str()); 148 args.push_back(Filename.c_str()); 149 args.push_back(nullptr); 150 151 errs() << "Running 'Graphviz' program... "; 152 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg); 153 } 154 155 // xdot 156 if (S.TryFindProgram("xdot|xdot.py", ViewerPath)) { 157 std::vector<const char *> args; 158 args.push_back(ViewerPath.c_str()); 159 args.push_back(Filename.c_str()); 160 161 args.push_back("-f"); 162 args.push_back(getProgramName(program)); 163 164 args.push_back(nullptr); 165 166 errs() << "Running 'xdot.py' program... "; 167 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg); 168 } 169 170 enum PSViewerKind { PSV_None, PSV_OSXOpen, PSV_XDGOpen, PSV_Ghostview }; 171 PSViewerKind PSViewer = PSV_None; 172 #ifdef __APPLE__ 173 if (!PSViewer && S.TryFindProgram("open", ViewerPath)) 174 PSViewer = PSV_OSXOpen; 175 #endif 176 if (!PSViewer && S.TryFindProgram("gv", ViewerPath)) 177 PSViewer = PSV_Ghostview; 178 if (!PSViewer && S.TryFindProgram("xdg-open", ViewerPath)) 179 PSViewer = PSV_XDGOpen; 180 181 // PostScript graph generator + PostScript viewer 182 std::string GeneratorPath; 183 if (PSViewer && 184 (S.TryFindProgram(getProgramName(program), GeneratorPath) || 185 S.TryFindProgram("dot|fdp|neato|twopi|circo", GeneratorPath))) { 186 std::string PSFilename = Filename + ".ps"; 187 188 std::vector<const char *> args; 189 args.push_back(GeneratorPath.c_str()); 190 args.push_back("-Tps"); 191 args.push_back("-Nfontname=Courier"); 192 args.push_back("-Gsize=7.5,10"); 193 args.push_back(Filename.c_str()); 194 args.push_back("-o"); 195 args.push_back(PSFilename.c_str()); 196 args.push_back(nullptr); 197 198 errs() << "Running '" << GeneratorPath << "' program... "; 199 200 if (ExecGraphViewer(GeneratorPath, args, Filename, wait, ErrMsg)) 201 return true; 202 203 args.clear(); 204 args.push_back(ViewerPath.c_str()); 205 switch (PSViewer) { 206 case PSV_OSXOpen: 207 args.push_back("-W"); 208 args.push_back(PSFilename.c_str()); 209 break; 210 case PSV_XDGOpen: 211 wait = false; 212 args.push_back(PSFilename.c_str()); 213 break; 214 case PSV_Ghostview: 215 args.push_back("--spartan"); 216 args.push_back(PSFilename.c_str()); 217 break; 218 case PSV_None: 219 llvm_unreachable("Invalid viewer"); 220 } 221 args.push_back(nullptr); 222 223 ErrMsg.clear(); 224 return ExecGraphViewer(ViewerPath, args, PSFilename, wait, ErrMsg); 225 } 226 227 // dotty 228 if (S.TryFindProgram("dotty", ViewerPath)) { 229 std::vector<const char *> args; 230 args.push_back(ViewerPath.c_str()); 231 args.push_back(Filename.c_str()); 232 args.push_back(nullptr); 233 234 // Dotty spawns another app and doesn't wait until it returns 235 #ifdef LLVM_ON_WIN32 236 wait = false; 237 #endif 238 errs() << "Running 'dotty' program... "; 239 return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg); 240 } 241 242 errs() << "Error: Couldn't find a usable graph viewer program:\n"; 243 errs() << S.LogBuffer << "\n"; 244 return true; 245 } 246