Minor cleanup: IWYU, clang-tidy, etc.

PiperOrigin-RevId: 601865102
pull/15601/head
Protobuf Team Bot 10 months ago committed by Copybara-Service
parent a892c20578
commit a46d82fd5d
  1. 2
      src/google/protobuf/BUILD.bazel
  2. 5
      src/google/protobuf/compiler/command_line_interface_tester.cc
  3. 8
      src/google/protobuf/compiler/cpp/generator.cc
  4. 6
      src/google/protobuf/compiler/cpp/generator_unittest.cc
  5. 162
      src/google/protobuf/descriptor.cc
  6. 87
      src/google/protobuf/descriptor_unittest.cc

@ -1156,8 +1156,10 @@ cc_test(
"//src/google/protobuf/stubs", "//src/google/protobuf/stubs",
"//src/google/protobuf/testing", "//src/google/protobuf/testing",
"//src/google/protobuf/testing:file", "//src/google/protobuf/testing:file",
"@com_google_absl//absl/base:log_severity",
"@com_google_absl//absl/container:btree", "@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/functional:any_invocable", "@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/log:absl_check", "@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log", "@com_google_absl//absl/log:absl_log",

@ -7,19 +7,22 @@
#include "google/protobuf/compiler/command_line_interface_tester.h" #include "google/protobuf/compiler/command_line_interface_tester.h"
#include <memory> #include <cstddef>
#include <string> #include <string>
#include <vector> #include <vector>
#include "google/protobuf/testing/file.h"
#include "google/protobuf/testing/file.h" #include "google/protobuf/testing/file.h"
#include "google/protobuf/testing/file.h" #include "google/protobuf/testing/file.h"
#include <gmock/gmock.h> #include <gmock/gmock.h>
#include "google/protobuf/testing/googletest.h" #include "google/protobuf/testing/googletest.h"
#include <gtest/gtest.h>
#include "absl/log/absl_check.h" #include "absl/log/absl_check.h"
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_replace.h" #include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h" #include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
namespace google { namespace google {
namespace protobuf { namespace protobuf {

@ -22,13 +22,16 @@
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/strings/match.h" #include "absl/strings/match.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h" #include "absl/strings/str_split.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "google/protobuf/compiler/code_generator.h"
#include "google/protobuf/compiler/cpp/file.h" #include "google/protobuf/compiler/cpp/file.h"
#include "google/protobuf/compiler/cpp/helpers.h" #include "google/protobuf/compiler/cpp/helpers.h"
#include "google/protobuf/compiler/cpp/options.h" #include "google/protobuf/compiler/cpp/options.h"
#include "google/protobuf/cpp_features.pb.h" #include "google/protobuf/cpp_features.pb.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_visitor.h" #include "google/protobuf/descriptor_visitor.h"
#include "google/protobuf/io/printer.h" #include "google/protobuf/io/printer.h"
@ -142,8 +145,9 @@ bool CppGenerator::Generate(const FileDescriptor* file,
} else if (key == "lite_implicit_weak_fields") { } else if (key == "lite_implicit_weak_fields") {
file_options.enforce_mode = EnforceOptimizeMode::kLiteRuntime; file_options.enforce_mode = EnforceOptimizeMode::kLiteRuntime;
file_options.lite_implicit_weak_fields = true; file_options.lite_implicit_weak_fields = true;
if (!value.empty()) { int num_cc_files;
file_options.num_cc_files = std::strtol(value.c_str(), nullptr, 10); if (!value.empty() && absl::SimpleAtoi(value, &num_cc_files)) {
file_options.num_cc_files = num_cc_files;
} }
} else if (key == "descriptor_implicit_weak_messages") { } else if (key == "descriptor_implicit_weak_messages") {
file_options.descriptor_implicit_weak_messages = true; file_options.descriptor_implicit_weak_messages = true;

@ -69,7 +69,7 @@ TEST_F(CppGeneratorTest, LegacyClosedEnumOnNonEnumField) {
R"schema( R"schema(
edition = "2023"; edition = "2023";
import "google/protobuf/cpp_features.proto"; import "google/protobuf/cpp_features.proto";
message Foo { message Foo {
int32 bar = 1 [features.(pb.cpp).legacy_closed_enum = true]; int32 bar = 1 [features.(pb.cpp).legacy_closed_enum = true];
})schema"); })schema");
@ -109,7 +109,7 @@ TEST_F(CppGeneratorTest, LegacyClosedEnumInherited) {
edition = "2023"; edition = "2023";
import "google/protobuf/cpp_features.proto"; import "google/protobuf/cpp_features.proto";
option features.(pb.cpp).legacy_closed_enum = true; option features.(pb.cpp).legacy_closed_enum = true;
enum TestEnum { enum TestEnum {
TEST_ENUM_UNKNOWN = 0; TEST_ENUM_UNKNOWN = 0;
} }
@ -130,7 +130,7 @@ TEST_F(CppGeneratorTest, LegacyClosedEnumImplicit) {
edition = "2023"; edition = "2023";
import "google/protobuf/cpp_features.proto"; import "google/protobuf/cpp_features.proto";
option features.(pb.cpp).legacy_closed_enum = true; option features.(pb.cpp).legacy_closed_enum = true;
enum TestEnum { enum TestEnum {
TEST_ENUM_UNKNOWN = 0; TEST_ENUM_UNKNOWN = 0;
} }

@ -11,23 +11,32 @@
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include <limits.h>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <atomic>
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <functional> #include <cstring>
#include <initializer_list>
#include <iterator> #include <iterator>
#include <limits> #include <limits>
#include <memory> #include <memory>
#include <new> // IWYU pragma: keep
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <tuple>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "google/protobuf/stubs/common.h" #include "absl/base/attributes.h"
#include "absl/base/call_once.h" #include "absl/base/call_once.h"
#include "absl/base/casts.h" #include "absl/base/casts.h"
#include "absl/base/const_init.h"
#include "absl/base/dynamic_annotations.h" #include "absl/base/dynamic_annotations.h"
#include "absl/base/thread_annotations.h"
#include "absl/container/btree_map.h" #include "absl/container/btree_map.h"
#include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h" #include "absl/container/flat_hash_set.h"
@ -50,17 +59,20 @@
#include "absl/strings/substitute.h" #include "absl/strings/substitute.h"
#include "absl/synchronization/mutex.h" #include "absl/synchronization/mutex.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include "absl/types/span.h"
#include "google/protobuf/any.h" #include "google/protobuf/any.h"
#include "google/protobuf/cpp_edition_defaults.h" #include "google/protobuf/cpp_edition_defaults.h"
#include "google/protobuf/cpp_features.pb.h" #include "google/protobuf/cpp_features.pb.h"
#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_database.h" #include "google/protobuf/descriptor_database.h"
#include "google/protobuf/descriptor_lite.h"
#include "google/protobuf/descriptor_visitor.h" #include "google/protobuf/descriptor_visitor.h"
#include "google/protobuf/dynamic_message.h" #include "google/protobuf/dynamic_message.h"
#include "google/protobuf/feature_resolver.h" #include "google/protobuf/feature_resolver.h"
#include "google/protobuf/generated_message_util.h" #include "google/protobuf/generated_message_util.h"
#include "google/protobuf/io/strtod.h" #include "google/protobuf/io/strtod.h"
#include "google/protobuf/io/tokenizer.h" #include "google/protobuf/io/tokenizer.h"
#include "google/protobuf/message_lite.h"
#include "google/protobuf/parse_context.h" #include "google/protobuf/parse_context.h"
#include "google/protobuf/port.h" #include "google/protobuf/port.h"
#include "google/protobuf/repeated_ptr_field.h" #include "google/protobuf/repeated_ptr_field.h"
@ -251,7 +263,7 @@ class FlatAllocation {
public: public:
static constexpr size_t kMaxAlign = Max(alignof(T)...); static constexpr size_t kMaxAlign = Max(alignof(T)...);
FlatAllocation(const TypeMap<IntT, T...>& ends) : ends_(ends) { explicit FlatAllocation(const TypeMap<IntT, T...>& ends) : ends_(ends) {
// The arrays start just after FlatAllocation, so adjust the ends. // The arrays start just after FlatAllocation, so adjust the ends.
Fold({(ends_.template Get<T>() += Fold({(ends_.template Get<T>() +=
RoundUpTo<kMaxAlign>(sizeof(FlatAllocation)))...}); RoundUpTo<kMaxAlign>(sizeof(FlatAllocation)))...});
@ -844,7 +856,7 @@ std::string EnumValueToPascalCase(const std::string& input) {
// Class to remove an enum prefix from enum values. // Class to remove an enum prefix from enum values.
class PrefixRemover { class PrefixRemover {
public: public:
PrefixRemover(absl::string_view prefix) { explicit PrefixRemover(absl::string_view prefix) {
// Strip underscores and lower-case the prefix. // Strip underscores and lower-case the prefix.
for (char character : prefix) { for (char character : prefix) {
if (character != '_') { if (character != '_') {
@ -1497,7 +1509,7 @@ DescriptorPool::Tables::Tables() {
DescriptorPool::Tables::~Tables() { ABSL_DCHECK(checkpoints_.empty()); } DescriptorPool::Tables::~Tables() { ABSL_DCHECK(checkpoints_.empty()); }
FileDescriptorTables::FileDescriptorTables() {} FileDescriptorTables::FileDescriptorTables() = default;
FileDescriptorTables::~FileDescriptorTables() { FileDescriptorTables::~FileDescriptorTables() {
delete fields_by_lowercase_name_.load(std::memory_order_acquire); delete fields_by_lowercase_name_.load(std::memory_order_acquire);
@ -1922,7 +1934,7 @@ const SourceCodeInfo_Location* FileDescriptorTables::GetSourceLocation(
// DescriptorPool // DescriptorPool
DescriptorPool::ErrorCollector::~ErrorCollector() {} DescriptorPool::ErrorCollector::~ErrorCollector() = default;
absl::string_view DescriptorPool::ErrorCollector::ErrorLocationName( absl::string_view DescriptorPool::ErrorCollector::ErrorLocationName(
ErrorLocation location) { ErrorLocation location) {
@ -2322,8 +2334,9 @@ void DescriptorPool::FindAllExtensions(
// ------------------------------------------------------------------- // -------------------------------------------------------------------
const FieldDescriptor* Descriptor::FindFieldByNumber(int key) const { const FieldDescriptor* Descriptor::FindFieldByNumber(int number) const {
const FieldDescriptor* result = file()->tables_->FindFieldByNumber(this, key); const FieldDescriptor* result =
file()->tables_->FindFieldByNumber(this, number);
if (result == nullptr || result->is_extension()) { if (result == nullptr || result->is_extension()) {
return nullptr; return nullptr;
} else { } else {
@ -2332,9 +2345,9 @@ const FieldDescriptor* Descriptor::FindFieldByNumber(int key) const {
} }
const FieldDescriptor* Descriptor::FindFieldByLowercaseName( const FieldDescriptor* Descriptor::FindFieldByLowercaseName(
absl::string_view key) const { absl::string_view lowercase_name) const {
const FieldDescriptor* result = const FieldDescriptor* result =
file()->tables_->FindFieldByLowercaseName(this, key); file()->tables_->FindFieldByLowercaseName(this, lowercase_name);
if (result == nullptr || result->is_extension()) { if (result == nullptr || result->is_extension()) {
return nullptr; return nullptr;
} else { } else {
@ -2343,9 +2356,9 @@ const FieldDescriptor* Descriptor::FindFieldByLowercaseName(
} }
const FieldDescriptor* Descriptor::FindFieldByCamelcaseName( const FieldDescriptor* Descriptor::FindFieldByCamelcaseName(
absl::string_view key) const { absl::string_view camelcase_name) const {
const FieldDescriptor* result = const FieldDescriptor* result =
file()->tables_->FindFieldByCamelcaseName(this, key); file()->tables_->FindFieldByCamelcaseName(this, camelcase_name);
if (result == nullptr || result->is_extension()) { if (result == nullptr || result->is_extension()) {
return nullptr; return nullptr;
} else { } else {
@ -2354,28 +2367,28 @@ const FieldDescriptor* Descriptor::FindFieldByCamelcaseName(
} }
const FieldDescriptor* Descriptor::FindFieldByName( const FieldDescriptor* Descriptor::FindFieldByName(
absl::string_view key) const { absl::string_view name) const {
const FieldDescriptor* field = const FieldDescriptor* field =
file()->tables_->FindNestedSymbol(this, key).field_descriptor(); file()->tables_->FindNestedSymbol(this, name).field_descriptor();
return field != nullptr && !field->is_extension() ? field : nullptr; return field != nullptr && !field->is_extension() ? field : nullptr;
} }
const OneofDescriptor* Descriptor::FindOneofByName( const OneofDescriptor* Descriptor::FindOneofByName(
absl::string_view key) const { absl::string_view name) const {
return file()->tables_->FindNestedSymbol(this, key).oneof_descriptor(); return file()->tables_->FindNestedSymbol(this, name).oneof_descriptor();
} }
const FieldDescriptor* Descriptor::FindExtensionByName( const FieldDescriptor* Descriptor::FindExtensionByName(
absl::string_view key) const { absl::string_view name) const {
const FieldDescriptor* field = const FieldDescriptor* field =
file()->tables_->FindNestedSymbol(this, key).field_descriptor(); file()->tables_->FindNestedSymbol(this, name).field_descriptor();
return field != nullptr && field->is_extension() ? field : nullptr; return field != nullptr && field->is_extension() ? field : nullptr;
} }
const FieldDescriptor* Descriptor::FindExtensionByLowercaseName( const FieldDescriptor* Descriptor::FindExtensionByLowercaseName(
absl::string_view key) const { absl::string_view name) const {
const FieldDescriptor* result = const FieldDescriptor* result =
file()->tables_->FindFieldByLowercaseName(this, key); file()->tables_->FindFieldByLowercaseName(this, name);
if (result == nullptr || !result->is_extension()) { if (result == nullptr || !result->is_extension()) {
return nullptr; return nullptr;
} else { } else {
@ -2384,9 +2397,9 @@ const FieldDescriptor* Descriptor::FindExtensionByLowercaseName(
} }
const FieldDescriptor* Descriptor::FindExtensionByCamelcaseName( const FieldDescriptor* Descriptor::FindExtensionByCamelcaseName(
absl::string_view key) const { absl::string_view name) const {
const FieldDescriptor* result = const FieldDescriptor* result =
file()->tables_->FindFieldByCamelcaseName(this, key); file()->tables_->FindFieldByCamelcaseName(this, name);
if (result == nullptr || !result->is_extension()) { if (result == nullptr || !result->is_extension()) {
return nullptr; return nullptr;
} else { } else {
@ -2395,18 +2408,18 @@ const FieldDescriptor* Descriptor::FindExtensionByCamelcaseName(
} }
const Descriptor* Descriptor::FindNestedTypeByName( const Descriptor* Descriptor::FindNestedTypeByName(
absl::string_view key) const { absl::string_view name) const {
return file()->tables_->FindNestedSymbol(this, key).descriptor(); return file()->tables_->FindNestedSymbol(this, name).descriptor();
} }
const EnumDescriptor* Descriptor::FindEnumTypeByName( const EnumDescriptor* Descriptor::FindEnumTypeByName(
absl::string_view key) const { absl::string_view name) const {
return file()->tables_->FindNestedSymbol(this, key).enum_descriptor(); return file()->tables_->FindNestedSymbol(this, name).enum_descriptor();
} }
const EnumValueDescriptor* Descriptor::FindEnumValueByName( const EnumValueDescriptor* Descriptor::FindEnumValueByName(
absl::string_view key) const { absl::string_view name) const {
return file()->tables_->FindNestedSymbol(this, key).enum_value_descriptor(); return file()->tables_->FindNestedSymbol(this, name).enum_value_descriptor();
} }
const FieldDescriptor* Descriptor::map_key() const { const FieldDescriptor* Descriptor::map_key() const {
@ -2422,54 +2435,54 @@ const FieldDescriptor* Descriptor::map_value() const {
} }
const EnumValueDescriptor* EnumDescriptor::FindValueByName( const EnumValueDescriptor* EnumDescriptor::FindValueByName(
absl::string_view key) const { absl::string_view name) const {
return file()->tables_->FindNestedSymbol(this, key).enum_value_descriptor(); return file()->tables_->FindNestedSymbol(this, name).enum_value_descriptor();
} }
const EnumValueDescriptor* EnumDescriptor::FindValueByNumber(int key) const { const EnumValueDescriptor* EnumDescriptor::FindValueByNumber(int number) const {
return file()->tables_->FindEnumValueByNumber(this, key); return file()->tables_->FindEnumValueByNumber(this, number);
} }
const EnumValueDescriptor* EnumDescriptor::FindValueByNumberCreatingIfUnknown( const EnumValueDescriptor* EnumDescriptor::FindValueByNumberCreatingIfUnknown(
int key) const { int number) const {
return file()->tables_->FindEnumValueByNumberCreatingIfUnknown(this, key); return file()->tables_->FindEnumValueByNumberCreatingIfUnknown(this, number);
} }
const MethodDescriptor* ServiceDescriptor::FindMethodByName( const MethodDescriptor* ServiceDescriptor::FindMethodByName(
absl::string_view key) const { absl::string_view name) const {
return file()->tables_->FindNestedSymbol(this, key).method_descriptor(); return file()->tables_->FindNestedSymbol(this, name).method_descriptor();
} }
const Descriptor* FileDescriptor::FindMessageTypeByName( const Descriptor* FileDescriptor::FindMessageTypeByName(
absl::string_view key) const { absl::string_view name) const {
return tables_->FindNestedSymbol(this, key).descriptor(); return tables_->FindNestedSymbol(this, name).descriptor();
} }
const EnumDescriptor* FileDescriptor::FindEnumTypeByName( const EnumDescriptor* FileDescriptor::FindEnumTypeByName(
absl::string_view key) const { absl::string_view name) const {
return tables_->FindNestedSymbol(this, key).enum_descriptor(); return tables_->FindNestedSymbol(this, name).enum_descriptor();
} }
const EnumValueDescriptor* FileDescriptor::FindEnumValueByName( const EnumValueDescriptor* FileDescriptor::FindEnumValueByName(
absl::string_view key) const { absl::string_view name) const {
return tables_->FindNestedSymbol(this, key).enum_value_descriptor(); return tables_->FindNestedSymbol(this, name).enum_value_descriptor();
} }
const ServiceDescriptor* FileDescriptor::FindServiceByName( const ServiceDescriptor* FileDescriptor::FindServiceByName(
absl::string_view key) const { absl::string_view name) const {
return tables_->FindNestedSymbol(this, key).service_descriptor(); return tables_->FindNestedSymbol(this, name).service_descriptor();
} }
const FieldDescriptor* FileDescriptor::FindExtensionByName( const FieldDescriptor* FileDescriptor::FindExtensionByName(
absl::string_view key) const { absl::string_view name) const {
const FieldDescriptor* field = const FieldDescriptor* field =
tables_->FindNestedSymbol(this, key).field_descriptor(); tables_->FindNestedSymbol(this, name).field_descriptor();
return field != nullptr && field->is_extension() ? field : nullptr; return field != nullptr && field->is_extension() ? field : nullptr;
} }
const FieldDescriptor* FileDescriptor::FindExtensionByLowercaseName( const FieldDescriptor* FileDescriptor::FindExtensionByLowercaseName(
absl::string_view key) const { absl::string_view name) const {
const FieldDescriptor* result = tables_->FindFieldByLowercaseName(this, key); const FieldDescriptor* result = tables_->FindFieldByLowercaseName(this, name);
if (result == nullptr || !result->is_extension()) { if (result == nullptr || !result->is_extension()) {
return nullptr; return nullptr;
} else { } else {
@ -2478,8 +2491,8 @@ const FieldDescriptor* FileDescriptor::FindExtensionByLowercaseName(
} }
const FieldDescriptor* FileDescriptor::FindExtensionByCamelcaseName( const FieldDescriptor* FileDescriptor::FindExtensionByCamelcaseName(
absl::string_view key) const { absl::string_view name) const {
const FieldDescriptor* result = tables_->FindFieldByCamelcaseName(this, key); const FieldDescriptor* result = tables_->FindFieldByCamelcaseName(this, name);
if (result == nullptr || !result->is_extension()) { if (result == nullptr || !result->is_extension()) {
return nullptr; return nullptr;
} else { } else {
@ -2696,14 +2709,14 @@ std::string FieldDescriptor::DefaultValueAsString(
// optimization. These constructors are never called, so we define them // optimization. These constructors are never called, so we define them
// out of line to make sure the debug info is emitted somewhere. // out of line to make sure the debug info is emitted somewhere.
Descriptor::Descriptor() {} Descriptor::Descriptor() = default;
FieldDescriptor::FieldDescriptor() {} FieldDescriptor::FieldDescriptor() {}
OneofDescriptor::OneofDescriptor() {} OneofDescriptor::OneofDescriptor() = default;
EnumDescriptor::EnumDescriptor() {} EnumDescriptor::EnumDescriptor() = default;
EnumValueDescriptor::EnumValueDescriptor() {} EnumValueDescriptor::EnumValueDescriptor() = default;
ServiceDescriptor::ServiceDescriptor() {} ServiceDescriptor::ServiceDescriptor() = default;
MethodDescriptor::MethodDescriptor() {} MethodDescriptor::MethodDescriptor() = default;
FileDescriptor::FileDescriptor() {} FileDescriptor::FileDescriptor() = default;
// CopyTo methods ==================================================== // CopyTo methods ====================================================
@ -2773,7 +2786,7 @@ void FileDescriptor::CopyJsonNameTo(FileDescriptorProto* proto) const {
void FileDescriptor::CopySourceCodeInfoTo(FileDescriptorProto* proto) const { void FileDescriptor::CopySourceCodeInfoTo(FileDescriptorProto* proto) const {
if (source_code_info_ && if (source_code_info_ &&
source_code_info_ != &SourceCodeInfo::default_instance()) { source_code_info_ != &SourceCodeInfo::default_instance()) {
proto->mutable_source_code_info()->CopyFrom(*source_code_info_); *proto->mutable_source_code_info() = *source_code_info_;
} }
} }
@ -2808,7 +2821,7 @@ void Descriptor::CopyTo(DescriptorProto* proto) const {
} }
if (&options() != &MessageOptions::default_instance()) { if (&options() != &MessageOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options()); *proto->mutable_options() = options();
} }
RestoreFeaturesToOptions(proto_features_, proto); RestoreFeaturesToOptions(proto_features_, proto);
@ -2896,7 +2909,7 @@ void FieldDescriptor::CopyTo(FieldDescriptorProto* proto) const {
} }
if (&options() != &FieldOptions::default_instance()) { if (&options() != &FieldOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options()); *proto->mutable_options() = options();
} }
RestoreFeaturesToOptions(proto_features_, proto); RestoreFeaturesToOptions(proto_features_, proto);
@ -2909,7 +2922,7 @@ void FieldDescriptor::CopyJsonNameTo(FieldDescriptorProto* proto) const {
void OneofDescriptor::CopyTo(OneofDescriptorProto* proto) const { void OneofDescriptor::CopyTo(OneofDescriptorProto* proto) const {
proto->set_name(name()); proto->set_name(name());
if (&options() != &OneofOptions::default_instance()) { if (&options() != &OneofOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options()); *proto->mutable_options() = options();
} }
RestoreFeaturesToOptions(proto_features_, proto); RestoreFeaturesToOptions(proto_features_, proto);
} }
@ -2930,7 +2943,7 @@ void EnumDescriptor::CopyTo(EnumDescriptorProto* proto) const {
} }
if (&options() != &EnumOptions::default_instance()) { if (&options() != &EnumOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options()); *proto->mutable_options() = options();
} }
RestoreFeaturesToOptions(proto_features_, proto); RestoreFeaturesToOptions(proto_features_, proto);
} }
@ -2940,7 +2953,7 @@ void EnumValueDescriptor::CopyTo(EnumValueDescriptorProto* proto) const {
proto->set_number(number()); proto->set_number(number());
if (&options() != &EnumValueOptions::default_instance()) { if (&options() != &EnumValueOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options()); *proto->mutable_options() = options();
} }
RestoreFeaturesToOptions(proto_features_, proto); RestoreFeaturesToOptions(proto_features_, proto);
} }
@ -2953,7 +2966,7 @@ void ServiceDescriptor::CopyTo(ServiceDescriptorProto* proto) const {
} }
if (&options() != &ServiceOptions::default_instance()) { if (&options() != &ServiceOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options()); *proto->mutable_options() = options();
} }
RestoreFeaturesToOptions(proto_features_, proto); RestoreFeaturesToOptions(proto_features_, proto);
} }
@ -2972,7 +2985,7 @@ void MethodDescriptor::CopyTo(MethodDescriptorProto* proto) const {
proto->mutable_output_type()->append(output_type()->full_name()); proto->mutable_output_type()->append(output_type()->full_name());
if (&options() != &MethodOptions::default_instance()) { if (&options() != &MethodOptions::default_instance()) {
proto->mutable_options()->CopyFrom(options()); *proto->mutable_options() = options();
} }
if (client_streaming_) { if (client_streaming_) {
@ -4608,7 +4621,7 @@ DescriptorBuilder::DescriptorBuilder(
std::true_type{}); std::true_type{});
} }
DescriptorBuilder::~DescriptorBuilder() {} DescriptorBuilder::~DescriptorBuilder() = default;
PROTOBUF_NOINLINE void DescriptorBuilder::AddError( PROTOBUF_NOINLINE void DescriptorBuilder::AddError(
const std::string& element_name, const Message& descriptor, const std::string& element_name, const Message& descriptor,
@ -5109,7 +5122,7 @@ bool DescriptorBuilder::AddSymbol(const std::string& full_name,
void DescriptorBuilder::AddPackage(const std::string& name, void DescriptorBuilder::AddPackage(const std::string& name,
const Message& proto, FileDescriptor* file) { const Message& proto, FileDescriptor* file) {
if (name.find('\0') != std::string::npos) { if (absl::StrContains(name, '\0')) {
AddError(name, proto, DescriptorPool::ErrorCollector::NAME, [&] { AddError(name, proto, DescriptorPool::ErrorCollector::NAME, [&] {
return absl::StrCat("\"", name, "\" contains null character."); return absl::StrCat("\"", name, "\" contains null character.");
}); });
@ -5691,7 +5704,7 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl(
SourceCodeInfo* info = nullptr; SourceCodeInfo* info = nullptr;
if (proto.has_source_code_info()) { if (proto.has_source_code_info()) {
info = alloc.AllocateArray<SourceCodeInfo>(1); info = alloc.AllocateArray<SourceCodeInfo>(1);
info->CopyFrom(proto.source_code_info()); *info = proto.source_code_info();
result->source_code_info_ = info; result->source_code_info_ = info;
} else { } else {
result->source_code_info_ = &SourceCodeInfo::default_instance(); result->source_code_info_ = &SourceCodeInfo::default_instance();
@ -5717,7 +5730,7 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl(
} }
result->pool_ = pool_; result->pool_ = pool_;
if (result->name().find('\0') != std::string::npos) { if (absl::StrContains(result->name(), '\0')) {
AddError(result->name(), proto, DescriptorPool::ErrorCollector::NAME, [&] { AddError(result->name(), proto, DescriptorPool::ErrorCollector::NAME, [&] {
return absl::StrCat("\"", result->name(), "\" contains null character."); return absl::StrCat("\"", result->name(), "\" contains null character.");
}); });
@ -6362,7 +6375,7 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
std::strtol(proto.default_value().c_str(), &end_pos, 0); std::strtol(proto.default_value().c_str(), &end_pos, 0);
break; break;
case FieldDescriptor::CPPTYPE_INT64: case FieldDescriptor::CPPTYPE_INT64:
static_assert(sizeof(int64_t) == sizeof(long long), static_assert(sizeof(int64_t) == sizeof(long long), // NOLINT
"sizeof int64_t is not sizeof long long"); "sizeof int64_t is not sizeof long long");
result->default_value_int64_t_ = result->default_value_int64_t_ =
std::strtoll(proto.default_value().c_str(), &end_pos, 0); std::strtoll(proto.default_value().c_str(), &end_pos, 0);
@ -6372,8 +6385,9 @@ void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto,
std::strtoul(proto.default_value().c_str(), &end_pos, 0); std::strtoul(proto.default_value().c_str(), &end_pos, 0);
break; break;
case FieldDescriptor::CPPTYPE_UINT64: case FieldDescriptor::CPPTYPE_UINT64:
static_assert(sizeof(uint64_t) == sizeof(unsigned long long), static_assert(
"sizeof uint64_t is not sizeof unsigned long long"); sizeof(uint64_t) == sizeof(unsigned long long), // NOLINT
"sizeof uint64_t is not sizeof unsigned long long");
result->default_value_uint64_t_ = result->default_value_uint64_t_ =
std::strtoull(proto.default_value().c_str(), &end_pos, 0); std::strtoull(proto.default_value().c_str(), &end_pos, 0);
break; break;
@ -8378,7 +8392,7 @@ DescriptorBuilder::OptionInterpreter::OptionInterpreter(
ABSL_CHECK(builder_); ABSL_CHECK(builder_);
} }
DescriptorBuilder::OptionInterpreter::~OptionInterpreter() {} DescriptorBuilder::OptionInterpreter::~OptionInterpreter() = default;
bool DescriptorBuilder::OptionInterpreter::InterpretOptionExtensions( bool DescriptorBuilder::OptionInterpreter::InterpretOptionExtensions(
OptionsToInterpret* options_to_interpret) { OptionsToInterpret* options_to_interpret) {
@ -8688,7 +8702,7 @@ bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption(
<< (*iter)->type(); << (*iter)->type();
return false; return false;
} }
unknown_fields.reset(parent_unknown_fields.release()); unknown_fields = std::move(parent_unknown_fields);
} }
// Now merge the UnknownFieldSet corresponding to the top-level message into // Now merge the UnknownFieldSet corresponding to the top-level message into

@ -13,8 +13,14 @@
#include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.h"
#include <limits.h>
#include <atomic>
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <deque>
#include <functional> #include <functional>
#include <iostream>
#include <limits> #include <limits>
#include <memory> #include <memory>
#include <string> #include <string>
@ -22,15 +28,15 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "google/protobuf/stubs/common.h"
#include "google/protobuf/any.pb.h" #include "google/protobuf/any.pb.h"
#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/descriptor.pb.h"
#include <gmock/gmock.h> #include <gmock/gmock.h>
#include "google/protobuf/testing/googletest.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "absl/base/log_severity.h"
#include "absl/container/btree_set.h" #include "absl/container/btree_set.h"
#include "absl/container/flat_hash_set.h" #include "absl/container/flat_hash_set.h"
#include "absl/flags/flag.h"
#include "absl/functional/any_invocable.h" #include "absl/functional/any_invocable.h"
#include "absl/log/absl_check.h" #include "absl/log/absl_check.h"
#include "absl/log/absl_log.h" #include "absl/log/absl_log.h"
@ -38,9 +44,12 @@
#include "absl/log/scoped_mock_log.h" #include "absl/log/scoped_mock_log.h"
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h" #include "absl/strings/str_format.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/strings/substitute.h" #include "absl/strings/substitute.h"
#include "absl/synchronization/notification.h" #include "absl/synchronization/notification.h"
#include "google/protobuf/compiler/importer.h" #include "google/protobuf/compiler/importer.h"
@ -50,6 +59,7 @@
#include "google/protobuf/descriptor_legacy.h" #include "google/protobuf/descriptor_legacy.h"
#include "google/protobuf/dynamic_message.h" #include "google/protobuf/dynamic_message.h"
#include "google/protobuf/feature_resolver.h" #include "google/protobuf/feature_resolver.h"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/tokenizer.h" #include "google/protobuf/io/tokenizer.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h" #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/test_textproto.h" #include "google/protobuf/test_textproto.h"
@ -220,8 +230,8 @@ void AddEmptyEnum(FileDescriptorProto* file, absl::string_view name) {
class MockErrorCollector : public DescriptorPool::ErrorCollector { class MockErrorCollector : public DescriptorPool::ErrorCollector {
public: public:
MockErrorCollector() {} MockErrorCollector() = default;
~MockErrorCollector() override {} ~MockErrorCollector() override = default;
std::string text_; std::string text_;
std::string warning_text_; std::string warning_text_;
@ -593,7 +603,7 @@ TEST_F(FileDescriptorTest, DebugStringRoundTrip) {
ASSERT_GE(debug_strings.size(), 3); ASSERT_GE(debug_strings.size(), 3);
DescriptorPool pool; DescriptorPool pool;
for (int i = 0; i < debug_strings.size(); ++i) { for (size_t i = 0; i < debug_strings.size(); ++i) {
const std::string& name = debug_strings[i].first; const std::string& name = debug_strings[i].first;
const std::string& content = debug_strings[i].second; const std::string& content = debug_strings[i].second;
io::ArrayInputStream input_stream(content.data(), content.size()); io::ArrayInputStream input_stream(content.data(), content.size());
@ -2605,7 +2615,7 @@ class MiscTest : public testing::Test {
} }
// Build the descriptors and get the pointers. // Build the descriptors and get the pointers.
pool_.reset(new DescriptorPool()); pool_ = std::make_unique<DescriptorPool>();
const FileDescriptor* file = pool_->BuildFile(file_proto); const FileDescriptor* file = pool_->BuildFile(file_proto);
if (file != nullptr && file->message_type_count() == 1 && if (file != nullptr && file->message_type_count() == 1 &&
@ -2995,10 +3005,10 @@ class AllowUnknownDependenciesTest
switch (mode()) { switch (mode()) {
case NO_DATABASE: case NO_DATABASE:
pool_.reset(new DescriptorPool); pool_ = std::make_unique<DescriptorPool>();
break; break;
case FALLBACK_DATABASE: case FALLBACK_DATABASE:
pool_.reset(new DescriptorPool(&db_)); pool_ = std::make_unique<DescriptorPool>(&db_);
break; break;
} }
@ -5720,8 +5730,8 @@ TEST_F(ValidationErrorTest, Int32OptionValueOutOfPositiveRange) {
" positive_int_value: 0x80000000 } " " positive_int_value: 0x80000000 } "
"}", "}",
"foo.proto: foo.proto: OPTION_VALUE: Value out of range, -2147483648 to 2147483647, " "foo.proto: foo.proto: OPTION_VALUE: Value out of range, -2147483648 to "
"for int32 option \"foo\".\n"); "2147483647, for int32 option \"foo\".\n");
} }
TEST_F(ValidationErrorTest, Int32OptionValueOutOfNegativeRange) { TEST_F(ValidationErrorTest, Int32OptionValueOutOfNegativeRange) {
@ -5737,8 +5747,8 @@ TEST_F(ValidationErrorTest, Int32OptionValueOutOfNegativeRange) {
" negative_int_value: -0x80000001 } " " negative_int_value: -0x80000001 } "
"}", "}",
"foo.proto: foo.proto: OPTION_VALUE: Value out of range, -2147483648 to 2147483647, " "foo.proto: foo.proto: OPTION_VALUE: Value out of range, -2147483648 to "
"for int32 option \"foo\".\n"); "2147483647, for int32 option \"foo\".\n");
} }
TEST_F(ValidationErrorTest, Int32OptionValueIsNotPositiveInt) { TEST_F(ValidationErrorTest, Int32OptionValueIsNotPositiveInt) {
@ -5753,8 +5763,8 @@ TEST_F(ValidationErrorTest, Int32OptionValueIsNotPositiveInt) {
" is_extension: true } " " is_extension: true } "
" string_value: \"5\" } }", " string_value: \"5\" } }",
"foo.proto: foo.proto: OPTION_VALUE: Value must be integer, from -2147483648 to 2147483647, " "foo.proto: foo.proto: OPTION_VALUE: Value must be integer, from "
"for int32 option \"foo\".\n"); "-2147483648 to 2147483647, for int32 option \"foo\".\n");
} }
TEST_F(ValidationErrorTest, Int64OptionValueOutOfRange) { TEST_F(ValidationErrorTest, Int64OptionValueOutOfRange) {
@ -5771,8 +5781,9 @@ TEST_F(ValidationErrorTest, Int64OptionValueOutOfRange) {
"} " "} "
"}", "}",
"foo.proto: foo.proto: OPTION_VALUE: Value out of range, -9223372036854775808 to 9223372036854775807, " "foo.proto: foo.proto: OPTION_VALUE: Value out of range, "
"for int64 option \"foo\".\n"); "-9223372036854775808 to 9223372036854775807, for int64 option "
"\"foo\".\n");
} }
TEST_F(ValidationErrorTest, Int64OptionValueIsNotPositiveInt) { TEST_F(ValidationErrorTest, Int64OptionValueIsNotPositiveInt) {
@ -5787,8 +5798,9 @@ TEST_F(ValidationErrorTest, Int64OptionValueIsNotPositiveInt) {
" is_extension: true } " " is_extension: true } "
" identifier_value: \"5\" } }", " identifier_value: \"5\" } }",
"foo.proto: foo.proto: OPTION_VALUE: Value must be integer, from -9223372036854775808 to 9223372036854775807, " "foo.proto: foo.proto: OPTION_VALUE: Value must be integer, from "
"for int64 option \"foo\".\n"); "-9223372036854775808 to 9223372036854775807, for int64 option "
"\"foo\".\n");
} }
TEST_F(ValidationErrorTest, UInt32OptionValueOutOfRange) { TEST_F(ValidationErrorTest, UInt32OptionValueOutOfRange) {
@ -5803,8 +5815,8 @@ TEST_F(ValidationErrorTest, UInt32OptionValueOutOfRange) {
" is_extension: true } " " is_extension: true } "
" positive_int_value: 0x100000000 } }", " positive_int_value: 0x100000000 } }",
"foo.proto: foo.proto: OPTION_VALUE: Value out of range, 0 to 4294967295, " "foo.proto: foo.proto: OPTION_VALUE: Value out of range, 0 to "
"for uint32 option \"foo\".\n"); "4294967295, for uint32 option \"foo\".\n");
} }
TEST_F(ValidationErrorTest, UInt32OptionValueIsNotPositiveInt) { TEST_F(ValidationErrorTest, UInt32OptionValueIsNotPositiveInt) {
@ -5819,8 +5831,8 @@ TEST_F(ValidationErrorTest, UInt32OptionValueIsNotPositiveInt) {
" is_extension: true } " " is_extension: true } "
" double_value: -5.6 } }", " double_value: -5.6 } }",
"foo.proto: foo.proto: OPTION_VALUE: Value must be integer, from 0 to 4294967295, " "foo.proto: foo.proto: OPTION_VALUE: Value must be integer, from 0 to "
"for uint32 option \"foo\".\n"); "4294967295, for uint32 option \"foo\".\n");
} }
TEST_F(ValidationErrorTest, UInt64OptionValueIsNotPositiveInt) { TEST_F(ValidationErrorTest, UInt64OptionValueIsNotPositiveInt) {
@ -5835,8 +5847,8 @@ TEST_F(ValidationErrorTest, UInt64OptionValueIsNotPositiveInt) {
" is_extension: true } " " is_extension: true } "
" negative_int_value: -5 } }", " negative_int_value: -5 } }",
"foo.proto: foo.proto: OPTION_VALUE: Value must be integer, from 0 to 18446744073709551615, " "foo.proto: foo.proto: OPTION_VALUE: Value must be integer, from 0 to "
"for uint64 option \"foo\".\n"); "18446744073709551615, for uint64 option \"foo\".\n");
} }
TEST_F(ValidationErrorTest, FloatOptionValueIsNotNumber) { TEST_F(ValidationErrorTest, FloatOptionValueIsNotNumber) {
@ -5960,8 +5972,7 @@ TEST_F(ValidationErrorTest, StringOptionValueIsNotString) {
" identifier_value: \"MOOO\" } }", " identifier_value: \"MOOO\" } }",
"foo.proto: foo.proto: OPTION_VALUE: Value must be quoted string " "foo.proto: foo.proto: OPTION_VALUE: Value must be quoted string "
"for " "for string option \"foo\".\n");
"string option \"foo\".\n");
} }
TEST_F(ValidationErrorTest, JsonNameOptionOnExtensions) { TEST_F(ValidationErrorTest, JsonNameOptionOnExtensions) {
@ -8422,11 +8433,11 @@ TEST_F(FeaturesTest, MapFieldFeaturesOverride) {
option features.(pb.test).int_file_feature = 99; option features.(pb.test).int_file_feature = 99;
option features.(pb.test).int_multiple_feature = 1; option features.(pb.test).int_multiple_feature = 1;
message Foo { message Foo {
option features.(pb.test).int_message_feature = 87; option features.(pb.test).int_message_feature = 87;
option features.(pb.test).int_multiple_feature = 2; option features.(pb.test).int_multiple_feature = 2;
map<string, string> map_field = 1 [ map<string, string> map_field = 1 [
features.(pb.test).int_field_feature = 100, features.(pb.test).int_field_feature = 100,
features.(pb.test).int_multiple_feature = 3 features.(pb.test).int_multiple_feature = 3
@ -11195,7 +11206,7 @@ static void AddToDatabase(SimpleDescriptorDatabase* database,
class DatabaseBackedPoolTest : public testing::Test { class DatabaseBackedPoolTest : public testing::Test {
protected: protected:
DatabaseBackedPoolTest() {} DatabaseBackedPoolTest() = default;
SimpleDescriptorDatabase database_; SimpleDescriptorDatabase database_;
@ -11226,8 +11237,8 @@ class DatabaseBackedPoolTest : public testing::Test {
// need an actual mock DescriptorDatabase to test errors. // need an actual mock DescriptorDatabase to test errors.
class ErrorDescriptorDatabase : public DescriptorDatabase { class ErrorDescriptorDatabase : public DescriptorDatabase {
public: public:
ErrorDescriptorDatabase() {} ErrorDescriptorDatabase() = default;
~ErrorDescriptorDatabase() override {} ~ErrorDescriptorDatabase() override = default;
// implements DescriptorDatabase --------------------------------- // implements DescriptorDatabase ---------------------------------
bool FindFileByName(const std::string& filename, bool FindFileByName(const std::string& filename,
@ -11262,11 +11273,11 @@ class DatabaseBackedPoolTest : public testing::Test {
// called and forwards to some other DescriptorDatabase. // called and forwards to some other DescriptorDatabase.
class CallCountingDatabase : public DescriptorDatabase { class CallCountingDatabase : public DescriptorDatabase {
public: public:
CallCountingDatabase(DescriptorDatabase* wrapped_db) explicit CallCountingDatabase(DescriptorDatabase* wrapped_db)
: wrapped_db_(wrapped_db) { : wrapped_db_(wrapped_db) {
Clear(); Clear();
} }
~CallCountingDatabase() override {} ~CallCountingDatabase() override = default;
DescriptorDatabase* wrapped_db_; DescriptorDatabase* wrapped_db_;
@ -11299,9 +11310,9 @@ class DatabaseBackedPoolTest : public testing::Test {
// DescriptorPool to reload foo.proto if it is already loaded. // DescriptorPool to reload foo.proto if it is already loaded.
class FalsePositiveDatabase : public DescriptorDatabase { class FalsePositiveDatabase : public DescriptorDatabase {
public: public:
FalsePositiveDatabase(DescriptorDatabase* wrapped_db) explicit FalsePositiveDatabase(DescriptorDatabase* wrapped_db)
: wrapped_db_(wrapped_db) {} : wrapped_db_(wrapped_db) {}
~FalsePositiveDatabase() override {} ~FalsePositiveDatabase() override = default;
DescriptorDatabase* wrapped_db_; DescriptorDatabase* wrapped_db_;
@ -11585,8 +11596,8 @@ TEST_F(DatabaseBackedPoolTest, DoesntReloadFilesUncesessarily) {
// build a descriptor for MessageN can require O(2^N) time. // build a descriptor for MessageN can require O(2^N) time.
class ExponentialErrorDatabase : public DescriptorDatabase { class ExponentialErrorDatabase : public DescriptorDatabase {
public: public:
ExponentialErrorDatabase() {} ExponentialErrorDatabase() = default;
~ExponentialErrorDatabase() override {} ~ExponentialErrorDatabase() override = default;
// implements DescriptorDatabase --------------------------------- // implements DescriptorDatabase ---------------------------------
bool FindFileByName(const std::string& filename, bool FindFileByName(const std::string& filename,
@ -11687,7 +11698,7 @@ TEST_F(DatabaseBackedPoolTest, DoesntFallbackOnWrongType) {
class AbortingErrorCollector : public DescriptorPool::ErrorCollector { class AbortingErrorCollector : public DescriptorPool::ErrorCollector {
public: public:
AbortingErrorCollector() {} AbortingErrorCollector() = default;
AbortingErrorCollector(const AbortingErrorCollector&) = delete; AbortingErrorCollector(const AbortingErrorCollector&) = delete;
AbortingErrorCollector& operator=(const AbortingErrorCollector&) = delete; AbortingErrorCollector& operator=(const AbortingErrorCollector&) = delete;

Loading…
Cancel
Save