diff options
author | Bill Wendling <isanbard@gmail.com> | 2013-11-27 05:27:22 +0000 |
---|---|---|
committer | Bill Wendling <isanbard@gmail.com> | 2013-11-27 05:27:22 +0000 |
commit | 45c2eed19c012d8b5e686aedb28cdbb36347fee5 (patch) | |
tree | 60d412adddaad4273fc4eae856d6ec77f240b3f8 | |
parent | abe9b1957c57392cd9b568fe1e893bf8755e288b (diff) | |
download | clang-45c2eed19c012d8b5e686aedb28cdbb36347fee5.tar.gz clang-45c2eed19c012d8b5e686aedb28cdbb36347fee5.tar.bz2 clang-45c2eed19c012d8b5e686aedb28cdbb36347fee5.tar.xz |
Merging r195303:
------------------------------------------------------------------------
r195303 | rsmith | 2013-11-20 17:53:02 -0800 (Wed, 20 Nov 2013) | 4 lines
PR10837: Warn if a null pointer constant is formed by a zero integer constant
expression that is not a zero literal, in C. This is a different, and more
targeted, approach than that in r194540.
------------------------------------------------------------------------
git-svn-id: https://llvm.org/svn/llvm-project/cfe/branches/release_34@195815 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 14 | ||||
-rw-r--r-- | test/Sema/warn-null.c | 11 | ||||
-rw-r--r-- | test/SemaTemplate/dependent-expr.cpp | 14 |
3 files changed, 33 insertions, 6 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 22242a5acc..164deedd57 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -6564,12 +6564,14 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS, // C99 6.5.16.1p1: the left operand is a pointer and the right is // a null pointer constant. - if ((LHSType->isPointerType() || - LHSType->isObjCObjectPointerType() || - LHSType->isBlockPointerType()) - && RHS.get()->isNullPointerConstant(Context, - Expr::NPC_ValueDependentIsNull)) { - RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); + if ((LHSType->isPointerType() || LHSType->isObjCObjectPointerType() || + LHSType->isBlockPointerType()) && + RHS.get()->isNullPointerConstant(Context, + Expr::NPC_ValueDependentIsNull)) { + CastKind Kind; + CXXCastPath Path; + CheckPointerConversion(RHS.get(), LHSType, Kind, Path, false); + RHS = ImpCastExprToType(RHS.take(), LHSType, Kind, VK_RValue, &Path); return Compatible; } diff --git a/test/Sema/warn-null.c b/test/Sema/warn-null.c new file mode 100644 index 0000000000..28fb6a5f69 --- /dev/null +++ b/test/Sema/warn-null.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 %s -verify + +// 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}} +void f() { + p = 0; + q = '\0'; // expected-warning{{expression which evaluates to zero treated as a null pointer constant}} + r = 1 - 1; // expected-warning{{expression which evaluates to zero treated as a null pointer constant}} +} diff --git a/test/SemaTemplate/dependent-expr.cpp b/test/SemaTemplate/dependent-expr.cpp index 01ac42ed42..2c26ec53a0 100644 --- a/test/SemaTemplate/dependent-expr.cpp +++ b/test/SemaTemplate/dependent-expr.cpp @@ -79,3 +79,17 @@ template<typename T> struct CastDependentIntToPointer { return ((void*)(((unsigned long)(x)|0x1ul))); } }; + +// Regression test for crasher in r194540. +namespace PR10837 { + typedef void t(int); + template<typename> struct A { + void f(); + static t g; + }; + t *p; + template<typename T> void A<T>::f() { + p = g; + } + template struct A<int>; +} |