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"
14#include "llvm/Bitcode/ReaderWriter.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Module.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/SourceMgr.h"
19#include "llvm/Support/Timer.h"
20#include "llvm/Support/raw_ostream.h"
21#include <system_error>
22
23using namespace llvm;
24
25namespace llvm {
26  extern bool TimePassesIsEnabled;
27}
28
29static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
30static const char *const TimeIRParsingName = "Parse IR";
31
32static std::unique_ptr<Module>
33getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
34                LLVMContext &Context, bool ShouldLazyLoadMetadata) {
35  if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
36                (const unsigned char *)Buffer->getBufferEnd())) {
37    ErrorOr<std::unique_ptr<Module>> ModuleOrErr = getLazyBitcodeModule(
38        std::move(Buffer), Context, ShouldLazyLoadMetadata);
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
50std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
51                                                  SMDiagnostic &Err,
52                                                  LLVMContext &Context,
53                                                  bool ShouldLazyLoadMetadata) {
54  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
55      MemoryBuffer::getFileOrSTDIN(Filename);
56  if (std::error_code EC = FileOrErr.getError()) {
57    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
58                       "Could not open input file: " + EC.message());
59    return nullptr;
60  }
61
62  return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
63                         ShouldLazyLoadMetadata);
64}
65
66std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef 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    ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
73        parseBitcodeFile(Buffer, Context);
74    if (std::error_code EC = ModuleOrErr.getError()) {
75      Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
76                         EC.message());
77      return nullptr;
78    }
79    return std::move(ModuleOrErr.get());
80  }
81
82  return parseAssembly(Buffer, Err, Context);
83}
84
85std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
86                                          LLVMContext &Context) {
87  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
88      MemoryBuffer::getFileOrSTDIN(Filename);
89  if (std::error_code EC = FileOrErr.getError()) {
90    Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
91                       "Could not open input file: " + EC.message());
92    return nullptr;
93  }
94
95  return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context);
96}
97
98//===----------------------------------------------------------------------===//
99// C API.
100//===----------------------------------------------------------------------===//
101
102LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
103                              LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
104                              char **OutMessage) {
105  SMDiagnostic Diag;
106
107  std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
108  *OutM =
109      wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
110
111  if(!*OutM) {
112    if (OutMessage) {
113      std::string buf;
114      raw_string_ostream os(buf);
115
116      Diag.print(nullptr, os, false);
117      os.flush();
118
119      *OutMessage = strdup(buf.c_str());
120    }
121    return 1;
122  }
123
124  return 0;
125}
126