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