1 //===- RemarkUtilHelpers.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 //
9 // Helpers for remark utilites
10 //
11 //===----------------------------------------------------------------------===//
12 #include "RemarkUtilHelpers.h"
13
14 namespace llvm {
15 namespace remarks {
16 /// \returns A MemoryBuffer for the input file on success, and an Error
17 /// otherwise.
18 Expected<std::unique_ptr<MemoryBuffer>>
getInputMemoryBuffer(StringRef InputFileName)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);
26 }
27
28 /// \returns A ToolOutputFile which can be used for outputting the results of
29 /// some tool mode.
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>>
getOutputFileWithFlags(StringRef OutputFileName,sys::fs::OpenFlags Flags)34 getOutputFileWithFlags(StringRef OutputFileName, sys::fs::OpenFlags Flags) {
35 if (OutputFileName == "")
36 OutputFileName = "-";
37 std::error_code ErrorCode;
38 auto OF = std::make_unique<ToolOutputFile>(OutputFileName, ErrorCode, Flags);
39 if (ErrorCode)
40 return errorCodeToError(ErrorCode);
41 return std::move(OF);
42 }
43
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.
47 /// \p OutputFormat
48 Expected<std::unique_ptr<ToolOutputFile>>
getOutputFileForRemarks(StringRef OutputFileName,Format OutputFormat)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
54 : sys::fs::OF_None);
55 }
56
57 Expected<FilterMatcher>
createRE(const llvm::cl::opt<std::string> & Arg)58 FilterMatcher::createRE(const llvm::cl::opt<std::string> &Arg) {
59 return createRE(Arg.ArgStr, Arg);
60 }
61
62 Expected<FilterMatcher>
createRE(StringRef Filter,const cl::list<std::string> & Arg)63 FilterMatcher::createRE(StringRef Filter, const cl::list<std::string> &Arg) {
64 return createRE(Arg.ArgStr, Filter);
65 }
66
createRE(StringRef Arg,StringRef Value)67 Expected<FilterMatcher> FilterMatcher::createRE(StringRef Arg,
68 StringRef Value) {
69 FilterMatcher FM(Value, true);
70 std::string Error;
71 if (!FM.FilterRE.isValid(Error))
72 return createStringError(make_error_code(std::errc::invalid_argument),
73 "invalid argument '--" + Arg + "=" + Value +
74 "': " + Error);
75 return std::move(FM);
76 }
77
78 Expected<std::optional<FilterMatcher>>
createExactOrRE(const llvm::cl::opt<std::string> & ExactArg,const llvm::cl::opt<std::string> & REArg)79 FilterMatcher::createExactOrRE(const llvm::cl::opt<std::string> &ExactArg,
80 const llvm::cl::opt<std::string> &REArg) {
81 if (!ExactArg.empty() && !REArg.empty())
82 return createStringError(make_error_code(std::errc::invalid_argument),
83 "conflicting arguments: --" + ExactArg.ArgStr +
84 " and --" + REArg.ArgStr);
85
86 if (!ExactArg.empty())
87 return createExact(ExactArg);
88
89 if (!REArg.empty())
90 return createRE(REArg);
91
92 return std::nullopt;
93 }
94
95 } // namespace remarks
96 } // namespace llvm
97