Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- YAMLRemarkSerializer.h - YAML Remark serialization ---*- 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 YAML.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_REMARKS_YAMLREMARKSERIALIZER_H
0014 #define LLVM_REMARKS_YAMLREMARKSERIALIZER_H
0015 
0016 #include "llvm/Remarks/RemarkSerializer.h"
0017 #include "llvm/Support/YAMLTraits.h"
0018 #include <optional>
0019 
0020 namespace llvm {
0021 namespace remarks {
0022 
0023 /// Serialize the remarks to YAML. One remark entry looks like this:
0024 /// --- !<TYPE>
0025 /// Pass:            <PASSNAME>
0026 /// Name:            <REMARKNAME>
0027 /// DebugLoc:        { File: <SOURCEFILENAME>, Line: <SOURCELINE>,
0028 ///                    Column: <SOURCECOLUMN> }
0029 /// Function:        <FUNCTIONNAME>
0030 /// Args:
0031 ///   - <KEY>: <VALUE>
0032 ///     DebugLoc:        { File: <FILE>, Line: <LINE>, Column: <COL> }
0033 /// ...
0034 struct YAMLRemarkSerializer : public RemarkSerializer {
0035   /// The YAML streamer.
0036   yaml::Output YAMLOutput;
0037 
0038   YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
0039                        std::optional<StringTable> StrTab = std::nullopt);
0040 
0041   void emit(const Remark &Remark) override;
0042   std::unique_ptr<MetaSerializer> metaSerializer(
0043       raw_ostream &OS,
0044       std::optional<StringRef> ExternalFilename = std::nullopt) override;
0045 
0046   static bool classof(const RemarkSerializer *S) {
0047     return S->SerializerFormat == Format::YAML;
0048   }
0049 
0050 protected:
0051   YAMLRemarkSerializer(Format SerializerFormat, raw_ostream &OS,
0052                        SerializerMode Mode,
0053                        std::optional<StringTable> StrTab = std::nullopt);
0054 };
0055 
0056 struct YAMLMetaSerializer : public MetaSerializer {
0057   std::optional<StringRef> ExternalFilename;
0058 
0059   YAMLMetaSerializer(raw_ostream &OS, std::optional<StringRef> ExternalFilename)
0060       : MetaSerializer(OS), ExternalFilename(ExternalFilename) {}
0061 
0062   void emit() override;
0063 };
0064 
0065 /// Serialize the remarks to YAML using a string table. An remark entry looks
0066 /// like the regular YAML remark but instead of string entries it's using
0067 /// numbers that map to an index in the string table.
0068 struct YAMLStrTabRemarkSerializer : public YAMLRemarkSerializer {
0069   /// Wether we already emitted the metadata in standalone mode.
0070   /// This should be set to true after the first invocation of `emit`.
0071   bool DidEmitMeta = false;
0072 
0073   YAMLStrTabRemarkSerializer(raw_ostream &OS, SerializerMode Mode)
0074       : YAMLRemarkSerializer(Format::YAMLStrTab, OS, Mode) {
0075     // We always need a string table for this type of serializer.
0076     StrTab.emplace();
0077   }
0078   YAMLStrTabRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
0079                              StringTable StrTab)
0080       : YAMLRemarkSerializer(Format::YAMLStrTab, OS, Mode, std::move(StrTab)) {}
0081 
0082   /// Override to emit the metadata if necessary.
0083   void emit(const Remark &Remark) override;
0084 
0085   std::unique_ptr<MetaSerializer> metaSerializer(
0086       raw_ostream &OS,
0087       std::optional<StringRef> ExternalFilename = std::nullopt) override;
0088 
0089   static bool classof(const RemarkSerializer *S) {
0090     return S->SerializerFormat == Format::YAMLStrTab;
0091   }
0092 };
0093 
0094 struct YAMLStrTabMetaSerializer : public YAMLMetaSerializer {
0095   /// The string table is part of the metadata.
0096   const StringTable &StrTab;
0097 
0098   YAMLStrTabMetaSerializer(raw_ostream &OS,
0099                            std::optional<StringRef> ExternalFilename,
0100                            const StringTable &StrTab)
0101       : YAMLMetaSerializer(OS, ExternalFilename), StrTab(StrTab) {}
0102 
0103   void emit() override;
0104 };
0105 
0106 } // end namespace remarks
0107 } // end namespace llvm
0108 
0109 #endif // LLVM_REMARKS_YAMLREMARKSERIALIZER_H