This is an archive of the discontinued LLVM Phabricator instance.

[SemaObjC] Fix a -Wbridge-cast false-positive
ClosedPublic

Authored by erik.pilkington on Mar 31 2021, 7:27 AM.

Details

Summary

Clang used to emit a bad -Wbridge-cast diagnostic on the cast in the attached test. This was because, after 09abecef7, struct __CFString was not added to lookup, so the objc_bridge attribute wasn't getting duplicated onto the most recent declaration, causing us to fail to find it in getObjCBridgeAttr. This patch fixes this by instead walking through the redeclarations to find an appropriate bridge attribute.

rdar://72823399

Diff Detail

Event Timeline

erik.pilkington requested review of this revision.Mar 31 2021, 7:27 AM
erik.pilkington created this revision.

I see a warning when I compile the following (contrived) example:

typedef const struct __attribute__((objc_bridge(NSError))) __CFString * CFStringRef;

extern "C" {
  typedef const struct __attribute__((objc_bridge(NSString))) __CFString * CFStringRef;
}

@interface NSString @end
@interface NSError @end

void CFStringGetLength(CFStringRef theString);

int main() {
  CFStringGetLength((__bridge CFStringRef)(NSString *)0);
}

Should we try to restore the behavior prior to https://reviews.llvm.org/rG09abecef7bbfda18d34f046954eaa4d491062839 as much as we can? Or that's not important?

Should we try to restore the behavior prior to https://reviews.llvm.org/rG09abecef7bbfda18d34f046954eaa4d491062839 as much as we can? Or that's not important?

Sure, the new patch starts iterating through the redeclarations starting at the most recent declaration, which should mimic the old behaviour. I'm not sure it matters, having multiple objc_bridge attributes has never actually worked (objc_bridge attributes appearing earlier get "shadowed" by the most recent one), i.e. in your testcase CFStringGetLength((__bridge CFStringRef)(NSError *)0); has always lead to a -Wbridge-cast diagnostic.

ahatanak accepted this revision.Apr 1 2021, 4:55 PM

I agree with you. If it's not going to work, maybe there should have been a warning.

This revision is now accepted and ready to land.Apr 1 2021, 4:55 PM
This revision was landed with ongoing or failed builds.Apr 5 2021, 8:43 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptApr 5 2021, 8:43 AM