From 636ed47428c1cf9304c4131d631f3c0b72f020ad Mon Sep 17 00:00:00 2001 From: Volodymyr Sapsai Date: Fri, 27 Oct 2017 00:56:23 +0000 Subject: [PATCH] [CodeGen] Add support for IncompleteArrayType in Obj-C ivars. Fixes an assertion failure when ivar is a struct containing incomplete array. Also completes support for direct flexible array members. rdar://problem/21054495 Reviewers: rjmccall, theraven Reviewed By: rjmccall Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D38774 llvm-svn: 316723 --- clang/lib/CodeGen/CGObjCMac.cpp | 5 ++++ .../test/CodeGenObjC/ivar-layout-flexible-array.m | 28 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 clang/test/CodeGenObjC/ivar-layout-flexible-array.m diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp index 85901a6..992da81 100644 --- a/clang/lib/CodeGen/CGObjCMac.cpp +++ b/clang/lib/CodeGen/CGObjCMac.cpp @@ -5084,6 +5084,11 @@ void IvarLayoutBuilder::visitField(const FieldDecl *field, // 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(); diff --git a/clang/test/CodeGenObjC/ivar-layout-flexible-array.m b/clang/test/CodeGenObjC/ivar-layout-flexible-array.m new file mode 100644 index 0000000..28849c8 --- /dev/null +++ b/clang/test/CodeGenObjC/ivar-layout-flexible-array.m @@ -0,0 +1,28 @@ +// RUN: %clang_cc1 -triple x86_64-apple-darwin -Wno-objc-root-class -fobjc-arc -emit-llvm -o - %s | FileCheck %s + +// rdar://problem/21054495 +@interface FlexibleArrayMember { + char flexible_array[][4][2]; +} +@end +@implementation FlexibleArrayMember +@end +// CHECK: @OBJC_METH_VAR_NAME_{{.*}} = private unnamed_addr constant {{.*}} c"flexible_array\00" +// CHECK-NEXT: @OBJC_METH_VAR_TYPE_{{.*}} = private unnamed_addr constant {{.*}} c"^[4[2c]]\00" + + +typedef char FlexibleArray[]; + +struct Packet { + int size; + FlexibleArray data; +}; + +@interface VariableSizeIvar { + struct Packet flexible_struct; +} +@end +@implementation VariableSizeIvar +@end +// CHECK: @OBJC_METH_VAR_NAME_{{.*}} = private unnamed_addr constant {{.*}} c"flexible_struct\00" +// CHECK-NEXT: @OBJC_METH_VAR_TYPE_{{.*}} = private unnamed_addr constant {{.*}} c"{Packet=\22size\22i\22data\22[0c]}\00" -- 2.7.4