[CodeGen] Backport fix for structs with flexible array members as Obj-C ivars.

Follow up to insufficient fix 76e580ab1c32495ae524bbced52af1e63540836b.
This one handles typedefs and nested arrays.

rdar://problem/21054495
diff --git a/lib/CodeGen/CGObjCMac.cpp b/lib/CodeGen/CGObjCMac.cpp
index 6f89b75..7d44bff 100644
--- a/lib/CodeGen/CGObjCMac.cpp
+++ b/lib/CodeGen/CGObjCMac.cpp
@@ -5084,16 +5084,16 @@
 
   // Drill down into arrays.
   uint64_t numElts = 1;
+  if (auto arrayType = CGM.getContext().getAsIncompleteArrayType(fieldType)) {
+    numElts = 0;
+    fieldType = arrayType->getElementType();
+  }
+  // Unlike incomplete arrays, constant arrays can be nested.
   while (auto arrayType = CGM.getContext().getAsConstantArrayType(fieldType)) {
     numElts *= arrayType->getSize().getZExtValue();
     fieldType = arrayType->getElementType();
   }
 
-  if (isa<IncompleteArrayType>(fieldType)) {
-    numElts = 0;
-    fieldType = fieldType->getAsArrayTypeUnsafe()->getElementType();
-  }
-
   assert(!fieldType->isArrayType() && "ivar of non-constant array type?");
 
   // If we ended up with a zero-sized array, we've done what we can do within
diff --git a/test/CodeGenObjC/ivar-layout-flexible-array.m b/test/CodeGenObjC/ivar-layout-flexible-array.m
index 2378de2..c085cad 100644
--- a/test/CodeGenObjC/ivar-layout-flexible-array.m
+++ b/test/CodeGenObjC/ivar-layout-flexible-array.m
@@ -1,9 +1,11 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -Wno-objc-root-class -fobjc-arc -emit-llvm -o - %s | FileCheck %s
 
 // rdar://problem/21054495
+typedef char FlexibleArray[];
+
 struct Packet {
   int size;
-  char data[];
+  FlexibleArray data;
 };
 
 @interface VariableSizeIvar {