RemarkSerializer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- RemarkSerializer.h - Remark serialization interface -----*- 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 for serializing remarks to different formats.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_REMARKS_REMARKSERIALIZER_H
  18. #define LLVM_REMARKS_REMARKSERIALIZER_H
  19. #include "llvm/Remarks/RemarkFormat.h"
  20. #include "llvm/Remarks/RemarkStringTable.h"
  21. #include <optional>
  22. namespace llvm {
  23. class raw_ostream;
  24. namespace remarks {
  25. struct Remark;
  26. enum class SerializerMode {
  27. Separate, // A mode where the metadata is serialized separately from the
  28. // remarks. Typically, this is used when the remarks need to be
  29. // streamed to a side file and the metadata is embedded into the
  30. // final result of the compilation.
  31. Standalone // A mode where everything can be retrieved in the same
  32. // file/buffer. Typically, this is used for storing remarks for
  33. // later use.
  34. };
  35. struct MetaSerializer;
  36. /// This is the base class for a remark serializer.
  37. /// It includes support for using a string table while emitting.
  38. struct RemarkSerializer {
  39. /// The format of the serializer.
  40. Format SerializerFormat;
  41. /// The open raw_ostream that the remark diagnostics are emitted to.
  42. raw_ostream &OS;
  43. /// The serialization mode.
  44. SerializerMode Mode;
  45. /// The string table containing all the unique strings used in the output.
  46. /// The table can be serialized to be consumed after the compilation.
  47. std::optional<StringTable> StrTab;
  48. RemarkSerializer(Format SerializerFormat, raw_ostream &OS,
  49. SerializerMode Mode)
  50. : SerializerFormat(SerializerFormat), OS(OS), Mode(Mode) {}
  51. /// This is just an interface.
  52. virtual ~RemarkSerializer() = default;
  53. /// Emit a remark to the stream.
  54. virtual void emit(const Remark &Remark) = 0;
  55. /// Return the corresponding metadata serializer.
  56. virtual std::unique_ptr<MetaSerializer>
  57. metaSerializer(raw_ostream &OS,
  58. std::optional<StringRef> ExternalFilename = std::nullopt) = 0;
  59. };
  60. /// This is the base class for a remark metadata serializer.
  61. struct MetaSerializer {
  62. /// The open raw_ostream that the metadata is emitted to.
  63. raw_ostream &OS;
  64. MetaSerializer(raw_ostream &OS) : OS(OS) {}
  65. /// This is just an interface.
  66. virtual ~MetaSerializer() = default;
  67. virtual void emit() = 0;
  68. };
  69. /// Create a remark serializer.
  70. Expected<std::unique_ptr<RemarkSerializer>>
  71. createRemarkSerializer(Format RemarksFormat, SerializerMode Mode,
  72. raw_ostream &OS);
  73. /// Create a remark serializer that uses a pre-filled string table.
  74. Expected<std::unique_ptr<RemarkSerializer>>
  75. createRemarkSerializer(Format RemarksFormat, SerializerMode Mode,
  76. raw_ostream &OS, remarks::StringTable StrTab);
  77. } // end namespace remarks
  78. } // end namespace llvm
  79. #endif // LLVM_REMARKS_REMARKSERIALIZER_H
  80. #ifdef __GNUC__
  81. #pragma GCC diagnostic pop
  82. #endif