From fd5167d40eb5d2c2671ddaf28262c229735d93d4 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Thu, 22 Jun 2023 11:12:44 -0700 Subject: [PATCH] Internal changes PiperOrigin-RevId: 542613464 --- src/google/protobuf/compiler/cpp/file.cc | 32 +++++++++++++++++------- src/google/protobuf/compiler/cpp/file.h | 3 +++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/google/protobuf/compiler/cpp/file.cc b/src/google/protobuf/compiler/cpp/file.cc index 3d0e5fa76e..a7becf187a 100644 --- a/src/google/protobuf/compiler/cpp/file.cc +++ b/src/google/protobuf/compiler/cpp/file.cc @@ -107,8 +107,26 @@ void UnmuteWuninitialized(io::Printer* p) { #endif // __llvm__ )"); } + } // namespace +bool FileGenerator::ShouldSkipDependencyImports( + const FileDescriptor* dep) const { + // Do not import weak deps. + if (!options_.opensource_runtime && IsDepWeak(dep)) { + return true; + } + + // Skip feature imports, which are a visible (but non-functional) deviation + // between editions and legacy syntax. + if (options_.strip_nonfunctional_codegen && + dep->name() == "third_party/protobuf/cpp_features.proto") { + return true; + } + + return false; +} + FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options) : file_(file), options_(options), scc_analyzer_(options) { std::vector msgs = FlattenMessagesInFile(file); @@ -481,10 +499,7 @@ void FileGenerator::GenerateSourceIncludes(io::Printer* p) { for (int i = 0; i < file_->dependency_count(); ++i) { const FileDescriptor* dep = file_->dependency(i); - if (!options_.opensource_runtime && - IsDepWeak(dep)) { // Do not import weak deps. - continue; - } + if (ShouldSkipDependencyImports(dep)) continue; std::string basename = StripProto(dep->name()); if (IsBootstrapProto(options_, file_)) { @@ -698,10 +713,10 @@ void FileGenerator::GetCrossFileReferencesForFile(const FileDescriptor* file, for (int i = 0; i < file->dependency_count(); ++i) { const FileDescriptor* dep = file->dependency(i); - if (IsDepWeak(dep)) { - refs->weak_reflection_files.insert(dep); - } else { + if (!ShouldSkipDependencyImports(file->dependency(i))) { refs->strong_reflection_files.insert(dep); + } else if (IsDepWeak(dep)) { + refs->weak_reflection_files.insert(dep); } } } @@ -1456,8 +1471,7 @@ void FileGenerator::GenerateDependencyIncludes(io::Printer* p) { for (int i = 0; i < file_->dependency_count(); ++i) { const FileDescriptor* dep = file_->dependency(i); - // Do not import weak deps. - if (IsDepWeak(dep)) { + if (ShouldSkipDependencyImports(dep)) { continue; } diff --git a/src/google/protobuf/compiler/cpp/file.h b/src/google/protobuf/compiler/cpp/file.h index 6514df048f..516809e573 100644 --- a/src/google/protobuf/compiler/cpp/file.h +++ b/src/google/protobuf/compiler/cpp/file.h @@ -177,6 +177,9 @@ class FileGenerator { // generally a breaking change so we prefer the #undef approach. void GenerateMacroUndefs(io::Printer* p); + // Calculates if we should skip importing a specific dependency. + bool ShouldSkipDependencyImports(const FileDescriptor* dep) const; + bool IsDepWeak(const FileDescriptor* dep) const { if (weak_deps_.count(dep) != 0) { ABSL_CHECK(!options_.opensource_runtime);