Fix field name conflict resolution for `has_` prefix.

Note: Code looks duplicated but in C case, it is for performance. For C++,
C++ and C may diverge in the future for certain methods.

PiperOrigin-RevId: 525826831
pull/13171/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 8c10056074
commit 0fa12dce2a
  1. 17
      protos_generator/gen_accessors.cc
  2. 14
      protos_generator/tests/BUILD
  3. 8
      protos_generator/tests/naming_conflict.proto
  4. 21
      upbc/names.cc

@ -735,16 +735,21 @@ std::string ResolveFieldName(const protobuf::FieldDescriptor* field,
"arena_", "arena_",
}); });
static constexpr absl::string_view kClearAccessor = "clear_"; // C++ specific prefixes used by code generator for field access.
static constexpr absl::string_view kSetAccessor = "set_"; static constexpr absl::string_view kClearMethodPrefix = "clear_";
static constexpr absl::string_view kSetMethodPrefix = "set_";
static constexpr absl::string_view kHasMethodPrefix = "has_";
static constexpr absl::string_view kDeleteMethodPrefix = "delete_";
static constexpr absl::string_view kAddToRepeatedMethodPrefix = "add_";
static constexpr absl::string_view kResizeArrayMethodPrefix = "resize_";
// List of generated accessor prefixes to check against. // List of generated accessor prefixes to check against.
// Example: // Example:
// optional repeated string phase = 236; // optional repeated string phase = 236;
// optional bool clear_phase = 237; // optional bool clear_phase = 237;
static constexpr absl::string_view kAccessorPrefixes[] = { static constexpr absl::string_view kAccessorPrefixes[] = {
kClearAccessor, "delete_", "add_", "resize_", kSetAccessor, kClearMethodPrefix, kDeleteMethodPrefix, kAddToRepeatedMethodPrefix,
}; kResizeArrayMethodPrefix, kSetMethodPrefix, kHasMethodPrefix};
absl::string_view field_name = field->name(); absl::string_view field_name = field->name();
if (kReservedNames.count(field_name) > 0) { if (kReservedNames.count(field_name) > 0) {
@ -765,8 +770,8 @@ std::string ResolveFieldName(const protobuf::FieldDescriptor* field,
if (candidate->is_repeated() || candidate->is_map() || if (candidate->is_repeated() || candidate->is_map() ||
(candidate->cpp_type() == (candidate->cpp_type() ==
protobuf::FieldDescriptor::CPPTYPE_STRING && protobuf::FieldDescriptor::CPPTYPE_STRING &&
prefix == kClearAccessor) || prefix == kClearMethodPrefix) ||
prefix == kSetAccessor) { prefix == kSetMethodPrefix || prefix == kHasMethodPrefix) {
return absl::StrCat(field_name, "_"); return absl::StrCat(field_name, "_");
} }
} }

@ -59,6 +59,13 @@ proto_library(
], ],
) )
proto_library(
name = "naming_conflict_proto",
srcs = [
"naming_conflict.proto",
],
)
proto_library( proto_library(
name = "no_package_enum_user_proto", name = "no_package_enum_user_proto",
srcs = [ srcs = [
@ -81,6 +88,12 @@ upb_cc_proto_library(
deps = [":test_model_proto"], deps = [":test_model_proto"],
) )
upb_cc_proto_library(
name = "naming_conflict_upb_cc_proto",
visibility = ["//protos:__pkg__"],
deps = [":naming_conflict_proto"],
)
upb_cc_proto_library( upb_cc_proto_library(
name = "no_package_upb_cc_proto", name = "no_package_upb_cc_proto",
deps = [ deps = [
@ -128,6 +141,7 @@ cc_test(
":no_package_upb_cc_proto", ":no_package_upb_cc_proto",
":test_model_upb_cc_proto", ":test_model_upb_cc_proto",
":test_model_upb_proto", ":test_model_upb_proto",
":naming_conflict_upb_cc_proto",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
"//:upb", "//:upb",
"//protos", "//protos",

@ -0,0 +1,8 @@
syntax = "proto3";
package protos_generator.test;
message HasChildCount {
optional HasChildCount has_child_count = 1;
optional int32 child_count = 2;
}

@ -37,16 +37,21 @@ namespace upbc {
namespace protobuf = ::google::protobuf; namespace protobuf = ::google::protobuf;
static constexpr absl::string_view kClearAccessor = "clear_"; // Prefixes used by C code generator for field access.
static constexpr absl::string_view kSetAccessor = "set_"; static constexpr absl::string_view kClearMethodPrefix = "clear_";
static constexpr absl::string_view kSetMethodPrefix = "set_";
static constexpr absl::string_view kHasMethodPrefix = "has_";
static constexpr absl::string_view kDeleteMethodPrefix = "delete_";
static constexpr absl::string_view kAddToRepeatedMethodPrefix = "add_";
static constexpr absl::string_view kResizeArrayMethodPrefix = "resize_";
// List of generated accessor prefixes to check against. // List of generated accessor prefixes to check against.
// Example: // Example:
// optional repeated string phase = 236; // optional repeated string phase = 236;
// optional bool clear_phase = 237; // optional bool clear_phase = 237;
static constexpr absl::string_view kAccessorPrefixes[] = { static constexpr absl::string_view kAccessorPrefixes[] = {
kClearAccessor, "delete_", "add_", "resize_", kSetAccessor, kClearMethodPrefix, kDeleteMethodPrefix, kAddToRepeatedMethodPrefix,
}; kResizeArrayMethodPrefix, kSetMethodPrefix, kHasMethodPrefix};
std::string ResolveFieldName(const protobuf::FieldDescriptor* field, std::string ResolveFieldName(const protobuf::FieldDescriptor* field,
const NameToFieldDescriptorMap& field_names) { const NameToFieldDescriptorMap& field_names) {
@ -62,8 +67,8 @@ std::string ResolveFieldName(const protobuf::FieldDescriptor* field,
if (candidate->is_repeated() || candidate->is_map() || if (candidate->is_repeated() || candidate->is_map() ||
(candidate->cpp_type() == (candidate->cpp_type() ==
protobuf::FieldDescriptor::CPPTYPE_STRING && protobuf::FieldDescriptor::CPPTYPE_STRING &&
prefix == kClearAccessor) || prefix == kClearMethodPrefix) ||
prefix == kSetAccessor) { prefix == kSetMethodPrefix || prefix == kHasMethodPrefix) {
return absl::StrCat(field_name, "_"); return absl::StrCat(field_name, "_");
} }
} }
@ -105,8 +110,8 @@ std::string ResolveFieldName(upb::FieldDefPtr field,
const auto candidate = match->second; const auto candidate = match->second;
if (candidate.IsSequence() || candidate.IsMap() || if (candidate.IsSequence() || candidate.IsMap() ||
(candidate.ctype() == kUpb_CType_String && (candidate.ctype() == kUpb_CType_String &&
prefix == kClearAccessor) || prefix == kClearMethodPrefix) ||
prefix == kSetAccessor) { prefix == kSetMethodPrefix || prefix == kHasMethodPrefix) {
return absl::StrCat(field_name, "_"); return absl::StrCat(field_name, "_");
} }
} }

Loading…
Cancel
Save