LLVM API Documentation

BitWriter.cpp

Go to the documentation of this file.
00001 //===-- BitWriter.cpp -----------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm-c/BitWriter.h"
00011 #include "llvm/Bitcode/ReaderWriter.h"
00012 #include <fstream>
00013 
00014 using namespace llvm;
00015 
00016 
00017 /*===-- Operations on modules ---------------------------------------------===*/
00018 
00019 int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
00020   std::ofstream OS(Path, std::ios_base::out|std::ios::trunc|std::ios::binary);
00021   
00022   if (!OS.fail())
00023     WriteBitcodeToFile(unwrap(M), OS);
00024   
00025   if (OS.fail())
00026     return -1;
00027   
00028   return 0;
00029 }
00030 
00031 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR >= 4)
00032 #include <ext/stdio_filebuf.h>
00033 
00034 // FIXME: Control this with configure? Provide some portable abstraction in
00035 // libSystem? As is, the user will just get a linker error if they use this on 
00036 // non-GCC. Some C++ stdlibs even have ofstream::ofstream(int fd).
00037 int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
00038   __gnu_cxx::stdio_filebuf<char> Buffer(FileHandle, std::ios_base::out |
00039                                                     std::ios::trunc |
00040                                                     std::ios::binary);
00041   std::ostream OS(&Buffer);
00042   
00043   if (!OS.fail())
00044     WriteBitcodeToFile(unwrap(M), OS);
00045   
00046   if (OS.fail())
00047     return -1;
00048   
00049   return 0;
00050 }
00051 
00052 #else
00053 
00054 int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
00055   return -1; // Not supported.
00056 }
00057 
00058 #endif