<div dir="ltr">On Fri, Jul 5, 2013 at 11:01 PM, Rafael Espindola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br><div class="gmail_extra">
<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: rafael<br>
Date: Fri Jul  5 16:01:08 2013<br>
New Revision: 185726<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=185726&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=185726&view=rev</a><br>
Log:<br>
Add a createUniqueFile function and switch llvm's users of unique_file.<br>
<br>
This function is complementary to createTemporaryFile. It handles the case were<br>
the unique file is *not* temporary: we will rename it in the end. Since we<br>
will rename it, the file has to be in the same filesystem as the final<br>
destination and we don't prepend the system temporary directory.<br>
<br>
This has a small semantic difference from unique_file: the default mode is 0666.<br>
This matches the behavior of most unix tools. For example, with this change<br>
lld now produces files with the same permissions as ld. I will add a test<br>
of this change when I port clang over to createUniqueFile (next commit).<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/Support/FileSystem.h<br>
    llvm/trunk/lib/Support/FileOutputBuffer.cpp<br>
    llvm/trunk/lib/Support/LockFileManager.cpp<br>
    llvm/trunk/lib/Support/Path.cpp<br>
    llvm/trunk/tools/bugpoint/ExecutionDriver.cpp<br>
    llvm/trunk/tools/bugpoint/ExtractFunction.cpp<br>
    llvm/trunk/tools/bugpoint/OptimizerDriver.cpp<br>
    llvm/trunk/tools/bugpoint/ToolRunner.cpp<br>
    llvm/trunk/tools/llvm-ar/ArchiveWriter.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Support/FileSystem.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=185726&r1=185725&r2=185726&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=185726&r1=185725&r2=185726&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/include/llvm/Support/FileSystem.h (original)<br>
