Skip to content

Commit 58cdbf5

Browse files
committed
Sema: add support for __attribute__((__swift_private__))
This attribute allows declarations to be restricted to the framework itself, enabling Swift to remove the declarations when importing libraries. This is useful in the case that the functions can be implemented in a more natural way for Swift. This is based on the work of the original changes in llvm/llvm-project-staging@8afaf3a Differential Revision: https://reviews.llvm.org/D87720 Reviewed By: Aaron Ballman
1 parent 152ff37 commit 58cdbf5

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,11 @@ def SwiftNewType : InheritableAttr {
21772177
let HasCustomParsing = 1;
21782178
}
21792179

2180+
def SwiftPrivate : InheritableAttr {
2181+
let Spellings = [GNU<"swift_private">];
2182+
let Documentation = [SwiftPrivateDocs];
2183+
}
2184+
21802185
def NoDeref : TypeAttr {
21812186
let Spellings = [Clang<"noderef">];
21822187
let Documentation = [NoDerefDocs];

clang/include/clang/Basic/AttrDocs.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,6 +3713,19 @@ ypedef.
37133713
}];
37143714
}
37153715

3716+
def SwiftPrivateDocs : Documentation {
3717+
let Category = SwiftDocs;
3718+
let Heading = "swift_private";
3719+
let Content = [{
3720+
Declarations marked with the ``swift_private`` attribute are hidden from the
3721+
framework client but are still made available for use within the framework or
3722+
Swift SDK overlay.
3723+
3724+
The purpose of this attribute is to permit a more idomatic implementation of
3725+
declarations in Swift while hiding the non-idiomatic one.
3726+
}];
3727+
}
3728+
37163729
def OMPDeclareSimdDocs : Documentation {
37173730
let Category = DocCatFunction;
37183731
let Heading = "#pragma omp declare simd";

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7899,6 +7899,9 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
78997899
case ParsedAttr::AT_SwiftObjCMembers:
79007900
handleSimpleAttribute<SwiftObjCMembersAttr>(S, D, AL);
79017901
break;
7902+
case ParsedAttr::AT_SwiftPrivate:
7903+
handleSimpleAttribute<SwiftPrivateAttr>(S, D, AL);
7904+
break;
79027905

79037906
// XRay attributes.
79047907
case ParsedAttr::AT_XRayLogArgs:

clang/test/AST/attr-swift_private.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_cc1 -ast-dump %s | FileCheck %s
2+
3+
@interface I
4+
- (void)method __attribute__((__swift_private__));
5+
@end
6+
7+
// CHECK: ObjCInterfaceDecl {{.*}} I
8+
// CHECK: ObjCMethodDecl {{.*}} method 'void'
9+
// CHECK: SwiftPrivateAttr
10+
11+
@interface J : I
12+
- (void)method;
13+
@end
14+
15+
// CHECK: ObjCInterfaceDecl {{.*}} J
16+
// CHECK: ObjCMethodDecl {{.*}} method 'void'
17+
// CHECK: SwiftPrivateAttr {{.*}} Inherited
18+
19+
void f(void) __attribute__((__swift_private__));
20+
// CHECK: FunctionDecl {{.*}} f 'void (void)'
21+
// CHECK: SwiftPrivateAttr
22+
23+
void f(void) {
24+
}
25+
// CHECK: FunctionDecl {{.*}} f 'void (void)'
26+
// CHECK: SwiftPrivateAttr {{.*}} Inherited
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_cc1 -verify -fsyntax-only -fobjc-arc %s
2+
3+
__attribute__((__swift_private__))
4+
@protocol P
5+
@end
6+
7+
__attribute__((__swift_private__))
8+
@interface I
9+
@end
10+
11+
@interface J
12+
@property id property __attribute__((__swift_private__));
13+
- (void)instanceMethod __attribute__((__swift_private__));
14+
+ (void)classMethod __attribute__((__swift_private__));
15+
@end
16+
17+
void f(void) __attribute__((__swift_private__));
18+
19+
struct __attribute__((__swift_private__)) S {};
20+
21+
enum __attribute__((__swift_private__)) E {
22+
one,
23+
two,
24+
};
25+
26+
typedef struct { } T __attribute__((__swift_private__));
27+
28+
void g(void) __attribute__((__swift_private__("private")));
29+
// expected-error@-1 {{'__swift_private__' attribute takes no arguments}}

0 commit comments

Comments
 (0)