Skip to content

Commit fd4fc9e

Browse files
committed
Hide GOOGLE_PROTOBUF_VERSION in port/
Our internal build does not define GOOGLE_PROTOBUF_VERSION.
1 parent f4e96ec commit fd4fc9e

File tree

5 files changed

+54
-44
lines changed

5 files changed

+54
-44
lines changed

port/protobuf.h

+45
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,51 @@ namespace protobuf_mutator {
6060

6161
namespace protobuf = google::protobuf;
6262

63+
inline bool RequiresUtf8Validation(
64+
const google::protobuf::FieldDescriptor& descriptor) {
65+
// commit d8c2501b43c1b56e3efa74048a18f8ce06ba07fe of >= v3.22.0
66+
#if GOOGLE_PROTOBUF_VERSION >= 4022000
67+
return descriptor.requires_utf8_validation();
68+
#else
69+
return descriptor.type() == google::protobuf::FieldDescriptor::TYPE_STRING &&
70+
descriptor.file()->syntax() ==
71+
google::protobuf::FileDescriptor::SYNTAX_PROTO3;
72+
#endif
73+
}
74+
75+
inline bool HasPresence(const google::protobuf::FieldDescriptor& descriptor) {
76+
// commit bb30225f06c36399757dc698b409d5f79738e8d1 of >=3.12.0
77+
#if GOOGLE_PROTOBUF_VERSION >= 3012000
78+
return descriptor.has_presence();
79+
#else
80+
// NOTE: This mimics Protobuf 3.21.12 ("3021012")
81+
return !descriptor.is_repeated() &&
82+
(descriptor.cpp_type() ==
83+
google::protobuf::FieldDescriptor::CppType::CPPTYPE_MESSAGE ||
84+
descriptor.containing_oneof() ||
85+
descriptor.file()->syntax() ==
86+
google::protobuf::FileDescriptor::SYNTAX_PROTO2);
87+
#endif
88+
}
89+
90+
inline void PrepareTextParser(google::protobuf::TextFormat::Parser& parser) {
91+
// commit d8c2501b43c1b56e3efa74048a18f8ce06ba07fe of >=3.8.0
92+
#if GOOGLE_PROTOBUF_VERSION >= 3008000
93+
parser.SetRecursionLimit(100);
94+
parser.AllowUnknownField(true);
95+
#endif
96+
}
97+
98+
constexpr bool TextParserCanSetRecursionLimit() {
99+
// commit d8c2501b43c1b56e3efa74048a18f8ce06ba07fe of >=3.8.0
100+
return GOOGLE_PROTOBUF_VERSION >= 3008000;
101+
}
102+
103+
constexpr bool TextParserCanAllowUnknownField() {
104+
// commit 176f7db11d8242b36a3ea6abb1cc436fca5bf75d of >=3.8.0
105+
return GOOGLE_PROTOBUF_VERSION >= 3008000;
106+
}
107+
63108
} // namespace protobuf_mutator
64109

65110
#endif // PORT_PROTOBUF_H_

src/field_instance.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,7 @@ class ConstFieldInstance {
190190
return descriptor_->message_type();
191191
}
192192

193-
bool EnforceUtf8() const {
194-
#if GOOGLE_PROTOBUF_VERSION >= 4022000 // v3(!).22.0 (commit d85c9944c55fb38f4eae149979a0f680ea125ecb) for requires_utf8_validation
195-
return descriptor_->requires_utf8_validation();
196-
#else
197-
return descriptor_->type() == protobuf::FieldDescriptor::TYPE_STRING &&
198-
descriptor()->file()->syntax() ==
199-
protobuf::FileDescriptor::SYNTAX_PROTO3;
200-
#endif
201-
}
193+
bool EnforceUtf8() const { return RequiresUtf8Validation(*descriptor_); }
202194

203195
const protobuf::FieldDescriptor* descriptor() const { return descriptor_; }
204196

src/mutator.cc

+3-23
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,7 @@ bool GetRandomBool(RandomEngine* random, size_t n = 2) {
8787
}
8888

8989
bool IsProto3SimpleField(const FieldDescriptor& field) {
90-
#if GOOGLE_PROTOBUF_VERSION >= 3012000 // commit bb30225f06c36399757dc698b409d5f79738e8d1 of >=3.12.0
91-
const bool has_presence = field.has_presence();
92-
#else
93-
// NOTE: This mimics Protobuf 3.21.12 ("3021012")
94-
const bool has_presence = ! field.is_repeated() && (
95-
field.cpp_type() == FieldDescriptor::CppType::CPPTYPE_MESSAGE
96-
|| field.containing_oneof()
97-
|| field.file()->syntax() == FileDescriptor::SYNTAX_PROTO2
98-
);
99-
#endif
100-
return !field.is_repeated() && !has_presence;
90+
return !field.is_repeated() && !HasPresence(field);
10191
}
10292

