This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by compnerd on Sep 11 2020, 10:57 AM.

Details

Summary

This extends semantic analysis of attributes for Swift interoperability
by introducing the swift_bridge attribute. This attribute enables
bridging Objective-C types to Swift specific types.

This is based on the work of the original changes in
https://github.com/llvm/llvm-project-staging/commit/8afaf3aad2af43cfedca7a24cd817848c4e95c0c

Diff Detail

Event Timeline

compnerd created this revision.Sep 11 2020, 10:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 11 2020, 10:57 AM
compnerd requested review of this revision.Sep 11 2020, 10:57 AM
aaron.ballman added inline comments.Sep 14 2020, 5:21 AM
clang/include/clang/Basic/Attr.td
2133

Is this a type or a declaration attribute? It looks like a declaration attribute based on the declaration and the list of subjects, but it looks like a type based on the ExpectedType diagnostic and the documentation. Or is this one of those unholy GNU attributes that's confused about what it appertains to?

Should this be inherited by redeclarations? Might be worth adding a test:

struct __attribute__((swift_bridge)) S;

struct S { // Should still have the attribute
  int i;
};
clang/include/clang/Basic/AttrDocs.td
3483

If this is a type attribute, should it be listed as a TypeAttr above?

clang/lib/Sema/SemaDeclAttr.cpp
5535

Can a type bridge across multiple types? e.g., could you say this bridges to "foo" and "bar"?

5536

Can drop the getAttrName() and just pass in AL directly.

clang/test/SemaObjC/attr-swift_bridged_typedef.m
11 ↗(On Diff #291272)

I'd appreciate a test that shows it diagnosing when given the wrong subject.

aaron.ballman added inline comments.Sep 14 2020, 5:30 AM
clang/include/clang/Basic/Attr.td
2137

Are you sure the ExpectedType is needed?

compnerd marked 4 inline comments as done.Sep 14 2020, 9:58 AM
compnerd added inline comments.
clang/lib/Sema/SemaDeclAttr.cpp
5535

No, I believe that you can not bridge to two different types.

compnerd updated this revision to Diff 291601.Sep 14 2020, 9:59 AM
compnerd marked an inline comment as done.

Address some feedback from @aaron.ballman

compnerd added inline comments.Sep 15 2020, 9:55 AM
clang/include/clang/Basic/Attr.td
2133

It is a declaration attribute, and yes, it should be inheritable.

aaron.ballman added inline comments.Sep 15 2020, 11:55 AM
clang/include/clang/Basic/Attr.td
2133

Okay, you should mark the attribute as an InheritableAttr above and a test.

2134

Is it intentional that this is swift_bridge but we just added swift_bridged_typedef (bridge vs bridged)? That seems like a bit of a spelling gotcha, if it can be corrected.

clang/include/clang/Basic/AttrDocs.td
3483

The documentation should probably also say that it is bridging the declarations rather than the types, just to be clear. Maybe something like: The swift_bridge attribute indicates that the declaration to which the attribute is applied is bridged to the named Swift type. ?

If not that formulation, you should at least change swift_bridged to be swift_bridge.

Adding a code example of how to use this properly would also be appreciated.

compnerd marked an inline comment as done.Sep 15 2020, 1:30 PM
compnerd added inline comments.
clang/include/clang/Basic/Attr.td
2134

Yes, that is intentional. This has already shipped, so Im afraid that changing the spelling isn't really reasonable at this point. I agree that it is is a spelling gotcha, but fortunately, most of the uses of these are done through macros (e.g. CF_REFINED_FOR_SWIFT).

compnerd marked 4 inline comments as done.Sep 15 2020, 1:44 PM
compnerd updated this revision to Diff 292018.Sep 15 2020, 1:48 PM

Address feedback

compnerd marked an inline comment as done.Sep 15 2020, 1:48 PM
aaron.ballman accepted this revision.Sep 15 2020, 2:19 PM

LGTM!

clang/include/clang/Basic/Attr.td
2134

Okie dokie, that's what I figured.

This revision is now accepted and ready to land.Sep 15 2020, 2:19 PM