IRReader.cpp revision 263508
1//===---- IRReader.cpp - Reader for LLVM IR files -------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "llvm/IRReader/IRReader.h" 11#include "llvm/ADT/OwningPtr.h" 12#include "llvm/Assembly/Parser.h" 13#include "llvm/Bitcode/ReaderWriter.h" 14#include "llvm/IR/LLVMContext.h" 15#include "llvm/IR/Module.h" 16#include "llvm/Support/MemoryBuffer.h" 17#include "llvm/Support/SourceMgr.h" 18#include "llvm/Support/system_error.h" 19#include "llvm/Support/Timer.h" 20#include "llvm/Support/raw_ostream.h" 21#include "llvm-c/Core.h" 22#include "llvm-c/IRReader.h" 23 24using namespace llvm; 25 26namespace llvm { 27 extern bool TimePassesIsEnabled; 28} 29 30static const char *const TimeIRParsingGroupName = "LLVM IR Parsing"; 31static const char *const TimeIRParsingName = "Parse IR"; 32 33 34Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err, 35 LLVMContext &Context) { 36 if (isBitcode((const unsigned char *)Buffer->getBufferStart(), 37 (const unsigned char *)Buffer->getBufferEnd())) { 38 std::string ErrMsg; 39 Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg); 40 if (M == 0) { 41 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, 42 ErrMsg); 43 // ParseBitcodeFile does not take ownership of the Buffer in the 44 // case of an error. 45 delete Buffer; 46 } 47 return M; 48 } 49 50 return ParseAssembly(Buffer, 0, Err, Context); 51} 52 53Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err, 54 LLVMContext &Context) { 55 OwningPtr<MemoryBuffer> File; 56 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) { 57 Err = SMDiagnostic(Filename, SourceMgr::DK_Error, 58 "Could not open input file: " + ec.message()); 59 return 0; 60 } 61 62 return getLazyIRModule(File.take(), Err, Context); 63} 64 65Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, 66 LLVMContext &Context) { 67 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName, 68 TimePassesIsEnabled); 69 if (isBitcode((const unsigned char *)Buffer->getBufferStart(), 70 (const unsigned char *)Buffer->getBufferEnd())) { 71 std::string ErrMsg; 72 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg); 73 if (M == 0) 74 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, 75 ErrMsg); 76 // ParseBitcodeFile does not take ownership of the Buffer. 77 delete Buffer; 78 return M; 79 } 80 81 return ParseAssembly(Buffer, 0, Err, Context); 82} 83 84Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err, 85 LLVMContext &Context) { 86 OwningPtr<MemoryBuffer> File; 87 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) { 88 Err = SMDiagnostic(Filename, SourceMgr::DK_Error, 89 "Could not open input file: " + ec.message()); 90 return 0; 91 } 92 93 return ParseIR(File.take(), Err, Context); 94} 95 96//===----------------------------------------------------------------------===// 97// C API. 98//===----------------------------------------------------------------------===// 99 100LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef, 101 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, 102 char **OutMessage) { 103 SMDiagnostic Diag; 104 105 *OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef))); 106 107 if(!*OutM) { 108 if (OutMessage) { 109 std::string buf; 110 raw_string_ostream os(buf); 111 112 Diag.print(NULL, os, false); 113 os.flush(); 114 115 *OutMessage = strdup(buf.c_str()); 116 } 117 return 1; 118 } 119 120 return 0; 121} 122