[llvm-commits] [llvm] r59219 - in /llvm/trunk: include/llvm/Support/raw_ostream.h lib/Support/raw_ostream.cpp tools/llc/llc.cpp tools/lto/LTOCodeGenerator.cpp

Daniel Dunbar daniel at zuster.org
Wed Nov 12 21:01:08 PST 2008


Author: ddunbar
Date: Wed Nov 12 23:01:07 2008
New Revision: 59219

URL: http://llvm.org/viewvc/llvm-project?rev=59219&view=rev
Log:
Add Binary flag to raw_fd_ostream constructor.

Document raw_fd_ostream's treatment of "-".

Modified:
    llvm/trunk/include/llvm/Support/raw_ostream.h
    llvm/trunk/lib/Support/raw_ostream.cpp
    llvm/trunk/tools/llc/llc.cpp
    llvm/trunk/tools/lto/LTOCodeGenerator.cpp

Modified: llvm/trunk/include/llvm/Support/raw_ostream.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/raw_ostream.h?rev=59219&r1=59218&r2=59219&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Support/raw_ostream.h (original)
+++ llvm/trunk/include/llvm/Support/raw_ostream.h Wed Nov 12 23:01:07 2008
@@ -156,7 +156,12 @@
   /// error occurs, information about the error is put into ErrorInfo,
   /// and the stream should be immediately destroyed; the string will
   /// be empty if no error occurred.
-  raw_fd_ostream(const char *Filename, std::string &ErrorInfo);
+  ///
+  /// \param Filename - The file to open. If this is "-" then the
+  /// stream will use stdout instead.
+  /// \param Binary - The file should be opened in binary mode on
+  /// platforms that support this distinction.
+  raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo);
   
   /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
   /// ShouldClose is true, this closes the file when 

Modified: llvm/trunk/lib/Support/raw_ostream.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=59219&r1=59218&r2=59219&view=diff

==============================================================================
--- llvm/trunk/lib/Support/raw_ostream.cpp (original)
+++ llvm/trunk/lib/Support/raw_ostream.cpp Wed Nov 12 23:01:07 2008
@@ -13,6 +13,7 @@
 
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/Format.h"
+#include "llvm/System/Program.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Config/config.h"
 #include <ostream>
@@ -200,17 +201,27 @@
 /// occurs, information about the error is put into ErrorInfo, and the
 /// stream should be immediately destroyed; the string will be empty
 /// if no error occurred.
-raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo) {
+raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
+                               std::string &ErrorInfo) {
   ErrorInfo.clear();
 
   // Handle "-" as stdout.
   if (Filename[0] == '-' && Filename[1] == 0) {
     FD = STDOUT_FILENO;
+    // If user requested binary then put stdout into binary mode if
+    // possible.
+    if (Binary)
+      sys::Program::ChangeStdoutToBinary();
     ShouldClose = false;
     return;
   }
   
-  FD = open(Filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+  int Flags = O_WRONLY|O_CREAT|O_TRUNC;
+#ifdef O_BINARY
+  if (Binary)
+    Flags |= O_BINARY;
+#endif
+  FD = open(Filename, Flags, 0644);
   if (FD < 0) {
     ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
     ShouldClose = false;

Modified: llvm/trunk/tools/llc/llc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llc/llc.cpp?rev=59219&r1=59218&r2=59219&view=diff

==============================================================================
--- llvm/trunk/tools/llc/llc.cpp (original)
+++ llvm/trunk/tools/llc/llc.cpp Wed Nov 12 23:01:07 2008
@@ -125,7 +125,7 @@
     sys::RemoveFileOnSignal(sys::Path(OutputFilename));
 
     std::string error;
-    raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), error);
+    raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), true, error);
     if (!error.empty()) {
       std::cerr << error << '\n';
       delete Out;
@@ -142,6 +142,7 @@
 
   OutputFilename = GetFileNameRoot(InputFilename);
     
+  bool Binary = false;
   switch (FileType) {
   case TargetMachine::AssemblyFile:
     if (MArch->Name[0] == 'c') {
@@ -156,9 +157,11 @@
     break;
   case TargetMachine::ObjectFile:
     OutputFilename += ".o";
+    Binary = true;
     break;
   case TargetMachine::DynamicLibrary:
     OutputFilename += LTDL_SHLIB_EXT;
+    Binary = true;
     break;
   }
   
@@ -175,7 +178,7 @@
   sys::RemoveFileOnSignal(sys::Path(OutputFilename));
   
   std::string error;
-  raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), error);
+  raw_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Binary, error);
   if (!error.empty()) {
     std::cerr << error << '\n';
     delete Out;

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

==============================================================================
--- llvm/trunk/tools/lto/LTOCodeGenerator.cpp (original)
+++ llvm/trunk/tools/lto/LTOCodeGenerator.cpp Wed Nov 12 23:01:07 2008
@@ -165,7 +165,7 @@
     // generate assembly code
     bool genResult = false;
     {
-      raw_fd_ostream asmFile(uniqueAsmPath.c_str(), errMsg);
+      raw_fd_ostream asmFile(uniqueAsmPath.c_str(), false, errMsg);
       if (!errMsg.empty())
         return NULL;
       genResult = this->generateAssemblyCode(asmFile, errMsg);





More information about the llvm-commits mailing list