[ASan] Make insertion of version mismatch guard configurable
[llvm-core.git] / lib / IRReader / IRReader.cpp
blob7ca6c2fca52a2959e194726a9c322f0e5c38e34a
1 //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IRReader/IRReader.h"
10 #include "llvm-c/IRReader.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/Bitcode/BitcodeReader.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/Timer.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <system_error>
21 using namespace llvm;
23 namespace llvm {
24 extern bool TimePassesIsEnabled;
27 static const char *const TimeIRParsingGroupName = "irparse";
28 static const char *const TimeIRParsingGroupDescription = "LLVM IR Parsing";
29 static const char *const TimeIRParsingName = "parse";
30 static const char *const TimeIRParsingDescription = "Parse IR";
32 std::unique_ptr<Module>
33 llvm::getLazyIRModule(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 Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
38 std::move(Buffer), Context, ShouldLazyLoadMetadata);
39 if (Error E = ModuleOrErr.takeError()) {
40 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
41 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
42 EIB.message());
43 });
44 return nullptr;
46 return std::move(ModuleOrErr.get());
49 return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
52 std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
53 SMDiagnostic &Err,
54 LLVMContext &Context,
55 bool ShouldLazyLoadMetadata) {
56 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
57 MemoryBuffer::getFileOrSTDIN(Filename);
58 if (std::error_code EC = FileOrErr.getError()) {
59 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
60 "Could not open input file: " + EC.message());
61 return nullptr;
64 return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
65 ShouldLazyLoadMetadata);
68 std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
69 LLVMContext &Context,
70 bool UpgradeDebugInfo,
71 StringRef DataLayoutString) {
72 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
73 TimeIRParsingGroupName, TimeIRParsingGroupDescription,
74 TimePassesIsEnabled);
75 if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
76 (const unsigned char *)Buffer.getBufferEnd())) {
77 Expected<std::unique_ptr<Module>> ModuleOrErr =
78 parseBitcodeFile(Buffer, Context);
79 if (Error E = ModuleOrErr.takeError()) {
80 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
81 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
82 EIB.message());
83 });
84 return nullptr;
86 if (!DataLayoutString.empty())
87 ModuleOrErr.get()->setDataLayout(DataLayoutString);
88 return std::move(ModuleOrErr.get());
91 return parseAssembly(Buffer, Err, Context, nullptr, UpgradeDebugInfo,
92 DataLayoutString);
95 std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
96 LLVMContext &Context,
97 bool UpgradeDebugInfo,
98 StringRef DataLayoutString) {
99 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
100 MemoryBuffer::getFileOrSTDIN(Filename);
101 if (std::error_code EC = FileOrErr.getError()) {
102 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
103 "Could not open input file: " + EC.message());
104 return nullptr;
107 return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context,
108 UpgradeDebugInfo, DataLayoutString);
111 //===----------------------------------------------------------------------===//
112 // C API.
113 //===----------------------------------------------------------------------===//
115 LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
116 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
117 char **OutMessage) {
118 SMDiagnostic Diag;
120 std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
121 *OutM =
122 wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
124 if(!*OutM) {
125 if (OutMessage) {
126 std::string buf;
127 raw_string_ostream os(buf);
129 Diag.print(nullptr, os, false);
130 os.flush();
132 *OutMessage = strdup(buf.c_str());
134 return 1;
137 return 0;