]> granicus.if.org Git - clang/commitdiff
Handle CK_NullToPointer casts in -Wtype-safety properly. Fixes PR14249.
authorDmitri Gribenko <gribozavr@gmail.com>
Sat, 3 Nov 2012 16:07:49 +0000 (16:07 +0000)
committerDmitri Gribenko <gribozavr@gmail.com>
Sat, 3 Nov 2012 16:07:49 +0000 (16:07 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@167358 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/Sema/warn-type-safety-mpi-hdf5.c
test/Sema/warn-type-safety.c
test/Sema/warn-type-safety.cpp

index 6a39ff003c0c146041b182f1e332bb5f247d97d3..4a5e8e05c71a892e1f86ef916c1ff114257ec7bd 100644 (file)
@@ -6189,7 +6189,9 @@ void Sema::CheckArgumentWithTypeTag(const ArgumentWithTypeTagAttr *Attr,
   if (IsPointerAttr) {
     // Skip implicit cast of pointer to `void *' (as a function argument).
     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgumentExpr))
-      if (ICE->getType()->isVoidPointerType())
+      if (ICE->getType()->isVoidPointerType() &&
+          ICE->getCastKind() != CK_NullToPointer &&
+          ICE->getCastKind() != CK_NullToMemberPointer)
         ArgumentExpr = ICE->getSubExpr();
   }
   QualType ArgumentType = ArgumentExpr->getType();
index 9c2ee965866d9a23dcd710f86b227fd798bb194e..8c50cb24bb60aefbe527f96ebec4aa571fe3cb9a 100644 (file)
@@ -147,6 +147,10 @@ void test_mpi_predefined_types(
   // Layout-compatible scalar types.
   MPI_Send(int_buf,   1, MPI_INT); // no-warning
 
+  // Null pointer constant.
+  MPI_Send(0,         0, MPI_INT); // no-warning
+  MPI_Send(NULL,      0, MPI_INT); // no-warning
+
   // Layout-compatible class types.
   MPI_Send(pfi, 1, MPI_FLOAT_INT); // no-warning
   MPI_Send(pii, 1, MPI_2INT); // no-warning
index 6f548aa2567d6288ff3cc6623a7bce27cfaa17c8..4ac453d380bd792b976e97193e662d294ae4b24f 100644 (file)
@@ -80,6 +80,14 @@ void test_tag_mismatch(int *ptr)
   C_func(ptr, 20); // should warn, but may cause false positives
 }
 
+void test_null_pointer()
+{
+  C_func(0, C_tag); // no-warning
+  C_func((void *) 0, C_tag); // no-warning
+  C_func((int *) 0, C_tag); // no-warning
+  C_func((long *) 0, C_tag); // expected-warning {{argument type 'long *' doesn't match specified 'c' type tag that requires 'int *'}}
+}
+
 // Check that we look through typedefs in the special case of allowing 'char'
 // to be matched with 'signed char' or 'unsigned char'.
 void E_func(void *ptr, int tag) __attribute__(( pointer_with_type_tag(e,1,2) ));
index d053fbaa21fca1cbb7df64ef8d2ddc843a024f1b..a73a9d9d2acc6c996c2753dfb4e1666415ba4536 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
 
 typedef struct ompi_datatype_t *MPI_Datatype;
 
@@ -52,6 +52,8 @@ void test1(C *c, int *int_buf)
 {
   c->MPI_Send(int_buf, 1, MPI_INT); // no-warning
   c->MPI_Send(int_buf, 1, MPI_FLOAT); // expected-warning {{argument type 'int *' doesn't match specified 'mpi' type tag that requires 'float *'}}
+  c->MPI_Send(0, 0, MPI_INT); // no-warning
+  c->MPI_Send(nullptr, 0, MPI_INT); // no-warning
 
   OperatorIntStar i;
   c->MPI_Send(i, 1, MPI_INT); // no-warning