1 //===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements misc. GraphWriter support routines.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/GraphWriter.h"
15 #include "llvm/System/Path.h"
16 #include "llvm/System/Program.h"
17 #include "llvm/Config/config.h"
20 std::string llvm::DOT::EscapeString(const std::string &Label) {
21 std::string Str(Label);
22 for (unsigned i = 0; i != Str.length(); ++i)
25 Str.insert(Str.begin()+i, '\\'); // Escape character...
30 Str.insert(Str.begin()+i, ' '); // Convert to two spaces
35 if (i+1 != Str.length())
37 case 'l': continue; // don't disturb \l
38 case '|': case '{': case '}':
39 Str.erase(Str.begin()+i); continue;
45 Str.insert(Str.begin()+i, '\\'); // Escape character...
46 ++i; // don't infinite loop
54 void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
55 GraphProgram::Name program) {
58 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
60 std::vector<const char*> args;
61 args.push_back(Graphviz.c_str());
62 args.push_back(Filename.c_str());
65 errs() << "Running 'Graphviz' program... ";
66 if (sys::Program::ExecuteAndWait(Graphviz, &args[0],0,0,0,0,&ErrMsg))
67 errs() << "Error viewing graph " << Filename.str() << ": " << ErrMsg
70 Filename.eraseFromDisk();
72 #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
73 HAVE_TWOPI || HAVE_CIRCO))
74 sys::Path PSFilename = Filename;
75 PSFilename.appendSuffix("ps");
79 // Set default grapher
81 prog = sys::Path(LLVM_PATH_CIRCO);
84 prog = sys::Path(LLVM_PATH_TWOPI);
87 prog = sys::Path(LLVM_PATH_NEATO);
90 prog = sys::Path(LLVM_PATH_FDP);
93 prog = sys::Path(LLVM_PATH_DOT);
96 // Find which program the user wants
98 if (program == GraphProgram::DOT)
99 prog = sys::Path(LLVM_PATH_DOT);
102 if (program == GraphProgram::FDP)
103 prog = sys::Path(LLVM_PATH_FDP);
106 if (program == GraphProgram::NEATO)
107 prog = sys::Path(LLVM_PATH_NEATO);
110 if (program == GraphProgram::TWOPI)
111 prog = sys::Path(LLVM_PATH_TWOPI);
114 if (program == GraphProgram::CIRCO)
115 prog = sys::Path(LLVM_PATH_CIRCO);
118 std::vector<const char*> args;
119 args.push_back(prog.c_str());
120 args.push_back("-Tps");
121 args.push_back("-Nfontname=Courier");
122 args.push_back("-Gsize=7.5,10");
123 args.push_back(Filename.c_str());
124 args.push_back("-o");
125 args.push_back(PSFilename.c_str());
128 errs() << "Running '" << prog.str() << "' program... ";
130 if (sys::Program::ExecuteAndWait(prog, &args[0], 0, 0, 0, 0, &ErrMsg)) {
131 errs() << "Error viewing graph " << Filename.str() << ": '"
134 errs() << " done. \n";
136 sys::Path gv(LLVM_PATH_GV);
138 args.push_back(gv.c_str());
139 args.push_back(PSFilename.c_str());
140 args.push_back("-spartan");
145 if (sys::Program::ExecuteAndWait(gv, &args[0],0,0,0,0,&ErrMsg))
146 errs() << "Error viewing graph: " << ErrMsg << "\n";
147 Filename.eraseFromDisk();
148 PSFilename.eraseFromDisk();
151 sys::Program::ExecuteNoWait(gv, &args[0],0,0,0,&ErrMsg);
152 errs() << "Remember to erase graph files: " << Filename.str() << " "
153 << PSFilename.str() << "\n";
157 sys::Path dotty(LLVM_PATH_DOTTY);
159 std::vector<const char*> args;
160 args.push_back(dotty.c_str());
161 args.push_back(Filename.c_str());
164 errs() << "Running 'dotty' program... ";
165 if (sys::Program::ExecuteAndWait(dotty, &args[0],0,0,0,0,&ErrMsg)) {
166 errs() << "Error viewing graph " << Filename.str() << ": "
169 #ifdef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
172 Filename.eraseFromDisk();