xref: /llvm-project/llvm/lib/Bitcode/Reader/BitReader.cpp (revision f3ebc3f3d2fdeb4737752fed0ea603634313d115)
1 //===-- BitReader.cpp -----------------------------------------------------===//
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-c/BitReader.h"
11 #include "llvm/Bitcode/ReaderWriter.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include <string>
14 
15 using namespace llvm;
16 
17 /* Builds a module from the bitcode in the specified memory buffer, returning a
18    reference to the module via the OutModule parameter. Returns 0 on success.
19    Optionally returns a human-readable error message via OutMessage. */
20 int LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
21                      LLVMModuleRef *OutModule, char **OutMessage) {
22   std::string Message;
23 
24   *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), &Message));
25   if (!*OutModule) {
26     if (OutMessage)
27       *OutMessage = strdup(Message.c_str());
28     return 1;
29   }
30 
31   return 0;
32 }
33 
34 /* Reads a module from the specified path, returning via the OutModule parameter
35    a module provider which performs lazy deserialization. Returns 0 on success.
36    Optionally returns a human-readable error message via OutMessage. */
37 int LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
38                                  LLVMModuleProviderRef *OutMP,
39                                  char **OutMessage) {
40   std::string Message;
41 
42   *OutMP = wrap(getBitcodeModuleProvider(unwrap(MemBuf), &Message));
43   if (!*OutMP) {
44     if (OutMessage)
45       *OutMessage = strdup(Message.c_str());
46     return 1;
47   }
48 
49   return 0;
50 }
51