2
0

BitReader.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "llvm-c/BitReader.h"
  10. #include "llvm/Bitcode/ReaderWriter.h"
  11. #include "llvm/IR/DiagnosticPrinter.h"
  12. #include "llvm/IR/LLVMContext.h"
  13. #include "llvm/IR/Module.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <cstring>
  17. #include <string>
  18. using namespace llvm;
  19. /* Builds a module from the bitcode in the specified memory buffer, returning a
  20. reference to the module via the OutModule parameter. Returns 0 on success.
  21. Optionally returns a human-readable error message via OutMessage. */
  22. LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
  23. LLVMModuleRef *OutModule, char **OutMessage) {
  24. return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
  25. OutMessage);
  26. }
  27. LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
  28. LLVMMemoryBufferRef MemBuf,
  29. LLVMModuleRef *OutModule,
  30. char **OutMessage) {
  31. MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
  32. LLVMContext &Ctx = *unwrap(ContextRef);
  33. std::string Message;
  34. raw_string_ostream Stream(Message);
  35. DiagnosticPrinterRawOStream DP(Stream);
  36. ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(
  37. Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); });
  38. if (ModuleOrErr.getError()) {
  39. if (OutMessage) {
  40. Stream.flush();
  41. *OutMessage = strdup(Message.c_str());
  42. }
  43. *OutModule = wrap((Module*)nullptr);
  44. return 1;
  45. }
  46. *OutModule = wrap(ModuleOrErr.get().release());
  47. return 0;
  48. }
  49. /* Reads a module from the specified path, returning via the OutModule parameter
  50. a module provider which performs lazy deserialization. Returns 0 on success.
  51. Optionally returns a human-readable error message via OutMessage. */
  52. LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
  53. LLVMMemoryBufferRef MemBuf,
  54. LLVMModuleRef *OutM,
  55. char **OutMessage) {
  56. std::string Message;
  57. std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
  58. ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
  59. getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
  60. Owner.release();
  61. if (std::error_code EC = ModuleOrErr.getError()) {
  62. *OutM = wrap((Module *)nullptr);
  63. if (OutMessage)
  64. *OutMessage = strdup(EC.message().c_str());
  65. return 1;
  66. }
  67. *OutM = wrap(ModuleOrErr.get().release());
  68. return 0;
  69. }
  70. LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
  71. char **OutMessage) {
  72. return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
  73. OutMessage);
  74. }
  75. /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
  76. LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
  77. LLVMMemoryBufferRef MemBuf,
  78. LLVMModuleProviderRef *OutMP,
  79. char **OutMessage) {
  80. return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
  81. reinterpret_cast<LLVMModuleRef*>(OutMP),
  82. OutMessage);
  83. }
  84. /* Deprecated: Use LLVMGetBitcodeModule instead. */
  85. LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
  86. LLVMModuleProviderRef *OutMP,
  87. char **OutMessage) {
  88. return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,
  89. OutMP, OutMessage);
  90. }