Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 1 | //===-- RemarkSerializer.h - Remark serialization interface -----*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file provides an interface for serializing remarks to different formats. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_REMARKS_REMARK_SERIALIZER_H |
| 14 | #define LLVM_REMARKS_REMARK_SERIALIZER_H |
| 15 | |
| 16 | #include "llvm/Remarks/Remark.h" |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 17 | #include "llvm/Remarks/RemarkFormat.h" |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 18 | #include "llvm/Remarks/RemarkStringTable.h" |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | namespace remarks { |
| 23 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 24 | enum class SerializerMode { |
| 25 | Separate, // A mode where the metadata is serialized separately from the |
| 26 | // remarks. Typically, this is used when the remarks need to be |
| 27 | // streamed to a side file and the metadata is embedded into the |
| 28 | // final result of the compilation. |
| 29 | Standalone // A mode where everything can be retrieved in the same |
| 30 | // file/buffer. Typically, this is used for storing remarks for |
| 31 | // later use. |
| 32 | }; |
| 33 | |
| 34 | struct MetaSerializer; |
| 35 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 36 | /// This is the base class for a remark serializer. |
| 37 | /// It includes support for using a string table while emitting. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 38 | struct RemarkSerializer { |
| 39 | /// The format of the serializer. |
| 40 | Format SerializerFormat; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 41 | /// The open raw_ostream that the remark diagnostics are emitted to. |
| 42 | raw_ostream &OS; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 43 | /// The serialization mode. |
| 44 | SerializerMode Mode; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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 | Optional<StringTable> StrTab; |
| 48 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 49 | RemarkSerializer(Format SerializerFormat, raw_ostream &OS, |
| 50 | SerializerMode Mode) |
| 51 | : SerializerFormat(SerializerFormat), OS(OS), Mode(Mode), StrTab() {} |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 52 | |
| 53 | /// This is just an interface. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 54 | virtual ~RemarkSerializer() = default; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 55 | /// Emit a remark to the stream. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 56 | virtual void emit(const Remark &Remark) = 0; |
| 57 | /// Return the corresponding metadata serializer. |
| 58 | virtual std::unique_ptr<MetaSerializer> |
| 59 | metaSerializer(raw_ostream &OS, |
| 60 | Optional<StringRef> ExternalFilename = None) = 0; |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 63 | /// This is the base class for a remark metadata serializer. |
| 64 | struct MetaSerializer { |
| 65 | /// The open raw_ostream that the metadata is emitted to. |
| 66 | raw_ostream &OS; |
| 67 | |
| 68 | MetaSerializer(raw_ostream &OS) : OS(OS) {} |
| 69 | |
| 70 | /// This is just an interface. |
| 71 | virtual ~MetaSerializer() = default; |
| 72 | virtual void emit() = 0; |
| 73 | }; |
| 74 | |
| 75 | /// Create a remark serializer. |
| 76 | Expected<std::unique_ptr<RemarkSerializer>> |
| 77 | createRemarkSerializer(Format RemarksFormat, SerializerMode Mode, |
| 78 | raw_ostream &OS); |
| 79 | |
| 80 | /// Create a remark serializer that uses a pre-filled string table. |
| 81 | Expected<std::unique_ptr<RemarkSerializer>> |
| 82 | createRemarkSerializer(Format RemarksFormat, SerializerMode Mode, |
| 83 | raw_ostream &OS, remarks::StringTable StrTab); |
| 84 | |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 85 | } // end namespace remarks |
| 86 | } // end namespace llvm |
| 87 | |
| 88 | #endif /* LLVM_REMARKS_REMARK_SERIALIZER_H */ |