PR10837: Warn if a null pointer constant is formed by a zero integer constant
expression that is not a zero literal, in C. Patch by Ivan A. Kosarev!


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194540 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 3c6b3b0..7cd5670 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -10614,8 +10614,17 @@
 
   switch (ConvTy) {
   case Compatible:
-      DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr);
-      return false;
+    // See if a proper null pointer constant is to be assigned.
+    if (DstType->isAnyPointerType() && !SrcType->isAnyPointerType() &&
+        SrcExpr->isNullPointerConstant(Context,
+                                       Expr::NPC_NeverValueDependent) ==
+            Expr::NPCK_ZeroExpression &&
+        !isUnevaluatedContext())
+      Diag(SrcExpr->getExprLoc(), diag::warn_non_literal_null_pointer)
+        << DstType << SrcExpr->getSourceRange();
+
+    DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr);
+    return false;
 
   case PointerToInt:
     DiagKind = diag::ext_typecheck_convert_pointer_int;
diff --git a/test/Sema/warn-null.c b/test/Sema/warn-null.c
new file mode 100644
index 0000000..8ac8c5c
--- /dev/null
+++ b/test/Sema/warn-null.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 %s -verify -fsyntax-only
+
+// 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}}