Skip to content

Commit

Permalink
Refactor ExecuteAndWait to take StringRefs.
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@334518 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Zachary Turner committed Jun 12, 2018
1 parent f91c23a commit 65eda1a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
14 changes: 7 additions & 7 deletions lib/Driver/Job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,22 +317,21 @@ int Command::Execute(ArrayRef<llvm::Optional<StringRef>> Redirects,
std::string *ErrMsg, bool *ExecutionFailed) const {
SmallVector<const char*, 128> Argv;

const char **Envp;
if (Environment.empty()) {
Envp = nullptr;
} else {
Optional<ArrayRef<StringRef>> Env;
if (!Environment.empty()) {
assert(Environment.back() == nullptr &&
"Environment vector should be null-terminated by now");
Envp = const_cast<const char **>(Environment.data());
Env = llvm::toStringRefArray(Environment.data());
}

if (ResponseFile == nullptr) {
Argv.push_back(Executable);
Argv.append(Arguments.begin(), Arguments.end());
Argv.push_back(nullptr);

auto Args = llvm::toStringRefArray(Argv.data());
return llvm::sys::ExecuteAndWait(
Executable, Argv.data(), Envp, Redirects, /*secondsToWait*/ 0,
Executable, Args, Env, Redirects, /*secondsToWait*/ 0,
/*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
}

Expand All @@ -357,7 +356,8 @@ int Command::Execute(ArrayRef<llvm::Optional<StringRef>> Redirects,
return -1;
}

return llvm::sys::ExecuteAndWait(Executable, Argv.data(), Envp, Redirects,
auto Args = llvm::toStringRefArray(Argv.data());
return llvm::sys::ExecuteAndWait(Executable, Args, Env, Redirects,
/*secondsToWait*/ 0,
/*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,9 @@ UbigraphViz::~UbigraphViz() {
std::string Ubiviz;
if (auto Path = llvm::sys::findProgramByName("ubiviz"))
Ubiviz = *Path;
const char *args[] = {Ubiviz.c_str(), Filename.c_str(), nullptr};
std::array<StringRef, 2> Args = {Ubiviz, Filename};

if (llvm::sys::ExecuteAndWait(Ubiviz, &args[0], nullptr, {}, 0, 0, &ErrMsg)) {
if (llvm::sys::ExecuteAndWait(Ubiviz, Args, llvm::None, {}, 0, 0, &ErrMsg)) {
llvm::errs() << "Error viewing graph: " << ErrMsg << "\n";
}

Expand Down
23 changes: 11 additions & 12 deletions tools/clang-offload-bundler/ClangOffloadBundler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,23 +536,22 @@ class ObjectFileHandler final : public FileHandler {
// close it and use the name to pass down to clang.
OS.close();
SmallString<128> TargetName = getTriple(TargetNames[HostInputIndex]);
const char *ClangArgs[] = {"clang",
"-r",
"-target",
TargetName.c_str(),
"-o",
OutputFileNames.front().c_str(),
InputFileNames[HostInputIndex].c_str(),
BitcodeFileName.c_str(),
"-nostdlib",
nullptr};
std::vector<StringRef> ClangArgs = {"clang",
"-r",
"-target",
TargetName.c_str(),
"-o",
OutputFileNames.front().c_str(),
InputFileNames[HostInputIndex].c_str(),
BitcodeFileName.c_str(),
"-nostdlib"};

// If the user asked for the commands to be printed out, we do that instead
// of executing it.
if (PrintExternalCommands) {
errs() << "\"" << ClangBinary.get() << "\"";
for (unsigned I = 1; ClangArgs[I]; ++I)
errs() << " \"" << ClangArgs[I] << "\"";
for (StringRef Arg : ClangArgs)
errs() << " \"" << Arg << "\"";
errs() << "\n";
} else {
// Write the bitcode contents to the temporary file.
Expand Down

0 comments on commit 65eda1a

Please sign in to comment.