Skip to content
Draft
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
21 changes: 16 additions & 5 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5652,11 +5652,22 @@ void MessageGenerator::GenerateSourceDefaultInstance(io::Printer* p) {
}},
{"section_decl",
[&] {
if (!use_implicit_weak_descriptor) return;
p->Emit({{"section",
WeakDefaultInstanceSection(
descriptor_, index_in_file_messages_, options_)}},
R"cc(__attribute__((section("$section$"))))cc");
if (use_implicit_weak_descriptor) {
p->Emit({{"section",
WeakDefaultInstanceSection(
descriptor_, index_in_file_messages_, options_)}},
R"cc(__attribute__((section("$section$"))))cc");
return;
}
// File descriptor proto is mutable.
if (is_file_descriptor_proto) return;

p->Emit(
R"cc(
#ifdef PROTOBUF_MESSAGE_GLOBALS
__attribute__((section(".data.rel.ro")))
#endif // PROTOBUF_MESSAGE_GLOBALS
)cc");
}},
{"const",
[&] {
Expand Down
12 changes: 10 additions & 2 deletions src/google/protobuf/compiler/java/java_features.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions src/google/protobuf/compiler/plugin.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion src/google/protobuf/cpp_features.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 36 additions & 19 deletions src/google/protobuf/message_unittest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ UNITTEST::NestedTestAllTypes InitNestedProto(int depth) {
}
} // namespace

TEST(MESSAGE_TEST_NAME, DieWhenDefaultInstanceIsMutated) {
#ifndef PROTOBUF_MESSAGE_GLOBALS
GTEST_SKIP() << "Skipping test as the failure is expected only with "
"PROTOBUF_MESSAGE_GLOBALS";
#endif // !PROTOBUF_MESSAGE_GLOBALS

auto* message = const_cast<UNITTEST::TestAllTypes*>(
&UNITTEST::TestAllTypes::default_instance());
EXPECT_DEATH(message->set_optional_int32(2), "SIGSEGV|Check failed");
}

TEST(MESSAGE_TEST_NAME, SerializeHelpers) {
// TODO: Test more helpers? They're all two-liners so it seems
// like a waste of time.
Expand Down Expand Up @@ -284,9 +295,11 @@ TEST(MESSAGE_TEST_NAME, ParseFailsIfNotInitialized) {

{
absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected);
EXPECT_CALL(log, Log(absl::LogSeverity::kError, testing::_, absl::StrCat(
"Can't parse message of type \"", UNITTEST_PACKAGE_NAME,
".TestRequired\" because it is missing required fields: a, b, c")));
EXPECT_CALL(log, Log(absl::LogSeverity::kError, testing::_,
absl::StrCat("Can't parse message of type \"",
UNITTEST_PACKAGE_NAME,
".TestRequired\" because it is missing "
"required fields: a, b, c")));
log.StartCapturingLogs();
EXPECT_FALSE(message.ParseFromString(""));
}
Expand All @@ -302,10 +315,13 @@ TEST(MESSAGE_TEST_NAME, ParseFailsIfSubmessageNotInitialized) {

{
absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected);
EXPECT_CALL(log, Log(absl::LogSeverity::kError, testing::_, absl::StrCat(
"Can't parse message of type \"", UNITTEST_PACKAGE_NAME,
".TestRequiredForeign\" because it is missing required fields: "
"optional_message.a, optional_message.b, optional_message.c")));
EXPECT_CALL(
log,
Log(absl::LogSeverity::kError, testing::_,
absl::StrCat(
"Can't parse message of type \"", UNITTEST_PACKAGE_NAME,
".TestRequiredForeign\" because it is missing required fields: "
"optional_message.a, optional_message.b, optional_message.c")));
log.StartCapturingLogs();
EXPECT_FALSE(message.ParseFromString(source.SerializePartialAsString()));
}
Expand All @@ -321,15 +337,17 @@ TEST(MESSAGE_TEST_NAME, ParseFailsIfExtensionNotInitialized) {
EXPECT_TRUE(message.ParsePartialFromString(serialized));
EXPECT_FALSE(message.IsInitialized());

{
{
absl::ScopedMockLog log(absl::MockLogDefault::kDisallowUnexpected);
EXPECT_CALL(log, Log(absl::LogSeverity::kError, testing::_, absl::Substitute(
"Can't parse message of type \"$0.TestChildExtension\" "
"because it is missing required fields: "
"optional_extension.($0.TestRequired.single).a, "
"optional_extension.($0.TestRequired.single).b, "
"optional_extension.($0.TestRequired.single).c",
UNITTEST_PACKAGE_NAME)));
EXPECT_CALL(log,
Log(absl::LogSeverity::kError, testing::_,
absl::Substitute(
"Can't parse message of type \"$0.TestChildExtension\" "
"because it is missing required fields: "
"optional_extension.($0.TestRequired.single).a, "
"optional_extension.($0.TestRequired.single).b, "
"optional_extension.($0.TestRequired.single).c",
UNITTEST_PACKAGE_NAME)));
log.StartCapturingLogs();
EXPECT_FALSE(message.ParseFromString(source.SerializePartialAsString()));
}
Expand Down Expand Up @@ -1430,9 +1448,8 @@ TEST(MESSAGE_TEST_NAME, MessageTraitsWork) {
EXPECT_EQ(
&UNITTEST::TestAllTypes::default_instance(),
internal::MessageTraits<UNITTEST::TestAllTypes>::default_instance());
EXPECT_EQ(
internal::GetClassData(UNITTEST::TestAllTypes::default_instance()),
internal::MessageTraits<UNITTEST::TestAllTypes>::class_data());
EXPECT_EQ(internal::GetClassData(UNITTEST::TestAllTypes::default_instance()),
internal::MessageTraits<UNITTEST::TestAllTypes>::class_data());
}


Expand Down Expand Up @@ -1885,7 +1902,7 @@ TEST(MESSAGE_TEST_NAME, TestInt32Parsers) {
if (field->name() == "other_field") continue;
SCOPED_TRACE(field->full_name());
for (int32_t value : {1, 0, -1, (std::numeric_limits<int32_t>::min)(),
(std::numeric_limits<int32_t>::max)()}) {
(std::numeric_limits<int32_t>::max)()}) {
SCOPED_TRACE(value);
auto encoded =
EncodeInt32Value(field->number(), value, non_canonical_bytes);
Expand Down
Loading