[dsymutil] Do not create temporary files in -no-output mode.
[oota-llvm.git] / tools / dsymutil / dsymutil.cpp
index 581da4ebfa06f1893b16e48497b7cd0f0e759557..cf5e6ed99a5f71da58984676288de74d6aa0b7c4 100644 (file)
@@ -79,21 +79,35 @@ static opt<bool> InputIsYAMLDebugMap(
     init(false), cat(DsymCategory));
 }
 
+static std::error_code getUniqueFile(const llvm::Twine &Model, int &ResultFD,
+                                     llvm::SmallVectorImpl<char> &ResultPath) {
+  // If in NoOutput mode, use the createUniqueFile variant that
+  // doesn't open the file but still generates a somewhat unique
+  // name. In the real usage scenario, we'll want to ensure that the
+  // file is trully unique, and creating it is the only way to achieve
+  // that.
+  if (NoOutput)
+    return llvm::sys::fs::createUniqueFile(Model, ResultPath);
+  return llvm::sys::fs::createUniqueFile(Model, ResultFD, ResultPath);
+}
+
 static std::string getOutputFileName(llvm::StringRef InputFile,
                                      bool TempFile = false) {
   if (TempFile) {
-    std::string OutputFile = (InputFile + ".tmp%%%%%%.dwarf").str();
+    llvm::Twine OutputFile = InputFile + ".tmp%%%%%%.dwarf";
     int FD;
     llvm::SmallString<128> UniqueFile;
-    if (auto EC = llvm::sys::fs::createUniqueFile(OutputFile, FD, UniqueFile)) {
+    if (auto EC = getUniqueFile(OutputFile, FD, UniqueFile)) {
       llvm::errs() << "error: failed to create temporary outfile '"
                    << OutputFile << "': " << EC.message() << '\n';
       return "";
     }
     llvm::sys::RemoveFileOnSignal(UniqueFile);
-    // Close the file immediately. We know it is unique. It will be
-    // reopened and written to later.
-    llvm::raw_fd_ostream CloseImmediately(FD, true /* shouldClose */, true);
+    if (!NoOutput) {
+      // Close the file immediately. We know it is unique. It will be
+      // reopened and written to later.
+      llvm::raw_fd_ostream CloseImmediately(FD, true /* shouldClose */, true);
+    }
     return UniqueFile.str();
   }