Path: blob/main/contrib/llvm-project/llvm/lib/Bitcode/Writer/BitWriter.cpp
35291 views
//===-- BitWriter.cpp -----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#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"14using namespace llvm;151617/*===-- Operations on modules ---------------------------------------------===*/1819int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {20std::error_code EC;21raw_fd_ostream OS(Path, EC, sys::fs::OF_None);2223if (EC)24return -1;2526WriteBitcodeToFile(*unwrap(M), OS);27return 0;28}2930int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,31int Unbuffered) {32raw_fd_ostream OS(FD, ShouldClose, Unbuffered);3334WriteBitcodeToFile(*unwrap(M), OS);35return 0;36}3738int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {39return LLVMWriteBitcodeToFD(M, FileHandle, true, false);40}4142LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M) {43std::string Data;44raw_string_ostream OS(Data);4546WriteBitcodeToFile(*unwrap(M), OS);47return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());48}495051