[llvm-commits] [llvm] r123557 - in /llvm/trunk: include/llvm/Bitcode/Archive.h lib/Archive/ArchiveWriter.cpp test/Linker/link-archive.ll

Michael J. Spencer bigcheesegs at gmail.com
Sat Jan 15 17:43:22 PST 2011


Author: mspencer
Date: Sat Jan 15 19:43:22 2011
New Revision: 123557

URL: http://llvm.org/viewvc/llvm-project?rev=123557&view=rev
Log:
Revert "Archive: Replace all internal uses of PathV1 with PathV2. The external API still uses PathV1."

Modified:
    llvm/trunk/include/llvm/Bitcode/Archive.h
    llvm/trunk/lib/Archive/ArchiveWriter.cpp
    llvm/trunk/test/Linker/link-archive.ll

Modified: llvm/trunk/include/llvm/Bitcode/Archive.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/Archive.h?rev=123557&r1=123556&r2=123557&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Bitcode/Archive.h (original)
+++ llvm/trunk/include/llvm/Bitcode/Archive.h Sat Jan 15 19:43:22 2011
@@ -25,7 +25,6 @@
 
 namespace llvm {
   class MemoryBuffer;
-  class raw_ostream;
 
 // Forward declare classes
 class Module;              // From VMCore
@@ -483,7 +482,7 @@
     bool loadSymbolTable(std::string* ErrMessage);
 
     /// @brief Write the symbol table to an ofstream.
-    void writeSymbolTable(raw_ostream& ARFile);
+    void writeSymbolTable(std::ofstream& ARFile);
 
     /// Writes one ArchiveMember to an ofstream. If an error occurs, returns
     /// false, otherwise true. If an error occurs and error is non-null then
@@ -492,7 +491,7 @@
     /// @returns true Writing member failed, \p error set to error message
     bool writeMember(
       const ArchiveMember& member, ///< The member to be written
-      raw_ostream& ARFile,       ///< The file to write member onto
+      std::ofstream& ARFile,       ///< The file to write member onto
       bool CreateSymbolTable,      ///< Should symbol table be created?
       bool TruncateNames,          ///< Should names be truncated to 11 chars?
       bool ShouldCompress,         ///< Should the member be compressed?

Modified: llvm/trunk/lib/Archive/ArchiveWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Archive/ArchiveWriter.cpp?rev=123557&r1=123556&r2=123557&view=diff
==============================================================================
--- llvm/trunk/lib/Archive/ArchiveWriter.cpp (original)
+++ llvm/trunk/lib/Archive/ArchiveWriter.cpp Sat Jan 15 19:43:22 2011
@@ -18,7 +18,6 @@
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Process.h"
-#include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/system_error.h"
 #include <fstream>
@@ -28,7 +27,7 @@
 
 // Write an integer using variable bit rate encoding. This saves a few bytes
 // per entry in the symbol table.
-static inline void writeInteger(unsigned num, raw_ostream& ARFile) {
+static inline void writeInteger(unsigned num, std::ofstream& ARFile) {
   while (1) {
     if (num < 0x80) { // done?
       ARFile << (unsigned char)num;
@@ -202,14 +201,14 @@
 bool
 Archive::writeMember(
   const ArchiveMember& member,
-  raw_ostream& ARFile,
+  std::ofstream& ARFile,
   bool CreateSymbolTable,
   bool TruncateNames,
   bool ShouldCompress,
   std::string* ErrMsg
 ) {
 
-  unsigned filepos = ARFile.tell();
+  unsigned filepos = ARFile.tellp();
   filepos -= 8;
 
   // Get the data and its size either from the
@@ -282,7 +281,7 @@
   ARFile.write(data,fSize);
 
   // Make sure the member is an even length
-  if ((ARFile.tell() & 1) == 1)
+  if ((ARFile.tellp() & 1) == 1)
     ARFile << ARFILE_PAD;
 
   // Close the mapped file if it was opened
@@ -292,7 +291,7 @@
 
 // Write out the LLVM symbol table as an archive member to the file.
 void
-Archive::writeSymbolTable(raw_ostream& ARFile) {
+Archive::writeSymbolTable(std::ofstream& ARFile) {
 
   // Construct the symbol table's header
   ArchiveMemberHeader Hdr;
@@ -316,7 +315,7 @@
 
 #ifndef NDEBUG
   // Save the starting position of the symbol tables data content.
-  unsigned startpos = ARFile.tell();
+  unsigned startpos = ARFile.tellp();
 #endif
 
   // Write out the symbols sequentially
@@ -333,7 +332,7 @@
 
 #ifndef NDEBUG
   // Now that we're done with the symbol table, get the ending file position
-  unsigned endpos = ARFile.tell();
+  unsigned endpos = ARFile.tellp();
 #endif
 
   // Make sure that the amount we wrote is what we pre-computed. This is
@@ -362,19 +361,25 @@
   }
 
   // Create a temporary file to store the archive in
-  SmallString<128> TempArchivePath;
-  int ArchFD;
-  if (error_code ec = sys::fs::unique_file("%%-%%-%%-%%" + archPath.str(),
-                                                 ArchFD, TempArchivePath)) {
-    if (ErrMsg) *ErrMsg = ec.message();
+  sys::Path TmpArchive = archPath;
+  if (TmpArchive.createTemporaryFileOnDisk(ErrMsg))
     return true;
-  }
 
   // Make sure the temporary gets removed if we crash
-  sys::RemoveFileOnSignal(sys::Path(TempArchivePath.str()));
+  sys::RemoveFileOnSignal(TmpArchive);
 
   // Create archive file for output.
-  raw_fd_ostream ArchiveFile(ArchFD, true);
+  std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
+                               std::ios::binary;
+  std::ofstream ArchiveFile(TmpArchive.c_str(), io_mode);
+
+  // Check for errors opening or creating archive file.
+  if (!ArchiveFile.is_open() || ArchiveFile.bad()) {
+    TmpArchive.eraseFromDisk();
+    if (ErrMsg)
+      *ErrMsg = "Error opening archive file: " + archPath.str();
+    return true;
+  }
 
   // If we're creating a symbol table, reset it now
   if (CreateSymbolTable) {
@@ -390,9 +395,8 @@
   for (MembersList::iterator I = begin(), E = end(); I != E; ++I) {
     if (writeMember(*I, ArchiveFile, CreateSymbolTable,
                      TruncateNames, Compress, ErrMsg)) {
+      TmpArchive.eraseFromDisk();
       ArchiveFile.close();
-      bool existed;
-      sys::fs::remove(TempArchivePath.str(), existed);
       return true;
     }
   }
@@ -407,12 +411,12 @@
     // ensure compatibility with other archivers we need to put the symbol
     // table first in the file. Unfortunately, this means mapping the file
     // we just wrote back in and copying it to the destination file.
-    SmallString<128> TempArchiveWithSymbolTablePath;
+    sys::Path FinalFilePath = archPath;
 
     // Map in the archive we just wrote.
     {
     OwningPtr<MemoryBuffer> arch;
-    if (error_code ec = MemoryBuffer::getFile(TempArchivePath.c_str(), arch)) {
+    if (error_code ec = MemoryBuffer::getFile(TmpArchive.c_str(), arch)) {
       if (ErrMsg)
         *ErrMsg = ec.message();
       return true;
@@ -421,14 +425,17 @@
 
     // Open another temporary file in order to avoid invalidating the
     // mmapped data
-    if (error_code ec = sys::fs::unique_file("%%-%%-%%-%%" + archPath.str(),
-                                      ArchFD, TempArchiveWithSymbolTablePath)) {
-      if (ErrMsg) *ErrMsg = ec.message();
+    if (FinalFilePath.createTemporaryFileOnDisk(ErrMsg))
       return true;
-    }
-    sys::RemoveFileOnSignal(sys::Path(TempArchiveWithSymbolTablePath.str()));
+    sys::RemoveFileOnSignal(FinalFilePath);
 
-    raw_fd_ostream FinalFile(ArchFD, true);
+    std::ofstream FinalFile(FinalFilePath.c_str(), io_mode);
+    if (!FinalFile.is_open() || FinalFile.bad()) {
+      TmpArchive.eraseFromDisk();
+      if (ErrMsg)
+        *ErrMsg = "Error opening archive file: " + FinalFilePath.str();
+      return true;
+    }
 
     // Write the file magic number
     FinalFile << ARFILE_MAGIC;
@@ -441,8 +448,7 @@
     if (foreignST) {
       if (writeMember(*foreignST, FinalFile, false, false, false, ErrMsg)) {
         FinalFile.close();
-        bool existed;
-        sys::fs::remove(TempArchiveWithSymbolTablePath.str(), existed);
+        TmpArchive.eraseFromDisk();
         return true;
       }
     }
@@ -460,11 +466,8 @@
     } // free arch.
 
     // Move the final file over top of TmpArchive
-    if (error_code ec = sys::fs::rename(TempArchiveWithSymbolTablePath.str(),
-                                        TempArchivePath.str())) {
-      if (ErrMsg) *ErrMsg = ec.message();
+    if (FinalFilePath.renamePathOnDisk(TmpArchive, ErrMsg))
       return true;
-    }
   }
 
   // Before we replace the actual archive, we need to forget all the
@@ -472,11 +475,8 @@
   // this because we cannot replace an open file on Windows.
   cleanUpMemory();
 
-  if (error_code ec = sys::fs::rename(TempArchivePath.str(),
-                                      archPath.str())) {
-    if (ErrMsg) *ErrMsg = ec.message();
+  if (TmpArchive.renamePathOnDisk(archPath, ErrMsg))
     return true;
-  }
 
   // Set correct read and write permissions after temporary file is moved
   // to final destination path.

Modified: llvm/trunk/test/Linker/link-archive.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Linker/link-archive.ll?rev=123557&r1=123556&r2=123557&view=diff
==============================================================================
--- llvm/trunk/test/Linker/link-archive.ll (original)
+++ llvm/trunk/test/Linker/link-archive.ll Sat Jan 15 19:43:22 2011
@@ -1,5 +1,4 @@
 ; Test linking of a bc file to an archive via llvm-ld. 
-; XFAIL: *
 ; PR1434
 ; RUN: llvm-as %s -o %t.bar.bc
 ; RUN: echo {define i32* @foo(i32 %x) \{ ret i32* @baz \} \





More information about the llvm-commits mailing list