From 8c92b87cd0c34c68f1025f39405e7b783074430b Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Sun, 14 Dec 2014 08:40:47 +0000 Subject: [PATCH] AST: Limit zero-sized constexpr behavior to array types Restricting this "extension" to array types maximizes our standards conformance while not miscompiling real-world programs. llvm-svn: 224215 --- clang/lib/AST/ExprConstant.cpp | 4 +++- clang/test/SemaCXX/constant-expression-cxx11.cpp | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 417f7931df47..ad36e76bea45 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -1426,7 +1426,9 @@ static bool isZeroSized(const LValue &Value) { const ValueDecl *Decl = GetLValueBaseDecl(Value); if (Decl && isa(Decl)) { QualType Ty = Decl->getType(); - return Ty->isIncompleteType() || Decl->getASTContext().getTypeSize(Ty) == 0; + if (Ty->isArrayType()) + return Ty->isIncompleteType() || + Decl->getASTContext().getTypeSize(Ty) == 0; } return false; } diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp index cf317950c772..dbb1255ef1c8 100644 --- a/clang/test/SemaCXX/constant-expression-cxx11.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp @@ -1960,6 +1960,7 @@ namespace PR21786 { extern void (*start[])(); extern void (*end[])(); static_assert(&start != &end, ""); // expected-error {{constant expression}} + static_assert(&start != nullptr, ""); struct Foo; struct Bar { @@ -1967,7 +1968,7 @@ namespace PR21786 { static const Foo y; }; static_assert(&Bar::x != nullptr, ""); - static_assert(&Bar::x != &Bar::y, ""); // expected-error {{constant expression}} + static_assert(&Bar::x != &Bar::y, ""); } namespace PR21859 {