RemarkLinker.h 3.7 KB

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