Skip to content

Commit

Permalink
internal changes
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 503201591
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jan 19, 2023
1 parent ecac6cd commit 58f6216
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/google/protobuf/compiler/cpp/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1100,10 +1100,11 @@ void FileGenerator::GenerateReflectionInitializationCode(io::Printer* p) {
}
)cc");

// For descriptor.proto we want to avoid doing any dynamic initialization,
// because in some situations that would otherwise pull in a lot of
// unnecessary code that can't be stripped by --gc-sections. Descriptor
// initialization will still be performed lazily when it's needed.
// For proto files included in the full runtime (i.e. descriptor.proto
// and extension_declarations.proto), we want to avoid doing any dynamic
// initialization, because in some situations that would otherwise pull
// in a lot of unnecessary code that can't be stripped by --gc-sections.
// Initialization will still be performed lazily when it's needed.
if (file_->name() == "net/proto2/proto/descriptor.proto") {
return;
}
Expand Down
14 changes: 12 additions & 2 deletions src/google/protobuf/descriptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <array>
#include <cstdlib>
#include <functional>
#include <iterator>
#include <limits>
#include <memory>
#include <sstream>
Expand All @@ -62,8 +63,11 @@
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/strings/substitute.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/optional.h"
#include "google/protobuf/any.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/descriptor_database.h"
Expand Down Expand Up @@ -1872,6 +1876,7 @@ void DescriptorPool::AddUnusedImportTrackFile(absl::string_view file_name,
unused_import_track_files_[file_name] = is_error;
}


void DescriptorPool::ClearUnusedImportTrackFiles() {
unused_import_track_files_.clear();
}
Expand Down Expand Up @@ -1912,7 +1917,11 @@ DescriptorPool* DescriptorPool::internal_generated_pool() {

const DescriptorPool* DescriptorPool::generated_pool() {
const DescriptorPool* pool = internal_generated_pool();
// Ensure that descriptor.proto has been registered in the generated pool.
// Ensure that descriptor.proto and extension_declarations.proto get
// registered in the generated pool. These protos are a special case
// because they are included in the full runtime. We have to avoid
// registering them pre-main, because we need to ensure that the linker
// --gc-sections step can strip out the full runtime if it is unused.
DescriptorProto::descriptor();
return pool;
}
Expand Down Expand Up @@ -6465,6 +6474,7 @@ void DescriptorBuilder::CrossLinkExtensionRange(
}
}


void DescriptorBuilder::CrossLinkField(FieldDescriptor* field,
const FieldDescriptorProto& proto) {
if (field->options_ == nullptr) {
Expand Down Expand Up @@ -7104,7 +7114,7 @@ void DescriptorBuilder::ValidateFieldOptions(
// determine whether the json_name option is set on the field. Here we
// compare it against the default calculated json_name value and consider
// the option set if they are different. This won't catch the case when
// an user explicitly sets json_name to the default value, but should be
// a user explicitly sets json_name to the default value, but should be
// good enough to catch common misuses.
if (field->is_extension() &&
(field->has_json_name() &&
Expand Down
7 changes: 7 additions & 0 deletions src/google/protobuf/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include "google/protobuf/stubs/logging.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/optional.h"
#include "google/protobuf/port.h"

// Must be included last.
Expand All @@ -83,6 +84,9 @@
namespace google {
namespace protobuf {

// Defined in extension_declaration.proto.
class ExtensionMetadata;

// Defined in this file.
class Descriptor;
class FieldDescriptor;
Expand Down Expand Up @@ -140,6 +144,7 @@ class Formatter;

namespace descriptor_unittest {
class DescriptorTest;
class ValidationErrorTest;
} // namespace descriptor_unittest

// Defined in printer.h
Expand Down Expand Up @@ -2056,6 +2061,7 @@ class PROTOBUF_EXPORT DescriptorPool {
friend class FileDescriptor;
friend class DescriptorBuilder;
friend class FileDescriptorTables;
friend class google::protobuf::descriptor_unittest::ValidationErrorTest;

// Return true if the given name is a sub-symbol of any non-package
// descriptor that already exists in the descriptor pool. (The full
Expand Down Expand Up @@ -2131,6 +2137,7 @@ class PROTOBUF_EXPORT DescriptorPool {
// Set of files to track for unused imports. The bool value when true means
// unused imports are treated as errors (and as warnings when false).
absl::flat_hash_map<std::string, bool> unused_import_track_files_;

};


Expand Down
3 changes: 3 additions & 0 deletions src/google/protobuf/descriptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/unittest.pb.h"
#include "google/protobuf/unittest_custom_options.pb.h"
#include "google/protobuf/stubs/common.h"
Expand Down Expand Up @@ -3933,6 +3934,7 @@ class ValidationErrorTest : public testing::Test {
return GOOGLE_CHECK_NOTNULL(pool_.BuildFile(file_proto));
}


// Parse file_text as a FileDescriptorProto in text format and add it
// to the DescriptorPool. Expect errors to be produced which match the
// given error text.
Expand Down Expand Up @@ -7078,6 +7080,7 @@ TEST_F(ValidationErrorTest, UnusedImportWithOtherError) {
}



TEST_F(ValidationErrorTest, PackageTooLong) {
BuildFileWithErrors(
"name: \"foo.proto\" "
Expand Down
4 changes: 4 additions & 0 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ static_assert(PROTOBUF_CPLUSPLUS_MIN(201402L), "Protobuf only supports C++14 and
// Owner: mkruskal@
#define PROTOBUF_FUTURE_REMOVE_CLEARED_API 1

// Used for descriptor proto extension declarations.
// Owner: shaod@, gberg@
#define PROTOBUF_FUTURE_DESCRIPTOR_EXTENSION_DECL 1

#else
#define PROTOBUF_FUTURE_FINAL
#endif
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/port_undef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
#undef PROTOBUF_FUTURE_MAP_PAIR_UPGRADE
#undef PROTOBUF_FUTURE_REMOVE_DEFAULT_FIELD_COMPARATOR
#undef PROTOBUF_FUTURE_REMOVE_CLEARED_API
#undef PROTOBUF_FUTURE_DESCRIPTOR_EXTENSION_DECL
#endif

#undef PROTOBUF_FUTURE_FINAL
Expand Down

0 comments on commit 58f6216

Please sign in to comment.