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