diff --git a/src/google/protobuf/compiler/objectivec/enum.cc b/src/google/protobuf/compiler/objectivec/enum.cc index 2c5190cedc..fae53a2606 100644 --- a/src/google/protobuf/compiler/objectivec/enum.cc +++ b/src/google/protobuf/compiler/objectivec/enum.cc @@ -39,7 +39,9 @@ std::string SafelyPrintIntToCode(int v) { EnumGenerator::EnumGenerator(const EnumDescriptor* descriptor, const GenerationOptions& generation_options) - : descriptor_(descriptor), name_(EnumName(descriptor_)) { + : descriptor_(descriptor), + generation_options_(generation_options), + name_(EnumName(descriptor_)) { // Track the names for the enum values, and if an alias overlaps a base // value, skip making a name for it. Likewise if two alias overlap, the // first one wins. @@ -86,7 +88,10 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) const { printer->Emit( { {"enum_name", name_}, - {"enum_comments", [&] { EmitCommentsString(printer, descriptor_); }}, + {"enum_comments", + [&] { + EmitCommentsString(printer, generation_options_, descriptor_); + }}, {"enum_deprecated_attribute", GetOptionalDeprecatedAttribute(descriptor_, descriptor_->file())}, {"maybe_unknown_value", @@ -112,7 +117,10 @@ void EnumGenerator::GenerateHeader(io::Printer* printer) const { { {"name", EnumValueName(v)}, {"comments", - [&] { EmitCommentsString(printer, v, comment_flags); }}, + [&] { + EmitCommentsString(printer, generation_options_, v, + comment_flags); + }}, {"deprecated_attribute", GetOptionalDeprecatedAttribute(v)}, {"value", SafelyPrintIntToCode(v->number())}, diff --git a/src/google/protobuf/compiler/objectivec/enum.h b/src/google/protobuf/compiler/objectivec/enum.h index 24a6e24884..22a7188a9d 100644 --- a/src/google/protobuf/compiler/objectivec/enum.h +++ b/src/google/protobuf/compiler/objectivec/enum.h @@ -37,6 +37,7 @@ class EnumGenerator { private: const EnumDescriptor* descriptor_; + const GenerationOptions& generation_options_; std::vector base_values_; std::vector all_values_; absl::flat_hash_set alias_values_to_skip_; diff --git a/src/google/protobuf/compiler/objectivec/extension.cc b/src/google/protobuf/compiler/objectivec/extension.cc index 9143885de9..993be00289 100644 --- a/src/google/protobuf/compiler/objectivec/extension.cc +++ b/src/google/protobuf/compiler/objectivec/extension.cc @@ -34,7 +34,8 @@ ExtensionGenerator::ExtensionGenerator( : method_name_(ExtensionMethodName(descriptor)), full_method_name_( absl::StrCat(root_or_message_class_name, "_", method_name_)), - descriptor_(descriptor) { + descriptor_(descriptor), + generation_options_(generation_options) { ABSL_CHECK(!descriptor->is_map()) << "error: Extension is a map<>!" << " That used to be blocked by the compiler."; @@ -43,7 +44,8 @@ ExtensionGenerator::ExtensionGenerator( void ExtensionGenerator::GenerateMembersHeader(io::Printer* printer) const { printer->Emit( {{"method_name", method_name_}, - {"comments", [&] { EmitCommentsString(printer, descriptor_); }}, + {"comments", + [&] { EmitCommentsString(printer, generation_options_, descriptor_); }}, {"storage_attribute", IsRetainedName(method_name_) ? "NS_RETURNS_NOT_RETAINED" : ""}, {"deprecated_attribute", diff --git a/src/google/protobuf/compiler/objectivec/extension.h b/src/google/protobuf/compiler/objectivec/extension.h index 2d4fb9fbac..991aeee85d 100644 --- a/src/google/protobuf/compiler/objectivec/extension.h +++ b/src/google/protobuf/compiler/objectivec/extension.h @@ -44,6 +44,7 @@ class ExtensionGenerator { std::string method_name_; std::string full_method_name_; const FieldDescriptor* descriptor_; + const GenerationOptions& generation_options_; }; } // namespace objectivec diff --git a/src/google/protobuf/compiler/objectivec/field.cc b/src/google/protobuf/compiler/objectivec/field.cc index b22cd0d06e..870b8c6d97 100644 --- a/src/google/protobuf/compiler/objectivec/field.cc +++ b/src/google/protobuf/compiler/objectivec/field.cc @@ -192,7 +192,7 @@ FieldGenerator* FieldGenerator::Make( FieldGenerator::FieldGenerator(const FieldDescriptor* descriptor, const GenerationOptions& generation_options) - : descriptor_(descriptor) { + : descriptor_(descriptor), generation_options_(generation_options) { SetCommonFieldVariables(descriptor, &variables_); } @@ -294,9 +294,12 @@ void SingleFieldGenerator::GenerateFieldStorageDeclaration( void SingleFieldGenerator::GeneratePropertyDeclaration( io::Printer* printer) const { auto vars = printer->WithVars(variables_); - printer->Emit( - {{"comments", [&] { EmitCommentsString(printer, descriptor_); }}}, - R"objc( + printer->Emit({{"comments", + [&] { + EmitCommentsString(printer, generation_options_, + descriptor_); + }}}, + R"objc( $comments$ @property(nonatomic, readwrite) $property_type$$name$$ deprecated_attribute$; )objc"); @@ -349,9 +352,12 @@ void ObjCObjFieldGenerator::GeneratePropertyDeclaration( // conventions (init*, new*, etc.) auto vars = printer->WithVars(variables_); - printer->Emit( - {{"comments", [&] { EmitCommentsString(printer, descriptor_); }}}, - R"objc( + printer->Emit({{"comments", + [&] { + EmitCommentsString(printer, generation_options_, + descriptor_); + }}}, + R"objc( $comments$ @property(nonatomic, readwrite, $property_storage_attribute$, null_resettable) $property_type$$name$$storage_attribute$$ deprecated_attribute$; )objc"); @@ -398,7 +404,8 @@ void RepeatedFieldGenerator::GeneratePropertyDeclaration( auto vars = printer->WithVars(variables_); printer->Emit( - {{"comments", [&] { EmitCommentsString(printer, descriptor_); }}, + {{"comments", + [&] { EmitCommentsString(printer, generation_options_, descriptor_); }}, {"array_comment", [&] { EmitArrayComment(printer); }}}, R"objc( $comments$ diff --git a/src/google/protobuf/compiler/objectivec/field.h b/src/google/protobuf/compiler/objectivec/field.h index db5af3ad79..06e08e1f0a 100644 --- a/src/google/protobuf/compiler/objectivec/field.h +++ b/src/google/protobuf/compiler/objectivec/field.h @@ -88,6 +88,7 @@ class FieldGenerator { bool WantsHasProperty() const; const FieldDescriptor* descriptor_; + const GenerationOptions& generation_options_; absl::flat_hash_map variables_; }; diff --git a/src/google/protobuf/compiler/objectivec/helpers.cc b/src/google/protobuf/compiler/objectivec/helpers.cc index b1d41803ec..12608f47d8 100644 --- a/src/google/protobuf/compiler/objectivec/helpers.cc +++ b/src/google/protobuf/compiler/objectivec/helpers.cc @@ -24,6 +24,7 @@ #include "absl/strings/string_view.h" #include "absl/strings/strip.h" #include "google/protobuf/compiler/objectivec/names.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/io/strtod.h" #include "google/protobuf/stubs/common.h" @@ -331,8 +332,14 @@ std::string ObjCClassDeclaration(absl::string_view class_name) { return absl::StrCat("GPBObjCClassDeclaration(", class_name, ");"); } -void EmitCommentsString(io::Printer* printer, const SourceLocation& location, +void EmitCommentsString(io::Printer* printer, const GenerationOptions& opts, + const SourceLocation& location, CommentStringFlags flags) { + if (opts.experimental_strip_nonfunctional_codegen) { + // Comments are inherently non-functional, and may change subtly on + // transformations. + return; + } absl::string_view comments = location.leading_comments.empty() ? location.trailing_comments : location.leading_comments; diff --git a/src/google/protobuf/compiler/objectivec/helpers.h b/src/google/protobuf/compiler/objectivec/helpers.h index 7b6c2b786d..edd0c1c096 100644 --- a/src/google/protobuf/compiler/objectivec/helpers.h +++ b/src/google/protobuf/compiler/objectivec/helpers.h @@ -15,6 +15,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" +#include "google/protobuf/compiler/objectivec/options.h" #include "google/protobuf/descriptor.h" #include "google/protobuf/descriptor.pb.h" #include "google/protobuf/io/printer.h" @@ -106,17 +107,19 @@ enum CommentStringFlags : unsigned int { // Emits HeaderDoc/appledoc style comments out of the comments in the .proto // file. -void EmitCommentsString(io::Printer* printer, const SourceLocation& location, +void EmitCommentsString(io::Printer* printer, const GenerationOptions& opts, + const SourceLocation& location, CommentStringFlags flags = kCommentStringFlags_None); // Emits HeaderDoc/appledoc style comments out of the comments in the .proto // file. template -void EmitCommentsString(io::Printer* printer, const TDescriptor* descriptor, +void EmitCommentsString(io::Printer* printer, const GenerationOptions& opts, + const TDescriptor* descriptor, CommentStringFlags flags = kCommentStringFlags_None) { SourceLocation location; if (descriptor->GetSourceLocation(&location)) { - EmitCommentsString(printer, location, flags); + EmitCommentsString(printer, opts, location, flags); } } diff --git a/src/google/protobuf/compiler/objectivec/message.cc b/src/google/protobuf/compiler/objectivec/message.cc index 55fd4d3f2d..c3f2ef6d3a 100644 --- a/src/google/protobuf/compiler/objectivec/message.cc +++ b/src/google/protobuf/compiler/objectivec/message.cc @@ -292,7 +292,7 @@ void MessageGenerator::GenerateMessageHeader(io::Printer* printer) const { .WithSuffix(";"), {"message_comments", [&] { - EmitCommentsString(printer, descriptor_, + EmitCommentsString(printer, generation_options_, descriptor_, kCommentStringFlags_ForceMultiline); }}, {"message_fieldnum_enum", diff --git a/src/google/protobuf/compiler/objectivec/oneof.cc b/src/google/protobuf/compiler/objectivec/oneof.cc index e6de298dc1..e2680f6185 100644 --- a/src/google/protobuf/compiler/objectivec/oneof.cc +++ b/src/google/protobuf/compiler/objectivec/oneof.cc @@ -23,7 +23,7 @@ namespace objectivec { OneofGenerator::OneofGenerator(const OneofDescriptor* descriptor, const GenerationOptions& generation_options) - : descriptor_(descriptor) { + : descriptor_(descriptor), generation_options_(generation_options) { variables_["enum_name"] = OneofEnumName(descriptor_); variables_["name"] = OneofName(descriptor_); variables_["capitalized_name"] = OneofNameCapitalized(descriptor_); @@ -64,9 +64,12 @@ void OneofGenerator::GenerateCaseEnum(io::Printer* printer) const { void OneofGenerator::GeneratePublicCasePropertyDeclaration( io::Printer* printer) const { auto vars = printer->WithVars(variables_); - printer->Emit( - {{"comments", [&] { EmitCommentsString(printer, descriptor_); }}}, - R"objc( + printer->Emit({{"comments", + [&] { + EmitCommentsString(printer, generation_options_, + descriptor_); + }}}, + R"objc( $comments$; @property(nonatomic, readonly) $enum_name$ $name$OneOfCase; )objc"); diff --git a/src/google/protobuf/compiler/objectivec/oneof.h b/src/google/protobuf/compiler/objectivec/oneof.h index e08e7ab6b8..c849e88512 100644 --- a/src/google/protobuf/compiler/objectivec/oneof.h +++ b/src/google/protobuf/compiler/objectivec/oneof.h @@ -45,6 +45,7 @@ class OneofGenerator { private: const OneofDescriptor* descriptor_; + const GenerationOptions& generation_options_; absl::flat_hash_map variables_; };