aboutsummaryrefslogtreecommitdiff
path: root/llvm/include/llvm/Remarks/YAMLRemarkSerializer.h
blob: 4dfe1841b51d070a018a5b4301d707a1da598b92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//===-- YAMLRemarkSerializer.h - YAML Remark serialization ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides an interface for serializing remarks to YAML.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_REMARKS_YAMLREMARKSERIALIZER_H
#define LLVM_REMARKS_YAMLREMARKSERIALIZER_H

#include "llvm/Remarks/RemarkSerializer.h"
#include "llvm/Support/YAMLTraits.h"

namespace llvm {
namespace remarks {

/// Serialize the remarks to YAML. One remark entry looks like this:
/// --- !<TYPE>
/// Pass:            <PASSNAME>
/// Name:            <REMARKNAME>
/// DebugLoc:        { File: <SOURCEFILENAME>, Line: <SOURCELINE>,
///                    Column: <SOURCECOLUMN> }
/// Function:        <FUNCTIONNAME>
/// Args:
///   - <KEY>: <VALUE>
///     DebugLoc:        { File: <FILE>, Line: <LINE>, Column: <COL> }
/// ...
struct YAMLRemarkSerializer : public RemarkSerializer {
  /// The YAML streamer.
  yaml::Output YAMLOutput;

  YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
                       Optional<StringTable> StrTab = None);

  void emit(const Remark &Remark) override;
  std::unique_ptr<MetaSerializer>
  metaSerializer(raw_ostream &OS,
                 Optional<StringRef> ExternalFilename = None) override;

  static bool classof(const RemarkSerializer *S) {
    return S->SerializerFormat == Format::YAML;
  }

protected:
  YAMLRemarkSerializer(Format SerializerFormat, raw_ostream &OS,
                       SerializerMode Mode,
                       Optional<StringTable> StrTab = None);
};

struct YAMLMetaSerializer : public MetaSerializer {
  Optional<StringRef> ExternalFilename;

  YAMLMetaSerializer(raw_ostream &OS, Optional<StringRef> ExternalFilename)
      : MetaSerializer(OS), ExternalFilename(ExternalFilename) {}

  void emit() override;
};

/// Serialize the remarks to YAML using a string table. An remark entry looks
/// like the regular YAML remark but instead of string entries it's using
/// numbers that map to an index in the string table.
struct YAMLStrTabRemarkSerializer : public YAMLRemarkSerializer {
  /// Wether we already emitted the metadata in standalone mode.
  /// This should be set to true after the first invocation of `emit`.
  bool DidEmitMeta = false;

  YAMLStrTabRemarkSerializer(raw_ostream &OS, SerializerMode Mode)
      : YAMLRemarkSerializer(Format::YAMLStrTab, OS, Mode) {
    // We always need a string table for this type of serializer.
    StrTab.emplace();
  }
  YAMLStrTabRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
                             StringTable StrTab)
      : YAMLRemarkSerializer(Format::YAMLStrTab, OS, Mode, std::move(StrTab)) {}

  /// Override to emit the metadata if necessary.
  void emit(const Remark &Remark) override;

  std::unique_ptr<MetaSerializer>
  metaSerializer(raw_ostream &OS,
                 Optional<StringRef> ExternalFilename = None) override;

  static bool classof(const RemarkSerializer *S) {
    return S->SerializerFormat == Format::YAMLStrTab;
  }
};

struct YAMLStrTabMetaSerializer : public YAMLMetaSerializer {
  /// The string table is part of the metadata.
  const StringTable &StrTab;

  YAMLStrTabMetaSerializer(raw_ostream &OS,
                           Optional<StringRef> ExternalFilename,
                           const StringTable &StrTab)
      : YAMLMetaSerializer(OS, ExternalFilename), StrTab(StrTab) {}

  void emit() override;
};

} // end namespace remarks
} // end namespace llvm

#endif // LLVM_REMARKS_YAMLREMARKSERIALIZER_H