blob: 935e81d3c8c50ecda9480773d3bf3af74e250183 [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"
11#include "llvm/ADT/OwningPtr.h"
12#include "llvm/Assembly/Parser.h"
13#include "llvm/Bitcode/ReaderWriter.h"
Peter Zotov285eed62013-11-06 09:21:15 +000014#include "llvm/IR/LLVMContext.h"
15#include "llvm/IR/Module.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000016#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/Support/SourceMgr.h"
18#include "llvm/Support/system_error.h"
Eli Benderskyb35a2112013-04-03 15:33:45 +000019#include "llvm/Support/Timer.h"
Peter Zotov285eed62013-11-06 09:21:15 +000020#include "llvm/Support/raw_ostream.h"
21#include "llvm-c/Core.h"
22#include "llvm-c/IRReader.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;
39 Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
40 if (M == 0) {
41 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
42 ErrMsg);
43 // ParseBitcodeFile does not take ownership of the Buffer in the
44 // case of an error.
45 delete Buffer;
46 }
47 return M;
48 }
49
50 return ParseAssembly(Buffer, 0, Err, Context);
51}
52
53Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
Eli Benderskye60fc2f2013-04-01 19:47:56 +000054 LLVMContext &Context) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000055 OwningPtr<MemoryBuffer> File;
Rafael Espindola8c811722013-06-25 05:28:34 +000056 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000057 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
58 "Could not open input file: " + ec.message());
59 return 0;
60 }
61
62 return getLazyIRModule(File.take(), Err, Context);
63}
64
65Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
66 LLVMContext &Context) {
Eli Benderskyb35a2112013-04-03 15:33:45 +000067 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
68 TimePassesIsEnabled);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000069 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
70 (const unsigned char *)Buffer->getBufferEnd())) {
71 std::string ErrMsg;
72 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
73 if (M == 0)
74 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
75 ErrMsg);
76 // ParseBitcodeFile does not take ownership of the Buffer.
77 delete Buffer;
78 return M;
79 }
80
81 return ParseAssembly(Buffer, 0, Err, Context);
82}
83
84Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
85 LLVMContext &Context) {
86 OwningPtr<MemoryBuffer> File;
Rafael Espindola8c811722013-06-25 05:28:34 +000087 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000088 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
89 "Could not open input file: " + ec.message());
90 return 0;
91 }
92
93 return ParseIR(File.take(), Err, Context);
94}
Peter Zotov285eed62013-11-06 09:21:15 +000095
96//===----------------------------------------------------------------------===//
97// C API.
98//===----------------------------------------------------------------------===//
99
100LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
101 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
102 char **OutMessage) {
103 SMDiagnostic Diag;
104
105 *OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef)));
106
107 if(!*OutM) {
108 if (OutMessage) {
109 std::string buf;
110 raw_string_ostream os(buf);
111
112 Diag.print(NULL, os, false);
113 os.flush();
114
115 *OutMessage = strdup(buf.c_str());
116 }
117 return 1;
118 }
119
120 return 0;
121}