1 // Copyright (c) 2009-2021, Google LLC
2 // All rights reserved.
3 //
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.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
19 // 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.
25
26 #include "protos_generator/gen_enums.h"
27
28 #include <algorithm>
29 #include <limits>
30 #include <string>
31 #include <vector>
32
33 #include "google/protobuf/descriptor.pb.h"
34 #include "google/protobuf/descriptor.h"
35 #include "protos_generator/gen_utils.h"
36
37 namespace protos_generator {
38
39 namespace protobuf = ::google::protobuf;
40
41 // Convert enum value to C++ literal.
42 //
43 // In C++, an value of -2147483648 gets interpreted as the negative of
44 // 2147483648, and since 2147483648 can't fit in an integer, this produces a
45 // compiler warning. This works around that issue.
EnumInt32ToString(int number)46 std::string EnumInt32ToString(int number) {
47 if (number == std::numeric_limits<int32_t>::min()) {
48 // This needs to be special-cased, see explanation here:
49 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52661
50 return absl::StrCat(number + 1, " - 1");
51 } else {
52 return absl::StrCat(number);
53 }
54 }
55
EnumTypeName(const protobuf::EnumDescriptor * enum_descriptor)56 std::string EnumTypeName(const protobuf::EnumDescriptor* enum_descriptor) {
57 auto containing_type = enum_descriptor->containing_type();
58 if (containing_type == nullptr) {
59 // enums types with no package name are prefixed with protos_ to prevent
60 // conflicts with generated C headers.
61 if (enum_descriptor->file()->package().empty()) {
62 return absl::StrCat(kNoPackageNamePrefix,
63 ToCIdent(enum_descriptor->name()));
64 }
65 return ToCIdent(enum_descriptor->name());
66 } else {
67 // Since the enum is in global name space (no package), it will have the
68 // same classified name as the C header include, to prevent collision
69 // rename as above.
70 if (containing_type->file()->package().empty()) {
71 return ToCIdent(absl::StrCat(containing_type->name(), "_",
72 kNoPackageNamePrefix,
73 enum_descriptor->name()));
74 } else {
75 return ToCIdent(
76 absl::StrCat(containing_type->name(), "_", enum_descriptor->name()));
77 }
78 }
79 }
80
EnumValueSymbolInNameSpace(const protobuf::EnumDescriptor * desc,const protobuf::EnumValueDescriptor * value)81 std::string EnumValueSymbolInNameSpace(
82 const protobuf::EnumDescriptor* desc,
83 const protobuf::EnumValueDescriptor* value) {
84 auto containing_type = desc->containing_type();
85 if (containing_type != nullptr) {
86 return ToCIdent(absl::StrCat(containing_type->name(), "_", desc->name(),
87 "_", value->name()));
88 } else {
89 // protos enum values with no package name are prefixed with protos_ to
90 // prevent conflicts with generated C headers.
91 if (desc->file()->package().empty()) {
92 return absl::StrCat(kNoPackageNamePrefix, ToCIdent(value->name()));
93 }
94 return ToCIdent(value->name());
95 }
96 }
97
WriteEnumValues(const protobuf::EnumDescriptor * desc,Output & output)98 void WriteEnumValues(const protobuf::EnumDescriptor* desc, Output& output) {
99 std::vector<const protobuf::EnumValueDescriptor*> values;
100 auto value_count = desc->value_count();
101 values.reserve(value_count);
102 for (int i = 0; i < value_count; i++) {
103 values.push_back(desc->value(i));
104 }
105 std::sort(values.begin(), values.end(),
106 [](const protobuf::EnumValueDescriptor* a,
107 const protobuf::EnumValueDescriptor* b) {
108 return a->number() < b->number();
109 });
110
111 for (size_t i = 0; i < values.size(); i++) {
112 auto value = values[i];
113 output(" $0", EnumValueSymbolInNameSpace(desc, value));
114 output(" = $0", EnumInt32ToString(value->number()));
115 if (i != values.size() - 1) {
116 output(",");
117 }
118 output("\n");
119 }
120 }
121
WriteEnumDeclarations(const std::vector<const protobuf::EnumDescriptor * > & enums,Output & output)122 void WriteEnumDeclarations(
123 const std::vector<const protobuf::EnumDescriptor*>& enums, Output& output) {
124 for (auto enumdesc : enums) {
125 output("enum $0 : int {\n", EnumTypeName(enumdesc));
126 WriteEnumValues(enumdesc, output);
127 output("};\n\n");
128 }
129 }
130
WriteHeaderEnumForwardDecls(std::vector<const protobuf::EnumDescriptor * > & enums,Output & output)131 void WriteHeaderEnumForwardDecls(
132 std::vector<const protobuf::EnumDescriptor*>& enums, Output& output) {
133 for (const auto* enumdesc : enums) {
134 output("enum $0 : int;\n", EnumTypeName(enumdesc));
135 }
136 }
137
138 } // namespace protos_generator
139