[clang] 58cdbf5 - Sema: add support for `__attribute__((__swift_private__))`

Saleem Abdulrasool via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 25 15:42:35 PDT 2020


Author: Saleem Abdulrasool
Date: 2020-09-25T22:33:53Z
New Revision: 58cdbf518b6ebaee59d0492375e2e8f7da87ca41

URL: https://github.com/llvm/llvm-project/commit/58cdbf518b6ebaee59d0492375e2e8f7da87ca41
DIFF: https://github.com/llvm/llvm-project/commit/58cdbf518b6ebaee59d0492375e2e8f7da87ca41.diff

LOG: 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
https://github.com/llvm/llvm-project-staging/commit/8afaf3aad2af43cfedca7a24cd817848c4e95c0c

Differential Revision: https://reviews.llvm.org/D87720
Reviewed By: Aaron Ballman

Added: 
    clang/test/AST/attr-swift_private.m
    clang/test/SemaObjC/attr-swift_private.m

Modified: 
    clang/include/clang/Basic/Attr.td
    clang/include/clang/Basic/AttrDocs.td
    clang/lib/Sema/SemaDeclAttr.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index a9cee8c770f9..4d7a65964887 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -2177,6 +2177,11 @@ def SwiftNewType : InheritableAttr {
   let HasCustomParsing = 1;
 }
 
+def SwiftPrivate : InheritableAttr {
+  let Spellings = [GNU<"swift_private">];
+  let Documentation = [SwiftPrivateDocs];
+}
+
 def NoDeref : TypeAttr {
   let Spellings = [Clang<"noderef">];
   let Documentation = [NoDerefDocs];

diff  --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 9c16fecfeaa8..644795d41c8c 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -3713,6 +3713,19 @@ ypedef.
   }];
 }
 
+def SwiftPrivateDocs : Documentation {
+  let Category = SwiftDocs;
+  let Heading = "swift_private";
+  let Content = [{
+Declarations marked with the ``swift_private`` attribute are hidden from the
+framework client but are still made available for use within the framework or
+Swift SDK overlay.
+
+The purpose of this attribute is to permit a more idomatic implementation of
+declarations in Swift while hiding the non-idiomatic one.
+  }];
+}
+
 def OMPDeclareSimdDocs : Documentation {
   let Category = DocCatFunction;
   let Heading = "#pragma omp declare simd";

diff  --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index d5f08faccc92..06a1205e3964 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -7899,6 +7899,9 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
   case ParsedAttr::AT_SwiftObjCMembers:
     handleSimpleAttribute<SwiftObjCMembersAttr>(S, D, AL);
     break;
+  case ParsedAttr::AT_SwiftPrivate:
+    handleSimpleAttribute<SwiftPrivateAttr>(S, D, AL);
+    break;
 
   // XRay attributes.
   case ParsedAttr::AT_XRayLogArgs:

diff  --git a/clang/test/AST/attr-swift_private.m b/clang/test/AST/attr-swift_private.m
new file mode 100644
index 000000000000..161172943843
--- /dev/null
+++ b/clang/test/AST/attr-swift_private.m
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -ast-dump %s | FileCheck %s
+
+ at interface I
+- (void)method __attribute__((__swift_private__));
+ at end
+
+// CHECK: ObjCInterfaceDecl {{.*}} I
+// CHECK: ObjCMethodDecl {{.*}} method 'void'
+// CHECK: SwiftPrivateAttr
+
+ at interface J : I
+- (void)method;
+ at end
+
+// CHECK: ObjCInterfaceDecl {{.*}} J
+// CHECK: ObjCMethodDecl {{.*}} method 'void'
+// CHECK: SwiftPrivateAttr {{.*}} Inherited
+
+void f(void) __attribute__((__swift_private__));
+// CHECK: FunctionDecl {{.*}} f 'void (void)'
+// CHECK: SwiftPrivateAttr
+
+void f(void) {
+}
+// CHECK: FunctionDecl {{.*}} f 'void (void)'
+// CHECK: SwiftPrivateAttr {{.*}} Inherited

diff  --git a/clang/test/SemaObjC/attr-swift_private.m b/clang/test/SemaObjC/attr-swift_private.m
new file mode 100644
index 000000000000..15ea5eb66fe5
--- /dev/null
+++ b/clang/test/SemaObjC/attr-swift_private.m
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -fobjc-arc %s
+
+__attribute__((__swift_private__))
+ at protocol P
+ at end
+
+__attribute__((__swift_private__))
+ at interface I
+ at end
+
+ at interface J
+ at property id property __attribute__((__swift_private__));
+- (void)instanceMethod __attribute__((__swift_private__));
++ (void)classMethod __attribute__((__swift_private__));
+ at end
+
+void f(void) __attribute__((__swift_private__));
+
+struct __attribute__((__swift_private__)) S {};
+
+enum __attribute__((__swift_private__)) E {
+  one,
+  two,
+};
+
+typedef struct { } T __attribute__((__swift_private__));
+
+void g(void) __attribute__((__swift_private__("private")));
+// expected-error at -1 {{'__swift_private__' attribute takes no arguments}}


        


More information about the cfe-commits mailing list