| 1 | //===-- llvm/Remarks/RemarkLinker.h -----------------------------*- 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 provides an interface to link together multiple remark files. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_REMARKS_REMARKLINKER_H |
| 14 | #define |
| 15 | |
| 16 | #include "llvm/Remarks/Remark.h" |
| 17 | #include "llvm/Remarks/RemarkFormat.h" |
| 18 | #include "llvm/Remarks/RemarkStringTable.h" |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include "llvm/Support/Error.h" |
| 21 | #include <memory> |
| 22 | #include <optional> |
| 23 | #include <set> |
| 24 | |
| 25 | namespace llvm { |
| 26 | |
| 27 | namespace object { |
| 28 | class ObjectFile; |
| 29 | } |
| 30 | |
| 31 | namespace remarks { |
| 32 | |
| 33 | struct { |
| 34 | private: |
| 35 | /// Compare through the pointers. |
| 36 | struct { |
| 37 | bool (const std::unique_ptr<Remark> &LHS, |
| 38 | const std::unique_ptr<Remark> &RHS) const { |
| 39 | assert(LHS && RHS && "Invalid pointers to compare." ); |
| 40 | return *LHS < *RHS; |
| 41 | }; |
| 42 | }; |
| 43 | |
| 44 | /// The main string table for the remarks. |
| 45 | /// Note: all remarks should use the strings from this string table to avoid |
| 46 | /// dangling references. |
| 47 | StringTable ; |
| 48 | |
| 49 | /// A set holding unique remarks. |
| 50 | /// FIXME: std::set is probably not the most appropriate data structure here. |
| 51 | /// Due to the limitation of having a move-only key, there isn't another |
| 52 | /// obvious choice for now. |
| 53 | std::set<std::unique_ptr<Remark>, RemarkPtrCompare> ; |
| 54 | |
| 55 | /// A path to append before the external file path found in remark metadata. |
| 56 | std::optional<std::string> ; |
| 57 | |
| 58 | /// If true, keep all remarks, otherwise only keep remarks with valid debug |
| 59 | /// locations. |
| 60 | bool = true; |
| 61 | |
| 62 | /// Keep this remark. If it's already in the set, discard it. |
| 63 | Remark &(std::unique_ptr<Remark> ); |
| 64 | |
| 65 | /// Returns true if \p R should be kept. If KeepAllRemarks is false, only |
| 66 | /// return true if \p R has a valid debug location. |
| 67 | bool (const Remark &R) { |
| 68 | return KeepAllRemarks ? true : R.Loc.has_value(); |
| 69 | } |
| 70 | |
| 71 | public: |
| 72 | /// Set a path to prepend to the external file path. |
| 73 | LLVM_ABI void (StringRef PrependPath); |
| 74 | |
| 75 | /// Set KeepAllRemarks to \p B. |
| 76 | void (bool B) { KeepAllRemarks = B; } |
| 77 | |
| 78 | /// Link the remarks found in \p Buffer. |
| 79 | /// If \p RemarkFormat is not provided, try to deduce it from the metadata in |
| 80 | /// \p Buffer. |
| 81 | /// \p Buffer can be either a standalone remark container or just |
| 82 | /// metadata. This takes care of uniquing and merging the remarks. |
| 83 | LLVM_ABI Error (StringRef Buffer, Format = Format::Auto); |
| 84 | |
| 85 | /// Link the remarks found in \p Obj by looking for the right section and |
| 86 | /// calling the method above. |
| 87 | LLVM_ABI Error (const object::ObjectFile &Obj, |
| 88 | Format = Format::Auto); |
| 89 | |
| 90 | /// Serialize the linked remarks to the stream \p OS, using the format \p |
| 91 | /// RemarkFormat. |
| 92 | /// This clears internal state such as the string table. |
| 93 | /// Note: this implies that the serialization mode is standalone. |
| 94 | LLVM_ABI Error (raw_ostream &OS, Format ) const; |
| 95 | |
| 96 | /// Check whether there are any remarks linked. |
| 97 | bool () const { return Remarks.empty(); } |
| 98 | |
| 99 | /// Return a collection of the linked unique remarks to iterate on. |
| 100 | /// Ex: |
| 101 | /// for (const Remark &R : RL.remarks() { [...] } |
| 102 | using = pointee_iterator<decltype(Remarks)::const_iterator>; |
| 103 | |
| 104 | iterator_range<iterator> () const { |
| 105 | return {Remarks.begin(), Remarks.end()}; |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | /// Returns a buffer with the contents of the remarks section depending on the |
| 110 | /// format of the file. If the section doesn't exist, this returns an empty |
| 111 | /// optional. |
| 112 | LLVM_ABI Expected<std::optional<StringRef>> |
| 113 | (const object::ObjectFile &Obj); |
| 114 | |
| 115 | } // end namespace remarks |
| 116 | } // end namespace llvm |
| 117 | |
| 118 | #endif // LLVM_REMARKS_REMARKLINKER_H |
| 119 | |