RemarkSerializer.h 3.3 KB

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