Skip to content

Exclude @objcImpl member impls from PrintAsObjC #63986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2783,14 +2783,34 @@ static bool hasExposeAttr(const ValueDecl *VD, bool isExtension = false) {
return false;
}

/// Skip \c \@objcImplementation \c extension member implementations and
/// overrides. They are already declared in handwritten headers, and they may
/// have attributes that aren't allowed in a category.
///
/// \return true if \p VD should \em not be included in the header.
static bool excludeForObjCImplementation(const ValueDecl *VD) {
// Exclude member implementations; they are declared elsewhere.
if (VD->isObjCMemberImplementation())
return true;
// Exclude overrides in an @_objcImplementation extension; the decl they're
// overriding is declared elsewhere.
if (VD->isImplicit() && VD->getOverriddenDecl()) {
auto ED = dyn_cast<ExtensionDecl>(VD->getDeclContext());
if (ED && ED->isObjCImplementation())
return true;
}
return false;
}

bool DeclAndTypePrinter::shouldInclude(const ValueDecl *VD) {
return !VD->isInvalid() && (!requiresExposedAttribute || hasExposeAttr(VD)) &&
(outputLang == OutputLanguageMode::Cxx
? cxx_translation::isVisibleToCxx(VD, minRequiredAccess) &&
cxx_translation::isExposableToCxx(VD)
: isVisibleToObjC(VD, minRequiredAccess)) &&
!VD->getAttrs().hasAttribute<ImplementationOnlyAttr>() &&
!isAsyncAlternativeOfOtherDecl(VD);
!isAsyncAlternativeOfOtherDecl(VD) &&
!excludeForObjCImplementation(VD);
}

void DeclAndTypePrinter::print(const Decl *D) {
Expand Down
5 changes: 5 additions & 0 deletions test/PrintAsObjC/Inputs/custom-modules/module.map
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ module EmitClangHeaderNonmodularIncludesStressTest {
header "header_subdirectory/header-symlink.h"
export *
}

module objc_implementation {
header "objc_implementation/objc_implementation.h"
export *
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import <Foundation.h>

@interface ObjCClass : NSObject

- (nullable id)swiftMethod;

@end

37 changes: 37 additions & 0 deletions test/PrintAsObjC/objc_implementation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Please keep this file in alphabetical order!

// REQUIRES: objc_interop

// RUN: %empty-directory(%t)

// FIXME: BEGIN -enable-source-import hackaround
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/ObjectiveC.swift -disable-objc-attr-requires-foundation-module
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/CoreGraphics.swift
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/Foundation.swift
// FIXME: END -enable-source-import hackaround


// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -I %S/Inputs/custom-modules -import-underlying-module -o %t %s -disable-objc-attr-requires-foundation-module
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -parse-as-library %t/objc_implementation.swiftmodule -typecheck -I %S/Inputs/custom-modules -emit-objc-header-path %t/objc_implementation-Swift.h -import-underlying-module -disable-objc-attr-requires-foundation-module
// RUN: %FileCheck %s --input-file %t/objc_implementation-Swift.h
// RUN: %FileCheck --check-prefix=NEGATIVE %s --input-file %t/objc_implementation-Swift.h
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ %t/objc_implementation-Swift.h
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ -fno-modules -Qunused-arguments %t/objc_implementation-Swift.h

import Foundation

extension ObjCClass {
// CHECK: - (id _Nullable)pureSwiftMethod SWIFT_WARN_UNUSED_RESULT;
@objc public func pureSwiftMethod() -> Any? { nil }
}

@_objcImplementation extension ObjCClass {
// NEGATIVE-NOT: )init{{ }}
// Implicit `override init()` to override superclass

// NEGATIVE-NOT: )swiftMethod{{ }}
@objc func swiftMethod() -> Any? { nil }

// NEGATIVE-NOT: )privateMethod{{ }}
@objc private func privateMethod() -> Any? { nil }
}