[llvm-commits] [llvm] r79840 - in /llvm/trunk: examples/BrainF/BrainFDriver.cpp examples/ModuleMaker/ModuleMaker.cpp include/llvm/Bitcode/ReaderWriter.h lib/Bitcode/Reader/Deserialize.cpp lib/Bitcode/Writer/BitWriter.cpp lib/Bitcode/Writer/BitcodeWriter.cpp lib/Bitcode/Writer/BitcodeWriterPass.cpp tools/bugpoint/OptimizerDriver.cpp tools/lto/LTOCodeGenerator.cpp

Chris Lattner sabre at nondot.org
Sun Aug 23 00:49:08 PDT 2009


Author: lattner
Date: Sun Aug 23 02:49:08 2009
New Revision: 79840

URL: http://llvm.org/viewvc/llvm-project?rev=79840&view=rev
Log:
eliminate the std::ostream forms of the bitcode writing APIs.

Modified:
    llvm/trunk/examples/BrainF/BrainFDriver.cpp
    llvm/trunk/examples/ModuleMaker/ModuleMaker.cpp
    llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
    llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp
    llvm/trunk/lib/Bitcode/Writer/BitWriter.cpp
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriterPass.cpp
    llvm/trunk/tools/bugpoint/OptimizerDriver.cpp
    llvm/trunk/tools/lto/LTOCodeGenerator.cpp

Modified: llvm/trunk/examples/BrainF/BrainFDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/BrainF/BrainFDriver.cpp?rev=79840&r1=79839&r2=79840&view=diff

==============================================================================
--- llvm/trunk/examples/BrainF/BrainFDriver.cpp (original)
+++ llvm/trunk/examples/BrainF/BrainFDriver.cpp Sun Aug 23 02:49:08 2009
@@ -32,11 +32,12 @@
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/ExecutionEngine/GenericValue.h"
 #include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/Target/TargetSelect.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ManagedStatic.h"
-#include "llvm/Target/TargetSelect.h"
-#include <fstream>
+#include "llvm/Support/raw_ostream.h"
 #include <iostream>
+#include <fstream>
 using namespace llvm;
 
 //Command line options
@@ -91,50 +92,49 @@
   LLVMContext &Context = getGlobalContext();
 
   if (InputFilename == "") {
-    std::cerr<<"Error: You must specify the filename of the program to "
+    errs() << "Error: You must specify the filename of the program to "
     "be compiled.  Use --help to see the options.\n";
     abort();
   }
 
   //Get the output stream
-  std::ostream *out = &std::cout;
+  raw_ostream *out = &outs();
   if (!JIT) {
     if (OutputFilename == "") {
       std::string base = InputFilename;
-      if (InputFilename == "-") {base = "a";}
+      if (InputFilename == "-") { base = "a"; }
 
-      //Use default filename
-      const char *suffix = ".bc";
-      OutputFilename = base+suffix;
+      // Use default filename.
+      OutputFilename = base+".bc";
     }
     if (OutputFilename != "-") {
-      out = new std::
-        ofstream(OutputFilename.c_str(),
-                 std::ios::out | std::ios::trunc | std::ios::binary);
+      std::string ErrInfo;
+      out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
+                               raw_fd_ostream::F_Force|
+                               raw_fd_ostream::F_Binary);
     }
   }
 
   //Get the input stream
   std::istream *in = &std::cin;
-  if (InputFilename != "-") {
+  if (InputFilename != "-")
     in = new std::ifstream(InputFilename.c_str());
-  }
 
   //Gather the compile flags
   BrainF::CompileFlags cf = BrainF::flag_off;
-  if (ArrayBoundsChecking) {
+  if (ArrayBoundsChecking)
     cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
-  }
 
   //Read the BrainF program
   BrainF bf;
   Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
-  if (in != &std::cin) {delete in;}
+  if (in != &std::cin)
+    delete in;
   addMainFunction(mod);
 
   //Verify generated code
   if (verifyModule(*mod)) {
-    std::cerr<<"Error: module failed verification.  This shouldn't happen.\n";
+    errs() << "Error: module failed verification.  This shouldn't happen.\n";
     abort();
   }
 
@@ -142,7 +142,7 @@
   if (JIT) {
     InitializeNativeTarget();
 
-    std::cout << "------- Running JIT -------\n";
+    outs() << "------- Running JIT -------\n";
     ExecutionEngine *ee = EngineBuilder(mod).create();
     std::vector<GenericValue> args;
     Function *brainf_func = mod->getFunction("brainf");
@@ -152,7 +152,8 @@
   }
 
   //Clean up
