[cfe-commits] r160379 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/SemaObjC/objc-literal-comparison.m

Jordan Rose jordan_rose at apple.com
Tue Jul 17 10:46:48 PDT 2012


Author: jrose
Date: Tue Jul 17 12:46:48 2012
New Revision: 160379

URL: http://llvm.org/viewvc/llvm-project?rev=160379&view=rev
Log:
-Wobjc-literal-compare: don't warn when comparing against nil.

Checks against nil often appear as guards in macros, and comparing
Objective-C literals to nil has well-defined behavior (if tautological).

On OS X, 'nil' has not been typed as 'id' since 10.6 (possibly earlier),
so the warning was already not firing, but other runtimes continue to use
((id)0) or some variant. This change accepts comparisons to any null pointer;
to keep it simple, it looks through all casts (not just casts to 'id').

PR13276

Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaObjC/objc-literal-comparison.m

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=160379&r1=160378&r2=160379&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Jul 17 12:46:48 2012
@@ -6742,10 +6742,24 @@
 static void diagnoseObjCLiteralComparison(Sema &S, SourceLocation Loc,
                                           ExprResult &LHS, ExprResult &RHS,
                                           BinaryOperator::Opcode Opc){
-  Expr *Literal = (isObjCObjectLiteral(LHS) ? LHS : RHS).get();
+  Expr *Literal;
+  Expr *Other;
+  if (isObjCObjectLiteral(LHS)) {
+    Literal = LHS.get();
+    Other = RHS.get();
+  } else {
+    Literal = RHS.get();
+    Other = LHS.get();
+  }
+
+  // Don't warn on comparisons against nil.
+  Other = Other->IgnoreParenCasts();
+  if (Other->isNullPointerConstant(S.getASTContext(),
+                                   Expr::NPC_ValueDependentIsNotNull))
+    return;
 
   // This should be kept in sync with warn_objc_literal_comparison.
-  // LK_String should always be last, since it has its own flag.
+  // LK_String should always be last, since it has its own warning flag.
   enum {
     LK_Array,
     LK_Dictionary,

Modified: cfe/trunk/test/SemaObjC/objc-literal-comparison.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/objc-literal-comparison.m?rev=160379&r1=160378&r2=160379&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/objc-literal-comparison.m (original)
+++ cfe/trunk/test/SemaObjC/objc-literal-comparison.m Tue Jul 17 12:46:48 2012
@@ -1,8 +1,10 @@
-// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare "-Dnil=((id)0)" -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare "-Dnil=(id)0" -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wno-everything -Wobjc-literal-compare "-Dnil=0" -verify %s
 
 // (test the warning flag as well)
 
-typedef unsigned char BOOL;
+typedef signed char BOOL;
 
 @interface BaseObject
 + (instancetype)new;
@@ -79,3 +81,16 @@
 
 #pragma clang diagnostic pop
 
+
+void testNilComparison() {
+  // Don't warn when comparing to nil in a macro.
+#define RETURN_IF_NIL(x) if (x == nil || nil == x) return
+  RETURN_IF_NIL(@"");
+  RETURN_IF_NIL(@1);
+  RETURN_IF_NIL(@1.0);
+  RETURN_IF_NIL(@[]);
+  RETURN_IF_NIL(@{});
+  RETURN_IF_NIL(@__objc_yes);
+  RETURN_IF_NIL(@(1+1));
+}
+





More information about the cfe-commits mailing list