]> granicus.if.org Git - clang/commitdiff
Refactor ExecuteAndWait to take StringRefs.
authorZachary Turner <zturner@google.com>
Tue, 12 Jun 2018 17:43:52 +0000 (17:43 +0000)
committerZachary Turner <zturner@google.com>
Tue, 12 Jun 2018 17:43:52 +0000 (17:43 +0000)
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

lib/Driver/Job.cpp
lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
tools/clang-offload-bundler/ClangOffloadBundler.cpp

index 71cda621043b3b699b61754a1c591876f99de933..74af597ad362707f3a5dfb86dd85c9117b48f9ba 100644 (file)
@@ -317,13 +317,11 @@ 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) {
@@ -331,8 +329,9 @@ int Command::Execute(ArrayRef<llvm::Optional<StringRef>> 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<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);
 }
index 9c5e04cc3c575c322a5417137a09a002777b01b4..3d86945a55ce5a5b2015b8066b7f3634767403a9 100644 (file)
@@ -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";
   }
 
index d9fb3898b51a8f42321d77a8340594d404663722..29cd9848d11150ba6ce3da707bded91e5a87b377 100644 (file)
@@ -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<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.