From e78343bd2b01bdd97388d648c95d794b82d0d971 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@224215 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ExprConstant.cpp | 4 +++- test/SemaCXX/constant-expression-cxx11.cpp | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index 417f7931df..ad36e76bea 100644 --- a/lib/AST/ExprConstant.cpp +++ b/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/test/SemaCXX/constant-expression-cxx11.cpp b/test/SemaCXX/constant-expression-cxx11.cpp index cf317950c7..dbb1255ef1 100644 --- a/test/SemaCXX/constant-expression-cxx11.cpp +++ b/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 { -- 2.40.0