2006-06-28 00:49:46 +08:00
|
|
|
//===-- GraphWriter.cpp - Implements GraphWriter support routines ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-28 00:49:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements misc. GraphWriter support routines.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-07-29 06:21:01 +08:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Config/config.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-06-14 01:20:48 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/Program.h"
|
2006-06-28 00:49:46 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2012-03-07 08:18:27 +08:00
|
|
|
static cl::opt<bool> ViewBackground("view-background", cl::Hidden,
|
|
|
|
cl::desc("Execute graph viewer in the background. Creates tmp file litter."));
|
|
|
|
|
2009-08-23 15:19:13 +08:00
|
|
|
std::string llvm::DOT::EscapeString(const std::string &Label) {
|
|
|
|
std::string Str(Label);
|
|
|
|
for (unsigned i = 0; i != Str.length(); ++i)
|
|
|
|
switch (Str[i]) {
|
|
|
|
case '\n':
|
|
|
|
Str.insert(Str.begin()+i, '\\'); // Escape character...
|
|
|
|
++i;
|
|
|
|
Str[i] = 'n';
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
Str.insert(Str.begin()+i, ' '); // Convert to two spaces
|
|
|
|
++i;
|
|
|
|
Str[i] = ' ';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
if (i+1 != Str.length())
|
|
|
|
switch (Str[i+1]) {
|
|
|
|
case 'l': continue; // don't disturb \l
|
|
|
|
case '|': case '{': case '}':
|
|
|
|
Str.erase(Str.begin()+i); continue;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
case '{': case '}':
|
|
|
|
case '<': case '>':
|
|
|
|
case '|': case '"':
|
|
|
|
Str.insert(Str.begin()+i, '\\'); // Escape character...
|
|
|
|
++i; // don't infinite loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Str;
|
|
|
|
}
|
|
|
|
|
2013-01-25 15:45:25 +08:00
|
|
|
/// \brief Get a color string for this node number. Simply round-robin selects
|
|
|
|
/// from a reasonable number of colors.
|
|
|
|
StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
|
|
|
|
static const int NumColors = 20;
|
|
|
|
static const char* Colors[NumColors] = {
|
|
|
|
"aaaaaa", "aa0000", "00aa00", "aa5500", "0055ff", "aa00aa", "00aaaa",
|
|
|
|
"555555", "ff5555", "55ff55", "ffff55", "5555ff", "ff55ff", "55ffff",
|
|
|
|
"ffaaaa", "aaffaa", "ffffaa", "aaaaff", "ffaaff", "aaffff"};
|
|
|
|
return Colors[ColorNumber % NumColors];
|
|
|
|
}
|
|
|
|
|
2013-06-15 00:43:15 +08:00
|
|
|
std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
|
|
|
|
FD = -1;
|
|
|
|
SmallString<128> Filename;
|
|
|
|
error_code EC = sys::fs::unique_file(Twine(Name) + "-%%%%%%%.dot",
|
|
|
|
FD, Filename);
|
|
|
|
if (EC) {
|
|
|
|
errs() << "Error: " << EC.message() << "\n";
|
2013-06-14 01:20:48 +08:00
|
|
|
return "";
|
|
|
|
}
|
2013-06-15 00:43:15 +08:00
|
|
|
|
|
|
|
errs() << "Writing '" << Filename << "'... ";
|
2013-06-14 01:20:48 +08:00
|
|
|
return Filename.str();
|
|
|
|
}
|
|
|
|
|
2012-03-07 08:18:27 +08:00
|
|
|
// Execute the graph viewer. Return true if successful.
|
2012-03-09 06:15:23 +08:00
|
|
|
static bool LLVM_ATTRIBUTE_UNUSED
|
2013-06-14 01:20:48 +08:00
|
|
|
ExecGraphViewer(StringRef ExecPath, std::vector<const char*> &args,
|
|
|
|
StringRef Filename, bool wait, std::string &ErrMsg) {
|
2012-03-07 08:18:27 +08:00
|
|
|
if (wait) {
|
2013-06-14 04:25:38 +08:00
|
|
|
if (sys::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) {
|
2012-03-07 08:18:27 +08:00
|
|
|
errs() << "Error: " << ErrMsg << "\n";
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-14 01:20:48 +08:00
|
|
|
bool Existed;
|
|
|
|
sys::fs::remove(Filename, Existed);
|
2012-03-07 08:18:27 +08:00
|
|
|
errs() << " done. \n";
|
|
|
|
}
|
|
|
|
else {
|
2013-06-14 04:25:38 +08:00
|
|
|
sys::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg);
|
2012-03-07 08:18:27 +08:00
|
|
|
errs() << "Remember to erase graph file: " << Filename.str() << "\n";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-23 15:19:13 +08:00
|
|
|
|
2013-06-14 01:27:45 +08:00
|
|
|
void llvm::DisplayGraph(StringRef FilenameRef, bool wait,
|
2009-07-10 01:06:18 +08:00
|
|
|
GraphProgram::Name program) {
|
2013-06-14 01:27:45 +08:00
|
|
|
std::string Filename = FilenameRef;
|
2012-03-07 08:18:27 +08:00
|
|
|
wait &= !ViewBackground;
|
2006-08-21 14:04:45 +08:00
|
|
|
std::string ErrMsg;
|
2006-06-28 00:49:46 +08:00
|
|
|
#if HAVE_GRAPHVIZ
|
2013-06-14 01:32:16 +08:00
|
|
|
std::string Graphviz(LLVM_PATH_GRAPHVIZ);
|
2006-06-28 00:49:46 +08:00
|
|
|
|
|
|
|
std::vector<const char*> args;
|
|
|
|
args.push_back(Graphviz.c_str());
|
|
|
|
args.push_back(Filename.c_str());
|
|
|
|
args.push_back(0);
|
2012-03-07 08:18:27 +08:00
|
|
|
|
2009-08-23 15:19:13 +08:00
|
|
|
errs() << "Running 'Graphviz' program... ";
|
2012-03-07 08:18:27 +08:00
|
|
|
if (!ExecGraphViewer(Graphviz, args, Filename, wait, ErrMsg))
|
2010-10-05 23:30:27 +08:00
|
|
|
return;
|
2009-07-10 01:06:18 +08:00
|
|
|
|
2010-09-28 00:28:34 +08:00
|
|
|
#elif HAVE_XDOT_PY
|
|
|
|
std::vector<const char*> args;
|
|
|
|
args.push_back(LLVM_PATH_XDOT_PY);
|
|
|
|
args.push_back(Filename.c_str());
|
|
|
|
|
|
|
|
switch (program) {
|
|
|
|
case GraphProgram::DOT: args.push_back("-f"); args.push_back("dot"); break;
|
|
|
|
case GraphProgram::FDP: args.push_back("-f"); args.push_back("fdp"); break;
|
|
|
|
case GraphProgram::NEATO: args.push_back("-f"); args.push_back("neato");break;
|
|
|
|
case GraphProgram::TWOPI: args.push_back("-f"); args.push_back("twopi");break;
|
|
|
|
case GraphProgram::CIRCO: args.push_back("-f"); args.push_back("circo");break;
|
|
|
|
}
|
2012-03-07 08:18:27 +08:00
|
|
|
|
2010-09-28 00:28:34 +08:00
|
|
|
args.push_back(0);
|
|
|
|
|
|
|
|
errs() << "Running 'xdot.py' program... ";
|
2013-06-14 01:32:16 +08:00
|
|
|
if (!ExecGraphViewer(LLVM_PATH_XDOT_PY, args, Filename, wait, ErrMsg))
|
2010-10-05 23:30:27 +08:00
|
|
|
return;
|
2010-09-28 00:28:34 +08:00
|
|
|
|
2009-07-10 01:06:18 +08:00
|
|
|
#elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
|
|
|
|
HAVE_TWOPI || HAVE_CIRCO))
|
2013-06-15 01:11:14 +08:00
|
|
|
std::string PSFilename = Filename + ".ps";
|
2013-06-14 01:20:48 +08:00
|
|
|
std::string prog;
|
2009-07-10 01:06:18 +08:00
|
|
|
|
|
|
|
// Set default grapher
|
|
|
|
#if HAVE_CIRCO
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_CIRCO;
|
2009-07-10 01:06:18 +08:00
|
|
|
#endif
|
|
|
|
#if HAVE_TWOPI
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_TWOPI;
|
2009-07-10 01:06:18 +08:00
|
|
|
#endif
|
|
|
|
#if HAVE_NEATO
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_NEATO;
|
2009-07-10 01:06:18 +08:00
|
|
|
#endif
|
2009-07-09 05:53:41 +08:00
|
|
|
#if HAVE_FDP
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_FDP;
|
2009-07-10 01:06:18 +08:00
|
|
|
#endif
|
|
|
|
#if HAVE_DOT
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_DOT;
|
2009-07-10 01:06:18 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Find which program the user wants
|
|
|
|
#if HAVE_DOT
|
2009-08-24 06:53:53 +08:00
|
|
|
if (program == GraphProgram::DOT)
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_DOT;
|
2009-07-10 01:06:18 +08:00
|
|
|
#endif
|
|
|
|
#if (HAVE_FDP)
|
2009-08-24 06:53:53 +08:00
|
|
|
if (program == GraphProgram::FDP)
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_FDP;
|
2009-07-10 01:06:18 +08:00
|
|
|
#endif
|
|
|
|
#if (HAVE_NEATO)
|
2009-08-24 06:53:53 +08:00
|
|
|
if (program == GraphProgram::NEATO)
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_NEATO;
|
2009-07-10 01:06:18 +08:00
|
|
|
#endif
|
|
|
|
#if (HAVE_TWOPI)
|
2009-08-24 06:53:53 +08:00
|
|
|
if (program == GraphProgram::TWOPI)
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_TWOPI;
|
2009-07-10 01:06:18 +08:00
|
|
|
#endif
|
|
|
|
#if (HAVE_CIRCO)
|
2009-08-24 06:53:53 +08:00
|
|
|
if (program == GraphProgram::CIRCO)
|
2013-06-14 01:20:48 +08:00
|
|
|
prog = LLVM_PATH_CIRCO;
|
2009-07-09 05:53:41 +08:00
|
|
|
#endif
|
2006-06-28 00:49:46 +08:00
|
|
|
|
|
|
|
std::vector<const char*> args;
|
2009-07-09 05:53:41 +08:00
|
|
|
args.push_back(prog.c_str());
|
2006-06-28 00:49:46 +08:00
|
|
|
args.push_back("-Tps");
|
|
|
|
args.push_back("-Nfontname=Courier");
|
|
|
|
args.push_back("-Gsize=7.5,10");
|
2013-06-14 01:27:45 +08:00
|
|
|
args.push_back(Filename.c_str());
|
2006-06-28 00:49:46 +08:00
|
|
|
args.push_back("-o");
|
|
|
|
args.push_back(PSFilename.c_str());
|
|
|
|
args.push_back(0);
|
2012-03-07 08:18:27 +08:00
|
|
|
|
2013-06-14 01:20:48 +08:00
|
|
|
errs() << "Running '" << prog << "' program... ";
|
2009-07-09 05:53:41 +08:00
|
|
|
|
2012-03-07 08:18:27 +08:00
|
|
|
if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
|
2010-04-18 11:35:23 +08:00
|
|
|
return;
|
2006-06-28 00:49:46 +08:00
|
|
|
|
2013-06-14 01:20:48 +08:00
|
|
|
std::string gv(LLVM_PATH_GV);
|
2010-04-18 11:35:23 +08:00
|
|
|
args.clear();
|
|
|
|
args.push_back(gv.c_str());
|
|
|
|
args.push_back(PSFilename.c_str());
|
|
|
|
args.push_back("--spartan");
|
|
|
|
args.push_back(0);
|
2012-03-07 08:18:27 +08:00
|
|
|
|
2010-04-18 11:35:23 +08:00
|
|
|
ErrMsg.clear();
|
2013-06-15 01:11:14 +08:00
|
|
|
if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg))
|
2012-03-07 08:18:27 +08:00
|
|
|
return;
|
|
|
|
|
2006-06-28 00:49:46 +08:00
|
|
|
#elif HAVE_DOTTY
|
2013-06-14 01:32:16 +08:00
|
|
|
std::string dotty(LLVM_PATH_DOTTY);
|
2006-06-28 00:49:46 +08:00
|
|
|
|
|
|
|
std::vector<const char*> args;
|
2007-05-04 02:32:10 +08:00
|
|
|
args.push_back(dotty.c_str());
|
2006-06-28 00:49:46 +08:00
|
|
|
args.push_back(Filename.c_str());
|
|
|
|
args.push_back(0);
|
2012-03-07 08:18:27 +08:00
|
|
|
|
2010-04-13 12:35:39 +08:00
|
|
|
// Dotty spawns another app and doesn't wait until it returns
|
|
|
|
#if defined (__MINGW32__) || defined (_WINDOWS)
|
2012-03-07 08:18:27 +08:00
|
|
|
wait = false;
|
2006-06-28 00:49:46 +08:00
|
|
|
#endif
|
2012-03-07 08:18:27 +08:00
|
|
|
errs() << "Running 'dotty' program... ";
|
|
|
|
if (!ExecGraphViewer(dotty, args, Filename, wait, ErrMsg))
|
|
|
|
return;
|
2006-06-28 00:49:46 +08:00
|
|
|
#endif
|
|
|
|
}
|