LLVM  10.0.0
RemarkLinker.h
Go to the documentation of this file.
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_REMARK_LINKER_H
14 #define LLVM_REMARKS_REMARK_LINKER_H
15 
16 #include "llvm/Object/ObjectFile.h"
17 #include "llvm/Remarks/Remark.h"
20 #include "llvm/Support/Error.h"
21 #include <memory>
22 #include <set>
23 
24 namespace llvm {
25 namespace remarks {
26 
27 struct RemarkLinker {
28 private:
29  /// Compare through the pointers.
30  struct RemarkPtrCompare {
31  bool operator()(const std::unique_ptr<Remark> &LHS,
32  const std::unique_ptr<Remark> &RHS) const {
33  assert(LHS && RHS && "Invalid pointers to compare.");
34  return *LHS < *RHS;
35  };
36  };
37 
38  /// The main string table for the remarks.
39  /// Note: all remarks should use the strings from this string table to avoid
40  /// dangling references.
41  StringTable StrTab;
42 
43  /// A set holding unique remarks.
44  /// FIXME: std::set is probably not the most appropriate data structure here.
45  /// Due to the limitation of having a move-only key, there isn't another
46  /// obvious choice for now.
47  std::set<std::unique_ptr<Remark>, RemarkPtrCompare> Remarks;
48 
49  /// A path to append before the external file path found in remark metadata.
50  Optional<std::string> PrependPath;
51 
52  /// Keep this remark. If it's already in the set, discard it.
53  Remark &keep(std::unique_ptr<Remark> Remark);
54 
55 public:
56  /// Set a path to prepend to the external file path.
57  void setExternalFilePrependPath(StringRef PrependPath);
58 
59  /// Link the remarks found in \p Buffer.
60  /// If \p RemarkFormat is not provided, try to deduce it from the metadata in
61  /// \p Buffer.
62  /// \p Buffer can be either a standalone remark container or just
63  /// metadata. This takes care of uniquing and merging the remarks.
64  Error link(StringRef Buffer, Optional<Format> RemarkFormat = None);
65 
66  /// Link the remarks found in \p Obj by looking for the right section and
67  /// calling the method above.
68  Error link(const object::ObjectFile &Obj,
69  Optional<Format> RemarkFormat = None);
70 
71  /// Serialize the linked remarks to the stream \p OS, using the format \p
72  /// RemarkFormat.
73  /// This clears internal state such as the string table.
74  /// Note: this implies that the serialization mode is standalone.
76 
77  /// Check whether there are any remarks linked.
78  bool empty() const { return Remarks.empty(); }
79 
80  /// Return a collection of the linked unique remarks to iterate on.
81  /// Ex:
82  /// for (const Remark &R : RL.remarks() { [...] }
83  using iterator =
85 
87  return {Remarks.begin(), Remarks.end()};
88  }
89 };
90 
91 /// Returns a buffer with the contents of the remarks section depending on the
92 /// format of the file. If the section doesn't exist, this returns an empty
93 /// optional.
96 
97 } // end namespace remarks
98 } // end namespace llvm
99 
100 #endif /* LLVM_REMARKS_REMARK_LINKER_H */
const NoneType None
Definition: None.h:23
void setExternalFilePrependPath(StringRef PrependPath)
Set a path to prepend to the external file path.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This class is the base class for all object file types.
Definition: ObjectFile.h:223
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25
A remark type used for both emission and parsing.
Definition: Remark.h:67
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
The string table used for serializing remarks.
bool empty() const
Check whether there are any remarks linked.
Definition: RemarkLinker.h:78
Expected< Optional< StringRef > > getRemarksSectionContents(const object::ObjectFile &Obj)
Returns a buffer with the contents of the remarks section depending on the format of the file...
Error serialize(raw_ostream &OS, Format RemarksFormat) const
Serialize the linked remarks to the stream OS, using the format RemarkFormat.
Error link(StringRef Buffer, Optional< Format > RemarkFormat=None)
Link the remarks found in Buffer.
iterator_range< iterator > remarks() const
Definition: RemarkLinker.h:86
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:286
A range adaptor for a pair of iterators.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54