xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-remarkutil/RemarkUtilHelpers.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1*5f757f3fSDimitry Andric //===- RemarkUtilHelpers.cpp ----------------------------------------------===//
2*5f757f3fSDimitry Andric //
3*5f757f3fSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*5f757f3fSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*5f757f3fSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*5f757f3fSDimitry Andric //
7*5f757f3fSDimitry Andric //===----------------------------------------------------------------------===//
8*5f757f3fSDimitry Andric //
9*5f757f3fSDimitry Andric // Helpers for remark utilites
10*5f757f3fSDimitry Andric //
11*5f757f3fSDimitry Andric //===----------------------------------------------------------------------===//
12*5f757f3fSDimitry Andric #include "RemarkUtilHelpers.h"
13*5f757f3fSDimitry Andric 
14*5f757f3fSDimitry Andric namespace llvm {
15*5f757f3fSDimitry Andric namespace remarks {
16*5f757f3fSDimitry Andric /// \returns A MemoryBuffer for the input file on success, and an Error
17*5f757f3fSDimitry Andric /// otherwise.
18*5f757f3fSDimitry Andric Expected<std::unique_ptr<MemoryBuffer>>
19*5f757f3fSDimitry Andric getInputMemoryBuffer(StringRef InputFileName) {
20*5f757f3fSDimitry Andric   auto MaybeBuf = MemoryBuffer::getFileOrSTDIN(InputFileName);
21*5f757f3fSDimitry Andric   if (auto ErrorCode = MaybeBuf.getError())
22*5f757f3fSDimitry Andric     return createStringError(ErrorCode,
23*5f757f3fSDimitry Andric                              Twine("Cannot open file '" + InputFileName +
24*5f757f3fSDimitry Andric                                    "': " + ErrorCode.message()));
25*5f757f3fSDimitry Andric   return std::move(*MaybeBuf);
26*5f757f3fSDimitry Andric }
27*5f757f3fSDimitry Andric 
28*5f757f3fSDimitry Andric /// \returns A ToolOutputFile which can be used for outputting the results of
29*5f757f3fSDimitry Andric /// some tool mode.
30*5f757f3fSDimitry Andric /// \p OutputFileName is the desired destination.
31*5f757f3fSDimitry Andric /// \p Flags controls whether or not the file is opened for writing in text
32*5f757f3fSDimitry Andric /// mode, as a binary, etc. See sys::fs::OpenFlags for more detail.
33*5f757f3fSDimitry Andric Expected<std::unique_ptr<ToolOutputFile>>
34*5f757f3fSDimitry Andric getOutputFileWithFlags(StringRef OutputFileName, sys::fs::OpenFlags Flags) {
35*5f757f3fSDimitry Andric   if (OutputFileName == "")
36*5f757f3fSDimitry Andric     OutputFileName = "-";
37*5f757f3fSDimitry Andric   std::error_code ErrorCode;
38*5f757f3fSDimitry Andric   auto OF = std::make_unique<ToolOutputFile>(OutputFileName, ErrorCode, Flags);
39*5f757f3fSDimitry Andric   if (ErrorCode)
40*5f757f3fSDimitry Andric     return errorCodeToError(ErrorCode);
41*5f757f3fSDimitry Andric   return std::move(OF);
42*5f757f3fSDimitry Andric }
43*5f757f3fSDimitry Andric 
44*5f757f3fSDimitry Andric /// \returns A ToolOutputFile which can be used for writing remarks on success,
45*5f757f3fSDimitry Andric /// and an Error otherwise.
46*5f757f3fSDimitry Andric /// \p OutputFileName is the desired destination.
47*5f757f3fSDimitry Andric /// \p OutputFormat
48*5f757f3fSDimitry Andric Expected<std::unique_ptr<ToolOutputFile>>
49*5f757f3fSDimitry Andric getOutputFileForRemarks(StringRef OutputFileName, Format OutputFormat) {
50*5f757f3fSDimitry Andric   assert((OutputFormat == Format::YAML || OutputFormat == Format::Bitstream) &&
51*5f757f3fSDimitry Andric          "Expected one of YAML or Bitstream!");
52*5f757f3fSDimitry Andric   return getOutputFileWithFlags(OutputFileName, OutputFormat == Format::YAML
53*5f757f3fSDimitry Andric                                                     ? sys::fs::OF_TextWithCRLF
54*5f757f3fSDimitry Andric                                                     : sys::fs::OF_None);
55*5f757f3fSDimitry Andric }
56*5f757f3fSDimitry Andric } // namespace remarks
57*5f757f3fSDimitry Andric } // namespace llvm
58