1//===-- llvm/Remarks/Remark.h - The remark type -----------------*- C++/-*-===//
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// This file defines an abstraction for handling remarks.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_REMARKS_REMARK_H
14#define LLVM_REMARKS_REMARK_H
15
16#include "llvm-c/Remarks.h"
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/CBindingWrapping.h"
20#include "llvm/Support/raw_ostream.h"
21#include <optional>
22#include <string>
23
24namespace llvm {
25namespace remarks {
26
27/// The current version of the remark entry.
28constexpr uint64_t CurrentRemarkVersion = 0;
29
30/// The debug location used to track a remark back to the source file.
31struct RemarkLocation {
32 /// Absolute path of the source file corresponding to this remark.
33 StringRef SourceFilePath;
34 unsigned SourceLine = 0;
35 unsigned SourceColumn = 0;
36
37 /// Implement operator<< on RemarkLocation.
38 void print(raw_ostream &OS) const;
39};
40
41// Create wrappers for C Binding types (see CBindingWrapping.h).
42DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RemarkLocation, LLVMRemarkDebugLocRef)
43
44/// A key-value pair with a debug location that is used to display the remarks
45/// at the right place in the source.
46struct Argument {
47 StringRef Key;
48 // FIXME: We might want to be able to store other types than strings here.
49 StringRef Val;
50 // If set, the debug location corresponding to the value.
51 std::optional<RemarkLocation> Loc;
52
53 /// Implement operator<< on Argument.
54 void print(raw_ostream &OS) const;
55 /// Return the value of argument as int.
56 std::optional<int> getValAsInt() const;
57 /// Check if the argument value can be parsed as int.
58 bool isValInt() const;
59};
60
61// Create wrappers for C Binding types (see CBindingWrapping.h).
62DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Argument, LLVMRemarkArgRef)
63
64/// The type of the remark.
65enum class Type {
66 Unknown,
67 Passed,
68 Missed,
69 Analysis,
70 AnalysisFPCommute,
71 AnalysisAliasing,
72 Failure,
73 First = Unknown,
74 Last = Failure
75};
76
77inline StringRef typeToStr(Type Ty) {
78 switch (Ty) {
79 case Type::Unknown:
80 return "Unknown";
81 case Type::Missed:
82 return "Missed";
83 case Type::Passed:
84 return "Passed";
85 case Type::Analysis:
86 return "Analysis";
87 case Type::AnalysisFPCommute:
88 return "AnalysisFPCommute";
89 case Type::AnalysisAliasing:
90 return "AnalysisAliasing";
91 default:
92 return "Failure";
93 }
94}
95
96/// A remark type used for both emission and parsing.
97struct Remark {
98 /// The type of the remark.
99 Type RemarkType = Type::Unknown;
100
101 /// Name of the pass that triggers the emission of this remark.
102 StringRef PassName;
103
104 /// Textual identifier for the remark (single-word, camel-case). Can be used
105 /// by external tools reading the output file for remarks to identify the
106 /// remark.
107 StringRef RemarkName;
108
109 /// Mangled name of the function that triggers the emssion of this remark.
110 StringRef FunctionName;
111
112 /// The location in the source file of the remark.
113 std::optional<RemarkLocation> Loc;
114
115 /// If profile information is available, this is the number of times the
116 /// corresponding code was executed in a profile instrumentation run.
117 std::optional<uint64_t> Hotness;
118
119 /// Arguments collected via the streaming interface.
120 SmallVector<Argument, 5> Args;
121
122 Remark() = default;
123 Remark(Remark &&) = default;
124 Remark &operator=(Remark &&) = default;
125
126 /// Return a message composed from the arguments as a string.
127 std::string getArgsAsMsg() const;
128
129 /// Clone this remark to explicitly ask for a copy.
130 Remark clone() const { return *this; }
131
132 /// Implement operator<< on Remark.
133 void print(raw_ostream &OS) const;
134
135private:
136 /// In order to avoid unwanted copies, "delete" the copy constructor.
137 /// If a copy is needed, it should be done through `Remark::clone()`.
138 Remark(const Remark &) = default;
139 Remark& operator=(const Remark &) = default;
140};
141
142// Create wrappers for C Binding types (see CBindingWrapping.h).
143DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Remark, LLVMRemarkEntryRef)
144
145/// Comparison operators for Remark objects and dependent objects.
146
147template <typename T>
148bool operator<(const std::optional<T> &LHS, const std::optional<T> &RHS) {
149 // Sorting based on optionals should result in all `None` entries to appear
150 // before the valid entries. For example, remarks with no debug location will
151 // appear first.
152 if (!LHS && !RHS)
153 return false;
154 if (!LHS && RHS)
155 return true;
156 if (LHS && !RHS)
157 return false;
158 return *LHS < *RHS;
159}
160
161inline bool operator==(const RemarkLocation &LHS, const RemarkLocation &RHS) {
162 return LHS.SourceFilePath == RHS.SourceFilePath &&
163 LHS.SourceLine == RHS.SourceLine &&
164 LHS.SourceColumn == RHS.SourceColumn;
165}
166
167inline bool operator!=(const RemarkLocation &LHS, const RemarkLocation &RHS) {
168 return !(LHS == RHS);
169}
170
171inline bool operator<(const RemarkLocation &LHS, const RemarkLocation &RHS) {
172 return std::make_tuple(args: LHS.SourceFilePath, args: LHS.SourceLine, args: LHS.SourceColumn) <
173 std::make_tuple(args: RHS.SourceFilePath, args: RHS.SourceLine, args: RHS.SourceColumn);
174}
175
176inline bool operator==(const Argument &LHS, const Argument &RHS) {
177 return LHS.Key == RHS.Key && LHS.Val == RHS.Val && LHS.Loc == RHS.Loc;
178}
179
180inline bool operator!=(const Argument &LHS, const Argument &RHS) {
181 return !(LHS == RHS);
182}
183
184inline bool operator<(const Argument &LHS, const Argument &RHS) {
185 return std::make_tuple(args: LHS.Key, args: LHS.Val, args: LHS.Loc) <
186 std::make_tuple(args: RHS.Key, args: RHS.Val, args: RHS.Loc);
187}
188
189inline bool operator==(const Remark &LHS, const Remark &RHS) {
190 return LHS.RemarkType == RHS.RemarkType && LHS.PassName == RHS.PassName &&
191 LHS.RemarkName == RHS.RemarkName &&
192 LHS.FunctionName == RHS.FunctionName && LHS.Loc == RHS.Loc &&
193 LHS.Hotness == RHS.Hotness && LHS.Args == RHS.Args;
194}
195
196inline bool operator!=(const Remark &LHS, const Remark &RHS) {
197 return !(LHS == RHS);
198}
199
200inline bool operator<(const Remark &LHS, const Remark &RHS) {
201 return std::make_tuple(args: LHS.RemarkType, args: LHS.PassName, args: LHS.RemarkName,
202 args: LHS.FunctionName, args: LHS.Loc, args: LHS.Hotness, args: LHS.Args) <
203 std::make_tuple(args: RHS.RemarkType, args: RHS.PassName, args: RHS.RemarkName,
204 args: RHS.FunctionName, args: RHS.Loc, args: RHS.Hotness, args: RHS.Args);
205}
206
207inline raw_ostream &operator<<(raw_ostream &OS, const RemarkLocation &RLoc) {
208 RLoc.print(OS);
209 return OS;
210}
211
212inline raw_ostream &operator<<(raw_ostream &OS, const Argument &Arg) {
213 Arg.print(OS);
214 return OS;
215}
216
217inline raw_ostream &operator<<(raw_ostream &OS, const Remark &Remark) {
218 Remark.print(OS);
219 return OS;
220}
221
222} // end namespace remarks
223} // end namespace llvm
224
225#endif /* LLVM_REMARKS_REMARK_H */
226

source code of llvm/include/llvm/Remarks/Remark.h