File: source.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (173 lines) | stat: -rw-r--r-- 5,386 bytes parent folder | download | duplicates (6)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC.  All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

#include <cstddef>
#include <string>

#include "google/protobuf/descriptor.upb.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/io/printer.h"
#include "upb/mem/arena.hpp"
#include "upb/reflection/def.hpp"
#include "upb/util/def_to_proto.h"
#include "upb_generator/common/names.h"
#include "upb_generator/minitable/names.h"
#include "upb_generator/reflection/context.h"
#include "upb_generator/reflection/header.h"
#include "upb_generator/reflection/names.h"

namespace upb::generator::reflection {
namespace {

using Sub = google::protobuf::io::Printer::Sub;

static std::string DefSourceFilename(upb::FileDefPtr file) {
  return StripExtension(file.name()) + ".upbdefs.c";
}

void WriteIncludes(upb::FileDefPtr file, Context& ctx) {
  ctx.Emit(
      {
          {"def_header_filename", DefHeaderFilename(file)},
          {"mini_table_header_filename", MiniTableHeaderFilename(file.name())},
      },
      R"(
        #include "upb/reflection/def.h"
        #include "$def_header_filename$"
        #include "$mini_table_header_filename$"
      )");
}

void WriteDefPoolFwdDecls(upb::FileDefPtr file, Context& ctx) {
  if (file.dependency_count() == 0) return;

  for (int i = 0; i < file.dependency_count(); i++) {
    ctx.Emit(
        {{"dllexport_decl", ctx.options().dllexport_decl},
         {"def_init_symbol", ReflectionFileSymbol(file.dependency(i).name())}},
        R"cc(
          extern$ dllexport_decl$ _upb_DefPool_Init $def_init_symbol$;
        )cc");
  }

  ctx.Emit("\n");
}

void WriteStringArrayLine(absl::string_view data, Context& ctx) {
  std::string line = absl::StrJoin(data, ", ", [](std::string* out, char ch) {
    absl::StrAppendFormat(out, "'%v'",
                          absl::CEscape(absl::string_view(&ch, 1)));
  });
  ctx.Emit({{"line", line}},
           R"cc(
             $line$,
           )cc");
}

void WriteStringArray(absl::string_view data, Context& ctx) {
  const size_t kMaxLineLength = 12;
  for (size_t i = 0; i < data.size(); i += kMaxLineLength) {
    WriteStringArrayLine(data.substr(i, kMaxLineLength), ctx);
  }
}

void WriteDescriptor(upb::FileDefPtr file, Context& ctx) {
  upb::Arena arena;
  google_protobuf_FileDescriptorProto* file_proto =
      upb_FileDef_ToProto(file.ptr(), arena.ptr());
  size_t serialized_size;
  const char* serialized = google_protobuf_FileDescriptorProto_serialize(
      file_proto, arena.ptr(), &serialized_size);
  absl::string_view file_data(serialized, serialized_size);

  ctx.Emit({{"serialized_size", file_data.size()},
            {"contents", [&] { WriteStringArray(file_data, ctx); }}},
           R"cc(
             static const char descriptor[$serialized_size$] = {
                 $contents$,
             };
           )cc");
  ctx.Emit("\n");
}

void WriteDependencies(upb::FileDefPtr file, Context& ctx) {
  auto write_dep = [&](int i) {
    ctx.Emit({{"sym", ReflectionFileSymbol(file.dependency(i).name())}},
             R"cc(
               &$sym$,
             )cc");
  };

  auto write_deps = [&] {
    for (int i = 0; i < file.dependency_count(); i++) write_dep(i);
  };

  ctx.Emit({{"dep_count", file.dependency_count() + 1},
            {google::protobuf::io::Printer::Sub("deps", write_deps).WithSuffix(",")}},
           R"cc(
             static _upb_DefPool_Init *deps[$dep_count$] = {
                 $deps$,
                 NULL,
             };
           )cc");
  ctx.Emit("\n");
}

void WriteDefPoolInitStruct(upb::FileDefPtr file, Context& ctx) {
  ctx.Emit(
      {
          {"defpool_init_name", ReflectionFileSymbol(file.name())},
          {"file_name", file.name()},
          {"mini_table_file_var_name", MiniTableFileVarName(file.name())},
      },
      R"cc(
        _upb_DefPool_Init $defpool_init_name$ = {
            deps,
            &$mini_table_file_var_name$,
            "$file_name$",
            UPB_STRINGVIEW_INIT(descriptor, sizeof(descriptor)),
        };
      )cc");
}

void WriteDefPoolInit(upb::FileDefPtr file, Context& ctx) {
  WriteDefPoolFwdDecls(file, ctx);
  WriteDescriptor(file, ctx);
  WriteDependencies(file, ctx);
  WriteDefPoolInitStruct(file, ctx);
}

void WriteDefSource(upb::FileDefPtr file, Context& ctx) {
  ctx.Emit(
      {
          {Sub("file_warning", FileWarning(file.name())).WithSuffix(";")},
          {Sub("includes", [&] { WriteIncludes(file, ctx); }).WithSuffix(";")},
          {Sub("def_pool_init", [&] { WriteDefPoolInit(file, ctx); })
               .WithSuffix(";")},
      },
      R"cc(
        $file_warning$;
        $includes$;

        $def_pool_init$;
      )cc");
}

}  // namespace

void GenerateReflectionSource(upb::FileDefPtr file, const Options& options,
                              google::protobuf::compiler::GeneratorContext* context) {
  Context c_context(options, context->Open(DefSourceFilename(file)));
  WriteDefSource(file, c_context);
}

}  // namespace upb::generator::reflection