From: Richard Smith Date: Thu, 6 Dec 2012 03:04:50 +0000 (+0000) Subject: Don't use dyn_cast on a Type* which might not be canonical. Fixes an extremely obscur... X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7808c6aa7e929486c289608edb1f9a1efc3e5c8a;p=platform%2Fupstream%2Fllvm.git Don't use dyn_cast on a Type* which might not be canonical. Fixes an extremely obscure record layout bug. llvm-svn: 169467 --- diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index a1faaa0..44f13b6 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/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/clang/test/SemaCXX/empty-class-layout.cpp b/clang/test/SemaCXX/empty-class-layout.cpp index 951f16c..3cfc491 100644 --- a/clang/test/SemaCXX/empty-class-layout.cpp +++ b/clang/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); +}