1//===- RemarkUtilHelpers.h ------------------------------------------------===//
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 "llvm-c/Remarks.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/Remarks/Remark.h"
15#include "llvm/Remarks/RemarkFormat.h"
16#include "llvm/Remarks/RemarkParser.h"
17#include "llvm/Remarks/YAMLRemarkSerializer.h"
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/Error.h"
20#include "llvm/Support/FileSystem.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/Regex.h"
23#include "llvm/Support/ToolOutputFile.h"
24
25// Keep input + output help + names consistent across the various modes via a
26// hideous macro.
27#define INPUT_OUTPUT_COMMAND_LINE_OPTIONS(SUBOPT) \
28 static cl::opt<std::string> InputFileName(cl::Positional, cl::init("-"), \
29 cl::desc("<input file>"), \
30 cl::sub(SUBOPT)); \
31 static cl::opt<std::string> OutputFileName( \
32 "o", cl::init("-"), cl::desc("Output"), cl::value_desc("filename"), \
33 cl::sub(SUBOPT));
34
35// Keep Input format and names consistent accross the modes via a macro.
36#define INPUT_FORMAT_COMMAND_LINE_OPTIONS(SUBOPT) \
37 static cl::opt<Format> InputFormat( \
38 "parser", cl::init(Format::Auto), \
39 cl::desc("Input remark format to parse"), \
40 cl::values( \
41 clEnumValN(Format::Auto, "auto", "Automatic detection (default)"), \
42 clEnumValN(Format::YAML, "yaml", "YAML"), \
43 clEnumValN(Format::Bitstream, "bitstream", "Bitstream")), \
44 cl::sub(SUBOPT));
45
46#define DEBUG_LOC_INFO_COMMAND_LINE_OPTIONS(SUBOPT) \
47 static cl::opt<bool> UseDebugLoc( \
48 "use-debug-loc", \
49 cl::desc( \
50 "Add debug loc information when generating tables for " \
51 "functions. The loc is represented as (path:line number:column " \
52 "number)"), \
53 cl::init(false), cl::sub(SUBOPT));
54
55namespace llvm {
56namespace remarks {
57Expected<std::unique_ptr<MemoryBuffer>>
58getInputMemoryBuffer(StringRef InputFileName);
59Expected<std::unique_ptr<ToolOutputFile>>
60getOutputFileWithFlags(StringRef OutputFileName, sys::fs::OpenFlags Flags);
61Expected<std::unique_ptr<ToolOutputFile>>
62getOutputFileForRemarks(StringRef OutputFileName, Format OutputFormat);
63
64/// Filter object which can be either a string or a regex to match with the
65/// remark properties.
66class FilterMatcher {
67 Regex FilterRE;
68 std::string FilterStr;
69 bool IsRegex;
70
71 FilterMatcher(StringRef Filter, bool IsRegex)
72 : FilterRE(Filter), FilterStr(Filter), IsRegex(IsRegex) {}
73
74 static Expected<FilterMatcher> createRE(StringRef Arg, StringRef Value);
75
76public:
77 static FilterMatcher createExact(StringRef Filter) { return {Filter, false}; }
78
79 static Expected<FilterMatcher>
80 createRE(const llvm::cl::opt<std::string> &Arg);
81
82 static Expected<FilterMatcher> createRE(StringRef Filter,
83 const cl::list<std::string> &Arg);
84
85 static Expected<std::optional<FilterMatcher>>
86 createExactOrRE(const llvm::cl::opt<std::string> &ExactArg,
87 const llvm::cl::opt<std::string> &REArg);
88
89 static FilterMatcher createAny() { return {".*", true}; }
90
91 bool match(StringRef StringToMatch) const {
92 if (IsRegex)
93 return FilterRE.match(String: StringToMatch);
94 return FilterStr == StringToMatch.trim().str();
95 }
96};
97
98} // namespace remarks
99} // namespace llvm
100