xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Remarks/Remark.h (revision f425b8be7e7a38e47677d7bd68819dccbdcc3134)
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/ArrayRef.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/CBindingWrapping.h"
21 #include <string>
22 
23 namespace llvm {
24 namespace remarks {
25 
26 constexpr uint64_t Version = 0;
27 
28 /// The debug location used to track a remark back to the source file.
29 struct RemarkLocation {
30   /// Absolute path of the source file corresponding to this remark.
31   StringRef SourceFilePath;
32   unsigned SourceLine;
33   unsigned SourceColumn;
34 };
35 
36 // Create wrappers for C Binding types (see CBindingWrapping.h).
37 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RemarkLocation, LLVMRemarkDebugLocRef)
38 
39 /// A key-value pair with a debug location that is used to display the remarks
40 /// at the right place in the source.
41 struct Argument {
42   StringRef Key;
43   // FIXME: We might want to be able to store other types than strings here.
44   StringRef Val;
45   // If set, the debug location corresponding to the value.
46   Optional<RemarkLocation> Loc;
47 };
48 
49 // Create wrappers for C Binding types (see CBindingWrapping.h).
50 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Argument, LLVMRemarkArgRef)
51 
52 /// The type of the remark.
53 enum class Type {
54   Unknown,
55   Passed,
56   Missed,
57   Analysis,
58   AnalysisFPCommute,
59   AnalysisAliasing,
60   Failure,
61   LastTypeValue = Failure
62 };
63 
64 /// A remark type used for both emission and parsing.
65 struct Remark {
66   /// The type of the remark.
67   Type RemarkType = Type::Unknown;
68 
69   /// Name of the pass that triggers the emission of this remark.
70   StringRef PassName;
71 
72   /// Textual identifier for the remark (single-word, camel-case). Can be used
73   /// by external tools reading the output file for remarks to identify the
74   /// remark.
75   StringRef RemarkName;
76 
77   /// Mangled name of the function that triggers the emssion of this remark.
78   StringRef FunctionName;
79 
80   /// The location in the source file of the remark.
81   Optional<RemarkLocation> Loc;
82 
83   /// If profile information is available, this is the number of times the
84   /// corresponding code was executed in a profile instrumentation run.
85   Optional<uint64_t> Hotness;
86 
87   /// Arguments collected via the streaming interface.
88   SmallVector<Argument, 5> Args;
89 
90   Remark() = default;
91   Remark(Remark &&) = default;
92   Remark &operator=(Remark &&) = default;
93 
94   /// Return a message composed from the arguments as a string.
95   std::string getArgsAsMsg() const;
96 
97   /// Clone this remark to explicitly ask for a copy.
98   Remark clone() const { return *this; }
99 
100 private:
101   /// In order to avoid unwanted copies, "delete" the copy constructor.
102   /// If a copy is needed, it should be done through `Remark::clone()`.
103   Remark(const Remark &) = default;
104   Remark& operator=(const Remark &) = default;
105 };
106 
107 // Create wrappers for C Binding types (see CBindingWrapping.h).
108 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Remark, LLVMRemarkEntryRef)
109 
110 } // end namespace remarks
111 } // end namespace llvm
112 
113 #endif /* LLVM_REMARKS_REMARK_H */
114