BitWriter.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //===-- BitWriter.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/BitWriter.h"
  9. #include "llvm/Bitcode/BitcodeWriter.h"
  10. #include "llvm/IR/Module.h"
  11. #include "llvm/Support/FileSystem.h"
  12. #include "llvm/Support/MemoryBuffer.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. using namespace llvm;
  15. /*===-- Operations on modules ---------------------------------------------===*/
  16. int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
  17. std::error_code EC;
  18. raw_fd_ostream OS(Path, EC, sys::fs::OF_None);
  19. if (EC)
  20. return -1;
  21. WriteBitcodeToFile(*unwrap(M), OS);
  22. return 0;
  23. }
  24. int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
  25. int Unbuffered) {
  26. raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
  27. WriteBitcodeToFile(*unwrap(M), OS);
  28. return 0;
  29. }
  30. int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
  31. return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
  32. }
  33. LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M) {
  34. std::string Data;
  35. raw_string_ostream OS(Data);
  36. WriteBitcodeToFile(*unwrap(M), OS);
  37. return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());
  38. }