1 //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IRReader/IRReader.h"
11 #include "llvm-c/Core.h"
12 #include "llvm-c/IRReader.h"
13 #include "llvm/ADT/OwningPtr.h"
14 #include "llvm/AsmParser/Parser.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/Timer.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/system_error.h"
27 extern bool TimePassesIsEnabled;
30 static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
31 static const char *const TimeIRParsingName = "Parse IR";
34 Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
35 LLVMContext &Context) {
36 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
37 (const unsigned char *)Buffer->getBufferEnd())) {
39 Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
41 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
43 // ParseBitcodeFile does not take ownership of the Buffer in the
50 return ParseAssembly(Buffer, 0, Err, Context);
53 Module *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());
62 return getLazyIRModule(File.take(), Err, Context);
65 Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
66 LLVMContext &Context) {
67 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
69 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
70 (const unsigned char *)Buffer->getBufferEnd())) {
72 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
74 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
76 // ParseBitcodeFile does not take ownership of the Buffer.
81 return ParseAssembly(Buffer, 0, Err, Context);
84 Module *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());
93 return ParseIR(File.take(), Err, Context);
96 //===----------------------------------------------------------------------===//
98 //===----------------------------------------------------------------------===//
100 LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
101 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
105 *OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef)));
110 raw_string_ostream os(buf);
112 Diag.print(NULL, os, false);
115 *OutMessage = strdup(buf.c_str());