BitReader.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===-- BitReader.cpp -----------------------------------------------------===//
  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. //===----------------------------------------------------------------------===//
  8. #include "llvm-c/BitReader.h"
  9. #include "llvm-c/Core.h"
  10. #include "llvm/Bitcode/BitcodeReader.h"
  11. #include "llvm/IR/LLVMContext.h"
  12. #include "llvm/IR/Module.h"
  13. #include "llvm/Support/MemoryBuffer.h"
  14. #include <cstring>
  15. #include <string>
  16. using namespace llvm;
  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. LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule,
  21. char **OutMessage) {
  22. return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
  23. OutMessage);
  24. }
  25. LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
  26. LLVMModuleRef *OutModule) {
  27. return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
  28. }
  29. LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
  30. LLVMMemoryBufferRef MemBuf,
  31. LLVMModuleRef *OutModule,
  32. char **OutMessage) {
  33. MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
  34. LLVMContext &Ctx = *unwrap(ContextRef);
  35. Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
  36. if (Error Err = ModuleOrErr.takeError()) {
  37. std::string Message;
  38. handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
  39. Message = EIB.message();
  40. });
  41. if (OutMessage)
  42. *OutMessage = strdup(Message.c_str());
  43. *OutModule = wrap((Module *)nullptr);
  44. return 1;
  45. }
  46. *OutModule = wrap(ModuleOrErr.get().release());
  47. return 0;
  48. }
  49. LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
  50. LLVMMemoryBufferRef MemBuf,
  51. LLVMModuleRef *OutModule) {
  52. MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
  53. LLVMContext &Ctx = *unwrap(ContextRef);
  54. ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
  55. expectedToErrorOrAndEmitErrors(Ctx, parseBitcodeFile(Buf, Ctx));
  56. if (ModuleOrErr.getError()) {
  57. *OutModule = wrap((Module *)nullptr);
  58. return 1;
  59. }
  60. *OutModule = wrap(ModuleOrErr.get().release());
  61. return 0;
  62. }
  63. /* Reads a module from the specified path, returning via the OutModule parameter
  64. a module provider which performs lazy deserialization. Returns 0 on success.
  65. Optionally returns a human-readable error message via OutMessage. */
  66. LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
  67. LLVMMemoryBufferRef MemBuf,
  68. LLVMModuleRef *OutM, char **OutMessage) {
  69. LLVMContext &Ctx = *unwrap(ContextRef);
  70. std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
  71. Expected<std::unique_ptr<Module>> ModuleOrErr =
  72. getOwningLazyBitcodeModule(std::move(Owner), Ctx);
  73. // Release the buffer if we didn't take ownership of it since we never owned
  74. // it anyway.
  75. (void)Owner.release();
  76. if (Error Err = ModuleOrErr.takeError()) {
  77. std::string Message;
  78. handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
  79. Message = EIB.message();
  80. });
  81. if (OutMessage)
  82. *OutMessage = strdup(Message.c_str());
  83. *OutM = wrap((Module *)nullptr);
  84. return 1;
  85. }
  86. *OutM = wrap(ModuleOrErr.get().release());
  87. return 0;
  88. }
  89. LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
  90. LLVMMemoryBufferRef MemBuf,
  91. LLVMModuleRef *OutM) {
  92. LLVMContext &Ctx = *unwrap(ContextRef);
  93. std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
  94. ErrorOr<std::unique_ptr<Module>> ModuleOrErr = expectedToErrorOrAndEmitErrors(
  95. Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
  96. Owner.release();
  97. if (ModuleOrErr.getError()) {
  98. *OutM = wrap((Module *)nullptr);
  99. return 1;
  100. }
  101. *OutM = wrap(ModuleOrErr.get().release());
  102. return 0;
  103. }
  104. LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
  105. char **OutMessage) {
  106. return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
  107. OutMessage);
  108. }
  109. LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
  110. LLVMModuleRef *OutM) {
  111. return LLVMGetBitcodeModuleInContext2(LLVMGetGlobalContext(), MemBuf, OutM);
  112. }