From 3437daad71d8694d4ddd25418751f2763c0f8979 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Fri, 28 Sep 2012 22:21:42 +0000 Subject: [PATCH] -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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@164857 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 4 +++- lib/Sema/SemaExprObjC.cpp | 27 +++++++++++----------- test/SemaObjC/weak-receiver-warn.m | 26 ++++++++++----------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index c0afb10ef9..57c6907f62 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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>, 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 " diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index 58e28a9404..f3ae25ed48 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -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 diff --git a/test/SemaObjC/weak-receiver-warn.m b/test/SemaObjC/weak-receiver-warn.m index 547f0087bc..2a109dde5e 100644 --- a/test/SemaObjC/weak-receiver-warn.m +++ b/test/SemaObjC/weak-receiver-warn.m @@ -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 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}} } -- 2.40.0