r195303 - PR10837: Warn if a null pointer constant is formed by a zero integer constant

Richard Smith richard-llvm at metafoo.co.uk
Wed Nov 20 17:53:03 PST 2013


Author: rsmith
Date: Wed Nov 20 19:53:02 2013
New Revision: 195303

URL: http://llvm.org/viewvc/llvm-project?rev=195303&view=rev
Log:
PR10837: Warn if a null pointer constant is formed by a zero integer constant
expression that is not a zero literal, in C. This is a different, and more
targeted, approach than that in r194540.

Added:
    cfe/trunk/test/Sema/warn-null.c
Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaTemplate/dependent-expr.cpp

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=195303&r1=195302&r2=195303&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Nov 20 19:53:02 2013
@@ -6565,12 +6565,14 @@ Sema::CheckSingleAssignmentConstraints(Q
 
   // C99 6.5.16.1p1: the left operand is a pointer and the right is
   // a null pointer constant.
-  if ((LHSType->isPointerType() ||
-       LHSType->isObjCObjectPointerType() ||
-       LHSType->isBlockPointerType())
-      && RHS.get()->isNullPointerConstant(Context,
-                                          Expr::NPC_ValueDependentIsNull)) {
-    RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer);
+  if ((LHSType->isPointerType() || LHSType->isObjCObjectPointerType() ||
+       LHSType->isBlockPointerType()) &&
+      RHS.get()->isNullPointerConstant(Context,
+                                       Expr::NPC_ValueDependentIsNull)) {
+    CastKind Kind;
+    CXXCastPath Path;
+    CheckPointerConversion(RHS.get(), LHSType, Kind, Path, false);
+    RHS = ImpCastExprToType(RHS.take(), LHSType, Kind, VK_RValue, &Path);
     return Compatible;
   }
 

Added: cfe/trunk/test/Sema/warn-null.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-null.c?rev=195303&view=auto
==============================================================================
--- cfe/trunk/test/Sema/warn-null.c (added)
+++ cfe/trunk/test/Sema/warn-null.c Wed Nov 20 19:53:02 2013
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 %s -verify
+
+// PR10837: Warn if a non-pointer-typed expression is folded to a null pointer
+int *p = 0;
+int *q = '\0'; // expected-warning{{expression which evaluates to zero treated as a null pointer constant}}
+int *r = (1 - 1); // expected-warning{{expression which evaluates to zero treated as a null pointer constant}}
+void f() {
+  p = 0;
+  q = '\0'; // expected-warning{{expression which evaluates to zero treated as a null pointer constant}}
+  r = 1 - 1; // expected-warning{{expression which evaluates to zero treated as a null pointer constant}}
+}

Modified: cfe/trunk/test/SemaTemplate/dependent-expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/dependent-expr.cpp?rev=195303&r1=195302&r2=195303&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/dependent-expr.cpp (original)
+++ cfe/trunk/test/SemaTemplate/dependent-expr.cpp Wed Nov 20 19:53:02 2013
@@ -79,3 +79,17 @@ template<typename T> struct CastDependen
     return ((void*)(((unsigned long)(x)|0x1ul)));
   }
 };
+
+// Regression test for crasher in r194540.
+namespace PR10837 {
+  typedef void t(int);
+  template<typename> struct A {
+    void f();
+    static t g;
+  };
+  t *p;
+  template<typename T> void A<T>::f() {
+    p = g;
+  }
+  template struct A<int>;
+}





More information about the cfe-commits mailing list