YAMLRemarkSerializer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- YAMLRemarkSerializer.h - YAML Remark serialization ---*- 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 YAML.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_REMARKS_YAMLREMARKSERIALIZER_H
  18. #define LLVM_REMARKS_YAMLREMARKSERIALIZER_H
  19. #include "llvm/Remarks/RemarkSerializer.h"
  20. #include "llvm/Support/YAMLTraits.h"
  21. namespace llvm {
  22. namespace remarks {
  23. /// Serialize the remarks to YAML. One remark entry looks like this:
  24. /// --- !<TYPE>
  25. /// Pass: <PASSNAME>
  26. /// Name: <REMARKNAME>
  27. /// DebugLoc: { File: <SOURCEFILENAME>, Line: <SOURCELINE>,
  28. /// Column: <SOURCECOLUMN> }
  29. /// Function: <FUNCTIONNAME>
  30. /// Args:
  31. /// - <KEY>: <VALUE>
  32. /// DebugLoc: { File: <FILE>, Line: <LINE>, Column: <COL> }
  33. /// ...
  34. struct YAMLRemarkSerializer : public RemarkSerializer {
  35. /// The YAML streamer.
  36. yaml::Output YAMLOutput;
  37. YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
  38. Optional<StringTable> StrTab = None);
  39. void emit(const Remark &Remark) override;
  40. std::unique_ptr<MetaSerializer>
  41. metaSerializer(raw_ostream &OS,
  42. Optional<StringRef> ExternalFilename = None) override;
  43. static bool classof(const RemarkSerializer *S) {
  44. return S->SerializerFormat == Format::YAML;
  45. }
  46. protected:
  47. YAMLRemarkSerializer(Format SerializerFormat, raw_ostream &OS,
  48. SerializerMode Mode,
  49. Optional<StringTable> StrTab = None);
  50. };
  51. struct YAMLMetaSerializer : public MetaSerializer {
  52. Optional<StringRef> ExternalFilename;
  53. YAMLMetaSerializer(raw_ostream &OS, Optional<StringRef> ExternalFilename)
  54. : MetaSerializer(OS), ExternalFilename(ExternalFilename) {}
  55. void emit() override;
  56. };
  57. /// Serialize the remarks to YAML using a string table. An remark entry looks
  58. /// like the regular YAML remark but instead of string entries it's using
  59. /// numbers that map to an index in the string table.
  60. struct YAMLStrTabRemarkSerializer : public YAMLRemarkSerializer {
  61. /// Wether we already emitted the metadata in standalone mode.
  62. /// This should be set to true after the first invocation of `emit`.
  63. bool DidEmitMeta = false;
  64. YAMLStrTabRemarkSerializer(raw_ostream &OS, SerializerMode Mode)
  65. : YAMLRemarkSerializer(Format::YAMLStrTab, OS, Mode) {
  66. // We always need a string table for this type of serializer.
  67. StrTab.emplace();
  68. }
  69. YAMLStrTabRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
  70. StringTable StrTab)
  71. : YAMLRemarkSerializer(Format::YAMLStrTab, OS, Mode, std::move(StrTab)) {}
  72. /// Override to emit the metadata if necessary.
  73. void emit(const Remark &Remark) override;
  74. std::unique_ptr<MetaSerializer>
  75. metaSerializer(raw_ostream &OS,
  76. Optional<StringRef> ExternalFilename = None) override;
  77. static bool classof(const RemarkSerializer *S) {
  78. return S->SerializerFormat == Format::YAMLStrTab;
  79. }
  80. };
  81. struct YAMLStrTabMetaSerializer : public YAMLMetaSerializer {
  82. /// The string table is part of the metadata.
  83. const StringTable &StrTab;
  84. YAMLStrTabMetaSerializer(raw_ostream &OS,
  85. Optional<StringRef> ExternalFilename,
  86. const StringTable &StrTab)
  87. : YAMLMetaSerializer(OS, ExternalFilename), StrTab(StrTab) {}
  88. void emit() override;
  89. };
  90. } // end namespace remarks
  91. } // end namespace llvm
  92. #endif // LLVM_REMARKS_YAMLREMARKSERIALIZER_H
  93. #ifdef __GNUC__
  94. #pragma GCC diagnostic pop
  95. #endif