LLVM  3.7.0
IRReader.cpp
Go to the documentation of this file.
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-c/Core.h"
12 #include "llvm-c/IRReader.h"
13 #include "llvm/AsmParser/Parser.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/Timer.h"
21 #include <system_error>
22 
23 using namespace llvm;
24 
25 namespace llvm {
26  extern bool TimePassesIsEnabled;
27 }
28 
29 static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
30 static const char *const TimeIRParsingName = "Parse IR";
31 
32 static std::unique_ptr<Module>
33 getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
34  LLVMContext &Context) {
35  if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
36  (const unsigned char *)Buffer->getBufferEnd())) {
37  ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
38  getLazyBitcodeModule(std::move(Buffer), Context);
39  if (std::error_code EC = ModuleOrErr.getError()) {
40  Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
41  EC.message());
42  return nullptr;
43  }
44  return std::move(ModuleOrErr.get());
45  }
46 
47  return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
48 }
49 
50 std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
51  SMDiagnostic &Err,
52  LLVMContext &Context) {
55  if (std::error_code EC = FileOrErr.getError()) {
56  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
57  "Could not open input file: " + EC.message());
58  return nullptr;
59  }
60 
61  return getLazyIRModule(std::move(FileOrErr.get()), Err, Context);
62 }
63 
64 std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
65  LLVMContext &Context) {
68  if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
69  (const unsigned char *)Buffer.getBufferEnd())) {
70  ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
71  parseBitcodeFile(Buffer, Context);
72  if (std::error_code EC = ModuleOrErr.getError()) {
74  EC.message());
75  return nullptr;
76  }
77  return std::move(ModuleOrErr.get());
78  }
79 
80  return parseAssembly(Buffer, Err, Context);
81 }
82 
83 std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
84  LLVMContext &Context) {
87  if (std::error_code EC = FileOrErr.getError()) {
88  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
89  "Could not open input file: " + EC.message());
90  return nullptr;
91  }
92 
93  return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context);
94 }
95 
96 //===----------------------------------------------------------------------===//
97 // C API.
98 //===----------------------------------------------------------------------===//
99 
101  LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
102  char **OutMessage) {
103  SMDiagnostic Diag;
104 
105  std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
106  *OutM =
107  wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
108 
109  if(!*OutM) {
110  if (OutMessage) {
111  std::string buf;
112  raw_string_ostream os(buf);
113 
114  Diag.print(nullptr, os, false);
115  os.flush();
116 
117  *OutMessage = strdup(buf.c_str());
118  }
119  return 1;
120  }
121 
122  return 0;
123 }
void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true) const
Definition: SourceMgr.cpp:335
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
Used to pass regions of memory through LLVM interfaces.
Definition: Support.h:36
std::unique_ptr< Module > getLazyIRFileModule(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context)
If the given file holds a bitcode image, return a Module for it which does lazy deserialization of fu...
Definition: IRReader.cpp:50
const char * getBufferStart() const
Definition: MemoryBuffer.h:161
NamedRegionTimer - This class is basically a combination of TimeRegion and Timer. ...
Definition: Timer.h:147
std::unique_ptr< Module > parseIRFile(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context)
If the given file holds a bitcode image, return a Module for it.
Definition: IRReader.cpp:83
LLVMTargetDataRef wrap(const DataLayout *P)
Definition: DataLayout.h:469
#define T
std::unique_ptr< Module > parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
parseAssemblyFile and parseAssemblyString are wrappers around this function.
Definition: Parser.cpp:34
static std::unique_ptr< Module > getLazyIRModule(std::unique_ptr< MemoryBuffer > Buffer, SMDiagnostic &Err, LLVMContext &Context)
Definition: IRReader.cpp:33
DataLayout * unwrap(LLVMTargetDataRef P)
Definition: DataLayout.h:465
static const char *const TimeIRParsingName
Definition: IRReader.cpp:30
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
const char * getBufferEnd() const
Definition: MemoryBuffer.h:162
ErrorOr< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr)
Read the specified bitcode file, returning the module.
struct LLVMOpaqueContext * LLVMContextRef
The top-level container for all LLVM global data.
Definition: Core.h:70
int LLVMBool
Definition: Support.h:29
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Module.h This file contains the declarations for the Module class.
bool isBitcode(const unsigned char *BufPtr, const unsigned char *BufEnd)
isBitcode - Return true if the given bytes are the magic bytes for LLVM IR bitcode, either with or without a wrapper.
Definition: ReaderWriter.h:103
LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage)
Read LLVM IR from a memory buffer and convert it into an in-memory Module object. ...
Definition: IRReader.cpp:100
static const char *const TimeIRParsingGroupName
Definition: IRReader.cpp:29
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Core.h:78
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:159
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:465
ErrorOr< std::unique_ptr< Module > > getLazyBitcodeModule(std::unique_ptr< MemoryBuffer > &&Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr, bool ShouldLazyLoadMetadata=false)
Read the header of the specified bitcode buffer and prepare for lazy deserialization of function bodi...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
bool TimePassesIsEnabled
If the user specifies the -time-passes argument on an LLVM tool command line then the value of this b...
Definition: IRReader.cpp:26
std::unique_ptr< Module > parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err, LLVMContext &Context)
If the given MemoryBuffer holds a bitcode image, return a Module for it.
Definition: IRReader.cpp:64
reference get()
Definition: ErrorOr.h:175
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:233