Skip to content

Commit 29c253f

Browse files
committed
Avoid proto comments during experimental_strip_nonfunctional_codegen.
Following upstream protobuf, honor the experimental_strip_nonfunctional_codegen generation option to not include comments in generated code.
1 parent 8ee79e4 commit 29c253f

7 files changed

+70
-9
lines changed

Sources/protoc-gen-swift/EnumGenerator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class EnumGenerator {
5656

5757
p.print(
5858
"",
59-
"\(enumDescriptor.protoSourceCommentsWithDeprecation())\(visibility)enum \(swiftRelativeName): \(namer.swiftProtobufModulePrefix)Enum {")
59+
"\(enumDescriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions))\(visibility)enum \(swiftRelativeName): \(namer.swiftProtobufModulePrefix)Enum {")
6060
p.withIndentation { p in
6161
p.print("\(visibility)typealias RawValue = Int")
6262

@@ -117,7 +117,7 @@ class EnumGenerator {
117117
private func generateCasesOrAliases(printer p: inout CodePrinter) {
118118
let visibility = generatorOptions.visibilitySourceSnippet
119119
for enumValueDescriptor in namer.uniquelyNamedValues(valueAliasInfo: aliasInfo) {
120-
let comments = enumValueDescriptor.protoSourceCommentsWithDeprecation()
120+
let comments = enumValueDescriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions)
121121
if !comments.isEmpty {
122122
p.print()
123123
}

Sources/protoc-gen-swift/ExtensionSetGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ExtensionSetGenerator {
5757

5858
swiftFullExtensionName = namer.fullName(extensionField: descriptor)
5959

60-
comments = descriptor.protoSourceCommentsWithDeprecation()
60+
comments = descriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions)
6161
containingTypeSwiftFullName = namer.fullName(message: fieldDescriptor.containingType)
6262
}
6363

Sources/protoc-gen-swift/MessageFieldGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
7171
swiftStorageType = descriptor.swiftStorageType(namer: namer)
7272
swiftDefaultValue = descriptor.swiftDefaultValue(namer: namer)
7373
traitsType = descriptor.traitsType(namer: namer)
74-
comments = descriptor.protoSourceCommentsWithDeprecation()
74+
comments = descriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions)
7575

7676
if usesHeapStorage {
7777
storedProperty = "_storage.\(underscoreSwiftName)"

Sources/protoc-gen-swift/MessageGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class MessageGenerator {
129129

130130
p.print(
131131
"",
132-
"\(descriptor.protoSourceCommentsWithDeprecation())\(visibility)struct \(swiftRelativeName): \(conformances.joined(separator: ", ")) {")
132+
"\(descriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions))\(visibility)struct \(swiftRelativeName): \(conformances.joined(separator: ", ")) {")
133133
p.withIndentation { p in
134134
p.print("""
135135
// \(namer.swiftProtobufModuleName).Message conformance is added in an extension below. See the

Sources/protoc-gen-swift/OneofGenerator.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class OneofGenerator {
4242
// Only valid on message fields.
4343
var messageType: Descriptor? { return fieldDescriptor.messageType }
4444

45-
init(descriptor: FieldDescriptor, namer: SwiftProtobufNamer) {
45+
init(descriptor: FieldDescriptor, generatorOptions: GeneratorOptions, namer: SwiftProtobufNamer) {
4646
precondition(descriptor.oneofIndex != nil)
4747

4848
// Set after creation.
@@ -57,7 +57,7 @@ class OneofGenerator {
5757
swiftType = descriptor.swiftType(namer: namer)
5858
swiftDefaultValue = descriptor.swiftDefaultValue(namer: namer)
5959
protoGenericType = descriptor.protoGenericType
60-
comments = descriptor.protoSourceCommentsWithDeprecation()
60+
comments = descriptor.protoSourceCommentsWithDeprecation(generatorOptions: generatorOptions)
6161

6262
super.init(descriptor: descriptor)
6363
}
@@ -130,7 +130,7 @@ class OneofGenerator {
130130
self.namer = namer
131131
self.usesHeapStorage = usesHeapStorage
132132

133-
comments = descriptor.protoSourceComments()
133+
comments = descriptor.protoSourceComments(generatorOptions: generatorOptions)
134134

135135
swiftRelativeName = namer.relativeName(oneof: descriptor)
136136
swiftFullName = namer.fullName(oneof: descriptor)
@@ -145,7 +145,9 @@ class OneofGenerator {
145145
}
146146

147147
fields = descriptor.fields.map {
148-
return MemberFieldGenerator(descriptor: $0, namer: namer)
148+
return MemberFieldGenerator(descriptor: $0,
149+
generatorOptions: generatorOptions,
150+
namer: namer)
149151
}
150152
fieldsSortedByNumber = fields.sorted {$0.number < $1.number}
151153

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Sources/protoc-gen-swift/ProvidesSourceCodeLocation+Extensions.swift
2+
//
3+
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See LICENSE.txt for license information:
7+
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
8+
//
9+
// -----------------------------------------------------------------------------
10+
11+
import Foundation
12+
13+
import SwiftProtobufPluginLibrary
14+
15+
extension ProvidesSourceCodeLocation {
16+
func protoSourceComments(
17+
generatorOptions: GeneratorOptions,
18+
commentPrefix: String = "///",
19+
leadingDetachedPrefix: String? = nil
20+
) -> String {
21+
if generatorOptions.experimentalStripNonfunctionalCodegen {
22+
// Comments are inherently non-functional, and may change subtly on
23+
// transformations.
24+
return String()
25+
}
26+
return protoSourceComments(commentPrefix: commentPrefix,
27+
leadingDetachedPrefix: leadingDetachedPrefix)
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Sources/protoc-gen-swift/ProvidesDeprecationComment+Extensions.swift
2+
//
3+
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See LICENSE.txt for license information:
7+
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
8+
//
9+
// -----------------------------------------------------------------------------
10+
11+
import Foundation
12+
13+
import SwiftProtobufPluginLibrary
14+
15+
extension ProvidesDeprecationComment where Self: ProvidesSourceCodeLocation {
16+
func protoSourceCommentsWithDeprecation(
17+
generatorOptions: GeneratorOptions,
18+
commentPrefix: String = "///",
19+
leadingDetachedPrefix: String? = nil
20+
) -> String {
21+
if generatorOptions.experimentalStripNonfunctionalCodegen {
22+
// Comments are inherently non-functional, and may change subtly on
23+
// transformations.
24+
return deprecationComment(commentPrefix: commentPrefix)
25+
}
26+
27+
return protoSourceCommentsWithDeprecation(commentPrefix: commentPrefix,
28+
leadingDetachedPrefix: leadingDetachedPrefix)
29+
}
30+
}

0 commit comments

Comments
 (0)