This is an archive of the discontinued LLVM Phabricator instance.

Sema: add support for `__attribute__((__swift_objc_members__))`
ClosedPublic

Authored by compnerd on Sep 9 2020, 10:07 AM.

Details

Summary

This adds the __swift_objc_members__ attribute to the semantic
analysis. It allows for annotating ObjC interfaces to provide Swift
semantics indicating that the types derived from this interface will be
back-bridged to Objective-C to allow interoperability with Objective-C
and Swift.

Diff Detail

Event Timeline

compnerd created this revision.Sep 9 2020, 10:07 AM
Herald added a project: Restricted Project. · View Herald Transcript
compnerd requested review of this revision.Sep 9 2020, 10:07 AM
gribozavr2 added inline comments.Sep 10 2020, 4:22 AM
clang/include/clang/Basic/AttrDocs.td
3481–3483
clang/test/SemaObjC/attr-swift_objc_members.m
5
20

Please also add a test for (erroneously) passing arguments to the attribute.

aaron.ballman added inline comments.Sep 10 2020, 10:57 AM
clang/include/clang/Basic/Attr.td
2133

Should this be inherited by redeclarations, or is that not a thing with ObjCInterfaceDecls?

clang/include/clang/Basic/AttrDocs.td
3482

The documentation is a bit hard to follow -- the user is going to be adding this attribute to an ObjC interface, so the docs should be talking about what that means from the ObjC side of things more so than the Swift side, no?

clang/lib/Sema/SemaDeclAttr.cpp
7540

Does it matter if the user writes this attribute on an interface that exposes no members? e.g., are there warnings we may want to give the user about using the attribute in a weird way? (Sorry if this should be obvious, but I don't have experience with Swift.)

clang/test/SemaObjC/attr-swift_objc_members.m
5

The typo fix makes sense to me, but for my own understanding, why switch to a string literal?

gribozavr2 added inline comments.Sep 10 2020, 12:56 PM
clang/include/clang/Basic/AttrDocs.td
3482

IIUC, this attribute has no effect on the Objective-C users of this type.

clang/test/SemaObjC/attr-swift_objc_members.m
5

IIUC, as it is now, the message is tokenized by the lexer -- and I think that's not the intent, none of these words are program code.

aaron.ballman added inline comments.Sep 10 2020, 1:06 PM
clang/include/clang/Basic/AttrDocs.td
3482

It may not have semantic impact on the ObjC side of things, but I'd still expect that since the user is writing it on an ObjC entity, the docs talk about it from that perspective.

e.g., I'd expect the documentation to be more along the lines of "The attribute indicates that the members of this Objective-C class/protocol/whatever, its subclasses and so on, will be exposed as Swift class members that do the awesome thing." instead of "In Swift, this other attribute does things similar to what this one does in Objective-C."

clang/test/SemaObjC/attr-swift_objc_members.m
5

Interesting and somewhat different from my understanding. My mental model for #error is that it "replays" the tokens into the diagnostic message up to the end of the line. Given that I prefer my diagnostics to be warning: you did the wrong thing and not warning: "you did the wrong thing" (with quotes), I usually leave the quotes off so that the error looks more consistent with other errors.

Neither form is more right than the other in this case, so I don't really care for this review (I was interested in it as a standards committee member who recently had to look at the specification for #error though).

gribozavr2 added inline comments.Sep 10 2020, 1:10 PM
clang/include/clang/Basic/AttrDocs.td
3482

Sure, how about this: "This attribute indicates that Swift subclasses and members of Swift extensions of this class will be implicitly marked with the `@objcMembers` Swift attribute."

(This attribute has no effect on this class itself or its members, even when imported in Swift -- only on Swift extensions & subclasses.)

aaron.ballman added inline comments.Sep 10 2020, 1:31 PM
clang/include/clang/Basic/AttrDocs.td
3482

That sounds better to me, thank you!

compnerd marked an inline comment as done.Sep 10 2020, 4:07 PM
compnerd added inline comments.
clang/include/clang/Basic/Attr.td
2133

Objective-C interfaces cannot be redeclared, so this actually is correct I believe.

clang/lib/Sema/SemaDeclAttr.cpp
7540

I think that the name is not exactly helpful in this case, but that ship has sailed since this is in production already. AIUI the attribute should apply to empty interfaces as it results in the Swift interface being exposed back to Objective-C.

clang/test/SemaObjC/attr-swift_objc_members.m
5

FWIW, the reason for the warning not being quoted currently is exactly what @aaron.ballman stated ... that is how I process the #error directive as well, and I tend to leave the quotes off to make the error match the other diagnostics. Is the quoting really that important?

gribozavr2 added inline comments.Sep 10 2020, 5:23 PM
clang/test/SemaObjC/attr-swift_objc_members.m
5

No, not at all important.

compnerd marked 14 inline comments as done.Sep 11 2020, 10:46 AM
compnerd updated this revision to Diff 291269.Sep 11 2020, 10:48 AM

address feedback from @aaron.ballman and @gribozavr2

This revision is now accepted and ready to land.Sep 11 2020, 3:35 PM
This revision was landed with ongoing or failed builds.Sep 14 2020, 8:36 AM
This revision was automatically updated to reflect the committed changes.