Skip to content

Commit cd54530

Browse files
authored
[APIGen] Don't record declarations from clang header files (#81086)
When Swift visits an `ExtensionDecl` from an `@objc @implementation` extension, it fetches the implemented Objective-C interface declaration from the header file and adds to the visitor. We don't want these API records from `APIGenRecorder` for Swift API descriptor, because we cannot correctly reason about their API access as they depend on the header group which Swift doesn't know about. Resolves rdar://148943382 <!-- If this pull request is targeting a release branch, please fill out the following form: https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1 Otherwise, replace this comment with a description of your changes and rationale. Provide links to external references/discussions if appropriate. If this pull request resolves any GitHub issues, link them like so: Resolves <link to issue>, resolves <link to another issue>. For more information about linking a pull request to an issue, see: https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue --> <!-- Before merging this pull request, you must run the Swift continuous integration tests. For information about triggering CI builds via @swift-ci, see: https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci Thank you for your contribution to Swift! -->
1 parent b670a6d commit cd54530

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

lib/IRGen/TBDGen.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ class APIGenRecorder final : public APIRecorder {
708708
apigen::APIAvailability availability;
709709
auto access = apigen::APIAccess::Public;
710710
if (decl) {
711+
if (!shouldRecordDecl(decl))
712+
return;
713+
711714
availability = getAvailability(decl);
712715
if (isSPI(decl))
713716
access = apigen::APIAccess::Private;
@@ -733,6 +736,9 @@ class APIGenRecorder final : public APIRecorder {
733736
auto access = apigen::APIAccess::Public;
734737
auto decl = method.hasDecl() ? method.getDecl() : nullptr;
735738
if (decl) {
739+
if (!shouldRecordDecl(decl))
740+
return;
741+
736742
availability = getAvailability(decl);
737743
if (decl->getDescriptiveKind() == DescriptiveDeclKind::ClassMethod)
738744
isInstanceMethod = false;
@@ -752,7 +758,7 @@ class APIGenRecorder final : public APIRecorder {
752758
}
753759

754760
private:
755-
apigen::APILoc getAPILocForDecl(const Decl *decl) {
761+
apigen::APILoc getAPILocForDecl(const Decl *decl) const {
756762
if (!decl)
757763
return defaultLoc;
758764

@@ -768,6 +774,15 @@ class APIGenRecorder final : public APIRecorder {
768774
return apigen::APILoc(std::string(displayName), line, col);
769775
}
770776

777+
bool shouldRecordDecl(const Decl *decl) const {
778+
// We cannot reason about API access for Clang declarations from header
779+
// files as we don't know the header group. API records for header
780+
// declarations should be deferred to Clang tools.
781+
if (getAPILocForDecl(decl).getFilename().ends_with_insensitive(".h"))
782+
return false;
783+
return true;
784+
}
785+
771786
/// Follow the naming schema that IRGen uses for Categories (see
772787
/// ClassDataBuilder).
773788
using CategoryNameKey = std::pair<const ClassDecl *, const ModuleDecl *>;
@@ -809,6 +824,9 @@ class APIGenRecorder final : public APIRecorder {
809824
}
810825

811826
apigen::ObjCInterfaceRecord *addOrGetObjCInterface(const ClassDecl *decl) {
827+
if (!shouldRecordDecl(decl))
828+
return nullptr;
829+
812830
auto entry = classMap.find(decl);
813831
if (entry != classMap.end())
814832
return entry->second;
@@ -847,6 +865,9 @@ class APIGenRecorder final : public APIRecorder {
847865
}
848866

849867
apigen::ObjCCategoryRecord *addOrGetObjCCategory(const ExtensionDecl *decl) {
868+
if (!shouldRecordDecl(decl))
869+
return nullptr;
870+
850871
auto entry = categoryMap.find(decl);
851872
if (entry != categoryMap.end())
852873
return entry->second;

test/APIJSON/objc-implement.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// REQUIRES: objc_interop, OS=macosx
2+
// RUN: %empty-directory(%t)
3+
// RUN: %empty-directory(%t/ModuleCache)
4+
// RUN: split-file %s %t
5+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) %t/objc-implement.swift -typecheck -parse-as-library -emit-module-interface-path %t/ObjcImplement.swiftinterface -enable-library-evolution -module-name ObjcImplement -import-underlying-module -swift-version 5 -emit-api-descriptor-path %t/api.json
6+
// RUN: %validate-json %t/api.json | %FileCheck %s
7+
8+
//--- objc-implement.swift
9+
import Foundation
10+
11+
// API record for ObjCClass shouldn't be emitted from swift.
12+
@objc @implementation
13+
extension ObjCClass {
14+
init?(char: CChar) {}
15+
}
16+
17+
// API record for ObjCClass and ObjCCategory shouldn't be emitted from swift.
18+
@objc(ObjCCategory) @implementation
19+
extension ObjCClass {
20+
convenience init?(int: Int32) {}
21+
}
22+
23+
// This is an actual class declaration that should be included in the API
24+
// descriptor.
25+
@objc
26+
public class SwiftObjCClass: NSObject {}
27+
28+
//--- ObjcImplement.h
29+
@interface Root
30+
@end
31+
32+
@interface ObjCClass : Root
33+
- (instancetype) initWithChar:(char)c;
34+
@end
35+
36+
@interface ObjCClass (ObjCCategory)
37+
- (instancetype) initWithInt:(int)i;
38+
@end
39+
40+
//--- module.modulemap
41+
module ObjcImplement {
42+
header "ObjcImplement.h"
43+
export *
44+
}
45+
46+
47+
// CHECK-NOT: "file": "{{.*}}.h"
48+
49+
// CHECK: "interfaces": [
50+
// CHECK-NEXT: {
51+
// CHECK-NEXT: "name": "_TtC13ObjcImplement14SwiftObjCClass",
52+
// CHECK-NEXT: "access": "public",
53+
// CHECK-NEXT: "file": "{{.*}}/objc-implement.swift",
54+
// CHECK-NEXT: "linkage": "exported",
55+
// CHECK-NEXT: "super": "NSObject",
56+
// CHECK-NEXT: "instanceMethods": [
57+
// CHECK-NEXT: {
58+
// CHECK-NEXT: "name": "init",
59+
// CHECK-NEXT: "access": "public",
60+
// CHECK-NEXT: "file": "{{.*}}/objc-implement.swift"
61+
// CHECK-NEXT: }
62+
// CHECK-NEXT: ],
63+
// CHECK-NEXT: "classMethods": []
64+
// CHECK-NEXT: }
65+
// CHECK-NEXT: ],
66+
// CHECK-NEXT: "categories": [],
67+
// CHECK-NEXT: "version": "1.0"

0 commit comments

Comments
 (0)