This is an archive of the discontinued LLVM Phabricator instance.

[Objective-C] Fix "repeated use of weak" warning with -fobjc-weak
ClosedPublic

Authored by bkelley on Mar 15 2017, 3:35 PM.

Details

Summary

-Warc-repeated-use-of-weak should produce the same warnings with -fobjc-weak as it does with -objc-arc. Also check for ObjCWeak along with ObjCAutoRefCount when recording the use of an evaluated weak variable. Add a -fobjc-weak run to the existing arc-repeated-weak test case and adapt it slightly to work in both modes.

Diff Detail

Event Timeline

jordan_rose edited edge metadata.
jordan_rose added a subscriber: rjmccall.

The warning-related parts look reasonable to me.

lib/Sema/SemaDecl.cpp
10184

This condition's getting complicated, and it shows up in a few places. Would it make sense to factor it out?

lib/Sema/SemaExpr.cpp
706–707

This is the one section that isn't dealing with the warning. I'd like @rjmccall to verify that it's correct.

10336

Does ObjCAutoRefCount imply ObjCWeak? If so, you could just use the latter.

bkelley added inline comments.Mar 15 2017, 4:10 PM
lib/Sema/SemaDecl.cpp
10184

What do you think about adding a member function like hasMRCNonTrivialWeakObjCLifetime(const ASTContext &Context) to QualType to factor out lines 10183-10184? We could use that in D31003, D31004, here, and D31007.

lib/Sema/SemaExpr.cpp
10336

I don't believe so. For Snow Leopard, ARC without weak references was supported so they can be independent.

jordan_rose added inline comments.Mar 15 2017, 5:05 PM
lib/Sema/SemaDecl.cpp
10184

I'm fine with it myself but I don't work on Clang very much anymore. Maybe someone else can say whether it's actually a good idea.

(By the way, the conventional abbreviation for this mode is "MRR" for "Manual Retain/Release", even though it's "ARC" and "Automated Reference Counting".)

lib/Sema/SemaExpr.cpp
10336

Sure, but in that case we don't need the warning, right?

arphaman added inline comments.
lib/Sema/SemaDecl.cpp
10184

Do you want to extract the out the entire

(!getLangOpts().ObjCAutoRefCount && getLangOpts().ObjCWeak &&
          VDecl->getType().getObjCLifetime() != Qualifiers::OCL_Weak)

?

It looks like the others patches use only getLangOpts().ObjCWeak && Type.getObjCLifetime() != Qualifiers::OCL_Weak so the use of hasMRCNonTrivialWeakObjCLifetime won't be equivalent to the original code in the other patches if you extract all code from lines 10183-10184.

test/SemaObjC/arc-repeated-weak.mm
468

NIT: You can keep the code in foo1 active and just use the #if on the expected-error, like:

void foo1() {
  INTFPtrTy tmp = (INTFPtrTy)e1; 
#if __has_feature(objc_arc)
// expected-error@-2 {{cast of 'E' to 'INTFPtrTy' (aka 'INTF *') is disallowed with ARC}}
#endif
}
bkelley marked 3 inline comments as done.Mar 17 2017, 4:00 PM
bkelley added inline comments.
lib/Sema/SemaDecl.cpp
10184

Yeah, my suspicion was that the addition of !getLangOpts().ObjCAutoRefCount() would have been fine, but most of the other code was simplified by using hasNonTrivialObjCLifetime() or another means, so this new function seems to only be necessary in this patch. I misnamed the proposed function, which would imply the qualifier is OCL_Weak, but we need not that, so my new proposed name is the odd looking isNonWeakInMRRWithObjCWeak().

lib/Sema/SemaExpr.cpp
10336

Oh, I see. Yeah, looks like I can update most of the checks to just use ObjCWeak. I think we need both conditions here, however, since checkUnsafeExprAssigns() emits the "assigning retained object to unsafe property" warning, which is only applicable in ARC.

bkelley updated this revision to Diff 92218.Mar 17 2017, 4:02 PM

Updated with feedback from @jordan_rose and @arphaman

Thanks again for the feedback. Is there anything further I should update in this diff or is it looking good?

Thanks!
Brian

rjmccall edited edge metadata.Mar 24 2017, 3:34 PM

Just a couple of minor requests.

lib/Sema/SemaExpr.cpp
707

Much like the other patches, it's probably more efficient to just check the qualifier here instead of testing the language option first.

2515–2516

Same thing.

lib/Sema/SemaExprMember.cpp
1504–1505

Same.

lib/Sema/SemaPseudoObject.cpp
843–844

Same.

bkelley updated this revision to Diff 93023.Mar 24 2017, 5:26 PM
bkelley marked 4 inline comments as done.

Updated with feedback from @rjmccall

Sorry for missing the unnecessary LangOpts checks here. Thanks again for the feedback!

rjmccall accepted this revision.Mar 25 2017, 11:22 AM

LGTM, thanks.

This revision is now accepted and ready to land.Mar 25 2017, 11:22 AM

Thank you @rjmccall for the approval. I don't have commit access; would someone be willing to commit this path for me please? Thanks!

bkelley closed this revision.Mar 29 2017, 11:07 AM