Skip to content

Commit

Permalink
Less generation when no messages, enums, or extensions.
Browse files Browse the repository at this point in the history
There's no need to generate any `import` directives or the version check
when the file is completely empty, and with different access levels on
imports that can get flagged with a newer compilers.

This is a common case when a .proto file contains a service.

Regenerate to get the one file update.
  • Loading branch information
thomasvl committed Aug 28, 2024
1 parent be11d18 commit 58db47c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
13 changes: 1 addition & 12 deletions Reference/upstream/google/protobuf/unittest_empty.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,4 @@
// This file intentionally left blank. (At one point this wouldn't compile
// correctly.)

import Foundation
import SwiftProtobuf

// If the compiler emits an error on this type, it is because this file
// was generated by a version of the `protoc` Swift plug-in that is
// incompatible with the version of SwiftProtobuf to which you are linking.
// Please ensure that you are building against the same version of the API
// that was used to generate this file.
fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck {
struct _2: SwiftProtobuf.ProtobufAPIVersion_2 {}
typealias Version = _2
}
// This file contained no messages, enums, or extensions.
23 changes: 15 additions & 8 deletions Sources/protoc-gen-swift/FileGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,25 @@ class FileGenerator {
} else if let location = fileDescriptor.sourceCodeInfoLocation(path: syntaxPath) {
commentLocation = location
}
var comments = String()
if let commentLocation = commentLocation {
comments = commentLocation.asSourceComment(commentPrefix: "///",
leadingDetachedPrefix: "//")
// If the was a leading or tailing comment it won't have a blank
// line, after it, so ensure there is one.
if !comments.isEmpty && !comments.hasSuffix("\n\n") {
comments.append("\n")
let comments = commentLocation.asSourceComment(commentPrefix: "///",
leadingDetachedPrefix: "//")
if !comments.isEmpty {
// If the was a leading or tailing comment it won't have a blank
// line, after it, so ensure there is one.
p.print(comments, newlines: !comments.hasSuffix("\n\n"))
}
}

p.print("\(comments)\(generatorOptions.importDirective.snippet) Foundation")
// If there is nothing to generate, then just record that and be done (usually means
// there just was one or more services).
let generateEmpty = fileDescriptor.enums.isEmpty && fileDescriptor.messages.isEmpty && fileDescriptor.extensions.isEmpty
guard !generateEmpty else {
p.print("// This file contained no messages, enums, or extensions.")
return
}

p.print("\(generatorOptions.importDirective.snippet) Foundation")

if fileDescriptor.isBundledProto {
p.print("// 'import \(namer.swiftProtobufModuleName)' suppressed, this proto file is meant to be bundled in the runtime.")
Expand Down

0 comments on commit 58db47c

Please sign in to comment.