Don't use dyn_cast on a Type* which might not be canonical. Fixes an extremely obscur...
authorRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 6 Dec 2012 03:04:50 +0000 (03:04 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 6 Dec 2012 03:04:50 +0000 (03:04 +0000)
llvm-svn: 169467

clang/lib/AST/ASTContext.cpp
clang/test/SemaCXX/empty-class-layout.cpp

index a1faaa0..44f13b6 100644 (file)
@@ -3994,7 +3994,8 @@ ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
   uint64_t ElementCount = 1;
   do {
     ElementCount *= CA->getSize().getZExtValue();
-    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
+    CA = dyn_cast_or_null<ConstantArrayType>(
+      CA->getElementType()->getAsArrayTypeUnsafe());
   } while (CA);
   return ElementCount;
 }
index 951f16c..3cfc491 100644 (file)
@@ -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);
+}