[cfe-commits] r170931 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaChecking.cpp lib/Sema/SemaExpr.cpp test/SemaObjC/arc.m

Ted Kremenek kremenek at apple.com
Fri Dec 21 13:59:39 PST 2012


Author: kremenek
Date: Fri Dec 21 15:59:39 2012
New Revision: 170931

URL: http://llvm.org/viewvc/llvm-project?rev=170931&view=rev
Log:
Change checkUnsafeAssignLiteral() to use the new Sema::CheckLiteralKind().

Along the way, fix a bug in CheckLiteralKind(), previously in diagnoseObjCLiteralComparison, where we didn't ignore parentheses
in boxed expressions for purpose of classification.

In other words, both @42 and @(42) should be classified as numeric
literals.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaObjC/arc.m

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=170931&r1=170930&r2=170931&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Dec 21 15:59:39 2012
@@ -3690,7 +3690,7 @@
   "; object will be released after assignment">,
   InGroup<ARCUnsafeRetainedAssign>;
 def warn_arc_literal_assign : Warning<
-  "assigning %select{dictionary literal|array literal|block literal|boxed expression}0"
+  "assigning %select{block literal|array literal|dictionary literal|numeric literal|boxed expression}0"
   " to a weak %select{property|variable}1"
   "; object will be released after assignment">,
   InGroup<ARCUnsafeRetainedAssign>;

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=170931&r1=170930&r2=170931&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Fri Dec 21 15:59:39 2012
@@ -5753,33 +5753,24 @@
   // immediately zapped in a weak reference.  Note that we explicitly
   // allow ObjCStringLiterals, since those are designed to never really die.
   RHS = RHS->IgnoreParenImpCasts();
-  // This enum needs to match with the 'select' in warn_arc_literal_assign.
-  enum Kind { Dictionary = 0, Array, Block, BoxedE, None };
-  unsigned kind = None;
-  switch (RHS->getStmtClass()) {
-    default:
-      break;
-    case Stmt::ObjCDictionaryLiteralClass:
-      kind = Dictionary;
-      break;
-    case Stmt::ObjCArrayLiteralClass:
-      kind = Array;
-      break;
-    case Stmt::BlockExprClass:
-      kind = Block;
-      break;
-    case Stmt::ObjCBoxedExprClass:
-      kind = BoxedE;
-      break;
-  }
-  if (kind != None) {
-    S.Diag(Loc, diag::warn_arc_literal_assign)
-    << (unsigned) kind
+
+  // Classification for diagnostic.
+  unsigned SelectVal = /* block literal */ 0;
+  if (!isa<BlockExpr>(RHS)) {
+    // This enum needs to match with the 'select' in
+    // warn_objc_arc_literal_assign (off-by-1).
+    Sema::ObjCLiteralKind Kind = S.CheckLiteralKind(RHS);
+    if (Kind == Sema::LK_String || Kind == Sema::LK_None)
+      return false;
+    SelectVal = (unsigned) Kind + 1;
+  }
+
+  S.Diag(Loc, diag::warn_arc_literal_assign)
+    << SelectVal
     << (isProperty ? 0 : 1)
     << RHS->getSourceRange();
-    return true;
-  }
-  return false;
+
+  return true;
 }
 
 static bool checkUnsafeAssignObject(Sema &S, SourceLocation Loc,

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=170931&r1=170930&r2=170931&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Dec 21 15:59:39 2012
@@ -6875,7 +6875,7 @@
       // "dictionary literal"
       return LK_Dictionary;
     case Stmt::ObjCBoxedExprClass: {
-      Expr *Inner = cast<ObjCBoxedExpr>(FromE)->getSubExpr();
+      Expr *Inner = cast<ObjCBoxedExpr>(FromE)->getSubExpr()->IgnoreParens();
       switch (Inner->getStmtClass()) {
         case Stmt::IntegerLiteralClass:
         case Stmt::FloatingLiteralClass:

Modified: cfe/trunk/test/SemaObjC/arc.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/arc.m?rev=170931&r1=170930&r2=170931&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/arc.m (original)
+++ cfe/trunk/test/SemaObjC/arc.m Fri Dec 21 15:59:39 2012
@@ -740,11 +740,15 @@
     __weak id y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
     __weak id z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
     __weak id b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
-    __weak id e = @(42); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
+    __weak id n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
+    __weak id e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
+    __weak id m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
     
     // Assignments.
     y = @{ key : value }; // expected-warning {{assigning dictionary literal to a weak variable; object will be released after assignment}}
     z = @[ value ]; // expected-warning {{assigning array literal to a weak variable; object will be released after assignment}}
     b = ^() {}; // expected-warning {{assigning block literal to a weak variable; object will be released after assignment}}
-    e = @(42); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
+    n = @42; // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
+    e = @(42); // expected-warning {{assigning numeric literal to a weak variable; object will be released after assignment}}
+    m = @(41 + 1); // expected-warning {{assigning boxed expression to a weak variable; object will be released after assignment}}
 }





More information about the cfe-commits mailing list