1 //===- RemarkUtilHelpers.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Helpers for remark utilites
11 //===----------------------------------------------------------------------===//
12 #include "RemarkUtilHelpers.h"
16 /// \returns A MemoryBuffer for the input file on success, and an Error
18 Expected
<std::unique_ptr
<MemoryBuffer
>>
19 getInputMemoryBuffer(StringRef InputFileName
) {
20 auto MaybeBuf
= MemoryBuffer::getFileOrSTDIN(InputFileName
);
21 if (auto ErrorCode
= MaybeBuf
.getError())
22 return createStringError(ErrorCode
,
23 Twine("Cannot open file '" + InputFileName
+
24 "': " + ErrorCode
.message()));
25 return std::move(*MaybeBuf
);
28 /// \returns A ToolOutputFile which can be used for outputting the results of
30 /// \p OutputFileName is the desired destination.
31 /// \p Flags controls whether or not the file is opened for writing in text
32 /// mode, as a binary, etc. See sys::fs::OpenFlags for more detail.
33 Expected
<std::unique_ptr
<ToolOutputFile
>>
34 getOutputFileWithFlags(StringRef OutputFileName
, sys::fs::OpenFlags Flags
) {
35 if (OutputFileName
== "")
37 std::error_code ErrorCode
;
38 auto OF
= std::make_unique
<ToolOutputFile
>(OutputFileName
, ErrorCode
, Flags
);
40 return errorCodeToError(ErrorCode
);
44 /// \returns A ToolOutputFile which can be used for writing remarks on success,
45 /// and an Error otherwise.
46 /// \p OutputFileName is the desired destination.
48 Expected
<std::unique_ptr
<ToolOutputFile
>>
49 getOutputFileForRemarks(StringRef OutputFileName
, Format OutputFormat
) {
50 assert((OutputFormat
== Format::YAML
|| OutputFormat
== Format::Bitstream
) &&
51 "Expected one of YAML or Bitstream!");
52 return getOutputFileWithFlags(OutputFileName
, OutputFormat
== Format::YAML
53 ? sys::fs::OF_TextWithCRLF
56 } // namespace remarks