Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:25

0001 //===-- RemarkSerializer.h - Remark serialization interface -----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file provides an interface for serializing remarks to different formats.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_REMARKS_REMARKSERIALIZER_H
0014 #define LLVM_REMARKS_REMARKSERIALIZER_H
0015 
0016 #include "llvm/Remarks/RemarkFormat.h"
0017 #include "llvm/Remarks/RemarkStringTable.h"
0018 #include <optional>
0019 
0020 namespace llvm {
0021 
0022 class raw_ostream;
0023 
0024 namespace remarks {
0025 
0026 struct Remark;
0027 
0028 enum class SerializerMode {
0029   Separate,  // A mode where the metadata is serialized separately from the
0030              // remarks. Typically, this is used when the remarks need to be
0031              // streamed to a side file and the metadata is embedded into the
0032              // final result of the compilation.
0033   Standalone // A mode where everything can be retrieved in the same
0034              // file/buffer. Typically, this is used for storing remarks for
0035              // later use.
0036 };
0037 
0038 struct MetaSerializer;
0039 
0040 /// This is the base class for a remark serializer.
0041 /// It includes support for using a string table while emitting.
0042 struct RemarkSerializer {
0043   /// The format of the serializer.
0044   Format SerializerFormat;
0045   /// The open raw_ostream that the remark diagnostics are emitted to.
0046   raw_ostream &OS;
0047   /// The serialization mode.
0048   SerializerMode Mode;
0049   /// The string table containing all the unique strings used in the output.
0050   /// The table can be serialized to be consumed after the compilation.
0051   std::optional<StringTable> StrTab;
0052 
0053   RemarkSerializer(Format SerializerFormat, raw_ostream &OS,
0054                    SerializerMode Mode)
0055       : SerializerFormat(SerializerFormat), OS(OS), Mode(Mode) {}
0056 
0057   /// This is just an interface.
0058   virtual ~RemarkSerializer() = default;
0059   /// Emit a remark to the stream.
0060   virtual void emit(const Remark &Remark) = 0;
0061   /// Return the corresponding metadata serializer.
0062   virtual std::unique_ptr<MetaSerializer>
0063   metaSerializer(raw_ostream &OS,
0064                  std::optional<StringRef> ExternalFilename = std::nullopt) = 0;
0065 };
0066 
0067 /// This is the base class for a remark metadata serializer.
0068 struct MetaSerializer {
0069   /// The open raw_ostream that the metadata is emitted to.
0070   raw_ostream &OS;
0071 
0072   MetaSerializer(raw_ostream &OS) : OS(OS) {}
0073 
0074   /// This is just an interface.
0075   virtual ~MetaSerializer() = default;
0076   virtual void emit() = 0;
0077 };
0078 
0079 /// Create a remark serializer.
0080 Expected<std::unique_ptr<RemarkSerializer>>
0081 createRemarkSerializer(Format RemarksFormat, SerializerMode Mode,
0082                        raw_ostream &OS);
0083 
0084 /// Create a remark serializer that uses a pre-filled string table.
0085 Expected<std::unique_ptr<RemarkSerializer>>
0086 createRemarkSerializer(Format RemarksFormat, SerializerMode Mode,
0087                        raw_ostream &OS, remarks::StringTable StrTab);
0088 
0089 } // end namespace remarks
0090 } // end namespace llvm
0091 
0092 #endif // LLVM_REMARKS_REMARKSERIALIZER_H