-Wreceiver-is-weak: rephrase warning text and add a suggestion Note.

New output:
  warning: weak property may be unpredictably set to nil
  note: property declared here
  note: assign the value to a strong variable to keep the object alive
        during use

<rdar://problem/12277204>

llvm-svn: 164857
This commit is contained in:
Jordan Rose 2012-09-28 22:21:42 +00:00
parent 62b379873d
commit 13d6b71929
3 changed files with 30 additions and 27 deletions

View File

@ -707,8 +707,10 @@ def err_gc_weak_property_strong_type : Error<
"weak attribute declared on a __strong type property in GC mode">;
def warn_receiver_is_weak : Warning <
"weak %select{receiver|property|implicit property}0 may be "
"unpredictably null in ARC mode">,
"unpredictably set to nil">,
InGroup<DiagGroup<"receiver-is-weak">>, DefaultIgnore;
def note_arc_assign_to_strong : Note<
"assign the value to a strong variable to keep the object alive during use">;
def warn_arc_repeated_use_of_weak : Warning <
"weak %select{variable|property|implicit property|instance variable}0 %1 is "
"accessed multiple times in this %select{function|method|block|lambda}2 "

View File

@ -1341,21 +1341,22 @@ static void DiagnoseARCUseOfWeakReceiver(Sema &S, Expr *Receiver) {
}
}
if (T.getObjCLifetime() == Qualifiers::OCL_Weak) {
S.Diag(Loc, diag::warn_receiver_is_weak)
<< ((!PDecl && !GDecl) ? 0 : (PDecl ? 1 : 2));
if (PDecl)
S.Diag(PDecl->getLocation(), diag::note_property_declare);
else if (GDecl)
S.Diag(GDecl->getLocation(), diag::note_method_declared_at) << GDecl;
return;
if (T.getObjCLifetime() != Qualifiers::OCL_Weak) {
if (!PDecl)
return;
if (!(PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak))
return;
}
if (PDecl &&
(PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)) {
S.Diag(Loc, diag::warn_receiver_is_weak) << 1;
S.Diag(Loc, diag::warn_receiver_is_weak)
<< ((!PDecl && !GDecl) ? 0 : (PDecl ? 1 : 2));
if (PDecl)
S.Diag(PDecl->getLocation(), diag::note_property_declare);
}
else if (GDecl)
S.Diag(GDecl->getLocation(), diag::note_method_declared_at) << GDecl;
S.Diag(Loc, diag::note_arc_assign_to_strong);
}
/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an

View File

@ -9,13 +9,13 @@
void test0(Test0 *x) {
__weak Test0 *weakx = x;
[x addBlock: ^{ [weakx actNow]; }]; // expected-warning {{weak receiver may be unpredictably null in ARC mode}}
[x setBlock: ^{ [weakx actNow]; }]; // expected-warning {{weak receiver may be unpredictably null in ARC mode}}
x.block = ^{ [weakx actNow]; }; // expected-warning {{weak receiver may be unpredictably null in ARC mode}}
[x addBlock: ^{ [weakx actNow]; }]; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
[x setBlock: ^{ [weakx actNow]; }]; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
x.block = ^{ [weakx actNow]; }; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
[weakx addBlock: ^{ [x actNow]; }]; // expected-warning {{weak receiver may be unpredictably null in ARC mode}}
[weakx setBlock: ^{ [x actNow]; }]; // expected-warning {{weak receiver may be unpredictably null in ARC mode}}
weakx.block = ^{ [x actNow]; }; // expected-warning {{weak receiver may be unpredictably null in ARC mode}}
[weakx addBlock: ^{ [x actNow]; }]; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
[weakx setBlock: ^{ [x actNow]; }]; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
weakx.block = ^{ [x actNow]; }; // expected-warning {{weak receiver may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
}
@interface Test
@ -36,12 +36,12 @@ void test0(Test0 *x) {
if (self.weak_atomic_prop) {
self.weak_atomic_prop = 0;
}
[self.weak_prop Meth]; // expected-warning {{weak property may be unpredictably null in ARC mode}}
[self.weak_prop Meth]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
id pi = self.P;
[self.weak_atomic_prop Meth]; // expected-warning {{weak property may be unpredictably null in ARC mode}}
[self.weak_atomic_prop Meth]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
[self.P Meth]; // expected-warning {{weak implicit property may be unpredictably null in ARC mode}}
[self.P Meth]; // expected-warning {{weak implicit property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
}
- (__weak id) P { return 0; }
@ -60,9 +60,9 @@ void test0(Test0 *x) {
- (void)doSomething
{
[[self parent] doSomething]; // expected-warning {{weak property may be unpredictably null in ARC mode}}
[[self parent] doSomething]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
(void)self.parent.doSomething; // expected-warning {{weak property may be unpredictably null in ARC mode}}
(void)self.parent.doSomething; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
}
@end
@ -74,7 +74,7 @@ void test0(Test0 *x) {
@end
void testProtocol(id <MyProtocol> input) {
[[input object] Meth]; // expected-warning {{weak property may be unpredictably null in ARC mode}}
[input.object Meth]; // expected-warning {{weak property may be unpredictably null in ARC mode}}
[[input object] Meth]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
[input.object Meth]; // expected-warning {{weak property may be unpredictably set to nil}} expected-note {{assign the value to a strong variable to keep the object alive during use}}
}