RemarkLinker.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/Remarks/RemarkLinker.h -----------------------------*- C++/-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file provides an interface to link together multiple remark files.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_REMARKS_REMARKLINKER_H
  18. #define LLVM_REMARKS_REMARKLINKER_H
  19. #include "llvm/Remarks/Remark.h"
  20. #include "llvm/Remarks/RemarkFormat.h"
  21. #include "llvm/Remarks/RemarkStringTable.h"
  22. #include "llvm/Support/Error.h"
  23. #include <memory>
  24. #include <optional>
  25. #include <set>
  26. namespace llvm {
  27. namespace object {
  28. class ObjectFile;
  29. }
  30. namespace remarks {
  31. struct RemarkLinker {
  32. private:
  33. /// Compare through the pointers.
  34. struct RemarkPtrCompare {
  35. bool operator()(const std::unique_ptr<Remark> &LHS,
  36. const std::unique_ptr<Remark> &RHS) const {
  37. assert(LHS && RHS && "Invalid pointers to compare.");
  38. return *LHS < *RHS;
  39. };
  40. };
  41. /// The main string table for the remarks.
  42. /// Note: all remarks should use the strings from this string table to avoid
  43. /// dangling references.
  44. StringTable StrTab;
  45. /// A set holding unique remarks.
  46. /// FIXME: std::set is probably not the most appropriate data structure here.
  47. /// Due to the limitation of having a move-only key, there isn't another
  48. /// obvious choice for now.
  49. std::set<std::unique_ptr<Remark>, RemarkPtrCompare> Remarks;
  50. /// A path to append before the external file path found in remark metadata.
  51. std::optional<std::string> PrependPath;
  52. /// Keep this remark. If it's already in the set, discard it.
  53. Remark &keep(std::unique_ptr<Remark> Remark);
  54. public:
  55. /// Set a path to prepend to the external file path.
  56. void setExternalFilePrependPath(StringRef PrependPath);
  57. /// Link the remarks found in \p Buffer.
  58. /// If \p RemarkFormat is not provided, try to deduce it from the metadata in
  59. /// \p Buffer.
  60. /// \p Buffer can be either a standalone remark container or just
  61. /// metadata. This takes care of uniquing and merging the remarks.
  62. Error link(StringRef Buffer,
  63. std::optional<Format> RemarkFormat = std::nullopt);
  64. /// Link the remarks found in \p Obj by looking for the right section and
  65. /// calling the method above.
  66. Error link(const object::ObjectFile &Obj,
  67. std::optional<Format> RemarkFormat = std::nullopt);
  68. /// Serialize the linked remarks to the stream \p OS, using the format \p
  69. /// RemarkFormat.
  70. /// This clears internal state such as the string table.
  71. /// Note: this implies that the serialization mode is standalone.
  72. Error serialize(raw_ostream &OS, Format RemarksFormat) const;
  73. /// Check whether there are any remarks linked.
  74. bool empty() const { return Remarks.empty(); }
  75. /// Return a collection of the linked unique remarks to iterate on.
  76. /// Ex:
  77. /// for (const Remark &R : RL.remarks() { [...] }
  78. using iterator = pointee_iterator<decltype(Remarks)::const_iterator>;
  79. iterator_range<iterator> remarks() const {
  80. return {Remarks.begin(), Remarks.end()};
  81. }
  82. };
  83. /// Returns a buffer with the contents of the remarks section depending on the
  84. /// format of the file. If the section doesn't exist, this returns an empty
  85. /// optional.
  86. Expected<std::optional<StringRef>>
  87. getRemarksSectionContents(const object::ObjectFile &Obj);
  88. } // end namespace remarks
  89. } // end namespace llvm
  90. #endif // LLVM_REMARKS_REMARKLINKER_H
  91. #ifdef __GNUC__
  92. #pragma GCC diagnostic pop
  93. #endif