-  if (out != &std::cout) {delete out;}
+  if (out != &outs())
+    delete out;
   delete mod;
 
   llvm_shutdown();

Modified: llvm/trunk/examples/ModuleMaker/ModuleMaker.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/examples/ModuleMaker/ModuleMaker.cpp?rev=79840&r1=79839&r2=79840&view=diff

==============================================================================
--- llvm/trunk/examples/ModuleMaker/ModuleMaker.cpp (original)
+++ llvm/trunk/examples/ModuleMaker/ModuleMaker.cpp Sun Aug 23 02:49:08 2009
@@ -19,7 +19,7 @@
 #include "llvm/Constants.h"
 #include "llvm/Instructions.h"
 #include "llvm/Bitcode/ReaderWriter.h"
-#include <iostream>
+#include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
 int main() {
@@ -56,7 +56,7 @@
   BB->getInstList().push_back(ReturnInst::Create(Context, Add));
 
   // Output the bitcode file to stdout
-  WriteBitcodeToFile(M, std::cout);
+  WriteBitcodeToFile(M, outs());
 
   // Delete the module and all of its contents.
   delete M;

Modified: llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/ReaderWriter.h?rev=79840&r1=79839&r2=79840&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Bitcode/ReaderWriter.h (original)
+++ llvm/trunk/include/llvm/Bitcode/ReaderWriter.h Sun Aug 23 02:49:08 2009
@@ -14,7 +14,6 @@
 #ifndef LLVM_BITCODE_H
 #define LLVM_BITCODE_H
 
-#include <iosfwd>
 #include <string>
 
 namespace llvm {
@@ -41,10 +40,6 @@
   Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
                            std::string *ErrMsg = 0);
 
-  /// WriteBitcodeToFile - Write the specified module to the specified output
-  /// stream.
-  void WriteBitcodeToFile(const Module *M, std::ostream &Out);
-
   /// WriteBitcodeToFile - Write the specified module to the specified
   /// raw output stream.
   void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
@@ -53,10 +48,6 @@
   /// raw output stream.
   void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
 
-  /// CreateBitcodeWriterPass - Create and return a pass that writes the module
-  /// to the specified ostream.
-  ModulePass *CreateBitcodeWriterPass(std::ostream &Str);
-
   /// createBitcodeWriterPass - Create and return a pass that writes the module
   /// to the specified ostream.
   ModulePass *createBitcodeWriterPass(raw_ostream &Str);

Modified: llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp?rev=79840&r1=79839&r2=79840&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/Deserialize.cpp Sun Aug 23 02:49:08 2009
@@ -12,11 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Bitcode/Deserialize.h"
-
-#ifdef DEBUG_BACKPATCH
-#include "llvm/Support/Streams.h"
-#endif
-
+#include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
 Deserializer::Deserializer(BitstreamReader& stream)
@@ -357,7 +353,7 @@
   assert (!HasFinalPtr(E) && "Pointer already registered.");
 
 #ifdef DEBUG_BACKPATCH
-  llvm::cerr << "RegisterPtr: " << PtrId << " => " << Ptr << "\n";
+  errs() << "RegisterPtr: " << PtrId << " => " << Ptr << "\n";
 #endif 
   
   SetPtr(E,Ptr);
@@ -377,8 +373,8 @@
     PtrRef = GetFinalPtr(E);
 
 #ifdef DEBUG_BACKPATCH
-    llvm::cerr << "ReadUintPtr: " << PtrId
-               << " <-- " <<  (void*) GetFinalPtr(E) << '\n';
+    errs() << "ReadUintPtr: " << PtrId
+           << " <-- " <<  (void*) GetFinalPtr(E) << '\n';
 #endif    
   }
   else {
@@ -386,7 +382,7 @@
             "Client forbids backpatching for this pointer.");
     
 #ifdef DEBUG_BACKPATCH
-    llvm::cerr << "ReadUintPtr: " << PtrId << " (NO PTR YET)\n";
+    errs() << "ReadUintPtr: " << PtrId << " (NO PTR YET)\n";
 #endif
     
     // Register backpatch.  Check the freelist for a BPNode.

Modified: llvm/trunk/lib/Bitcode/Writer/BitWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitWriter.cpp?rev=79840&r1=79839&r2=79840&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitWriter.cpp Sun Aug 23 02:49:08 2009
@@ -9,43 +9,31 @@
 
 #include "llvm-c/BitWriter.h"
 #include "llvm/Bitcode/ReaderWriter.h"
