BitReader.cpp 4.8 KB

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