Reduce usage of sys::Path in the graph writer.

Now PathV1.h is not needed in GraphWriter.h.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183919 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2013-06-13 17:20:48 +00:00
parent ba0e380ea9
commit 4d39727eae
2 changed files with 52 additions and 49 deletions

View File

@ -26,7 +26,6 @@
#include "llvm/ADT/GraphTraits.h" #include "llvm/ADT/GraphTraits.h"
#include "llvm/Support/DOTGraphTraits.h" #include "llvm/Support/DOTGraphTraits.h"
#include "llvm/Support/Path.h" #include "llvm/Support/Path.h"
#include "llvm/Support/PathV1.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <cassert> #include <cassert>
#include <vector> #include <vector>
@ -51,13 +50,8 @@ namespace GraphProgram {
}; };
} }
void DisplayGraph(const sys::Path& Filename, bool wait=true, GraphProgram::Name program = GraphProgram::DOT); void DisplayGraph(StringRef Filename, bool wait = true,
GraphProgram::Name program = GraphProgram::DOT);
inline void DisplayGraph(StringRef Filename, bool wait = true,
GraphProgram::Name program = GraphProgram::DOT) {
sys::Path P(Filename);
DisplayGraph(P, wait, program);
}
template<typename GraphType> template<typename GraphType>
class GraphWriter { class GraphWriter {
@ -325,22 +319,13 @@ raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
return O; return O;
} }
template<typename GraphType> std::string createGraphFilename(const Twine &Name);
sys::Path WriteGraph(const GraphType &G, const Twine &Name,
bool ShortNames = false, const Twine &Title = "") {
std::string ErrMsg;
sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
if (Filename.isEmpty()) {
errs() << "Error: " << ErrMsg << "\n";
return Filename;
}
Filename.appendComponent((Name + ".dot").str());
if (Filename.makeUnique(true,&ErrMsg)) {
errs() << "Error: " << ErrMsg << "\n";
return sys::Path();
}
errs() << "Writing '" << Filename.str() << "'... "; template <typename GraphType>
std::string WriteGraph(const GraphType &G, const Twine &Name,
bool ShortNames = false, const Twine &Title = "") {
std::string Filename = createGraphFilename(Name);
errs() << "Writing '" << Filename << "'... ";
std::string ErrorInfo; std::string ErrorInfo;
raw_fd_ostream O(Filename.c_str(), ErrorInfo); raw_fd_ostream O(Filename.c_str(), ErrorInfo);
@ -349,8 +334,8 @@ sys::Path WriteGraph(const GraphType &G, const Twine &Name,
llvm::WriteGraph(O, G, ShortNames, Title); llvm::WriteGraph(O, G, ShortNames, Title);
errs() << " done. \n"; errs() << " done. \n";
} else { } else {
errs() << "error opening file '" << Filename.str() << "' for writing!\n"; errs() << "error opening file '" << Filename << "' for writing!\n";
Filename.clear(); return "";
} }
return Filename; return Filename;
@ -363,9 +348,9 @@ template<typename GraphType>
void ViewGraph(const GraphType &G, const Twine &Name, void ViewGraph(const GraphType &G, const Twine &Name,
bool ShortNames = false, const Twine &Title = "", bool ShortNames = false, const Twine &Title = "",
GraphProgram::Name Program = GraphProgram::DOT) { GraphProgram::Name Program = GraphProgram::DOT) {
sys::Path Filename = llvm::WriteGraph(G, Name, ShortNames, Title); std::string Filename = llvm::WriteGraph(G, Name, ShortNames, Title);
if (Filename.isEmpty()) if (Filename.empty())
return; return;
DisplayGraph(Filename, true, Program); DisplayGraph(Filename, true, Program);

View File

@ -14,6 +14,7 @@
#include "llvm/Support/GraphWriter.h" #include "llvm/Support/GraphWriter.h"
#include "llvm/Config/config.h" #include "llvm/Config/config.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h" #include "llvm/Support/Path.h"
#include "llvm/Support/Program.h" #include "llvm/Support/Program.h"
using namespace llvm; using namespace llvm;
@ -64,26 +65,42 @@ StringRef llvm::DOT::getColorString(unsigned ColorNumber) {
return Colors[ColorNumber % NumColors]; return Colors[ColorNumber % NumColors];
} }
std::string llvm::createGraphFilename(const Twine &Name) {
std::string ErrMsg;
sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
if (Filename.isEmpty()) {
errs() << "Error: " << ErrMsg << "\n";
return "";
}
Filename.appendComponent((Name + ".dot").str());
if (Filename.makeUnique(true,&ErrMsg)) {
errs() << "Error: " << ErrMsg << "\n";
return "";
}
return Filename.str();
}
// Execute the graph viewer. Return true if successful. // Execute the graph viewer. Return true if successful.
static bool LLVM_ATTRIBUTE_UNUSED static bool LLVM_ATTRIBUTE_UNUSED
ExecGraphViewer(const sys::Path &ExecPath, std::vector<const char*> &args, ExecGraphViewer(StringRef ExecPath, std::vector<const char*> &args,
const sys::Path &Filename, bool wait, std::string &ErrMsg) { StringRef Filename, bool wait, std::string &ErrMsg) {
if (wait) { if (wait) {
if (sys::ExecuteAndWait(ExecPath, &args[0],0,0,0,0,&ErrMsg)) { if (sys::ExecuteAndWait(sys::Path(ExecPath), &args[0],0,0,0,0,&ErrMsg)) {
errs() << "Error: " << ErrMsg << "\n"; errs() << "Error: " << ErrMsg << "\n";
return false; return false;
} }
Filename.eraseFromDisk(); bool Existed;
sys::fs::remove(Filename, Existed);
errs() << " done. \n"; errs() << " done. \n";
} }
else { else {
sys::ExecuteNoWait(ExecPath, &args[0],0,0,0,&ErrMsg); sys::ExecuteNoWait(sys::Path(ExecPath), &args[0],0,0,0,&ErrMsg);
errs() << "Remember to erase graph file: " << Filename.str() << "\n"; errs() << "Remember to erase graph file: " << Filename.str() << "\n";
} }
return true; return true;
} }
void llvm::DisplayGraph(const sys::Path &Filename, bool wait, void llvm::DisplayGraph(StringRef Filename, bool wait,
GraphProgram::Name program) { GraphProgram::Name program) {
wait &= !ViewBackground; wait &= !ViewBackground;
std::string ErrMsg; std::string ErrMsg;
@ -120,66 +137,67 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
#elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \ #elif (HAVE_GV && (HAVE_DOT || HAVE_FDP || HAVE_NEATO || \
HAVE_TWOPI || HAVE_CIRCO)) HAVE_TWOPI || HAVE_CIRCO))
sys::Path PSFilename = Filename; sys::Path PSFilename = sys::Path(Filename);
PSFilename.appendSuffix("ps"); PSFilename.appendSuffix("ps");
sys::Path prog; std::string prog;
// Set default grapher // Set default grapher
#if HAVE_CIRCO #if HAVE_CIRCO
prog = sys::Path(LLVM_PATH_CIRCO); prog = LLVM_PATH_CIRCO;
#endif #endif
#if HAVE_TWOPI #if HAVE_TWOPI
prog = sys::Path(LLVM_PATH_TWOPI); prog = LLVM_PATH_TWOPI;
#endif #endif
#if HAVE_NEATO #if HAVE_NEATO
prog = sys::Path(LLVM_PATH_NEATO); prog = LLVM_PATH_NEATO;
#endif #endif
#if HAVE_FDP #if HAVE_FDP
prog = sys::Path(LLVM_PATH_FDP); prog = LLVM_PATH_FDP;
#endif #endif
#if HAVE_DOT #if HAVE_DOT
prog = sys::Path(LLVM_PATH_DOT); prog = LLVM_PATH_DOT;
#endif #endif
// Find which program the user wants // Find which program the user wants
#if HAVE_DOT #if HAVE_DOT
if (program == GraphProgram::DOT) if (program == GraphProgram::DOT)
prog = sys::Path(LLVM_PATH_DOT); prog = LLVM_PATH_DOT;
#endif #endif
#if (HAVE_FDP) #if (HAVE_FDP)
if (program == GraphProgram::FDP) if (program == GraphProgram::FDP)
prog = sys::Path(LLVM_PATH_FDP); prog = LLVM_PATH_FDP;
#endif #endif
#if (HAVE_NEATO) #if (HAVE_NEATO)
if (program == GraphProgram::NEATO) if (program == GraphProgram::NEATO)
prog = sys::Path(LLVM_PATH_NEATO); prog = LLVM_PATH_NEATO;
#endif #endif
#if (HAVE_TWOPI) #if (HAVE_TWOPI)
if (program == GraphProgram::TWOPI) if (program == GraphProgram::TWOPI)
prog = sys::Path(LLVM_PATH_TWOPI); prog = LLVM_PATH_TWOPI;
#endif #endif
#if (HAVE_CIRCO) #if (HAVE_CIRCO)
if (program == GraphProgram::CIRCO) if (program == GraphProgram::CIRCO)
prog = sys::Path(LLVM_PATH_CIRCO); prog = LLVM_PATH_CIRCO;
#endif #endif
std::vector<const char*> args; std::vector<const char*> args;
std::string FilenameStr = Filename;
args.push_back(prog.c_str()); args.push_back(prog.c_str());
args.push_back("-Tps"); args.push_back("-Tps");
args.push_back("-Nfontname=Courier"); args.push_back("-Nfontname=Courier");
args.push_back("-Gsize=7.5,10"); args.push_back("-Gsize=7.5,10");
args.push_back(Filename.c_str()); args.push_back(FilenameStr.c_str());
args.push_back("-o"); args.push_back("-o");
args.push_back(PSFilename.c_str()); args.push_back(PSFilename.c_str());
args.push_back(0); args.push_back(0);
errs() << "Running '" << prog.str() << "' program... "; errs() << "Running '" << prog << "' program... ";
if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg)) if (!ExecGraphViewer(prog, args, Filename, wait, ErrMsg))
return; return;
sys::Path gv(LLVM_PATH_GV); std::string gv(LLVM_PATH_GV);
args.clear(); args.clear();
args.push_back(gv.c_str()); args.push_back(gv.c_str());
args.push_back(PSFilename.c_str()); args.push_back(PSFilename.c_str());
@ -187,7 +205,7 @@ void llvm::DisplayGraph(const sys::Path &Filename, bool wait,
args.push_back(0); args.push_back(0);
ErrMsg.clear(); ErrMsg.clear();
if (!ExecGraphViewer(gv, args, PSFilename, wait, ErrMsg)) if (!ExecGraphViewer(gv, args, PSFilename.str(), wait, ErrMsg))
return; return;
#elif HAVE_DOTTY #elif HAVE_DOTTY