Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 1 | //===-- BitReader.cpp -----------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Gordon Henriksen and is distributed under the |
| 6 | // University of Illinois Open Source 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 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 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) { |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 22 | std::string Message; |
| 23 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 24 | *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), &Message)); |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 25 | if (!*OutModule) { |
| 26 | if (OutMessage) |
| 27 | *OutMessage = strdup(Message.c_str()); |
| 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
Gordon Henriksen | da1435f | 2007-12-19 22:30:40 +0000 | [diff] [blame] | 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; |
Gordon Henriksen | bbc6597 | 2007-12-11 00:20:48 +0000 | [diff] [blame] | 50 | } |