typo from Ryan Brown.
[oota-llvm.git] / lib / Archive / ArchiveWriter.cpp
index 096a9993a81be718539c3adb2c27d06cad95636a..ee55201383896602025d299772cf085e0fc2e96b 100644 (file)
@@ -7,19 +7,18 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// Builds up an LLVM archive file (.a) containing LLVM bytecode.
+// Builds up an LLVM archive file (.a) containing LLVM bitcode.
 //
 //===----------------------------------------------------------------------===//
 
 #include "ArchiveInternals.h"
-#include "llvm/Bytecode/Reader.h"
-#include "llvm/Support/Compressor.h"
+#include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/System/Signals.h"
 #include "llvm/System/Process.h"
+#include "llvm/ModuleProvider.h"
 #include <fstream>
-#include <iostream>
+#include <ostream>
 #include <iomanip>
-
 using namespace llvm;
 
 // Write an integer using variable bit rate encoding. This saves a few bytes
@@ -154,13 +153,20 @@ Archive::fillHeader(const ArchiveMember &mbr, ArchiveMemberHeader& hdr,
 bool
 Archive::addFileBefore(const sys::Path& filePath, iterator where, 
                         std::string* ErrMsg) {
-  assert(filePath.exists() && "Can't add a non-existent file");
+  if (!filePath.exists()) {
+    if (ErrMsg)
+      *ErrMsg = "Can not add a non-existent file to archive";
+    return true;
+  }
 
   ArchiveMember* mbr = new ArchiveMember(this);
 
   mbr->data = 0;
   mbr->path = filePath;
-  if (mbr->path.getFileStatus(mbr->info, ErrMsg))
+  const sys::FileStatus *FSInfo = mbr->path.getFileStatus(false, ErrMsg);
+  if (FSInfo)
+    mbr->info = *FSInfo;
+  else
     return true;
 
   unsigned flags = 0;
@@ -172,11 +178,8 @@ Archive::addFileBefore(const sys::Path& filePath, iterator where,
   std::string magic;
   mbr->path.getMagicNumber(magic,4);
   switch (sys::IdentifyFileType(magic.c_str(),4)) {
-    case sys::BytecodeFileType:
-      flags |= ArchiveMember::BytecodeFlag;
-      break;
-    case sys::CompressedBytecodeFileType:
-      flags |= ArchiveMember::CompressedBytecodeFlag;
+    case sys::Bitcode_FileType:
+      flags |= ArchiveMember::BitcodeFlag;
       break;
     default:
       break;
@@ -215,17 +218,17 @@ Archive::writeMember(
   }
 
   // Now that we have the data in memory, update the
-  // symbol table if its a bytecode file.
-  if (CreateSymbolTable &&
-      (member.isBytecode() || member.isCompressedBytecode())) {
+  // symbol table if its a bitcode file.
+  if (CreateSymbolTable && member.isBitcode()) {
     std::vector<std::string> symbols;
     std::string FullMemberName = archPath.toString() + "(" +
       member.getPath().toString()
       + ")";
-    ModuleProvider* MP = GetBytecodeSymbols(
-      (const unsigned char*)data,fSize,FullMemberName, symbols, ErrMsg);
+    ModuleProvider* MP = 
+      GetBitcodeSymbols((const unsigned char*)data,fSize,
+                        FullMemberName, symbols, ErrMsg);
 
-    // If the bytecode parsed successfully
+    // If the bitcode parsed successfully
     if ( MP ) {
       for (std::vector<std::string>::iterator SI = symbols.begin(),
            SE = symbols.end(); SI != SE; ++SI) {
@@ -247,46 +250,13 @@ Archive::writeMember(
         delete mFile;
       }
       if (ErrMsg)
-        *ErrMsg = "Can't parse bytecode member: " + member.getPath().toString()
+        *ErrMsg = "Can't parse bitcode member: " + member.getPath().toString()
           + ": " + *ErrMsg;
       return true;
     }
   }
 
-  // Determine if we actually should compress this member
-  bool willCompress =
-      (ShouldCompress &&
-      !member.isCompressed() &&
-      !member.isCompressedBytecode() &&
-      !member.isLLVMSymbolTable() &&
-      !member.isSVR4SymbolTable() &&
-      !member.isBSD4SymbolTable());
-
-  // Perform the compression. Note that if the file is uncompressed bytecode
-  // then we turn the file into compressed bytecode rather than treating it as
-  // compressed data. This is necessary since it allows us to determine that the
-  // file contains bytecode instead of looking like a regular compressed data
-  // member. A compressed bytecode file has its content compressed but has a
-  // magic number of "llvc". This acounts for the +/-4 arithmetic in the code
-  // below.
-  int hdrSize;
-  if (willCompress) {
-    char* output = 0;
-    if (member.isBytecode()) {
-      data +=4;
-      fSize -= 4;
-    }
-    fSize = Compressor::compressToNewBuffer(data,fSize,output,ErrMsg);
-    if (fSize == 0)
-      return true;
-    data = output;
-    if (member.isBytecode())
-      hdrSize = -fSize-4;
-    else
-      hdrSize = -fSize;
-  } else {
-    hdrSize = fSize;
-  }
+  int hdrSize = fSize;
 
   // Compute the fields of the header
   ArchiveMemberHeader Hdr;
@@ -301,10 +271,6 @@ Archive::writeMember(
                  member.getPath().toString().length());
   }
 
-  // Make sure we write the compressed bytecode magic number if we should.
-  if (willCompress && member.isBytecode())
-    ARFile.write("llvc",4);
-
   // Write the (possibly compressed) member's content to the file.
   ARFile.write(data,fSize);
 
@@ -312,11 +278,6 @@ Archive::writeMember(
   if ((ARFile.tellp() & 1) == 1)
     ARFile << ARFILE_PAD;
 
-  // Free the compressed data, if necessary
-  if (willCompress) {
-    free((void*)data);
-  }
-
   // Close the mapped file if it was opened
   if (mFile != 0) {
     mFile->close();
@@ -386,8 +347,11 @@ Archive::writeToDisk(bool CreateSymbolTable, bool TruncateNames, bool Compress,
 {
   // Make sure they haven't opened up the file, not loaded it,
   // but are now trying to write it which would wipe out the file.
-  assert(!(members.empty() && mapfile->size() > 8) &&
-         "Can't write an archive not opened for writing");
+  if (members.empty() && mapfile->size() > 8) {
+    if (ErrMsg)
+      *ErrMsg = "Can't write an archive not opened for writing";
+    return true;
+  }
 
   // Create a temporary file to store the archive in
   sys::Path TmpArchive = archPath;