Automated rollback of commit 694d7935747a5b4eac351b757ad8ff22599c8077.
[google-protobuf.git] / upb_generator / minitable / main.cc
blobeff0186dd3aaa5de55a3537e392a60b8119a531d
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
8 #include <string>
9 #include <utility>
10 #include <vector>
12 #include "absl/log/absl_log.h"
13 #include "absl/strings/string_view.h"
14 #include "absl/strings/substitute.h"
15 #include "google/protobuf/compiler/code_generator_lite.h"
16 #include "upb/base/status.hpp"
17 #include "upb/base/string_view.h"
18 #include "upb/reflection/def.hpp"
19 #include "upb_generator/common.h"
20 #include "upb_generator/common/names.h"
21 #include "upb_generator/file_layout.h"
22 #include "upb_generator/minitable/generator.h"
23 #include "upb_generator/minitable/names_internal.h"
24 #include "upb_generator/plugin.h"
26 // Must be last.
27 #include "upb/port/def.inc"
29 namespace upb {
30 namespace generator {
32 std::string SourceFilename(upb::FileDefPtr file) {
33 return StripExtension(file.name()) + ".upb_minitable.c";
36 absl::string_view ToStringView(upb_StringView str) {
37 return absl::string_view(str.data, str.size);
40 void GenerateFile(const DefPoolPair& pools, upb::FileDefPtr file,
41 const MiniTableOptions& options, Plugin* plugin) {
42 Output h_output;
43 WriteMiniTableHeader(pools, file, options, h_output);
44 plugin->AddOutputFile(MiniTableHeaderFilename(file.name(), false),
45 h_output.output());
47 Output c_output;
48 WriteMiniTableSource(pools, file, options, c_output);
49 plugin->AddOutputFile(SourceFilename(file), c_output.output());
51 if (options.one_output_per_message) {
52 WriteMiniTableMultipleSources(pools, file, options, plugin);
56 bool ParseOptions(MiniTableOptions* options, Plugin* plugin) {
57 for (const auto& pair : ParseGeneratorParameter(plugin->parameter())) {
58 if (pair.first == "bootstrap_stage") {
59 options->bootstrap = true;
60 } else if (pair.first == "experimental_strip_nonfunctional_codegen") {
61 options->strip_nonfunctional_codegen = true;
62 } else if (pair.first == "one_output_per_message") {
63 options->one_output_per_message = true;
64 } else {
65 plugin->SetError(absl::Substitute("Unknown parameter: $0", pair.first));
66 return false;
70 return true;
73 int PluginMain(int argc, char** argv) {
74 DefPoolPair pools;
75 MiniTableOptions options;
76 Plugin plugin;
77 if (!ParseOptions(&options, &plugin)) return 0;
78 plugin.GenerateFilesRaw(
79 [&](const UPB_DESC(FileDescriptorProto) * file_proto, bool generate) {
80 upb::Status status;
81 upb::FileDefPtr file = pools.AddFile(file_proto, &status);
82 if (!file) {
83 absl::string_view name =
84 ToStringView(UPB_DESC(FileDescriptorProto_name)(file_proto));
85 ABSL_LOG(FATAL) << "Couldn't add file " << name
86 << " to DefPool: " << status.error_message();
88 if (generate) GenerateFile(pools, file, options, &plugin);
89 });
90 return 0;
93 } // namespace generator
94 } // namespace upb
96 int main(int argc, char** argv) {
97 return upb::generator::PluginMain(argc, argv);