1 //===---- llvm/Support/IRReader.h - Reader for LLVM IR files ----*- C++ -*-===//
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 // This file defines functions for reading LLVM IR. They support both
11 // Bitcode and Assembly, automatically detecting the input format.
13 // These functions must be defined in a header file in order to avoid
14 // library dependencies, since they reference both Bitcode and Assembly
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_SUPPORT_IRREADER_H
20 #define LLVM_SUPPORT_IRREADER_H
22 #include "llvm/ADT/OwningPtr.h"
23 #include "llvm/Assembly/Parser.h"
24 #include "llvm/Bitcode/ReaderWriter.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/system_error.h"
31 /// If the given MemoryBuffer holds a bitcode image, return a Module for it
32 /// which does lazy deserialization of function bodies. Otherwise, attempt to
33 /// parse it as LLVM Assembly and return a fully populated Module. This
34 /// function *always* takes ownership of the given MemoryBuffer.
35 inline Module *getLazyIRModule(MemoryBuffer *Buffer,
37 LLVMContext &Context) {
38 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
39 (const unsigned char *)Buffer->getBufferEnd())) {
41 Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
43 Err = SMDiagnostic(Buffer->getBufferIdentifier(), ErrMsg);
44 // ParseBitcodeFile does not take ownership of the Buffer in the
51 return ParseAssembly(Buffer, 0, Err, Context);
54 /// If the given file holds a bitcode image, return a Module
55 /// for it which does lazy deserialization of function bodies. Otherwise,
56 /// attempt to parse it as LLVM Assembly and return a fully populated
58 inline Module *getLazyIRFileModule(const std::string &Filename,
60 LLVMContext &Context) {
61 OwningPtr<MemoryBuffer> File;
62 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
63 Err = SMDiagnostic(Filename,
64 "Could not open input file: " + ec.message());
68 return getLazyIRModule(File.take(), Err, Context);
71 /// If the given MemoryBuffer holds a bitcode image, return a Module
72 /// for it. Otherwise, attempt to parse it as LLVM Assembly and return
73 /// a Module for it. This function *always* takes ownership of the given
75 inline Module *ParseIR(MemoryBuffer *Buffer,
77 LLVMContext &Context) {
78 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
79 (const unsigned char *)Buffer->getBufferEnd())) {
81 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
83 Err = SMDiagnostic(Buffer->getBufferIdentifier(), ErrMsg);
84 // ParseBitcodeFile does not take ownership of the Buffer.
89 return ParseAssembly(Buffer, 0, Err, Context);
92 /// If the given file holds a bitcode image, return a Module for it.
93 /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
95 inline Module *ParseIRFile(const std::string &Filename,
97 LLVMContext &Context) {
98 OwningPtr<MemoryBuffer> File;
99 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
100 Err = SMDiagnostic(Filename,
101 "Could not open input file: " + ec.message());
105 return ParseIR(File.take(), Err, Context);