From 3511f8acf9ea095c1c872343d2e725ed90217414 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 2 Aug 2023 14:14:52 -0700 Subject: [PATCH] Annotate C++ oneof clear functions with set semantics. PiperOrigin-RevId: 553253587 --- src/google/protobuf/compiler/cpp/message.cc | 2 +- .../protobuf/compiler/cpp/metadata_test.cc | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index b9dfb7a70e..2cee739d9f 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -948,7 +948,7 @@ void MessageGenerator::GenerateFieldAccessorDeclarations(io::Printer* p) { for (auto oneof : OneOfRange(descriptor_)) { p->Emit({{"oneof_name", oneof->name()}, Sub{"clear_oneof_name", absl::StrCat("clear_", oneof->name())} - .AnnotatedAs(oneof), + .AnnotatedAs({oneof, Semantic::kSet}), {"OneOfName", UnderscoresToCamelCase(oneof->name(), true)}}, R"cc( void $clear_oneof_name$(); diff --git a/src/google/protobuf/compiler/cpp/metadata_test.cc b/src/google/protobuf/compiler/cpp/metadata_test.cc index e95ee566f8..2319e508c0 100644 --- a/src/google/protobuf/compiler/cpp/metadata_test.cc +++ b/src/google/protobuf/compiler/cpp/metadata_test.cc @@ -35,6 +35,7 @@ #include "google/protobuf/compiler/cpp/generator.h" #include "google/protobuf/compiler/command_line_interface.h" #include "google/protobuf/descriptor.pb.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/testing/googletest.h" #include #include "absl/log/absl_check.h" @@ -505,6 +506,9 @@ constexpr absl::string_view kMessageFieldTestFile = R"( message Message { optional SMessage mfield = 1; repeated SMessage rmfield = 2; + oneof ofield { + int32 oint = 3; + } } )"; @@ -556,6 +560,23 @@ TEST_F(CppMetadataTest, AnnotatesMessageSemantics) { annotation->semantic()); } } + field_path.clear(); + field_path.push_back(FileDescriptorProto::kMessageTypeFieldNumber); + field_path.push_back(1); + field_path.push_back(DescriptorProto::kOneofDeclFieldNumber); + field_path.push_back(0); + annotations.clear(); + atu::FindAnnotationsOnPath(info, "test.proto", field_path, &annotations); + EXPECT_TRUE(!annotations.empty()); + for (const auto* annotation : annotations) { + auto substring = atu::GetAnnotationSubstring(pb_h, *annotation); + ASSERT_TRUE(substring.has_value()); + if (*substring == "ofield_case") { + EXPECT_EQ(GeneratedCodeInfo::Annotation::NONE, annotation->semantic()); + } else if (*substring == "clear_ofield") { + EXPECT_EQ(GeneratedCodeInfo::Annotation::SET, annotation->semantic()); + } + } } constexpr absl::string_view kLazyMessageFieldTestFile = R"(