+++ llvm/trunk/include/llvm/Support/FileSystem.h Fri Jul  5 16:01:08 2013<br>
@@ -575,6 +575,35 @@ error_code unique_file(const Twine &mode<br>
 error_code unique_file(const Twine &Model, SmallVectorImpl<char> &ResultPath,<br>
                        bool MakeAbsolute = true);<br>
<br>
+/// @brief Create a uniquely named file.<br>
+///<br>
+/// Generates a unique path suitable for a temporary file and then opens it as a<br>
+/// file. The name is based on \a model with '%' replaced by a random char in<br>
+/// [0-9a-f]. If \a model is not an absolute path, a suitable temporary<br>
+/// directory will be prepended.<br>
+///<br>
+/// Example: clang-%%-%%-%%-%%-%%.s => clang-a0-b1-c2-d3-e4.s<br>
+///<br>
+/// This is an atomic operation. Either the file is created and opened, or the<br>
+/// file system is left untouched.<br>
+///<br>
+/// The intendend use is for files that are to be kept, possibly after<br>
+/// renaming them. For example, when running 'clang -c foo.o', the file can<br>
+/// be first created as foo-abc123.o and then renamed.<br>
+///<br>
+/// @param Model Name to base unique path off of.<br>
+/// @param ResultFD Set to the opened file's file descriptor.<br>
+/// @param ResultPath Set to the opened file's absolute path.<br>
+/// @returns errc::success if Result{FD,Path} have been successfully set,<br>
+///          otherwise a platform specific error_code.<br>
+error_code createUniqueFile(const Twine &Model, int &ResultFD,<br>
+                            SmallVectorImpl<char> &ResultPath,<br>
+                            unsigned Mode = all_read | all_write);<br>
+<br>
+/// @brief Simpler version for clients that don't want an open file.<br>
+error_code createUniqueFile(const Twine &Model,<br>
+                            SmallVectorImpl<char> &ResultPath);<br>
+<br>
 /// @brief Create a file in the system temporary directory.<br>
 ///<br>
 /// The filename is of the form prefix-random_chars.suffix. Since the directory<br>
<br>
Modified: llvm/trunk/lib/Support/FileOutputBuffer.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileOutputBuffer.cpp?rev=185726&r1=185725&r2=185726&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileOutputBuffer.cpp?rev=185726&r1=185725&r2=185726&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/lib/Support/FileOutputBuffer.cpp (original)<br>
+++ llvm/trunk/lib/Support/FileOutputBuffer.cpp Fri Jul  5 16:01:08 2013<br>
@@ -65,8 +65,8 @@ error_code FileOutputBuffer::create(Stri<br>
   // Create new file in same directory but with random name.<br>
   SmallString<128> TempFilePath;<br>
   int FD;<br>
-  EC = sys::fs::unique_file(Twine(FilePath) + ".tmp%%%%%%%",<br>
-                            FD, TempFilePath, false, 0644);<br>
+  EC = sys::fs::createUniqueFile(Twine(FilePath) + ".tmp%%%%%%%",<br>
+                                 FD, TempFilePath);<br>
   if (EC)<br>
     return EC;<br>
<br>
<br>
Modified: llvm/trunk/lib/Support/LockFileManager.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/LockFileManager.cpp?rev=185726&r1=185725&r2=185726&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/LockFileManager.cpp?rev=185726&r1=185725&r2=185726&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/lib/Support/LockFileManager.cpp (original)<br>
+++ llvm/trunk/lib/Support/LockFileManager.cpp Fri Jul  5 16:01:08 2013<br>
@@ -78,10 +78,9 @@ LockFileManager::LockFileManager(StringR<br>
   UniqueLockFileName += "-%%%%%%%%";<br>
   int UniqueLockFileID;<br>
   if (error_code EC<br>
-        = sys::fs::unique_file(UniqueLockFileName.str(),<br>
-                                     UniqueLockFileID,<br>
-                                     UniqueLockFileName,<br>
-                                     /*makeAbsolute=*/false)) {<br>
+        = sys::fs::createUniqueFile(UniqueLockFileName.str(),<br>
+                                    UniqueLockFileID,<br>
+                                    UniqueLockFileName)) {<br>
     Error = EC;<br>
     return;<br>
   }<br>
<br>
Modified: llvm/trunk/lib/Support/Path.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=185726&r1=185725&r2=185726&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=185726&r1=185725&r2=185726&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/lib/Support/Path.cpp (original)<br>
+++ llvm/trunk/lib/Support/Path.cpp Fri Jul  5 16:01:08 2013<br>
@@ -654,6 +654,17 @@ error_code unique_file(const Twine &Mode<br>
   return createUniqueEntity(Model, Dummy, ResultPath, MakeAbsolute, 0, FS_Name);<br>
 }<br>
<br>
+error_code createUniqueFile(const Twine &Model, int &ResultFd,<br>
+                            SmallVectorImpl<char> &ResultPath, unsigned Mode) {<br>
+  return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);<br>
+}<br>
+<br>
+error_code createUniqueFile(const Twine &Model,<br>
+                            SmallVectorImpl<char> &ResultPath) {<br>
+  int Dummy;<br>
+  return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);<br>
+}<br>
+<br>
 static error_code createTemporaryFile(const Twine &Model, int &ResultFD,<br>
                                       llvm::SmallVectorImpl<char> &ResultPath,<br>
                                       FSEntity Type) {<br>
<br>
Modified: llvm/trunk/tools/bugpoint/ExecutionDriver.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/ExecutionDriver.cpp?rev=185726&r1=185725&r2=185726&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/ExecutionDriver.cpp?rev=185726&r1=185725&r2=185726&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/bugpoint/ExecutionDriver.cpp (original)<br>
+++ llvm/trunk/tools/bugpoint/ExecutionDriver.cpp Fri Jul  5 16:01:08 2013<br>
@@ -267,7 +267,7 @@ void BugDriver::compileProgram(Module *M<br>
   // Emit the program to a bitcode file...<br>
   SmallString<128> BitcodeFile;<br>
   int BitcodeFD;<br>
-  error_code EC = sys::fs::unique_file(<br>
+  error_code EC = sys::fs::createUniqueFile(<br>
       OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);<br>
   if (EC) {<br>
     errs() << ToolName << ": Error making unique filename: " << EC.message()<br>
@@ -306,7 +306,7 @@ std::string BugDriver::executeProgram(co<br>
     // Emit the program to a bitcode file...<br>
     SmallString<128> UniqueFilename;<br>
     int UniqueFD;<br>
-    error_code EC = sys::fs::unique_file(<br>
+    error_code EC = sys::fs::createUniqueFile(<br>
         OutputPrefix + "-test-program-%%%%%%%.bc", UniqueFD, UniqueFilename);<br>
     if (EC) {<br>
       errs() << ToolName << ": Error making unique filename: "<br>
@@ -332,7 +332,7 @@ std::string BugDriver::executeProgram(co<br>
<br>
   // Check to see if this is a valid output filename...<br>
   SmallString<128> UniqueFile;<br>
-  error_code EC = sys::fs::unique_file(OutputFile, UniqueFile);<br>
+  error_code EC = sys::fs::createUniqueFile(OutputFile, UniqueFile);<br>
   if (EC) {<br>
     errs() << ToolName << ": Error making unique filename: "<br>
            << EC.message() << "\n";<br>
<br>
Modified: llvm/trunk/tools/bugpoint/ExtractFunction.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/ExtractFunction.cpp?rev=185726&r1=185725&r2=185726&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/ExtractFunction.cpp?rev=185726&r1=185725&r2=185726&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/bugpoint/ExtractFunction.cpp (original)<br>
+++ llvm/trunk/tools/bugpoint/ExtractFunction.cpp Fri Jul  5 16:01:08 2013<br>
@@ -365,8 +365,8 @@ Module *BugDriver::ExtractMappedBlocksFr<br>
                                                  Module *M) {<br>
   SmallString<128> Filename;<br>
   int FD;<br>
-  error_code EC = sys::fs::unique_file(OutputPrefix + "-extractblocks%%%%%%%",<br>
-                                       FD, Filename);<br>
+  error_code EC = sys::fs::createUniqueFile(<br>
+      OutputPrefix + "-extractblocks%%%%%%%", FD, Filename);<br>
   if (EC) {<br>
     outs() << "*** Basic Block extraction failed!\n";<br>
     errs() << "Error creating temporary file: " << EC.message() << "\n";<br>
<br>
Modified: llvm/trunk/tools/bugpoint/OptimizerDriver.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/OptimizerDriver.cpp?rev=185726&r1=185725&r2=185726&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/OptimizerDriver.cpp?rev=185726&r1=185725&r2=185726&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/bugpoint/OptimizerDriver.cpp (original)<br>
+++ llvm/trunk/tools/bugpoint/OptimizerDriver.cpp Fri Jul  5 16:01:08 2013<br>
@@ -124,8 +124,8 @@ bool BugDriver::runPasses(Module *Progra<br>
   // setup the output file name<br>
   outs().flush();<br>
   SmallString<128> UniqueFilename;<br>
-  error_code EC =<br>
-      sys::fs::unique_file(OutputPrefix + "-output-%%%%%%%.bc", UniqueFilename);<br>
+  error_code EC = sys::fs::createUniqueFile(<br>
+      OutputPrefix + "-output-%%%%%%%.bc", UniqueFilename);<br>
   if (EC) {<br>
     errs() << getToolName() << ": Error making unique filename: "<br>
            << EC.message() << "\n";<br>
@@ -136,8 +136,8 @@ bool BugDriver::runPasses(Module *Progra<br>
   // set up the input file name<br>
   SmallString<128> InputFilename;<br>
   int InputFD;<br>
-  EC = sys::fs::unique_file(OutputPrefix + "-input-%%%%%%%.bc", InputFD,<br>
-                            InputFilename);<br>
+  EC = sys::fs::createUniqueFile(OutputPrefix + "-input-%%%%%%%.bc", InputFD,<br>
+                                 InputFilename);<br>
   if (EC) {<br>
     errs() << getToolName() << ": Error making unique filename: "<br>
            << EC.message() << "\n";<br>
<br>
Modified: llvm/trunk/tools/bugpoint/ToolRunner.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/ToolRunner.cpp?rev=185726&r1=185725&r2=185726&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/ToolRunner.cpp?rev=185726&r1=185725&r2=185726&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/bugpoint/ToolRunner.cpp (original)<br>
+++ llvm/trunk/tools/bugpoint/ToolRunner.cpp Fri Jul  5 16:01:08 2013<br>
@@ -478,7 +478,7 @@ GCC::FileType LLC::OutputCode(const std:<br>
<br>
   SmallString<128> UniqueFile;<br>
   error_code EC =<br>
-      sys::fs::unique_file(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);<br>
+      sys::fs::createUniqueFile(Bitcode + "-%%%%%%%" + Suffix, UniqueFile);<br>
   if (EC) {<br>
     errs() << "Error making unique filename: " << EC.message() << "\n";<br>
     exit(1);<br>
@@ -715,7 +715,7 @@ int GCC::ExecuteProgram(const std::strin<br>
<br>
   SmallString<128> OutputBinary;<br>
   error_code EC =<br>
-      sys::fs::unique_file(ProgramFile+ "-%%%%%%%.gcc.exe", OutputBinary);<br>
+      sys::fs::createUniqueFile(ProgramFile + "-%%%%%%%.gcc.exe", OutputBinary);<br>
   if (EC) {<br>
     errs() << "Error making unique filename: " << EC.message() << "\n";<br>
     exit(1);<br>
@@ -824,8 +824,8 @@ int GCC::MakeSharedObject(const std::str<br>
                           const std::vector<std::string> &ArgsForGCC,<br>
                           std::string &Error) {<br>
   SmallString<128> UniqueFilename;<br>
-  error_code EC = sys::fs::unique_file(InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT,<br>
-                                       UniqueFilename);<br>
+  error_code EC = sys::fs::createUniqueFile(<br>
+      InputFile + "-%%%%%%%" + LTDL_SHLIB_EXT, UniqueFilename);<br>
   if (EC) {<br>
     errs() << "Error making unique filename: " << EC.message() << "\n";<br>
     exit(1);<br>
<br>
Modified: llvm/trunk/tools/llvm-ar/ArchiveWriter.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-ar/ArchiveWriter.cpp?rev=185726&r1=185725&r2=185726&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-ar/ArchiveWriter.cpp?rev=185726&r1=185725&r2=185726&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/tools/llvm-ar/ArchiveWriter.cpp (original)<br>
+++ llvm/trunk/tools/llvm-ar/ArchiveWriter.cpp Fri Jul  5 16:01:08 2013<br>
@@ -260,8 +260,8 @@ bool Archive::writeToDisk(bool TruncateN<br>
   // Create a temporary file to store the archive in<br>
   int TmpArchiveFD;<br>
   SmallString<128> TmpArchive;<br>
-  error_code EC = sys::fs::unique_file("temp-archive-%%%%%%%.a", TmpArchiveFD,<br>
-                                       TmpArchive, true, sys::fs::all_read | sys::fs::all_write);<br>
+  error_code EC = sys::fs::createUniqueFile("temp-archive-%%%%%%%.a",<br>
+                                            TmpArchiveFD, TmpArchive);<br></blockquote><div><br></div><div>This regressed the Object/nm-archive test. We need to anchor on the output file to make sure we can actually write the temporary file.</div>
<div><br></div><div>Fixed in r185825.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
   if (EC)<br>
     return true;<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>