From: Zachary Turner Date: Tue, 12 Jun 2018 17:43:52 +0000 (+0000) Subject: Refactor ExecuteAndWait to take StringRefs. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=65eda1a8360bec118893f240d584aa20e79e972f;p=clang 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@334518 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Driver/Job.cpp b/lib/Driver/Job.cpp index 71cda62104..74af597ad3 100644 --- a/lib/Driver/Job.cpp +++ b/lib/Driver/Job.cpp @@ -317,13 +317,11 @@ int Command::Execute(ArrayRef> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const { SmallVector Argv; - const char **Envp; - if (Environment.empty()) { - Envp = nullptr; - } else { + Optional> Env; + if (!Environment.empty()) { assert(Environment.back() == nullptr && "Environment vector should be null-terminated by now"); - Envp = const_cast(Environment.data()); + Env = llvm::toStringRefArray(Environment.data()); } if (ResponseFile == nullptr) { @@ -331,8 +329,9 @@ int Command::Execute(ArrayRef> Redirects, 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); } @@ -357,7 +356,8 @@ int Command::Execute(ArrayRef> 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); } diff --git a/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp b/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp index 9c5e04cc3c..3d86945a55 100644 --- a/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp +++ b/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp @@ -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 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"; } diff --git a/tools/clang-offload-bundler/ClangOffloadBundler.cpp b/tools/clang-offload-bundler/ClangOffloadBundler.cpp index d9fb3898b5..29cd9848d1 100644 --- a/tools/clang-offload-bundler/ClangOffloadBundler.cpp +++ b/tools/clang-offload-bundler/ClangOffloadBundler.cpp @@ -536,23 +536,22 @@ public: // 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 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.