Annotate C++ oneof clear functions with set semantics.

PiperOrigin-RevId: 553253587
pull/13449/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent 0681fe6278
commit 3511f8acf9
  1. 2
      src/google/protobuf/compiler/cpp/message.cc
  2. 21
      src/google/protobuf/compiler/cpp/metadata_test.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$();

@ -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 <gtest/gtest.h>
#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"(

Loading…
Cancel
Save