Skip to content

Commit

Permalink
Annotate C++ oneof clear functions with set semantics.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 553253587
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Aug 2, 2023
1 parent 0681fe6 commit 3511f8a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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$();
Expand Down
21 changes: 21 additions & 0 deletions src/google/protobuf/compiler/cpp/metadata_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
}
)";

Expand Down Expand Up @@ -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"(
Expand Down

0 comments on commit 3511f8a

Please sign in to comment.