Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- llvm/Remarks/RemarkLinker.h -----------------------------*- 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 provides an interface to link together multiple remark files.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_REMARKS_REMARKLINKER_H
0014 #define LLVM_REMARKS_REMARKLINKER_H
0015 
0016 #include "llvm/Remarks/Remark.h"
0017 #include "llvm/Remarks/RemarkFormat.h"
0018 #include "llvm/Remarks/RemarkStringTable.h"
0019 #include "llvm/Support/Error.h"
0020 #include <memory>
0021 #include <optional>
0022 #include <set>
0023 
0024 namespace llvm {
0025 
0026 namespace object {
0027 class ObjectFile;
0028 }
0029 
0030 namespace remarks {
0031 
0032 struct RemarkLinker {
0033 private:
0034   /// Compare through the pointers.
0035   struct RemarkPtrCompare {
0036     bool operator()(const std::unique_ptr<Remark> &LHS,
0037                     const std::unique_ptr<Remark> &RHS) const {
0038       assert(LHS && RHS && "Invalid pointers to compare.");
0039       return *LHS < *RHS;
0040     };
0041   };
0042 
0043   /// The main string table for the remarks.
0044   /// Note: all remarks should use the strings from this string table to avoid
0045   /// dangling references.
0046   StringTable StrTab;
0047 
0048   /// A set holding unique remarks.
0049   /// FIXME: std::set is probably not the most appropriate data structure here.
0050   /// Due to the limitation of having a move-only key, there isn't another
0051   /// obvious choice for now.
0052   std::set<std::unique_ptr<Remark>, RemarkPtrCompare> Remarks;
0053 
0054   /// A path to append before the external file path found in remark metadata.
0055   std::optional<std::string> PrependPath;
0056 
0057   /// If true, keep all remarks, otherwise only keep remarks with valid debug
0058   /// locations.
0059   bool KeepAllRemarks = true;
0060 
0061   /// Keep this remark. If it's already in the set, discard it.
0062   Remark &keep(std::unique_ptr<Remark> Remark);
0063 
0064   /// Returns true if \p R should be kept. If KeepAllRemarks is false, only
0065   /// return true if \p R has a valid debug location.
0066   bool shouldKeepRemark(const Remark &R) {
0067     return KeepAllRemarks ? true : R.Loc.has_value();
0068   }
0069 
0070 public:
0071   /// Set a path to prepend to the external file path.
0072   void setExternalFilePrependPath(StringRef PrependPath);
0073 
0074   /// Set KeepAllRemarks to \p B.
0075   void setKeepAllRemarks(bool B) { KeepAllRemarks = B; }
0076 
0077   /// Link the remarks found in \p Buffer.
0078   /// If \p RemarkFormat is not provided, try to deduce it from the metadata in
0079   /// \p Buffer.
0080   /// \p Buffer can be either a standalone remark container or just
0081   /// metadata. This takes care of uniquing and merging the remarks.
0082   Error link(StringRef Buffer,
0083              std::optional<Format> RemarkFormat = std::nullopt);
0084 
0085   /// Link the remarks found in \p Obj by looking for the right section and
0086   /// calling the method above.
0087   Error link(const object::ObjectFile &Obj,
0088              std::optional<Format> RemarkFormat = std::nullopt);
0089 
0090   /// Serialize the linked remarks to the stream \p OS, using the format \p
0091   /// RemarkFormat.
0092   /// This clears internal state such as the string table.
0093   /// Note: this implies that the serialization mode is standalone.
0094   Error serialize(raw_ostream &OS, Format RemarksFormat) const;
0095 
0096   /// Check whether there are any remarks linked.
0097   bool empty() const { return Remarks.empty(); }
0098 
0099   /// Return a collection of the linked unique remarks to iterate on.
0100   /// Ex:
0101   /// for (const Remark &R : RL.remarks() { [...] }
0102   using iterator = pointee_iterator<decltype(Remarks)::const_iterator>;
0103 
0104   iterator_range<iterator> remarks() const {
0105     return {Remarks.begin(), Remarks.end()};
0106   }
0107 };
0108 
0109 /// Returns a buffer with the contents of the remarks section depending on the
0110 /// format of the file. If the section doesn't exist, this returns an empty
0111 /// optional.
0112 Expected<std::optional<StringRef>>
0113 getRemarksSectionContents(const object::ObjectFile &Obj);
0114 
0115 } // end namespace remarks
0116 } // end namespace llvm
0117 
0118 #endif // LLVM_REMARKS_REMARKLINKER_H