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/Assembly/Parser.h"
23 #include "llvm/Bitcode/ReaderWriter.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SourceMgr.h"
29 /// If the given MemoryBuffer holds a bitcode image, return a Module for it
30 /// which does lazy deserialization of function bodies. Otherwise, attempt to
31 /// parse it as LLVM Assembly and return a fully populated Module. This
32 /// function *always* takes ownership of the given MemoryBuffer.
33 inline Module *getLazyIRModule(MemoryBuffer *Buffer,
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(), ErrMsg);
42 // ParseBitcodeFile does not take ownership of the Buffer in the
49 return ParseAssembly(Buffer, 0, Err, Context);
52 /// If the given file holds a bitcode image, return a Module
53 /// for it which does lazy deserialization of function bodies. Otherwise,
54 /// attempt to parse it as LLVM Assembly and return a fully populated
56 inline Module *getLazyIRFileModule(const std::string &Filename,
58 LLVMContext &Context) {
60 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
62 Err = SMDiagnostic(Filename,
63 "Could not open input file '" + Filename + "'");
67 return getLazyIRModule(F, Err, Context);
70 /// If the given MemoryBuffer holds a bitcode image, return a Module
71 /// for it. Otherwise, attempt to parse it as LLVM Assembly and return
72 /// a Module for it. This function *always* takes ownership of the given
74 inline Module *ParseIR(MemoryBuffer *Buffer,
76 LLVMContext &Context) {
77 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
78 (const unsigned char *)Buffer->getBufferEnd())) {
80 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
81 // ParseBitcodeFile does not take ownership of the Buffer.
84 Err = SMDiagnostic(Buffer->getBufferIdentifier(), ErrMsg);
88 return ParseAssembly(Buffer, 0, Err, Context);
91 /// If the given file holds a bitcode image, return a Module for it.
92 /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
94 inline Module *ParseIRFile(const std::string &Filename,
96 LLVMContext &Context) {
98 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
100 Err = SMDiagnostic(Filename,
101 "Could not open input file '" + Filename + "'");
105 return ParseIR(F, Err, Context);