r347730 - PR39809: (const void*)0 is not a null pointer constant in C.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 27 22:25:06 PST 2018


Author: rsmith
Date: Tue Nov 27 22:25:06 2018
New Revision: 347730

URL: http://llvm.org/viewvc/llvm-project?rev=347730&view=rev
Log:
PR39809: (const void*)0 is not a null pointer constant in C.

Modified:
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/test/Sema/conditional.c

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=347730&r1=347729&r2=347730&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Tue Nov 27 22:25:06 2018
@@ -3441,20 +3441,20 @@ Expr::isNullPointerConstant(ASTContext &
       // Check that it is a cast to void*.
       if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
         QualType Pointee = PT->getPointeeType();
+        Qualifiers Qs = Pointee.getQualifiers();
         // Only (void*)0 or equivalent are treated as nullptr. If pointee type
         // has non-default address space it is not treated as nullptr.
         // (__generic void*)0 in OpenCL 2.0 should not be treated as nullptr
         // since it cannot be assigned to a pointer to constant address space.
-        bool PointeeHasDefaultAS =
-            Pointee.getAddressSpace() == LangAS::Default ||
-            (Ctx.getLangOpts().OpenCLVersion >= 200 &&
+        if ((Ctx.getLangOpts().OpenCLVersion >= 200 &&
              Pointee.getAddressSpace() == LangAS::opencl_generic) ||
             (Ctx.getLangOpts().OpenCL &&
              Ctx.getLangOpts().OpenCLVersion < 200 &&
-             Pointee.getAddressSpace() == LangAS::opencl_private);
+             Pointee.getAddressSpace() == LangAS::opencl_private))
+          Qs.removeAddressSpace();
 
-        if (PointeeHasDefaultAS && Pointee->isVoidType() && // to void*
-            CE->getSubExpr()->getType()->isIntegerType())   // from int.
+        if (Pointee->isVoidType() && Qs.empty() && // to void*
+            CE->getSubExpr()->getType()->isIntegerType()) // from int
           return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
       }
     }

Modified: cfe/trunk/test/Sema/conditional.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/conditional.c?rev=347730&r1=347729&r2=347730&view=diff
==============================================================================
--- cfe/trunk/test/Sema/conditional.c (original)
+++ cfe/trunk/test/Sema/conditional.c Tue Nov 27 22:25:06 2018
@@ -12,3 +12,10 @@ int _php_stream_free1() {
 int _php_stream_free2() {
   return (1 ? _efree(0) : free(0));  // expected-error {{returning 'void' from a function with incompatible result type 'int'}}
 }
+
+void pr39809() {
+  _Generic(0 ? (int const *)0 : (void *)0, int const *: (void)0);
+  _Generic(0 ? (int const *)0 : (void *)1, void const *: (void)0);
+  _Generic(0 ? (int volatile*)0 : (void const*)1, void volatile const*: (void)0);
+  _Generic(0 ? (int volatile*)0 : (void const*)0, void volatile const*: (void)0);
+}




More information about the cfe-commits mailing list