Skip to content

[Clang importer] Import Swift declaration modifiers from swift_attr. #37970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsClangImporter.def
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ NOTE(unresolvable_clang_decl_is_a_framework_bug,none,
"please report this issue to the owners of '%0'",
(StringRef))

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

WARNING(implicit_bridging_header_imported_from_module,none,
"implicit import of bridging header '%0' via module %1 "
Expand Down
3 changes: 2 additions & 1 deletion include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,8 @@ class Parser {

/// Parse the optional modifiers before a declaration.
bool parseDeclModifierList(DeclAttributes &Attributes, SourceLoc &StaticLoc,
StaticSpellingKind &StaticSpelling);
StaticSpellingKind &StaticSpelling,
bool isFromClangAttribute = false);

/// Parse an availability attribute of the form
/// @available(*, introduced: 1.0, deprecated: 3.1).
Expand Down
17 changes: 13 additions & 4 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8728,19 +8728,28 @@ void ClangImporter::Implementation::importAttributes(
// Prime the lexer.
parser.consumeTokenWithoutFeedingReceiver();

bool hadError = false;
SourceLoc atLoc;
if (parser.consumeIf(tok::at_sign, atLoc)) {
(void)parser.parseDeclAttribute(
hadError = parser.parseDeclAttribute(
MappedDecl->getAttrs(), atLoc, initContext,
/*isFromClangAttribute=*/true);
/*isFromClangAttribute=*/true).isError();
} else {
// Complain about the missing '@'.
SourceLoc staticLoc;
StaticSpellingKind staticSpelling;
hadError = parser.parseDeclModifierList(
MappedDecl->getAttrs(), staticLoc, staticSpelling,
/*isFromClangAttribute=*/true);
}

if (hadError) {
// Complain about the unhandled attribute or modifier.
auto &clangSrcMgr = getClangASTContext().getSourceManager();
ClangSourceBufferImporter &bufferImporter =
getBufferImporterForDiagnostics();
SourceLoc attrLoc = bufferImporter.resolveSourceLocation(
clangSrcMgr, swiftAttr->getLocation());
diagnose(attrLoc, diag::clang_swift_attr_without_at,
diagnose(attrLoc, diag::clang_swift_attr_unhandled,
swiftAttr->getAttribute());
}
continue;
Expand Down
6 changes: 4 additions & 2 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3554,7 +3554,8 @@ ParserStatus Parser::parseDeclAttributeList(DeclAttributes &Attributes) {
// 'distributed'
bool Parser::parseDeclModifierList(DeclAttributes &Attributes,
SourceLoc &StaticLoc,
StaticSpellingKind &StaticSpelling) {
StaticSpellingKind &StaticSpelling,
bool isFromClangAttribute) {
SyntaxParsingContext ListContext(SyntaxContext, SyntaxKind::ModifierList);
bool isError = false;
bool hasModifier = false;
Expand Down Expand Up @@ -3622,7 +3623,8 @@ bool Parser::parseDeclModifierList(DeclAttributes &Attributes,

SyntaxParsingContext ModContext(SyntaxContext,
SyntaxKind::DeclModifier);
isError |= parseNewDeclAttribute(Attributes, /*AtLoc=*/{}, Kind);
isError |= parseNewDeclAttribute(
Attributes, /*AtLoc=*/{}, Kind, isFromClangAttribute);
hasModifier = true;
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions test/ClangImporter/objc_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ actor MySubclassCheckingSwiftAttributes : ProtocolWithSwiftAttributes {
syncMethod() // expected-error{{ctor-isolated instance method 'syncMethod()' can not be referenced from a non-isolated context}}
}

nonisolated func nonisolatedMethod() {
}

@MainActor func mainActorMethod() {
syncMethod() // expected-error{{actor-isolated instance method 'syncMethod()' can not be referenced from the main actor}}
}
Expand Down
1 change: 1 addition & 0 deletions test/IDE/print_clang_objc_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import _Concurrency

// CHECK-LABEL: protocol ProtocolWithSwiftAttributes {
// CHECK-NEXT: nonisolated func independentMethod()
// CHECK-NEXT: nonisolated func nonisolatedMethod()
// CHECK-NEXT: {{^}} @objc @MainActor func mainActorMethod()
// CHECK-NEXT: {{^}} @objc @MainActor func uiActorMethod()
// CHECK-NEXT: {{^}} @objc optional func missingAtAttributeMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ typedef void (^CompletionHandler)(NSString * _Nullable, NSString * _Nullable_res

@protocol ProtocolWithSwiftAttributes
-(void)independentMethod __attribute__((__swift_attr__("@actorIndependent")));
-(void)nonisolatedMethod __attribute__((__swift_attr__("nonisolated")));
-(void)mainActorMethod __attribute__((__swift_attr__("@MainActor")));
-(void)uiActorMethod __attribute__((__swift_attr__("@UIActor")));

Expand Down