blob: 7356fd005adf8666ff9b78129cec20b435dd947e [file] [log] [blame]
Chandler Carruthe60e57b2013-03-26 02:25:37 +00001//===---- 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"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000011#include "llvm-c/Core.h"
12#include "llvm-c/IRReader.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000013#include "llvm/ADT/OwningPtr.h"
Chandler Carruth9aca9182014-01-07 12:34:26 +000014#include "llvm/AsmParser/Parser.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000015#include "llvm/Bitcode/ReaderWriter.h"
Peter Zotov285eed62013-11-06 09:21:15 +000016#include "llvm/IR/LLVMContext.h"
17#include "llvm/IR/Module.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000018#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/SourceMgr.h"
Eli Benderskyb35a2112013-04-03 15:33:45 +000020#include "llvm/Support/Timer.h"
Peter Zotov285eed62013-11-06 09:21:15 +000021#include "llvm/Support/raw_ostream.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/Support/system_error.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000023
24using namespace llvm;
25
Eli Benderskyb35a2112013-04-03 15:33:45 +000026namespace llvm {
27 extern bool TimePassesIsEnabled;
28}
29
Craig Topperd3a34f82013-07-16 01:17:10 +000030static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
31static const char *const TimeIRParsingName = "Parse IR";
Eli Benderskyb35a2112013-04-03 15:33:45 +000032
33
Chandler Carruthe60e57b2013-03-26 02:25:37 +000034Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
Eli Benderskye60fc2f2013-04-01 19:47:56 +000035 LLVMContext &Context) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000036 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
37 (const unsigned char *)Buffer->getBufferEnd())) {
38 std::string ErrMsg;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000039 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
40 if (error_code EC = ModuleOrErr.getError()) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000041 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000042 EC.message());
Chandler Carruthe60e57b2013-03-26 02:25:37 +000043 // ParseBitcodeFile does not take ownership of the Buffer in the
44 // case of an error.
45 delete Buffer;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000046 return NULL;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000047 }
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000048 return ModuleOrErr.get();
Chandler Carruthe60e57b2013-03-26 02:25:37 +000049 }
50
51 return ParseAssembly(Buffer, 0, Err, Context);
52}
53
54Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
Eli Benderskye60fc2f2013-04-01 19:47:56 +000055 LLVMContext &Context) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000056 OwningPtr<MemoryBuffer> File;
Rafael Espindola8c811722013-06-25 05:28:34 +000057 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000058 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
59 "Could not open input file: " + ec.message());
60 return 0;
61 }
62
63 return getLazyIRModule(File.take(), Err, Context);
64}
65
66Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
67 LLVMContext &Context) {
Eli Benderskyb35a2112013-04-03 15:33:45 +000068 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
69 TimePassesIsEnabled);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000070 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
71 (const unsigned char *)Buffer->getBufferEnd())) {
72 std::string ErrMsg;
73 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
74 if (M == 0)
75 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
76 ErrMsg);
77 // ParseBitcodeFile does not take ownership of the Buffer.
78 delete Buffer;
79 return M;
80 }
81
82 return ParseAssembly(Buffer, 0, Err, Context);
83}
84
85Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
86 LLVMContext &Context) {
87 OwningPtr<MemoryBuffer> File;
Rafael Espindola8c811722013-06-25 05:28:34 +000088 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000089 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
90 "Could not open input file: " + ec.message());
91 return 0;
92 }
93
94 return ParseIR(File.take(), Err, Context);
95}
Peter Zotov285eed62013-11-06 09:21:15 +000096
97//===----------------------------------------------------------------------===//
98// C API.
99//===----------------------------------------------------------------------===//
100
101LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
102 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
103 char **OutMessage) {
104 SMDiagnostic Diag;
105
106 *OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef)));
107
108 if(!*OutM) {
109 if (OutMessage) {
110 std::string buf;
111 raw_string_ostream os(buf);
112
113 Diag.print(NULL, os, false);
114 os.flush();
115
116 *OutMessage = strdup(buf.c_str());
117 }
118 return 1;
119 }
120
121 return 0;
122}