Skip to content

Commit 2018942

Browse files
authored
Merge pull request #37975 from DougGregor/clang-swift-attr-for-modifiers-5.5
[Clang importer] Import Swift declaration modifiers from swift_attr.
2 parents 5c8daca + a4b6743 commit 2018942

File tree

7 files changed

+26
-9
lines changed

7 files changed

+26
-9
lines changed

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ NOTE(unresolvable_clang_decl_is_a_framework_bug,none,
7575
"please report this issue to the owners of '%0'",
7676
(StringRef))
7777

78-
WARNING(clang_swift_attr_without_at,none,
79-
"Swift attribute '%0' does not start with '@'", (StringRef))
78+
WARNING(clang_swift_attr_unhandled,none,
79+
"Ignoring unknown Swift attribute or modifier '%0'", (StringRef))
8080

8181
WARNING(implicit_bridging_header_imported_from_module,none,
8282
"implicit import of bridging header '%0' via module %1 "

include/swift/Parse/Parser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,8 @@ class Parser {
996996

997997
/// Parse the optional modifiers before a declaration.
998998
bool parseDeclModifierList(DeclAttributes &Attributes, SourceLoc &StaticLoc,
999-
StaticSpellingKind &StaticSpelling);
999+
StaticSpellingKind &StaticSpelling,
1000+
bool isFromClangAttribute = false);
10001001

10011002
/// Parse an availability attribute of the form
10021003
/// @available(*, introduced: 1.0, deprecated: 3.1).

lib/ClangImporter/ImportDecl.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8672,19 +8672,28 @@ void ClangImporter::Implementation::importAttributes(
86728672
// Prime the lexer.
86738673
parser.consumeTokenWithoutFeedingReceiver();
86748674

8675+
bool hadError = false;
86758676
SourceLoc atLoc;
86768677
if (parser.consumeIf(tok::at_sign, atLoc)) {
8677-
(void)parser.parseDeclAttribute(
8678+
hadError = parser.parseDeclAttribute(
86788679
MappedDecl->getAttrs(), atLoc, initContext,
8679-
/*isFromClangAttribute=*/true);
8680+
/*isFromClangAttribute=*/true).isError();
86808681
} else {
8681-
// Complain about the missing '@'.
8682+
SourceLoc staticLoc;
8683+
StaticSpellingKind staticSpelling;
8684+
hadError = parser.parseDeclModifierList(
8685+
MappedDecl->getAttrs(), staticLoc, staticSpelling,
8686+
/*isFromClangAttribute=*/true);
8687+
}
8688+
8689+
if (hadError) {
8690+
// Complain about the unhandled attribute or modifier.
86828691
auto &clangSrcMgr = getClangASTContext().getSourceManager();
86838692
ClangSourceBufferImporter &bufferImporter =
86848693
getBufferImporterForDiagnostics();
86858694
SourceLoc attrLoc = bufferImporter.resolveSourceLocation(
86868695
clangSrcMgr, swiftAttr->getLocation());
8687-
diagnose(attrLoc, diag::clang_swift_attr_without_at,
8696+
diagnose(attrLoc, diag::clang_swift_attr_unhandled,
86888697
swiftAttr->getAttribute());
86898698
}
86908699
continue;

lib/Parse/ParseDecl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,7 +3582,8 @@ ParserStatus Parser::parseDeclAttributeList(DeclAttributes &Attributes) {
35823582
// 'actor'
35833583
bool Parser::parseDeclModifierList(DeclAttributes &Attributes,
35843584
SourceLoc &StaticLoc,
3585-
StaticSpellingKind &StaticSpelling) {
3585+
StaticSpellingKind &StaticSpelling,
3586+
bool isFromClangAttribute) {
35863587
SyntaxParsingContext ListContext(SyntaxContext, SyntaxKind::ModifierList);
35873588
bool isError = false;
35883589
bool hasModifier = false;
@@ -3650,7 +3651,8 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes,
36503651

36513652
SyntaxParsingContext ModContext(SyntaxContext,
36523653
SyntaxKind::DeclModifier);
3653-
isError |= parseNewDeclAttribute(Attributes, /*AtLoc=*/{}, Kind);
3654+
isError |= parseNewDeclAttribute(
3655+
Attributes, /*AtLoc=*/{}, Kind, isFromClangAttribute);
36543656
hasModifier = true;
36553657
continue;
36563658
}

test/ClangImporter/objc_async.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ actor MySubclassCheckingSwiftAttributes : ProtocolWithSwiftAttributes {
121121
syncMethod() // expected-error{{ctor-isolated instance method 'syncMethod()' can not be referenced from a non-isolated context}}
122122
}
123123

124+
nonisolated func nonisolatedMethod() {
125+
}
126+
124127
@MainActor func mainActorMethod() {
125128
syncMethod() // expected-error{{actor-isolated instance method 'syncMethod()' can not be referenced from the main actor}}
126129
}

test/IDE/print_clang_objc_async.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import _Concurrency
9898

9999
// CHECK-LABEL: protocol ProtocolWithSwiftAttributes {
100100
// CHECK-NEXT: @actorIndependent func independentMethod()
101+
// CHECK-NEXT: nonisolated func nonisolatedMethod()
101102
// CHECK-NEXT: {{^}} @objc @MainActor func mainActorMethod()
102103
// CHECK-NEXT: {{^}} @objc @MainActor func uiActorMethod()
103104
// CHECK-NEXT: {{^}} @objc optional func missingAtAttributeMethod()

test/Inputs/clang-importer-sdk/usr/include/ObjCConcurrency.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ typedef void (^CompletionHandler)(NSString * _Nullable, NSString * _Nullable_res
9696

9797
@protocol ProtocolWithSwiftAttributes
9898
-(void)independentMethod __attribute__((__swift_attr__("@actorIndependent")));
99+
-(void)nonisolatedMethod __attribute__((__swift_attr__("nonisolated")));
99100
-(void)mainActorMethod __attribute__((__swift_attr__("@MainActor")));
100101
-(void)uiActorMethod __attribute__((__swift_attr__("@UIActor")));
101102

0 commit comments

Comments
 (0)