Path: blob/main/contrib/llvm-project/llvm/tools/llvm-remarkutil/RemarkUtilHelpers.cpp
35231 views
//===- RemarkUtilHelpers.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//===----------------------------------------------------------------------===//7//8// Helpers for remark utilites9//10//===----------------------------------------------------------------------===//11#include "RemarkUtilHelpers.h"1213namespace llvm {14namespace remarks {15/// \returns A MemoryBuffer for the input file on success, and an Error16/// otherwise.17Expected<std::unique_ptr<MemoryBuffer>>18getInputMemoryBuffer(StringRef InputFileName) {19auto MaybeBuf = MemoryBuffer::getFileOrSTDIN(InputFileName);20if (auto ErrorCode = MaybeBuf.getError())21return createStringError(ErrorCode,22Twine("Cannot open file '" + InputFileName +23"': " + ErrorCode.message()));24return std::move(*MaybeBuf);25}2627/// \returns A ToolOutputFile which can be used for outputting the results of28/// some tool mode.29/// \p OutputFileName is the desired destination.30/// \p Flags controls whether or not the file is opened for writing in text31/// mode, as a binary, etc. See sys::fs::OpenFlags for more detail.32Expected<std::unique_ptr<ToolOutputFile>>33getOutputFileWithFlags(StringRef OutputFileName, sys::fs::OpenFlags Flags) {34if (OutputFileName == "")35OutputFileName = "-";36std::error_code ErrorCode;37auto OF = std::make_unique<ToolOutputFile>(OutputFileName, ErrorCode, Flags);38if (ErrorCode)39return errorCodeToError(ErrorCode);40return std::move(OF);41}4243/// \returns A ToolOutputFile which can be used for writing remarks on success,44/// and an Error otherwise.45/// \p OutputFileName is the desired destination.46/// \p OutputFormat47Expected<std::unique_ptr<ToolOutputFile>>48getOutputFileForRemarks(StringRef OutputFileName, Format OutputFormat) {49assert((OutputFormat == Format::YAML || OutputFormat == Format::Bitstream) &&50"Expected one of YAML or Bitstream!");51return getOutputFileWithFlags(OutputFileName, OutputFormat == Format::YAML52? sys::fs::OF_TextWithCRLF53: sys::fs::OF_None);54}55} // namespace remarks56} // namespace llvm575859