From 58db47c67eb8e7413cafcff2a11b136a88e744cc Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Wed, 28 Aug 2024 12:32:36 -0400 Subject: [PATCH] Less generation when no messages, enums, or extensions. 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. --- .../google/protobuf/unittest_empty.pb.swift | 13 +---------- Sources/protoc-gen-swift/FileGenerator.swift | 23 ++++++++++++------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Reference/upstream/google/protobuf/unittest_empty.pb.swift b/Reference/upstream/google/protobuf/unittest_empty.pb.swift index 03ed9adee..826fd255b 100644 --- a/Reference/upstream/google/protobuf/unittest_empty.pb.swift +++ b/Reference/upstream/google/protobuf/unittest_empty.pb.swift @@ -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. diff --git a/Sources/protoc-gen-swift/FileGenerator.swift b/Sources/protoc-gen-swift/FileGenerator.swift index 2e2e99775..4b9bfde0a 100644 --- a/Sources/protoc-gen-swift/FileGenerator.swift +++ b/Sources/protoc-gen-swift/FileGenerator.swift @@ -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.")