From d5e839429771ad4d1a8b3db598cbbc6d93621f75 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 6 Dec 2012 03:04:50 +0000 Subject: [PATCH] Don't use dyn_cast on a Type* which might not be canonical. Fixes an extremely obscure record layout bug. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169467 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AST/ASTContext.cpp | 3 ++- test/SemaCXX/empty-class-layout.cpp | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index a1faaa00ec..44f13b6906 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -3994,7 +3994,8 @@ ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { uint64_t ElementCount = 1; do { ElementCount *= CA->getSize().getZExtValue(); - CA = dyn_cast(CA->getElementType()); + CA = dyn_cast_or_null( + CA->getElementType()->getAsArrayTypeUnsafe()); } while (CA); return ElementCount; } diff --git a/test/SemaCXX/empty-class-layout.cpp b/test/SemaCXX/empty-class-layout.cpp index 951f16c1b0..3cfc491ef6 100644 --- a/test/SemaCXX/empty-class-layout.cpp +++ b/test/SemaCXX/empty-class-layout.cpp @@ -156,3 +156,18 @@ namespace Test7 { }; SA(0, sizeof(Test) == 2); } + +namespace Test8 { + // Test that type sugar doesn't make us incorrectly determine the size of an + // array of empty classes. + struct Empty1 {}; + struct Empty2 {}; + struct Empties : Empty1, Empty2 {}; + typedef Empty1 Sugar[4]; + struct A : Empty2, Empties { + // This must go at offset 2, because if it were at offset 0, + // V[0][1] would overlap Empties::Empty1. + Sugar V[1]; + }; + SA(0, sizeof(A) == 6); +} -- 2.40.0