Skip to content

Rename enum constants in ObjC proto code if they would clash with reserved names. #2461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("_");
}
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down