From 5ac744e006f843ac624387c0d3c43ae7068a84a2 Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Sat, 3 Nov 2012 16:07:49 +0000 Subject: [PATCH] Handle CK_NullToPointer casts in -Wtype-safety properly. Fixes PR14249. llvm-svn: 167358 --- clang/lib/Sema/SemaChecking.cpp | 4 +++- clang/test/Sema/warn-type-safety-mpi-hdf5.c | 4 ++++ clang/test/Sema/warn-type-safety.c | 8 ++++++++ clang/test/Sema/warn-type-safety.cpp | 4 +++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 6a39ff0..4a5e8e0 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -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(ArgumentExpr)) - if (ICE->getType()->isVoidPointerType()) + if (ICE->getType()->isVoidPointerType() && + ICE->getCastKind() != CK_NullToPointer && + ICE->getCastKind() != CK_NullToMemberPointer) ArgumentExpr = ICE->getSubExpr(); } QualType ArgumentType = ArgumentExpr->getType(); diff --git a/clang/test/Sema/warn-type-safety-mpi-hdf5.c b/clang/test/Sema/warn-type-safety-mpi-hdf5.c index 9c2ee96..8c50cb2 100644 --- a/clang/test/Sema/warn-type-safety-mpi-hdf5.c +++ b/clang/test/Sema/warn-type-safety-mpi-hdf5.c @@ -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 diff --git a/clang/test/Sema/warn-type-safety.c b/clang/test/Sema/warn-type-safety.c index 6f548aa..4ac453d 100644 --- a/clang/test/Sema/warn-type-safety.c +++ b/clang/test/Sema/warn-type-safety.c @@ -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) )); diff --git a/clang/test/Sema/warn-type-safety.cpp b/clang/test/Sema/warn-type-safety.cpp index d053fba..a73a9d9 100644 --- a/clang/test/Sema/warn-type-safety.cpp +++ b/clang/test/Sema/warn-type-safety.cpp @@ -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 -- 2.7.4