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/CommandLine.h" 15 #include "llvm/Support/GraphWriter.h" 16 #include "llvm/Support/Path.h" 17 #include "llvm/Support/Program.h" 18 #include "llvm/Config/config.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 // Execute the graph viewer. Return true if successful. 57 static bool LLVM_ATTRIBUTE_UNUSED 58 ExecGraphViewer(const sys::Path &ExecPath, std::vector<const char*> &args, 59 const sys::Path &Filename, bool wait, std::string &ErrMsg) { 60 if (wait) { 61 if (sys::Program::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) { 62 errs() << "Error: " << ErrMsg << "\n"; 63 return false; 64 } 65 Filename.eraseFromDisk(); 66 errs() << " done. \n"; 67 } 68 else { 69 sys::Program::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg); 70 errs() << "Remember to erase graph file: " << Filename.str() << "\n"; 71 } 72 return true; 73 } 74 75 void llvm::DisplayGraph(const sys::Path &Filename, bool wait, 76 GraphProgram::Name program) { 77 wait &= !ViewBackground; 78 std::string ErrMsg; 79 #if HAVE_GRAPHVIZ 80 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ); 81 82 std::vector<const char*> args; 83 args.push_back(Graphviz.c_str()); 84 args.push_back(Filename.c_str()); 85 args.push_back(0); 86 87 errs() << "Running 'Graphviz' program... "; 88 if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg)) 89 return; 90 91 #elif HAVE_XDOT_PY 92 std::vector<const char*> args; 93 args.push_back(LLVM_PATH_XDOT_PY); 94 args.push_back(Filename.c_str()); 95 96 switch (program) { 97 case GraphProgram::DOT: args.push_back("-f"); args.push_back("dot"); break; 98 case GraphProgram::FDP: args.push_back("-f"); args.push_back("fdp"); break; 99 case GraphProgram::NEATO: args.push_back("-f"); args.push_back("neato");break; 100 case GraphProgram::TWOPI: args.push_back("-f"); args.push_back("twopi");break; 101 case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break; 102 } 103 104 args.push_back(0); 105 106 errs() << "Running 'xdot.py' program... "; 107 if (!ExecGraphViewer(sys::Path(LLVM_PATH_XDOT_PY), args, Filename, wait, ErrMsg)) 108 return; 109 110 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \ 111 HAVE_TWOPI || HAVE_CIRCO)) 112 sys::Path PSFilename = Filename; 113 PSFilename.appendSuffix("ps"); 114 115 sys::Path prog; 116 117 // Set default grapher 118 #if HAVE_CIRCO 119 prog = sys::Path(LLVM_PATH_CIRCO); 120 #endif 121 #if HAVE_TWOPI 122 prog = sys::Path(LLVM_PATH_TWOPI); 123 #endif 124 #if HAVE_NEATO 125 prog = sys::Path(LLVM_PATH_NEATO); 126 #endif 127 #if HAVE_FDP 128 prog = sys::Path(LLVM_PATH_FDP); 129 #endif 130 #if HAVE_DOT 131 prog = sys::Path(LLVM_PATH_DOT); 132 #endif 133 134 // Find which program the user wants 135 #if HAVE_DOT 136 if (program == GraphProgram::DOT) 137 prog = sys::Path(LLVM_PATH_DOT); 138 #endif 139 #if (HAVE_FDP) 140 if (program == GraphProgram::FDP) 141 prog = sys::Path(LLVM_PATH_FDP); 142 #endif 143 #if (HAVE_NEATO) 144 if (program == GraphProgram::NEATO) 145 prog = sys::Path(LLVM_PATH_NEATO); 146 #endif 147 #if (HAVE_TWOPI) 148 if (program == GraphProgram::TWOPI) 149 prog = sys::Path(LLVM_PATH_TWOPI); 150 #endif 151 #if (HAVE_CIRCO) 152 if (program == GraphProgram::CIRCO) 153 prog = sys::Path(LLVM_PATH_CIRCO); 154 #endif 155 156 std::vector<const char*> args; 157 args.push_back(prog.c_str()); 158 args.push_back("-Tps"); 159 args.push_back("-Nfontname=Courier"); 160 args.push_back("-Gsize=7.5,10"); 161 args.push_back(Filename.c_str()); 162 args.push_back("-o"); 163 args.push_back(PSFilename.c_str()); 164 args.push_back(0); 165 166 errs() << "Running '" << prog.str() << "' program... "; 167 168 if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg)) 169 return; 170 171 sys::Path gv(LLVM_PATH_GV); 172 args.clear(); 173 args.push_back(gv.c_str()); 174 args.push_back(PSFilename.c_str()); 175 args.push_back("--spartan"); 176 args.push_back(0); 177 178 ErrMsg.clear(); 179 if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg)) 180 return; 181 182 #elif HAVE_DOTTY 183 sys::Path dotty(LLVM_PATH_DOTTY); 184 185 std::vector<const char*> args; 186 args.push_back(dotty.c_str()); 187 args.push_back(Filename.c_str()); 188 args.push_back(0); 189 190 // Dotty spawns another app and doesn't wait until it returns 191 #if defined (__MINGW32__) || defined (_WINDOWS) 192 wait = false; 193 #endif 194 errs() << "Running 'dotty' program... "; 195 if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg)) 196 return; 197 #endif 198 } 199