1 // Copyright (c) 2009-2021, Google LLC
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of Google LLC nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "absl/strings/str_replace.h"
27 #include "upbc/common.h"
32 namespace protobuf = ::google::protobuf;
34 void AddMessages(const protobuf::Descriptor* message,
35 std::vector<const protobuf::Descriptor*>* messages) {
36 messages->push_back(message);
37 for (int i = 0; i < message->nested_type_count(); i++) {
38 AddMessages(message->nested_type(i), messages);
44 std::string StripExtension(absl::string_view fname) {
45 size_t lastdot = fname.find_last_of('.');
46 if (lastdot == std::string::npos) {
47 return std::string(fname);
49 return std::string(fname.substr(0, lastdot));
52 std::string ToCIdent(absl::string_view str) {
53 return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}});
56 std::string ToPreproc(absl::string_view str) {
57 return absl::AsciiStrToUpper(ToCIdent(str));
60 void EmitFileWarning(const protobuf::FileDescriptor* file, Output& output) {
62 "/* This file was generated by upbc (the upb compiler) from the input\n"
67 " * Do not edit -- your changes will be discarded when the file is\n"
68 " * regenerated. */\n\n",
72 std::vector<const protobuf::Descriptor*> SortedMessages(
73 const protobuf::FileDescriptor* file) {
74 std::vector<const protobuf::Descriptor*> messages;
75 for (int i = 0; i < file->message_type_count(); i++) {
76 AddMessages(file->message_type(i), &messages);
81 std::string MessageName(const protobuf::Descriptor* descriptor) {
82 return ToCIdent(descriptor->full_name());
85 std::string MessageInit(const protobuf::Descriptor* descriptor) {
86 return MessageName(descriptor) + "_msginit";