-#include <fstream>
-
+#include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
 
 /*===-- Operations on modules ---------------------------------------------===*/
 
 int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
-  std::ofstream OS(Path, std::ios_base::out|std::ios::trunc|std::ios::binary);
-  
-  if (!OS.fail())
-    WriteBitcodeToFile(unwrap(M), OS);
+  std::string ErrorInfo;
+  raw_fd_ostream OS(Path, ErrorInfo,
+                    raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
   
-  if (OS.fail())
+  if (!ErrorInfo.empty())
     return -1;
   
+  WriteBitcodeToFile(unwrap(M), OS);
   return 0;
 }
 
 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR >= 4)
 #include <ext/stdio_filebuf.h>
 
-// FIXME: Control this with configure? Provide some portable abstraction in
-// libSystem? As is, the user will just get a linker error if they use this on 
-// non-GCC. Some C++ stdlibs even have ofstream::ofstream(int fd).
 int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
-  __gnu_cxx::stdio_filebuf<char> Buffer(FileHandle, std::ios_base::out |
-                                                    std::ios::trunc |
-                                                    std::ios::binary);
-  std::ostream OS(&Buffer);
-  
-  if (!OS.fail())
-    WriteBitcodeToFile(unwrap(M), OS);
-  
-  if (OS.fail())
-    return -1;
+  raw_fd_ostream OS(FileHandle, false);
   
+  WriteBitcodeToFile(unwrap(M), OS);
   return 0;
 }
 

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=79840&r1=79839&r2=79840&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Sun Aug 23 02:49:08 2009
@@ -26,7 +26,6 @@
 #include "llvm/ValueSymbolTable.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
-#include "llvm/Support/Streams.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/System/Program.h"
 using namespace llvm;
@@ -1468,16 +1467,6 @@
 
 /// WriteBitcodeToFile - Write the specified module to the specified output
 /// stream.