10393
struct CreateDefaultField : public FieldFunction<CreateDefaultField> {
@@ -390,23 +380,13 @@ std::unique_ptr<Message> UnpackAny(const Any& any) {
390380
}
391381

392382
const Any* CastToAny(const Message* message) {
393-
#if GOOGLE_PROTOBUF_VERSION >= 3008000 // commit 1467e08d7c26a7087e5e5b14a4ab2755926e7249 of >=3.8.0
394-
const Descriptor* any_descriptor = Any::GetDescriptor();
395-
#else
396-
const Descriptor* any_descriptor = Any::descriptor();
397-
#endif
398-
return any_descriptor == message->GetDescriptor()
383+
return Any::descriptor() == message->GetDescriptor()
399384
? protobuf::DownCastMessage<Any>(message)
400385
: nullptr;
401386
}
402387

403388
Any* CastToAny(Message* message) {
404-
#if GOOGLE_PROTOBUF_VERSION >= 3008000 // commit 1467e08d7c26a7087e5e5b14a4ab2755926e7249 of >=3.8.0
405-
const Descriptor* any_descriptor = Any::GetDescriptor();
406-
#else
407-
const Descriptor* any_descriptor = Any::descriptor();
408-
#endif
409-
return any_descriptor == message->GetDescriptor()
389+
return Any::descriptor() == message->GetDescriptor()
410390
? protobuf::DownCastMessage<Any>(message)
411391
: nullptr;
412392
}

src/mutator_test.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -678,18 +678,16 @@ TYPED_TEST(MutatorTypedTest, Serialization) {
678678
}
679679

680680
TYPED_TEST(MutatorTypedTest, UnknownFieldTextFormat) {
681-
#if GOOGLE_PROTOBUF_VERSION < 3008000 // commit 176f7db11d8242b36a3ea6abb1cc436fca5bf75d of >=3.8.0
682-
GTEST_SKIP() << "TextFormat::Parser::AllowUnknownField() is not available";
683-
#endif // GOOGLE_PROTOBUF_VERSION
681+
if (!TextParserCanAllowUnknownField())
682+
GTEST_SKIP() << "TextFormat::Parser::AllowUnknownField() is not available";
684683
typename TestFixture::Message parsed;
685684
EXPECT_TRUE(ParseTextMessage(kUnknownFieldInput, &parsed));
686685
EXPECT_EQ(SaveMessageAsText(parsed), kUnknownFieldExpected);
687686
}
688687

689688
TYPED_TEST(MutatorTypedTest, DeepRecursion) {
690-
#if GOOGLE_PROTOBUF_VERSION < 3008000 // commit d8c2501b43c1b56e3efa74048a18f8ce06ba07fe of >=3.8.0
691-
GTEST_SKIP() << "TextFormat::Parser::SetRecursionLimit() is not available";
692-
#endif // GOOGLE_PROTOBUF_VERSION
689+
if (!TextParserCanSetRecursionLimit())
690+
GTEST_SKIP() << "TextFormat::Parser::SetRecursionLimit() is not available";
693691
typename TestFixture::Message message;
694692
typename TestFixture::Message* last = &message;
695693
for (int i = 0; i < 150; ++i) {

src/text_format.cc

+1-6
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@ bool ParseTextMessage(const uint8_t* data, size_t size, Message* output) {
2828
bool ParseTextMessage(const std::string& data, protobuf::Message* output) {
2929
output->Clear();
3030
TextFormat::Parser parser;
31-
#if GOOGLE_PROTOBUF_VERSION >= 3008000 // commit d8c2501b43c1b56e3efa74048a18f8ce06ba07fe of >=3.8.0
32-
parser.SetRecursionLimit(100);
33-
#endif
3431
parser.AllowPartialMessage(true);
35-
#if GOOGLE_PROTOBUF_VERSION >= 3008000 // commit 176f7db11d8242b36a3ea6abb1cc436fca5bf75d of >=3.8.0
36-
parser.AllowUnknownField(true);
37-
#endif
32+
PrepareTextParser(parser);
3833
if (!parser.ParseFromString(data, output)) {
3934
output->Clear();
4035
return false;

0 commit comments

Comments
 (0)