summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/GraphWriter.cpp
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2018-06-12 17:43:52 +0000
committerZachary Turner <zturner@google.com>2018-06-12 17:43:52 +0000
commit08426e1f9f76f137d6958a450b1b5febed9b2ff2 (patch)
tree03d4c0c327b4cd0f00bd250af505ad1d53a0b79f /llvm/lib/Support/GraphWriter.cpp
parent70a9e47f530089261061d5bb3192f43aad28250a (diff)
downloadbcm5719-llvm-08426e1f9f76f137d6958a450b1b5febed9b2ff2.tar.gz
bcm5719-llvm-08426e1f9f76f137d6958a450b1b5febed9b2ff2.zip
Refactor ExecuteAndWait to take StringRefs.
This simplifies some code which had StringRefs to begin with, and makes other code more complicated which had const char* to begin with. In the end, I think this makes for a more idiomatic and platform agnostic API. Not all platforms launch process with null terminated c-string arrays for the environment pointer and argv, but the api was designed that way because it allowed easy pass-through for posix-based platforms. There's a little additional overhead now since on posix based platforms we'll be takign StringRefs which were constructed from null terminated strings and then copying them to null terminate them again, but from a readability and usability standpoint of the API user, I think this API signature is strictly better. llvm-svn: 334518
Diffstat (limited to 'llvm/lib/Support/GraphWriter.cpp')
-rw-r--r--llvm/lib/Support/GraphWriter.cpp64
1 files changed, 27 insertions, 37 deletions
diff --git a/llvm/lib/Support/GraphWriter.cpp b/llvm/lib/Support/GraphWriter.cpp
index 794c6ba3a85..9335daffc3e 100644
--- a/llvm/lib/Support/GraphWriter.cpp
+++ b/llvm/lib/Support/GraphWriter.cpp
@@ -91,20 +91,18 @@ std::string llvm::createGraphFilename(const Twine &Name, int &FD) {
}
// Execute the graph viewer. Return true if there were errors.
-static bool ExecGraphViewer(StringRef ExecPath, std::vector<const char *> &args,
+static bool ExecGraphViewer(StringRef ExecPath, std::vector<StringRef> &args,
StringRef Filename, bool wait,
std::string &ErrMsg) {
- assert(args.back() == nullptr);
if (wait) {
- if (sys::ExecuteAndWait(ExecPath, args.data(), nullptr, {}, 0, 0,
- &ErrMsg)) {
+ if (sys::ExecuteAndWait(ExecPath, args, None, {}, 0, 0, &ErrMsg)) {
errs() << "Error: " << ErrMsg << "\n";
return true;
}
sys::fs::remove(Filename);
errs() << " done. \n";
} else {
- sys::ExecuteNoWait(ExecPath, args.data(), nullptr, {}, 0, &ErrMsg);
+ sys::ExecuteNoWait(ExecPath, args, None, {}, 0, &ErrMsg);
errs() << "Remember to erase graph file: " << Filename << "\n";
}
return false;
@@ -158,22 +156,20 @@ bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
#ifdef __APPLE__
wait &= !ViewBackground;
if (S.TryFindProgram("open", ViewerPath)) {
- std::vector<const char *> args;
- args.push_back(ViewerPath.c_str());
+ std::vector<StringRef> args;
+ args.push_back(ViewerPath);
if (wait)
args.push_back("-W");
- args.push_back(Filename.c_str());
- args.push_back(nullptr);
+ args.push_back(Filename);
errs() << "Trying 'open' program... ";
if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
return false;
}
#endif
if (S.TryFindProgram("xdg-open", ViewerPath)) {
- std::vector<const char *> args;
- args.push_back(ViewerPath.c_str());
- args.push_back(Filename.c_str());
- args.push_back(nullptr);
+ std::vector<StringRef> args;
+ args.push_back(ViewerPath);
+ args.push_back(Filename);
errs() << "Trying 'xdg-open' program... ";
if (!ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg))
return false;
@@ -181,10 +177,9 @@ bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
// Graphviz
if (S.TryFindProgram("Graphviz", ViewerPath)) {
- std::vector<const char *> args;
- args.push_back(ViewerPath.c_str());
- args.push_back(Filename.c_str());
- args.push_back(nullptr);
+ std::vector<StringRef> args;
+ args.push_back(ViewerPath);
+ args.push_back(Filename);
errs() << "Running 'Graphviz' program... ";
return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
@@ -192,15 +187,13 @@ bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
// xdot
if (S.TryFindProgram("xdot|xdot.py", ViewerPath)) {
- std::vector<const char *> args;
- args.push_back(ViewerPath.c_str());
- args.push_back(Filename.c_str());
+ std::vector<StringRef> args;
+ args.push_back(ViewerPath);
+ args.push_back(Filename);
args.push_back("-f");
args.push_back(getProgramName(program));
- args.push_back(nullptr);
-
errs() << "Running 'xdot.py' program... ";
return ExecGraphViewer(ViewerPath, args, Filename, wait, ErrMsg);
}
@@ -235,18 +228,17 @@ bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
std::string OutputFilename =
Filename + (Viewer == VK_CmdStart ? ".pdf" : ".ps");
- std::vector<const char *> args;
- args.push_back(GeneratorPath.c_str());
+ std::vector<StringRef> args;
+ args.push_back(GeneratorPath);
if (Viewer == VK_CmdStart)
args.push_back("-Tpdf");
else
args.push_back("-Tps");
args.push_back("-Nfontname=Courier");
args.push_back("-Gsize=7.5,10");
- args.push_back(Filename.c_str());
+ args.push_back(Filename);
args.push_back("-o");
- args.push_back(OutputFilename.c_str());
- args.push_back(nullptr);
+ args.push_back(OutputFilename);
errs() << "Running '" << GeneratorPath << "' program... ";
@@ -258,31 +250,30 @@ bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
std::string StartArg;
args.clear();
- args.push_back(ViewerPath.c_str());
+ args.push_back(ViewerPath);
switch (Viewer) {
case VK_OSXOpen:
args.push_back("-W");
- args.push_back(OutputFilename.c_str());
+ args.push_back(OutputFilename);
break;
case VK_XDGOpen:
wait = false;
- args.push_back(OutputFilename.c_str());
+ args.push_back(OutputFilename);
break;
case VK_Ghostview:
args.push_back("--spartan");
- args.push_back(OutputFilename.c_str());
+ args.push_back(OutputFilename);
break;
case VK_CmdStart:
args.push_back("/S");
args.push_back("/C");
StartArg =
(StringRef("start ") + (wait ? "/WAIT " : "") + OutputFilename).str();
- args.push_back(StartArg.c_str());
+ args.push_back(StartArg);
break;
case VK_None:
llvm_unreachable("Invalid viewer");
}
- args.push_back(nullptr);
ErrMsg.clear();
return ExecGraphViewer(ViewerPath, args, OutputFilename, wait, ErrMsg);
@@ -290,10 +281,9 @@ bool llvm::DisplayGraph(StringRef FilenameRef, bool wait,
// dotty
if (S.TryFindProgram("dotty", ViewerPath)) {
- std::vector<const char *> args;
- args.push_back(ViewerPath.c_str());
- args.push_back(Filename.c_str());
- args.push_back(nullptr);
+ std::vector<StringRef> args;
+ args.push_back(ViewerPath);
+ args.push_back(Filename);
// Dotty spawns another app and doesn't wait until it returns
#ifdef _WIN32
OpenPOWER on IntegriCloud