-void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
-  raw_os_ostream RawOut(Out);
-  // If writing to stdout, set binary mode.
-  if (llvm::cout == Out)
-    sys::Program::ChangeStdoutToBinary();
-  WriteBitcodeToFile(M, RawOut);
-}
-
-/// WriteBitcodeToFile - Write the specified module to the specified output
-/// stream.
 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
   std::vector<unsigned char> Buffer;
   BitstreamWriter Stream(Buffer);

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriterPass.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriterPass.cpp?rev=79840&r1=79839&r2=79840&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriterPass.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriterPass.cpp Sun Aug 23 02:49:08 2009
@@ -17,24 +17,16 @@
 
 namespace {
   class WriteBitcodePass : public ModulePass {
-    // FIXME: Kill off std::ostream
-    std::ostream *Out;
-    raw_ostream *RawOut; // raw_ostream to print on
+    raw_ostream &OS; // raw_ostream to print on
   public:
     static char ID; // Pass identification, replacement for typeid
-    explicit WriteBitcodePass(std::ostream &o)
-      : ModulePass(&ID), Out(&o), RawOut(0) {}
     explicit WriteBitcodePass(raw_ostream &o)
-      : ModulePass(&ID), Out(0), RawOut(&o) {}
+      : ModulePass(&ID), OS(o) {}
     
     const char *getPassName() const { return "Bitcode Writer"; }
     
     bool runOnModule(Module &M) {
-      if (Out) {
-        WriteBitcodeToFile(&M, *Out);
-      } else {
-        WriteBitcodeToFile(&M, *RawOut);
-      }
+      WriteBitcodeToFile(&M, OS);
       return false;
     }
   };
@@ -42,13 +34,6 @@
 
 char WriteBitcodePass::ID = 0;
 
-/// CreateBitcodeWriterPass - Create and return a pass that writes the module
-/// to the specified ostream.
-ModulePass *llvm::CreateBitcodeWriterPass(std::ostream &Str) {
-  return new WriteBitcodePass(Str);
-}
-
-
 /// createBitcodeWriterPass - Create and return a pass that writes the module
 /// to the specified ostream.
 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str) {

Modified: llvm/trunk/tools/bugpoint/OptimizerDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/OptimizerDriver.cpp?rev=79840&r1=79839&r2=79840&view=diff

==============================================================================
--- llvm/trunk/tools/bugpoint/OptimizerDriver.cpp (original)
+++ llvm/trunk/tools/bugpoint/OptimizerDriver.cpp Sun Aug 23 02:49:08 2009
@@ -51,10 +51,10 @@
 ///
 bool BugDriver::writeProgramToFile(const std::string &Filename,
                                    Module *M) const {
-  std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
-                               std::ios::binary;
-  std::ofstream Out(Filename.c_str(), io_mode);
-  if (!Out.good()) return true;
+  std::string ErrInfo;
+  raw_fd_ostream Out(Filename.c_str(), ErrInfo,
+                     raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
+  if (!ErrInfo.empty()) return true;
   
   WriteBitcodeToFile(M ? M : Program, Out);
   return false;
@@ -83,11 +83,10 @@
 }
 
 int BugDriver::runPassesAsChild(const std::vector<const PassInfo*> &Passes) {
-
-  std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
-                               std::ios::binary;
-  std::ofstream OutFile(ChildOutput.c_str(), io_mode);
-  if (!OutFile.good()) {
+  std::string ErrInfo;
+  raw_fd_ostream OutFile(ChildOutput.c_str(), ErrInfo,
+                         raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
+  if (!ErrInfo.empty()) {
     errs() << "Error opening bitcode file: " << ChildOutput << "\n";
     return 1;
   }
@@ -106,7 +105,7 @@
   PM.add(createVerifierPass());
 
   // Write bitcode out to disk as the last step...
-  PM.add(CreateBitcodeWriterPass(OutFile));
+  PM.add(createBitcodeWriterPass(OutFile));
 
   // Run all queued passes.
   PM.run(*Program);
@@ -146,12 +145,15 @@
            << ErrMsg << "\n";
     return(1);
   }
-  std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
-                               std::ios::binary;
-  std::ofstream InFile(inputFilename.c_str(), io_mode);
-  if (!InFile.good()) {
+  
+  std::string ErrInfo;
+  raw_fd_ostream InFile(inputFilename.c_str(), ErrInfo,
+                        raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
+  
+  
+  if (!ErrInfo.empty()) {
     errs() << "Error opening bitcode file: " << inputFilename << "\n";
-    return(1);
+    return 1;
   }
   WriteBitcodeToFile(Program, InFile);
   InFile.close();

Modified: llvm/trunk/tools/lto/LTOCodeGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/LTOCodeGenerator.cpp?rev=79840&r1=79839&r2=79840&view=diff

==============================================================================
--- llvm/trunk/tools/lto/LTOCodeGenerator.cpp (original)
+++ llvm/trunk/tools/lto/LTOCodeGenerator.cpp Sun Aug 23 02:49:08 2009
@@ -47,10 +47,7 @@
 #include "llvm/Transforms/IPO.h"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Config/config.h"
-
-
 #include <cstdlib>
-#include <fstream>
 #include <unistd.h>
 #include <fcntl.h>
 
@@ -139,31 +136,34 @@
 }
 
 
-bool LTOCodeGenerator::writeMergedModules(const char* path, std::string& errMsg)
-{
-    if ( this->determineTarget(errMsg) ) 
-        return true;
+bool LTOCodeGenerator::writeMergedModules(const char *path,
+                                          std::string &errMsg) {
+  if (determineTarget(errMsg))
+    return true;
 
-    // mark which symbols can not be internalized 
-    this->applyScopeRestrictions();
+  // mark which symbols can not be internalized 
+  applyScopeRestrictions();
 
-    // create output file
-    std::ofstream out(path, std::ios_base::out|std::ios::trunc|std::ios::binary);
-    if ( out.fail() ) {
-        errMsg = "could not open bitcode file for writing: ";
-        errMsg += path;
-        return true;
-    }
-    
-    // write bitcode to it
-    WriteBitcodeToFile(_linker.getModule(), out);
-    if ( out.fail() ) {
-        errMsg = "could not write bitcode file: ";
-        errMsg += path;
-        return true;
-    }
+  // create output file
+  std::string ErrInfo;
+  raw_fd_ostream Out(path, ErrInfo,
+                     raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
+  if (!ErrInfo.empty()) {
+    errMsg = "could not open bitcode file for writing: ";
+    errMsg += path;
+    return true;
+  }
     
-    return false;
+  // write bitcode to it
+  WriteBitcodeToFile(_linker.getModule(), Out);
+  
+  if (Out.has_error()) {
+    errMsg = "could not write bitcode file: ";
+    errMsg += path;
+    return true;
+  }
+  
+  return false;
 }
 
 





More information about the llvm-commits mailing list