Skip to content

Commit 80d4a3d

Browse files
eamonnmcmanuscopybara-github
authored andcommitted
Rename enum constants in ObjC code if they would clash with reserved names.
Previously the code just didn't emit a constant at all. Now, we do emit one, but with an extra underscore. For example, if the enum constant is `OVERFLOW`, which collides with a macro in `/usr/include/math.h`, we'll output `OVERFLOW_` instead. So client code can still reference it, albeit a little clumsily. Eventually we should change the hardcoded list of reserved names to use the same [reserved names list](https://github.com/google/j2objc/blob/master/translator/src/main/resources/com/google/devtools/j2objc/reserved_names.txt) that the j2objc translator uses. PiperOrigin-RevId: 743258491
1 parent 309666d commit 80d4a3d

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_enum.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) {
191191
"@property(class, readonly, retain) $classname$ *$name$ "
192192
"NS_SWIFT_NAME($name$);\n",
193193
"classname", ClassName(descriptor_), "name",
194-
canonical_values_[i]->name());
194+
SafeName(canonical_values_[i]->name()));
195195
}
196196
}
197197
if (!descriptor_->is_closed()) {
@@ -330,11 +330,12 @@ void EnumGenerator::GenerateSource(io::Printer* printer) {
330330
for (int i = 0; i < canonical_values_.size(); i++) {
331331
if (CanGenerateProperty(canonical_values_[i])) {
332332
printer->Print(
333-
"+ ($classname$ *) $name$ {\n"
333+
"+ ($classname$ *) $safe_name$ {\n"
334334
" return $classname$_get_$name$();\n"
335335
"}\n",
336336
"classname", ClassName(descriptor_), "name",
337-
canonical_values_[i]->name());
337+
canonical_values_[i]->name(), "safe_name",
338+
SafeName(canonical_values_[i]->name()));
338339
}
339340
}
340341

protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ std::string GetClassPrefix(const FileDescriptor *file,
229229

230230
} // namespace
231231

232-
std::string SafeName(const std::string &name) {
233-
std::string result = name;
232+
std::string SafeName(absl::string_view name) {
233+
std::string result = std::string(name);
234234
if (kKeywords.count(result) > 0) {
235235
result.append("_");
236236
}
@@ -700,8 +700,7 @@ bool CanGenerateProperty(const FieldDescriptor *descriptor) {
700700
}
701701

702702
bool CanGenerateProperty(const EnumValueDescriptor *descriptor) {
703-
return IsGenerateProperties(descriptor->file()) &&
704-
kKeywords.find(descriptor->name()) == kKeywords.end();
703+
return IsGenerateProperties(descriptor->file());
705704
}
706705

707706
void ParsePrefixLine(std::string line) {

protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace protobuf {
4444
namespace compiler {
4545
namespace j2objc {
4646

47-
std::string SafeName(const std::string &name);
47+
std::string SafeName(absl::string_view name);
4848

4949
std::string UnderscoresToCamelCase(absl::string_view input,
5050
bool cap_next_letter);

0 commit comments

Comments
 (0)