Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:25

0001 //===-- llvm/Remarks/Remark.h - The remark type -----------------*- C++/-*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines an abstraction for handling remarks.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_REMARKS_REMARK_H
0014 #define LLVM_REMARKS_REMARK_H
0015 
0016 #include "llvm-c/Remarks.h"
0017 #include "llvm/ADT/SmallVector.h"
0018 #include "llvm/ADT/StringRef.h"
0019 #include "llvm/Support/CBindingWrapping.h"
0020 #include "llvm/Support/raw_ostream.h"
0021 #include <optional>
0022 #include <string>
0023 
0024 namespace llvm {
0025 namespace remarks {
0026 
0027 /// The current version of the remark entry.
0028 constexpr uint64_t CurrentRemarkVersion = 0;
0029 
0030 /// The debug location used to track a remark back to the source file.
0031 struct RemarkLocation {
0032   /// Absolute path of the source file corresponding to this remark.
0033   StringRef SourceFilePath;
0034   unsigned SourceLine = 0;
0035   unsigned SourceColumn = 0;
0036 
0037   /// Implement operator<< on RemarkLocation.
0038   void print(raw_ostream &OS) const;
0039 };
0040 
0041 // Create wrappers for C Binding types (see CBindingWrapping.h).
0042 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RemarkLocation, LLVMRemarkDebugLocRef)
0043 
0044 /// A key-value pair with a debug location that is used to display the remarks
0045 /// at the right place in the source.
0046 struct Argument {
0047   StringRef Key;
0048   // FIXME: We might want to be able to store other types than strings here.
0049   StringRef Val;
0050   // If set, the debug location corresponding to the value.
0051   std::optional<RemarkLocation> Loc;
0052 
0053   /// Implement operator<< on Argument.
0054   void print(raw_ostream &OS) const;
0055   /// Return the value of argument as int.
0056   std::optional<int> getValAsInt() const;
0057   /// Check if the argument value can be parsed as int.
0058   bool isValInt() const;
0059 };
0060 
0061 // Create wrappers for C Binding types (see CBindingWrapping.h).
0062 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Argument, LLVMRemarkArgRef)
0063 
0064 /// The type of the remark.
0065 enum class Type {
0066   Unknown,
0067   Passed,
0068   Missed,
0069   Analysis,
0070   AnalysisFPCommute,
0071   AnalysisAliasing,
0072   Failure,
0073   First = Unknown,
0074   Last = Failure
0075 };
0076 
0077 inline StringRef typeToStr(Type Ty) {
0078   switch (Ty) {
0079   case Type::Unknown:
0080     return "Unknown";
0081   case Type::Missed:
0082     return "Missed";
0083   case Type::Passed:
0084     return "Passed";
0085   case Type::Analysis:
0086     return "Analysis";
0087   case Type::AnalysisFPCommute:
0088     return "AnalysisFPCommute";
0089   case Type::AnalysisAliasing:
0090     return "AnalysisAliasing";
0091   default:
0092     return "Failure";
0093   }
0094 }
0095 
0096 /// A remark type used for both emission and parsing.
0097 struct Remark {
0098   /// The type of the remark.
0099   Type RemarkType = Type::Unknown;
0100 
0101   /// Name of the pass that triggers the emission of this remark.
0102   StringRef PassName;
0103 
0104   /// Textual identifier for the remark (single-word, camel-case). Can be used
0105   /// by external tools reading the output file for remarks to identify the
0106   /// remark.
0107   StringRef RemarkName;
0108 
0109   /// Mangled name of the function that triggers the emssion of this remark.
0110   StringRef FunctionName;
0111 
0112   /// The location in the source file of the remark.
0113   std::optional<RemarkLocation> Loc;
0114 
0115   /// If profile information is available, this is the number of times the
0116   /// corresponding code was executed in a profile instrumentation run.
0117   std::optional<uint64_t> Hotness;
0118 
0119   /// Arguments collected via the streaming interface.
0120   SmallVector<Argument, 5> Args;
0121 
0122   Remark() = default;
0123   Remark(Remark &&) = default;
0124   Remark &operator=(Remark &&) = default;
0125 
0126   /// Return a message composed from the arguments as a string.
0127   std::string getArgsAsMsg() const;
0128 
0129   /// Clone this remark to explicitly ask for a copy.
0130   Remark clone() const { return *this; }
0131 
0132   /// Implement operator<< on Remark.
0133   void print(raw_ostream &OS) const;
0134 
0135 private:
0136   /// In order to avoid unwanted copies, "delete" the copy constructor.
0137   /// If a copy is needed, it should be done through `Remark::clone()`.
0138   Remark(const Remark &) = default;
0139   Remark& operator=(const Remark &) = default;
0140 };
0141 
0142 // Create wrappers for C Binding types (see CBindingWrapping.h).
0143 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Remark, LLVMRemarkEntryRef)
0144 
0145 /// Comparison operators for Remark objects and dependent objects.
0146 
0147 template <typename T>
0148 bool operator<(const std::optional<T> &LHS, const std::optional<T> &RHS) {
0149   // Sorting based on optionals should result in all `None` entries to appear
0150   // before the valid entries. For example, remarks with no debug location will
0151   // appear first.
0152   if (!LHS && !RHS)
0153     return false;
0154   if (!LHS && RHS)
0155     return true;
0156   if (LHS && !RHS)
0157     return false;
0158   return *LHS < *RHS;
0159 }
0160 
0161 inline bool operator==(const RemarkLocation &LHS, const RemarkLocation &RHS) {
0162   return LHS.SourceFilePath == RHS.SourceFilePath &&
0163          LHS.SourceLine == RHS.SourceLine &&
0164          LHS.SourceColumn == RHS.SourceColumn;
0165 }
0166 
0167 inline bool operator!=(const RemarkLocation &LHS, const RemarkLocation &RHS) {
0168   return !(LHS == RHS);
0169 }
0170 
0171 inline bool operator<(const RemarkLocation &LHS, const RemarkLocation &RHS) {
0172   return std::make_tuple(LHS.SourceFilePath, LHS.SourceLine, LHS.SourceColumn) <
0173          std::make_tuple(RHS.SourceFilePath, RHS.SourceLine, RHS.SourceColumn);
0174 }
0175 
0176 inline bool operator==(const Argument &LHS, const Argument &RHS) {
0177   return LHS.Key == RHS.Key && LHS.Val == RHS.Val && LHS.Loc == RHS.Loc;
0178 }
0179 
0180 inline bool operator!=(const Argument &LHS, const Argument &RHS) {
0181   return !(LHS == RHS);
0182 }
0183 
0184 inline bool operator<(const Argument &LHS, const Argument &RHS) {
0185   return std::make_tuple(LHS.Key, LHS.Val, LHS.Loc) <
0186          std::make_tuple(RHS.Key, RHS.Val, RHS.Loc);
0187 }
0188 
0189 inline bool operator==(const Remark &LHS, const Remark &RHS) {
0190   return LHS.RemarkType == RHS.RemarkType && LHS.PassName == RHS.PassName &&
0191          LHS.RemarkName == RHS.RemarkName &&
0192          LHS.FunctionName == RHS.FunctionName && LHS.Loc == RHS.Loc &&
0193          LHS.Hotness == RHS.Hotness && LHS.Args == RHS.Args;
0194 }
0195 
0196 inline bool operator!=(const Remark &LHS, const Remark &RHS) {
0197   return !(LHS == RHS);
0198 }
0199 
0200 inline bool operator<(const Remark &LHS, const Remark &RHS) {
0201   return std::make_tuple(LHS.RemarkType, LHS.PassName, LHS.RemarkName,
0202                          LHS.FunctionName, LHS.Loc, LHS.Hotness, LHS.Args) <
0203          std::make_tuple(RHS.RemarkType, RHS.PassName, RHS.RemarkName,
0204                          RHS.FunctionName, RHS.Loc, RHS.Hotness, RHS.Args);
0205 }
0206 
0207 inline raw_ostream &operator<<(raw_ostream &OS, const RemarkLocation &RLoc) {
0208   RLoc.print(OS);
0209   return OS;
0210 }
0211 
0212 inline raw_ostream &operator<<(raw_ostream &OS, const Argument &Arg) {
0213   Arg.print(OS);
0214   return OS;
0215 }
0216 
0217 inline raw_ostream &operator<<(raw_ostream &OS, const Remark &Remark) {
0218   Remark.print(OS);
0219   return OS;
0220 }
0221 
0222 } // end namespace remarks
0223 } // end namespace llvm
0224 
0225 #endif /* LLVM_REMARKS_REMARK_H */