From 672dc7baa335ee582a7e23393709796bd2a414a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89amonn=20McManus?= Date: Wed, 2 Apr 2025 13:29:21 -0700 Subject: [PATCH] Rename enum constants in ObjC proto 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 --- .../src/google/protobuf/compiler/j2objc/j2objc_enum.cc | 7 ++++--- .../src/google/protobuf/compiler/j2objc/j2objc_helpers.cc | 7 +++---- .../src/google/protobuf/compiler/j2objc/j2objc_helpers.h | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_enum.cc b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_enum.cc index 5510dff849..5fed21764f 100644 --- a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_enum.cc +++ b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_enum.cc @@ -191,7 +191,7 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) { "@property(class, readonly, retain) $classname$ *$name$ " "NS_SWIFT_NAME($name$);\n", "classname", ClassName(descriptor_), "name", - canonical_values_[i]->name()); + SafeName(canonical_values_[i]->name())); } } if (!descriptor_->is_closed()) { @@ -330,11 +330,12 @@ void EnumGenerator::GenerateSource(io::Printer* printer) { for (int i = 0; i < canonical_values_.size(); i++) { if (CanGenerateProperty(canonical_values_[i])) { printer->Print( - "+ ($classname$ *) $name$ {\n" + "+ ($classname$ *) $safe_name$ {\n" " return $classname$_get_$name$();\n" "}\n", "classname", ClassName(descriptor_), "name", - canonical_values_[i]->name()); + canonical_values_[i]->name(), "safe_name", + SafeName(canonical_values_[i]->name())); } } diff --git a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.cc b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.cc index 9ff220f49b..49bbf425d7 100644 --- a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.cc +++ b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.cc @@ -229,8 +229,8 @@ std::string GetClassPrefix(const FileDescriptor *file, } // namespace -std::string SafeName(const std::string &name) { - std::string result = name; +std::string SafeName(absl::string_view name) { + std::string result = std::string(name); if (kKeywords.count(result) > 0) { result.append("_"); } @@ -700,8 +700,7 @@ bool CanGenerateProperty(const FieldDescriptor *descriptor) { } bool CanGenerateProperty(const EnumValueDescriptor *descriptor) { - return IsGenerateProperties(descriptor->file()) && - kKeywords.find(descriptor->name()) == kKeywords.end(); + return IsGenerateProperties(descriptor->file()); } void ParsePrefixLine(std::string line) { diff --git a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.h b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.h index d47c95f32b..3eea1dbc4f 100644 --- a/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.h +++ b/protobuf/compiler/src/google/protobuf/compiler/j2objc/j2objc_helpers.h @@ -44,7 +44,7 @@ namespace protobuf { namespace compiler { namespace j2objc { -std::string SafeName(const std::string &name); +std::string SafeName(absl::string_view name); std::string UnderscoresToCamelCase(absl::string_view input, bool cap_next_letter);