RemarkLinker.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef LLVM_REMARKS_REMARKLINKER_H
  13. #define LLVM_REMARKS_REMARKLINKER_H
  14. #include "llvm/Object/ObjectFile.h"
  15. #include "llvm/Remarks/Remark.h"
  16. #include "llvm/Remarks/RemarkFormat.h"
  17. #include "llvm/Remarks/RemarkStringTable.h"
  18. #include "llvm/Support/Error.h"
  19. #include <memory>
  20. #include <set>
  21. namespace llvm {
  22. namespace remarks {
  23. struct RemarkLinker {
  24. private:
  25. /// Compare through the pointers.
  26. struct RemarkPtrCompare {
  27. bool operator()(const std::unique_ptr<Remark> &LHS,
  28. const std::unique_ptr<Remark> &RHS) const {
  29. assert(LHS && RHS && "Invalid pointers to compare.");
  30. return *LHS < *RHS;
  31. };
  32. };
  33. /// The main string table for the remarks.
  34. /// Note: all remarks should use the strings from this string table to avoid
  35. /// dangling references.
  36. StringTable StrTab;
  37. /// A set holding unique remarks.
  38. /// FIXME: std::set is probably not the most appropriate data structure here.
  39. /// Due to the limitation of having a move-only key, there isn't another
  40. /// obvious choice for now.
  41. std::set<std::unique_ptr<Remark>, RemarkPtrCompare> Remarks;
  42. /// A path to append before the external file path found in remark metadata.
  43. Optional<std::string> PrependPath;
  44. /// Keep this remark. If it's already in the set, discard it.
  45. Remark &keep(std::unique_ptr<Remark> Remark);
  46. public:
  47. /// Set a path to prepend to the external file path.
  48. void setExternalFilePrependPath(StringRef PrependPath);
  49. /// Link the remarks found in \p Buffer.
  50. /// If \p RemarkFormat is not provided, try to deduce it from the metadata in
  51. /// \p Buffer.
  52. /// \p Buffer can be either a standalone remark container or just
  53. /// metadata. This takes care of uniquing and merging the remarks.
  54. Error link(StringRef Buffer, Optional<Format> RemarkFormat = None);
  55. /// Link the remarks found in \p Obj by looking for the right section and
  56. /// calling the method above.
  57. Error link(const object::ObjectFile &Obj,
  58. Optional<Format> RemarkFormat = None);
  59. /// Serialize the linked remarks to the stream \p OS, using the format \p
  60. /// RemarkFormat.
  61. /// This clears internal state such as the string table.
  62. /// Note: this implies that the serialization mode is standalone.
  63. Error serialize(raw_ostream &OS, Format RemarksFormat) const;
  64. /// Check whether there are any remarks linked.
  65. bool empty() const { return Remarks.empty(); }
  66. /// Return a collection of the linked unique remarks to iterate on.
  67. /// Ex:
  68. /// for (const Remark &R : RL.remarks() { [...] }
  69. using iterator = pointee_iterator<decltype(Remarks)::const_iterator>;
  70. iterator_range<iterator> remarks() const {
  71. return {Remarks.begin(), Remarks.end()};
  72. }
  73. };
  74. /// Returns a buffer with the contents of the remarks section depending on the
  75. /// format of the file. If the section doesn't exist, this returns an empty
  76. /// optional.
  77. Expected<Optional<StringRef>>
  78. getRemarksSectionContents(const object::ObjectFile &Obj);
  79. } // end namespace remarks
  80. } // end namespace llvm
  81. #endif // LLVM_REMARKS_REMARKLINKER_H