Skip to content

Commit

Permalink
Avoid proto comments during experimental_strip_nonfunctional_codegen.
Browse files Browse the repository at this point in the history
Following upstream protobuf, honor the experimental_strip_nonfunctional_codegen
generation option to not include comments in generated code.
  • Loading branch information
thomasvl committed Feb 21, 2024
1 parent 8ee79e4 commit 29c253f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Sources/protoc-gen-swift/EnumGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class EnumGenerator {

p.print(
"",
"\(enumDescriptor.protoSourceCommentsWithDeprecation())\(visibility)enum \(swiftRelativeName): \(namer.swiftProtobufModulePrefix)Enum {")
"\(enumDescriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions))\(visibility)enum \(swiftRelativeName): \(namer.swiftProtobufModulePrefix)Enum {")
p.withIndentation { p in
p.print("\(visibility)typealias RawValue = Int")

Expand Down Expand Up @@ -117,7 +117,7 @@ class EnumGenerator {
private func generateCasesOrAliases(printer p: inout CodePrinter) {
let visibility = generatorOptions.visibilitySourceSnippet
for enumValueDescriptor in namer.uniquelyNamedValues(valueAliasInfo: aliasInfo) {
let comments = enumValueDescriptor.protoSourceCommentsWithDeprecation()
let comments = enumValueDescriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions)
if !comments.isEmpty {
p.print()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/protoc-gen-swift/ExtensionSetGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ExtensionSetGenerator {

swiftFullExtensionName = namer.fullName(extensionField: descriptor)

comments = descriptor.protoSourceCommentsWithDeprecation()
comments = descriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions)
containingTypeSwiftFullName = namer.fullName(message: fieldDescriptor.containingType)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/protoc-gen-swift/MessageFieldGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
swiftStorageType = descriptor.swiftStorageType(namer: namer)
swiftDefaultValue = descriptor.swiftDefaultValue(namer: namer)
traitsType = descriptor.traitsType(namer: namer)
comments = descriptor.protoSourceCommentsWithDeprecation()
comments = descriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions)

if usesHeapStorage {
storedProperty = "_storage.\(underscoreSwiftName)"
Expand Down
2 changes: 1 addition & 1 deletion Sources/protoc-gen-swift/MessageGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class MessageGenerator {

p.print(
"",
"\(descriptor.protoSourceCommentsWithDeprecation())\(visibility)struct \(swiftRelativeName): \(conformances.joined(separator: ", ")) {")
"\(descriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions))\(visibility)struct \(swiftRelativeName): \(conformances.joined(separator: ", ")) {")
p.withIndentation { p in
p.print("""
// \(namer.swiftProtobufModuleName).Message conformance is added in an extension below. See the
Expand Down
10 changes: 6 additions & 4 deletions Sources/protoc-gen-swift/OneofGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OneofGenerator {
// Only valid on message fields.
var messageType: Descriptor? { return fieldDescriptor.messageType }

init(descriptor: FieldDescriptor, namer: SwiftProtobufNamer) {
init(descriptor: FieldDescriptor, generatorOptions: GeneratorOptions, namer: SwiftProtobufNamer) {
precondition(descriptor.oneofIndex != nil)

// Set after creation.
Expand All @@ -57,7 +57,7 @@ class OneofGenerator {
swiftType = descriptor.swiftType(namer: namer)
swiftDefaultValue = descriptor.swiftDefaultValue(namer: namer)
protoGenericType = descriptor.protoGenericType
comments = descriptor.protoSourceCommentsWithDeprecation()
comments = descriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions)

super.init(descriptor: descriptor)
}
Expand Down Expand Up @@ -130,7 +130,7 @@ class OneofGenerator {
self.namer = namer
self.usesHeapStorage = usesHeapStorage

comments = descriptor.protoSourceComments()
comments = descriptor.protoSourceComments(generatorOptions: generatorOptions)

swiftRelativeName = namer.relativeName(oneof: descriptor)
swiftFullName = namer.fullName(oneof: descriptor)
Expand All @@ -145,7 +145,9 @@ class OneofGenerator {
}

fields = descriptor.fields.map {
return MemberFieldGenerator(descriptor: $0, namer: namer)
return MemberFieldGenerator(descriptor: $0,
generatorOptions: generatorOptions,
namer: namer)
}
fieldsSortedByNumber = fields.sorted {$0.number < $1.number}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Sources/protoc-gen-swift/ProvidesSourceCodeLocation+Extensions.swift
//
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See LICENSE.txt for license information:
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
//
// -----------------------------------------------------------------------------

import Foundation

import SwiftProtobufPluginLibrary

extension ProvidesSourceCodeLocation {
func protoSourceComments(
generatorOptions: GeneratorOptions,
commentPrefix: String = "///",
leadingDetachedPrefix: String? = nil
) -> String {
if generatorOptions.experimentalStripNonfunctionalCodegen {
// Comments are inherently non-functional, and may change subtly on
// transformations.
return String()
}
return protoSourceComments(commentPrefix: commentPrefix,
leadingDetachedPrefix: leadingDetachedPrefix)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Sources/protoc-gen-swift/ProvidesDeprecationComment+Extensions.swift
//
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See LICENSE.txt for license information:
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
//
// -----------------------------------------------------------------------------

import Foundation

import SwiftProtobufPluginLibrary

extension ProvidesDeprecationComment where Self: ProvidesSourceCodeLocation {
func protoSourceCommentsWithDeprecation(
generatorOptions: GeneratorOptions,
commentPrefix: String = "///",
leadingDetachedPrefix: String? = nil
) -> String {
if generatorOptions.experimentalStripNonfunctionalCodegen {
// Comments are inherently non-functional, and may change subtly on
// transformations.
return deprecationComment(commentPrefix: commentPrefix)
}

return protoSourceCommentsWithDeprecation(commentPrefix: commentPrefix,
leadingDetachedPrefix: leadingDetachedPrefix)
}
}

0 comments on commit 29c253f

Please sign in to comment.