]> granicus.if.org Git - clang/commitdiff
Move the null pointer constant check from Sema::CheckSingleInitializer/ActOnCallExpr...
authorSteve Naroff <snaroff@apple.com>
Tue, 27 Nov 2007 17:58:44 +0000 (17:58 +0000)
committerSteve Naroff <snaroff@apple.com>
Tue, 27 Nov 2007 17:58:44 +0000 (17:58 +0000)
Thanks to Seo Sanghyeon for the bug, follow-through, and patch!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44366 91177308-0d34-0410-b5e6-96231b3b80d8

Sema/SemaDecl.cpp
Sema/SemaExpr.cpp
Sema/SemaStmt.cpp
test/Sema/assign-null.c [new file with mode: 0644]

index e50fe1101af4697899ae88944de8aeb2a9077725..8ecbd7cb525a5a9cf9caa9115c1ee58695165df4 100644 (file)
@@ -391,13 +391,9 @@ bool Sema::CheckSingleInitializer(Expr *&Init, bool isStatic,
          Init->getSourceRange());
     return true;
   case PointerFromInt:
-    // check for null pointer constant (C99 6.3.2.3p3)
-    if (!Init->isNullPointerConstant(Context)) {
-      Diag(loc, diag::ext_typecheck_assign_pointer_int,
-           DeclType.getAsString(), rhsType.getAsString(), 
-           Init->getSourceRange());
-      return true;
-    }
+    Diag(loc, diag::ext_typecheck_assign_pointer_int,
+         DeclType.getAsString(), rhsType.getAsString(), 
+         Init->getSourceRange());
     break;
   case IntFromPointer: 
     Diag(loc, diag::ext_typecheck_assign_pointer_int, 
index 2cea607343e41eb2cedaf7c88040f4ad50c06adf..14302aab08275593d741e43f6d33c73b6bc0a638 100644 (file)
@@ -613,12 +613,9 @@ ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
       case Compatible:
         break;
       case PointerFromInt:
-        // check for null pointer constant (C99 6.3.2.3p3)
-        if (!argExpr->isNullPointerConstant(Context)) {
-          Diag(l, diag::ext_typecheck_passing_pointer_int, 
-               lhsType.getAsString(), rhsType.getAsString(),
-               Fn->getSourceRange(), argExpr->getSourceRange());
-        }
+        Diag(l, diag::ext_typecheck_passing_pointer_int, 
+             lhsType.getAsString(), rhsType.getAsString(),
+             Fn->getSourceRange(), argExpr->getSourceRange());
         break;
       case IntFromPointer:
         Diag(l, diag::ext_typecheck_passing_pointer_int, 
@@ -1125,6 +1122,12 @@ Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
 
 Sema::AssignmentCheckResult
 Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
+  // C99 6.5.16.1p1: the left operand is a pointer and the right is
+  // a null pointer constant.
+  if (lhsType->isPointerType() && rExpr->isNullPointerConstant(Context)) {
+    promoteExprToType(rExpr, lhsType);
+    return Compatible;
+  }
   // This check seems unnatural, however it is necessary to ensure the proper
   // conversion of functions/arrays. If the conversion were done for all
   // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
@@ -1412,12 +1415,9 @@ inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
     hadError = true;
     break;
   case PointerFromInt:
-    // check for null pointer constant (C99 6.3.2.3p3)
-    if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) {
-      Diag(loc, diag::ext_typecheck_assign_pointer_int,
-           lhsType.getAsString(), rhsType.getAsString(),
-           lex->getSourceRange(), rex->getSourceRange());
-    }
+    Diag(loc, diag::ext_typecheck_assign_pointer_int,
+         lhsType.getAsString(), rhsType.getAsString(),
+         lex->getSourceRange(), rex->getSourceRange());
     break;
   case IntFromPointer: 
     Diag(loc, diag::ext_typecheck_assign_pointer_int, 
@@ -2067,12 +2067,9 @@ bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
     case Compatible:
       break;
     case PointerFromInt:
-      // check for null pointer constant (C99 6.3.2.3p3)
-      if (!argExpr->isNullPointerConstant(Context)) {
-        Diag(l, diag::ext_typecheck_sending_pointer_int, 
-             lhsType.getAsString(), rhsType.getAsString(),
-             argExpr->getSourceRange());
-      }
+      Diag(l, diag::ext_typecheck_sending_pointer_int, 
+           lhsType.getAsString(), rhsType.getAsString(),
+           argExpr->getSourceRange());
       break;
     case IntFromPointer:
       Diag(l, diag::ext_typecheck_sending_pointer_int, 
index d051f6b333eb6fbf70877b4272150d43d76c1b34..d00501ae0a2c1d686861fa59ceb1093e77df4ee5 100644 (file)
@@ -617,12 +617,9 @@ Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) {
          RetValExp->getSourceRange());
     break;
   case PointerFromInt:
-    // check for null pointer constant (C99 6.3.2.3p3)
-    if (!RetValExp->isNullPointerConstant(Context)) {
-      Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
-           lhsType.getAsString(), rhsType.getAsString(),
-           RetValExp->getSourceRange());
-    }
+    Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
+         lhsType.getAsString(), rhsType.getAsString(),
+         RetValExp->getSourceRange());
     break;
   case IntFromPointer:
     Diag(ReturnLoc, diag::ext_typecheck_return_pointer_int,
diff --git a/test/Sema/assign-null.c b/test/Sema/assign-null.c
new file mode 100644 (file)
index 0000000..6972d90
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: clang -fsyntax-only -verify %s
+
+#include <stddef.h>
+
+typedef void (*hookfunc)(void *arg);
+hookfunc hook;
+
+void clear_hook() {
+  hook = NULL;
+}