Update getLazyBitcodeModule to use ErrorOr for error handling.
[oota-llvm.git] / lib / IRReader / IRReader.cpp
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/ADT/OwningPtr.h"
14 #include "llvm/AsmParser/Parser.h"
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/Timer.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/system_error.h"
23
24 using namespace llvm;
25
26 namespace llvm {
27   extern bool TimePassesIsEnabled;
28 }
29
30 static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
31 static const char *const TimeIRParsingName = "Parse IR";
32
33
34 Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
35                               LLVMContext &Context) {
36   if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
37                 (const unsigned char *)Buffer->getBufferEnd())) {
38     std::string ErrMsg;
39     ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
40     if (error_code EC = ModuleOrErr.getError()) {
41       Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
42                          EC.message());
43       // ParseBitcodeFile does not take ownership of the Buffer in the
44       // case of an error.
45       delete Buffer;
46       return NULL;
47     }
48     return ModuleOrErr.get();
49   }
50
51   return ParseAssembly(Buffer, 0, Err, Context);
52 }
53
54 Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
55                                   LLVMContext &Context) {
56   OwningPtr<MemoryBuffer> File;
57   if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
58     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
66 Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
67                       LLVMContext &Context) {
68   NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
69                      TimePassesIsEnabled);
70   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
85 Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
86                           LLVMContext &Context) {
87   OwningPtr<MemoryBuffer> File;
88   if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
89     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 }
96
97 //===----------------------------------------------------------------------===//
98 // C API.
99 //===----------------------------------------------------------------------===//
100
101 LLVMBool 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 }