Skip to content

Commit b611901

Browse files
committed
[SourceKit] Print custom attributes in interface-gen requests
Custom attributes were not printed because they are marked 'UserInaccesible'. * Make CustomAttr 'RejectByParser' instead of 'UserInaccessible' * Remove special treatment for Result Builder attributes * Load implicit modules in module/header interface gen requests rdar://79927502
1 parent 9f95d01 commit b611901

File tree

12 files changed

+93
-25
lines changed

12 files changed

+93
-25
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ SIMPLE_DECL_ATTR(_implementationOnly, ImplementationOnly,
494494
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
495495
84)
496496
DECL_ATTR(_custom, Custom,
497-
OnAnyDecl | UserInaccessible |
497+
OnAnyDecl | RejectByParser |
498498
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
499499
85)
500500
SIMPLE_DECL_ATTR(propertyWrapper, PropertyWrapper,

lib/AST/Attr.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -685,17 +685,11 @@ void DeclAttributes::print(ASTPrinter &Printer, const PrintOptions &Options,
685685
AttributeVector attributes;
686686
AttributeVector modifiers;
687687

688-
CustomAttr *FuncBuilderAttr = nullptr;
689-
if (auto *VD = dyn_cast_or_null<ValueDecl>(D)) {
690-
FuncBuilderAttr = VD->getAttachedResultBuilder();
691-
}
692688
for (auto DA : llvm::reverse(FlattenedAttrs)) {
693689
// Always print result builder attribute.
694-
bool isResultBuilderAttr = DA == FuncBuilderAttr;
695690
if (!Options.PrintImplicitAttrs && DA->isImplicit())
696691
continue;
697692
if (!Options.PrintUserInaccessibleAttrs &&
698-
!isResultBuilderAttr &&
699693
DeclAttribute::isUserInaccessible(DA->getKind()))
700694
continue;
701695
if (Options.excludeAttrKind(DA->getKind()))

test/IDE/print_clang_objc_effectful_properties.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
// CHECK-NEXT: var fromNullableHandler: String { get async }
3333

3434
// CHECK: @available(*, renamed: "getter:mainDogProp()")
35-
// CHECK-NEXT: func getMainDog(_ completion: @escaping @Sendable (String) -> Void)
35+
// CHECK-NEXT: func getMainDog(_ completion: @escaping @MainActor (String) -> Void)
3636
// CHECK-NEXT: var mainDogProp: String { get async }
3737

3838
// CHECK: @available(*, renamed: "regularMainDog()")
39-
// CHECK-NEXT: func regularMainDog(_ completion: @escaping @Sendable (String) -> Void)
39+
// CHECK-NEXT: func regularMainDog(_ completion: @escaping @MainActor (String) -> Void)
4040
// CHECK-NEXT: @discardableResult
4141
// CHECK-NEXT: func regularMainDog() async -> String
4242
// CHECK: }

test/IDE/print_swift_module.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public func returnsAlias() -> Alias<Int> {
2828
}
2929

3030
@resultBuilder
31-
struct BridgeBuilder {
31+
public struct BridgeBuilder {
3232
static func buildBlock(_: Any...) {}
3333
}
3434

test/SourceKit/Inputs/concurrency/gen_concurrency.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ class ClassWithAsyncAndHandler {
22
@available(*, renamed: "foo(_:)")
33
func foo(_ operation: String, completionHandler handler: @escaping (Int) -> Void) {}
44
func foo(_ operation: String) async -> Int { 0 }
5+
6+
@MainActor
7+
func mainActorMethod() {}
58
}

test/SourceKit/Inputs/concurrency/header_concurrency.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
@interface ClassWithHandlerMethod
44
-(void)methodWithHandler:(NSString *)operation completionHandler:(void (^)(NSInteger))handler;
5+
6+
-(void)mainActorMethod __attribute__((__swift_attr__("@UIActor")));
57
@end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// REQUIRES: concurrency
2+
3+
// RUN: %empty-directory(%t)
4+
5+
// RUN: %sourcekitd-test -req=interface-gen %s -- %s -target %target-triple -module-name MyModule | %FileCheck %s --check-prefix=SWIFTSOURCE
6+
7+
// RUN: %target-swift-frontend -emit-module -module-name MyModule -o %t/MyModule.swiftmodule %s
8+
// RUN: %sourcekitd-test -req=interface-gen -module MyModule -- -target %target-triple -I %t | %FileCheck %s --check-prefix=SWIFTMODULE
9+
// RUN: %sourcekitd-test -req=doc-info -module MyModule -- -target %target-triple -I %t | %FileCheck %s --check-prefix=DOCINFO
10+
11+
@propertyWrapper
12+
public struct OrLess {
13+
private var threshold: Int
14+
private var number: Int = 0
15+
init(wrappedValue: Int, threshold: Int) {
16+
self.threshold = threshold
17+
self.wrappedValue = wrappedValue
18+
}
19+
public var wrappedValue: Int {
20+
get { return number }
21+
set { number = min(newValue, 12) }
22+
}
23+
}
24+
25+
@resultBuilder
26+
public struct IntBuilder {
27+
public static func buildBlock(_ val: Int) -> Int { val }
28+
}
29+
30+
public struct TestStruct {
31+
32+
@MainActor public func mainActorMethod() {}
33+
@MainActor(unsafe) public func mainActorUnsafeMethod() {}
34+
35+
@OrLess(threshold: 12) public var twelveOrLess = 13
36+
37+
@IntBuilder public var intValue: Int { 1 }
38+
}
39+
40+
// SWIFTSOURCE: public struct TestStruct {
41+
// SWIFTSOURCE: @MainActor public func mainActorMethod()
42+
// SWIFTSOURCE: @MainActor public func mainActorUnsafeMethod()
43+
// SWIFTSOURCE: @MyModule.OrLess public var twelveOrLess: Int { get set }
44+
// SWIFTSOURCE: @MyModule.IntBuilder public var intValue: Int { get }
45+
// SWIFTSOURCE: }
46+
47+
// SWIFTMODULE: public struct TestStruct {
48+
// SWIFTMODULE: @MainActor public func mainActorMethod()
49+
// SWIFTMODULE: @MainActor public func mainActorUnsafeMethod()
50+
// SWIFTMODULE: @MyModule.OrLess public var twelveOrLess: Int
51+
// SWIFTMODULE: @MyModule.IntBuilder public var intValue: Int { get }
52+
// SWIFTMODULE: }
53+
54+
// DOCINFO: struct TestStruct {
55+
// DOCINFO: @MainActor func mainActorMethod()
56+
// DOCINFO: @MainActor func mainActorUnsafeMethod()
57+
// DOCINFO: @MyModule.OrLess var twelveOrLess: Int
58+
// DOCINFO: @MyModule.IntBuilder var intValue: Int { get }
59+
// DOCINFO: }

test/SourceKit/InterfaceGen/gen_objc_concurrency.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@
1111
// SWIFT-GEN-INTERFACE-NEXT: internal func foo(_ operation: String, completionHandler handler: @escaping (Int) -> Void)
1212
// SWIFT-GEN-INTERFACE: internal func foo(_ operation: String) async -> Int
1313

14+
// SWIFT-GEN-INTERFACE: @MainActor internal func mainActorMethod()
15+
16+
1417
// RUN: %sourcekitd-test -req=interface-gen -using-swift-args -header %S/../Inputs/concurrency/header_concurrency.h -- %s -Xfrontend -enable-objc-interop -import-objc-header %S/../Inputs/concurrency/header_concurrency.h -sdk %clang-importer-sdk | %FileCheck %s --check-prefix=OBJC-GEN-INTERFACE
1518

1619
// But don't print @available if it was implicitly added to an imported Clang decl (rdar://76685011).
1720
// OBJC-GEN-INTERFACE-LABEL: class ClassWithHandlerMethod {
1821
// OBJC-GEN-INTERFACE-NOT: @available
1922
// OBJC-GEN-INTERFACE: func method(withHandler operation: String!, completionHandler handler: (@Sendable (Int) -> Void)!)
2023
// OBJC-GEN-INTERFACE: func method(withHandler operation: String!) async -> Int
24+
25+
// OBJC-GEN-INTERFACE: @MainActor open func mainActorMethod()

test/attr/attributes.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,5 @@ enum E1 {
342342

343343
func foo() -> @_nonEphemeral UnsafeMutableRawPointer? { return nil } // expected-error {{attribute can only be applied to declarations, not types}}
344344
}
345+
346+
@_custom func testCustomAttribute() {} // expected-error {{unknown attribute '_custom'}}

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,12 @@ static bool reportModuleDocInfo(CompilerInvocation Invocation,
10861086
ASTContext &Ctx = CI.getASTContext();
10871087
registerIDERequestFunctions(Ctx.evaluator);
10881088

1089+
// Load implict imports so that Clang importer can use it.
1090+
for (auto unloadedImport :
1091+
CI.getMainModule()->getImplicitImportInfo().AdditionalUnloadedImports) {
1092+
(void)Ctx.getModule(unloadedImport.module.getModulePath());
1093+
}
1094+
10891095
SourceTextInfo IFaceInfo;
10901096
if (getModuleInterfaceInfo(Ctx, ModuleName, IFaceInfo))
10911097
return true;

0 commit comments

Comments
 (0)