From b1ead8ba0f35dc3bf852be933dc2fe123fefa2ea Mon Sep 17 00:00:00 2001 From: Volodymyr Sapsai Date: Mon, 23 Oct 2017 22:01:41 +0000 Subject: [PATCH] [Sema] Add support for flexible array members in Obj-C. Allow Obj-C ivars with incomplete array type but only as the last ivar. Also add a requirement for ivars that contain a flexible array member to be at the end of class too. It is possible to add in a subclass another ivar at the end but we'll emit a warning in this case. Also we'll emit a warning if a variable sized ivar is declared in class extension or in implementation because subclasses won't know they should avoid adding new ivars. In ARC incomplete array objects are treated as __unsafe_unretained so require them to be marked as such. Prohibit synthesizing ivars with flexible array members because order of synthesized ivars is not obvious and tricky to control. Spelling out ivar explicitly gives control to developers and helps to avoid surprises with unexpected ivar ordering. For C and C++ changed diagnostic to tell explicitly a field is not the last one and point to the next field. It is not as useful as in Obj-C but it is an improvement and it is consistent with Obj-C. For C for unions emit more specific err_flexible_array_union instead of generic err_field_incomplete. rdar://problem/21054495 Reviewers: rjmccall, theraven Reviewed By: rjmccall Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D38773 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316381 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticGroups.td | 2 + include/clang/Basic/DiagnosticSemaKinds.td | 22 ++ lib/Sema/SemaDecl.cpp | 115 ++++---- lib/Sema/SemaDeclObjC.cpp | 112 ++++++++ lib/Sema/SemaObjCProperty.cpp | 8 + test/Sema/transparent-union.c | 2 +- test/SemaCXX/flexible-array-test.cpp | 5 + test/SemaObjC/flexible-array-arc.m | 36 +++ test/SemaObjC/flexible-array.m | 288 +++++++++++++++++++++ test/SemaObjC/ivar-sem-check-1.m | 5 +- test/SemaObjCXX/flexible-array.mm | 37 +++ 11 files changed, 577 insertions(+), 55 deletions(-) create mode 100644 test/SemaObjC/flexible-array-arc.m create mode 100644 test/SemaObjC/flexible-array.m create mode 100644 test/SemaObjCXX/flexible-array.mm diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td index 1896280761..4cceaf6ccc 100644 --- a/include/clang/Basic/DiagnosticGroups.td +++ b/include/clang/Basic/DiagnosticGroups.td @@ -374,6 +374,7 @@ def ObjCRootClass : DiagGroup<"objc-root-class">; def ObjCPointerIntrospectPerformSelector : DiagGroup<"deprecated-objc-pointer-introspection-performSelector">; def ObjCPointerIntrospect : DiagGroup<"deprecated-objc-pointer-introspection", [ObjCPointerIntrospectPerformSelector]>; def ObjCMultipleMethodNames : DiagGroup<"objc-multiple-method-names">; +def ObjCFlexibleArray : DiagGroup<"objc-flexible-array">; def OpenCLUnsupportedRGBA: DiagGroup<"opencl-unsupported-rgba">; def DeprecatedObjCIsaUsage : DiagGroup<"deprecated-objc-isa-usage">; def ExplicitInitializeCall : DiagGroup<"explicit-initialize-call">; @@ -750,6 +751,7 @@ def Most : DiagGroup<"most", [ VolatileRegisterVar, ObjCMissingSuperCalls, ObjCDesignatedInit, + ObjCFlexibleArray, OverloadedVirtual, PrivateExtern, SelTypeCast, diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index ce79ac2e90..b1cba0655c 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -5222,6 +5222,28 @@ def ext_flexible_array_empty_aggregate_gnu : Extension< def ext_flexible_array_union_gnu : Extension< "flexible array member %0 in a union is a GNU extension">, InGroup; +def err_flexible_array_not_at_end : Error< + "flexible array member %0 with type %1 is not at the end of" + " %select{struct|interface|union|class|enum}2">; +def err_objc_variable_sized_type_not_at_end : Error< + "field %0 with variable sized type %1 is not at the end of class">; +def note_next_field_declaration : Note< + "next field declaration is here">; +def note_next_ivar_declaration : Note< + "next %select{instance variable declaration|synthesized instance variable}0" + " is here">; +def err_synthesize_variable_sized_ivar : Error< + "synthesized property with variable size type %0" + " requires an existing instance variable">; +def err_flexible_array_arc_retainable : Error< + "ARC forbids flexible array members with retainable object type">; +def warn_variable_sized_ivar_visibility : Warning< + "field %0 with variable sized type %1 is not visible to subclasses and" + " can conflict with their instance variables">, InGroup; +def warn_superclass_variable_sized_type_not_at_end : Warning< + "field %0 can overwrite instance variable %1 with variable sized type %2" + " in superclass %3">, InGroup; + let CategoryName = "ARC Semantic Issue" in { // ARC-mode diagnostics. diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index d75a469970..573c1af8d2 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -14997,6 +14997,7 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, // possibly recursively, a member that is such a structure) // shall not be a member of a structure or an element of an // array. + bool IsLastField = (i + 1 == Fields.end()); if (FDTy->isFunctionType()) { // Field declared as a function. Diag(FD->getLocation(), diag::err_field_declared_as_function) @@ -15004,60 +15005,70 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, FD->setInvalidDecl(); EnclosingDecl->setInvalidDecl(); continue; - } else if (FDTy->isIncompleteArrayType() && Record && - ((i + 1 == Fields.end() && !Record->isUnion()) || - ((getLangOpts().MicrosoftExt || - getLangOpts().CPlusPlus) && - (i + 1 == Fields.end() || Record->isUnion())))) { - // Flexible array member. - // Microsoft and g++ is more permissive regarding flexible array. - // It will accept flexible array in union and also - // as the sole element of a struct/class. - unsigned DiagID = 0; - if (Record->isUnion()) - DiagID = getLangOpts().MicrosoftExt - ? diag::ext_flexible_array_union_ms - : getLangOpts().CPlusPlus - ? diag::ext_flexible_array_union_gnu - : diag::err_flexible_array_union; - else if (NumNamedMembers < 1) - DiagID = getLangOpts().MicrosoftExt - ? diag::ext_flexible_array_empty_aggregate_ms - : getLangOpts().CPlusPlus - ? diag::ext_flexible_array_empty_aggregate_gnu - : diag::err_flexible_array_empty_aggregate; - - if (DiagID) - Diag(FD->getLocation(), DiagID) << FD->getDeclName() - << Record->getTagKind(); - // While the layout of types that contain virtual bases is not specified - // by the C++ standard, both the Itanium and Microsoft C++ ABIs place - // virtual bases after the derived members. This would make a flexible - // array member declared at the end of an object not adjacent to the end - // of the type. - if (CXXRecordDecl *RD = dyn_cast(Record)) - if (RD->getNumVBases() != 0) - Diag(FD->getLocation(), diag::err_flexible_array_virtual_base) + } else if (FDTy->isIncompleteArrayType() && + (Record || isa(EnclosingDecl))) { + if (Record) { + // Flexible array member. + // Microsoft and g++ is more permissive regarding flexible array. + // It will accept flexible array in union and also + // as the sole element of a struct/class. + unsigned DiagID = 0; + if (!Record->isUnion() && !IsLastField) { + Diag(FD->getLocation(), diag::err_flexible_array_not_at_end) + << FD->getDeclName() << FD->getType() << Record->getTagKind(); + Diag((*(i + 1))->getLocation(), diag::note_next_field_declaration); + FD->setInvalidDecl(); + EnclosingDecl->setInvalidDecl(); + continue; + } else if (Record->isUnion()) + DiagID = getLangOpts().MicrosoftExt + ? diag::ext_flexible_array_union_ms + : getLangOpts().CPlusPlus + ? diag::ext_flexible_array_union_gnu + : diag::err_flexible_array_union; + else if (NumNamedMembers < 1) + DiagID = getLangOpts().MicrosoftExt + ? diag::ext_flexible_array_empty_aggregate_ms + : getLangOpts().CPlusPlus + ? diag::ext_flexible_array_empty_aggregate_gnu + : diag::err_flexible_array_empty_aggregate; + + if (DiagID) + Diag(FD->getLocation(), DiagID) << FD->getDeclName() + << Record->getTagKind(); + // While the layout of types that contain virtual bases is not specified + // by the C++ standard, both the Itanium and Microsoft C++ ABIs place + // virtual bases after the derived members. This would make a flexible + // array member declared at the end of an object not adjacent to the end + // of the type. + if (CXXRecordDecl *RD = dyn_cast(Record)) + if (RD->getNumVBases() != 0) + Diag(FD->getLocation(), diag::err_flexible_array_virtual_base) + << FD->getDeclName() << Record->getTagKind(); + if (!getLangOpts().C99) + Diag(FD->getLocation(), diag::ext_c99_flexible_array_member) << FD->getDeclName() << Record->getTagKind(); - if (!getLangOpts().C99) - Diag(FD->getLocation(), diag::ext_c99_flexible_array_member) - << FD->getDeclName() << Record->getTagKind(); - // If the element type has a non-trivial destructor, we would not - // implicitly destroy the elements, so disallow it for now. - // - // FIXME: GCC allows this. We should probably either implicitly delete - // the destructor of the containing class, or just allow this. - QualType BaseElem = Context.getBaseElementType(FD->getType()); - if (!BaseElem->isDependentType() && BaseElem.isDestructedType()) { - Diag(FD->getLocation(), diag::err_flexible_array_has_nontrivial_dtor) - << FD->getDeclName() << FD->getType(); - FD->setInvalidDecl(); - EnclosingDecl->setInvalidDecl(); - continue; + // If the element type has a non-trivial destructor, we would not + // implicitly destroy the elements, so disallow it for now. + // + // FIXME: GCC allows this. We should probably either implicitly delete + // the destructor of the containing class, or just allow this. + QualType BaseElem = Context.getBaseElementType(FD->getType()); + if (!BaseElem->isDependentType() && BaseElem.isDestructedType()) { + Diag(FD->getLocation(), diag::err_flexible_array_has_nontrivial_dtor) + << FD->getDeclName() << FD->getType(); + FD->setInvalidDecl(); + EnclosingDecl->setInvalidDecl(); + continue; + } + // Okay, we have a legal flexible array member at the end of the struct. + Record->setHasFlexibleArrayMember(true); + } else { + // In ObjCContainerDecl ivars with incomplete array type are accepted, + // unless they are followed by another ivar. That check is done + // elsewhere, after synthesized ivars are known. } - // Okay, we have a legal flexible array member at the end of the struct. - Record->setHasFlexibleArrayMember(true); } else if (!FDTy->isDependentType() && RequireCompleteType(FD->getLocation(), FD->getType(), diag::err_field_incomplete)) { @@ -15074,7 +15085,7 @@ void Sema::ActOnFields(Scope *S, SourceLocation RecLoc, Decl *EnclosingDecl, // If this is a struct/class and this is not the last element, reject // it. Note that GCC supports variable sized arrays in the middle of // structures. - if (i + 1 != Fields.end()) + if (!IsLastField) Diag(FD->getLocation(), diag::ext_variable_sized_type_in_struct) << FD->getDeclName() << FD->getType(); else { diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp index 607ffe984e..abdcc4da62 100644 --- a/lib/Sema/SemaDeclObjC.cpp +++ b/lib/Sema/SemaDeclObjC.cpp @@ -3721,6 +3721,26 @@ static void DiagnoseWeakIvars(Sema &S, ObjCImplementationDecl *ID) { } } +/// Diagnose attempts to use flexible array member with retainable object type. +static void DiagnoseRetainableFlexibleArrayMember(Sema &S, + ObjCInterfaceDecl *ID) { + if (!S.getLangOpts().ObjCAutoRefCount) + return; + + for (auto ivar = ID->all_declared_ivar_begin(); ivar; + ivar = ivar->getNextIvar()) { + if (ivar->isInvalidDecl()) + continue; + QualType IvarTy = ivar->getType(); + if (IvarTy->isIncompleteArrayType() && + (IvarTy.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) && + IvarTy->isObjCLifetimeType()) { + S.Diag(ivar->getLocation(), diag::err_flexible_array_arc_retainable); + ivar->setInvalidDecl(); + } + } +} + Sema::ObjCContainerKind Sema::getObjCContainerKind() const { switch (CurContext->getDeclKind()) { case Decl::ObjCInterface: @@ -3741,6 +3761,96 @@ Sema::ObjCContainerKind Sema::getObjCContainerKind() const { } } +static bool IsVariableSizedType(QualType T) { + if (T->isIncompleteArrayType()) + return true; + const auto *RecordTy = T->getAs(); + return (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()); +} + +static void DiagnoseVariableSizedIvars(Sema &S, ObjCContainerDecl *OCD) { + ObjCInterfaceDecl *IntfDecl = nullptr; + ObjCInterfaceDecl::ivar_range Ivars = llvm::make_range( + ObjCInterfaceDecl::ivar_iterator(), ObjCInterfaceDecl::ivar_iterator()); + if ((IntfDecl = dyn_cast(OCD))) { + Ivars = IntfDecl->ivars(); + } else if (auto *ImplDecl = dyn_cast(OCD)) { + IntfDecl = ImplDecl->getClassInterface(); + Ivars = ImplDecl->ivars(); + } else if (auto *CategoryDecl = dyn_cast(OCD)) { + if (CategoryDecl->IsClassExtension()) { + IntfDecl = CategoryDecl->getClassInterface(); + Ivars = CategoryDecl->ivars(); + } + } + + // Check if variable sized ivar is in interface and visible to subclasses. + if (!isa(OCD)) { + for (auto ivar : Ivars) { + if (!ivar->isInvalidDecl() && IsVariableSizedType(ivar->getType())) { + S.Diag(ivar->getLocation(), diag::warn_variable_sized_ivar_visibility) + << ivar->getDeclName() << ivar->getType(); + } + } + } + + // Subsequent checks require interface decl. + if (!IntfDecl) + return; + + // Check if variable sized ivar is followed by another ivar. + for (ObjCIvarDecl *ivar = IntfDecl->all_declared_ivar_begin(); ivar; + ivar = ivar->getNextIvar()) { + if (ivar->isInvalidDecl() || !ivar->getNextIvar()) + continue; + QualType IvarTy = ivar->getType(); + bool IsInvalidIvar = false; + if (IvarTy->isIncompleteArrayType()) { + S.Diag(ivar->getLocation(), diag::err_flexible_array_not_at_end) + << ivar->getDeclName() << IvarTy + << TTK_Class; // Use "class" for Obj-C. + IsInvalidIvar = true; + } else if (const RecordType *RecordTy = IvarTy->getAs()) { + if (RecordTy->getDecl()->hasFlexibleArrayMember()) { + S.Diag(ivar->getLocation(), + diag::err_objc_variable_sized_type_not_at_end) + << ivar->getDeclName() << IvarTy; + IsInvalidIvar = true; + } + } + if (IsInvalidIvar) { + S.Diag(ivar->getNextIvar()->getLocation(), + diag::note_next_ivar_declaration) + << ivar->getNextIvar()->getSynthesize(); + ivar->setInvalidDecl(); + } + } + + // Check if ObjC container adds ivars after variable sized ivar in superclass. + // Perform the check only if OCD is the first container to declare ivars to + // avoid multiple warnings for the same ivar. + ObjCIvarDecl *FirstIvar = + (Ivars.begin() == Ivars.end()) ? nullptr : *Ivars.begin(); + if (FirstIvar && (FirstIvar == IntfDecl->all_declared_ivar_begin())) { + const ObjCInterfaceDecl *SuperClass = IntfDecl->getSuperClass(); + while (SuperClass && SuperClass->ivar_empty()) + SuperClass = SuperClass->getSuperClass(); + if (SuperClass) { + auto IvarIter = SuperClass->ivar_begin(); + std::advance(IvarIter, SuperClass->ivar_size() - 1); + const ObjCIvarDecl *LastIvar = *IvarIter; + if (IsVariableSizedType(LastIvar->getType())) { + S.Diag(FirstIvar->getLocation(), + diag::warn_superclass_variable_sized_type_not_at_end) + << FirstIvar->getDeclName() << LastIvar->getDeclName() + << LastIvar->getType() << SuperClass->getDeclName(); + S.Diag(LastIvar->getLocation(), diag::note_entity_declared_at) + << LastIvar->getDeclName(); + } + } + } +} + // Note: For class/category implementations, allMethods is always null. Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef allMethods, ArrayRef allTUVars) { @@ -3872,6 +3982,7 @@ Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef allMethods, if (IDecl->hasDesignatedInitializers()) DiagnoseMissingDesignatedInitOverrides(IC, IDecl); DiagnoseWeakIvars(*this, IC); + DiagnoseRetainableFlexibleArrayMember(*this, IDecl); bool HasRootClassAttr = IDecl->hasAttr(); if (IDecl->getSuperClass() == nullptr) { @@ -3941,6 +4052,7 @@ Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef allMethods, } } } + DiagnoseVariableSizedIvars(*this, OCD); if (isInterfaceDeclKind) { // Reject invalid vardecls. for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { diff --git a/lib/Sema/SemaObjCProperty.cpp b/lib/Sema/SemaObjCProperty.cpp index 9c61d45158..ea5b1da46f 100644 --- a/lib/Sema/SemaObjCProperty.cpp +++ b/lib/Sema/SemaObjCProperty.cpp @@ -1290,6 +1290,14 @@ Decl *Sema::ActOnPropertyImplDecl(Scope *S, // An abstract type is as bad as an incomplete type. CompleteTypeErr = true; } + if (!CompleteTypeErr) { + const RecordType *RecordTy = PropertyIvarType->getAs(); + if (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()) { + Diag(PropertyIvarLoc, diag::err_synthesize_variable_sized_ivar) + << PropertyIvarType; + CompleteTypeErr = true; // suppress later diagnostics about the ivar + } + } if (CompleteTypeErr) Ivar->setInvalidDecl(); ClassImpDecl->addDecl(Ivar); diff --git a/test/Sema/transparent-union.c b/test/Sema/transparent-union.c index dfbadcfe62..0481127674 100644 --- a/test/Sema/transparent-union.c +++ b/test/Sema/transparent-union.c @@ -102,7 +102,7 @@ union pr15134v2 { union pr30520v { void b; } __attribute__((transparent_union)); // expected-error {{field has incomplete type 'void'}} -union pr30520a { int b[]; } __attribute__((transparent_union)); // expected-error {{field has incomplete type 'int []'}} +union pr30520a { int b[]; } __attribute__((transparent_union)); // expected-error {{flexible array member 'b' in a union is not allowed}} // expected-note@+1 2 {{forward declaration of 'struct stb'}} union pr30520s { struct stb b; } __attribute__((transparent_union)); // expected-error {{field has incomplete type 'struct stb'}} diff --git a/test/SemaCXX/flexible-array-test.cpp b/test/SemaCXX/flexible-array-test.cpp index 2d74fa5245..f2105ca361 100644 --- a/test/SemaCXX/flexible-array-test.cpp +++ b/test/SemaCXX/flexible-array-test.cpp @@ -66,6 +66,11 @@ union B { char c[]; }; +class C { + char c[]; // expected-error {{flexible array member 'c' with type 'char []' is not at the end of class}} + int s; // expected-note {{next field declaration is here}} +}; + namespace rdar9065507 { struct StorageBase { diff --git a/test/SemaObjC/flexible-array-arc.m b/test/SemaObjC/flexible-array-arc.m new file mode 100644 index 0000000000..1490329b18 --- /dev/null +++ b/test/SemaObjC/flexible-array-arc.m @@ -0,0 +1,36 @@ +// RUN: %clang_cc1 -fsyntax-only -fobjc-arc -verify -Wno-objc-root-class %s +// RUN: %clang_cc1 -fsyntax-only -verify -Wno-objc-root-class -DNOARC %s +#ifdef NOARC +// expected-no-diagnostics +#endif + +@interface RetainableArray { + id flexible[]; +#ifndef NOARC + // expected-error@-2 {{ARC forbids flexible array members with retainable object type}} +#endif +} +@end +@implementation RetainableArray +@end + +// Emit diagnostic only if have @implementation. +@interface RetainableArrayWithoutImpl { + id flexible[]; +} +@end + +// With ARC flexible array member objects can be only __unsafe_unretained +@interface UnsafeUnretainedArray { + __unsafe_unretained id flexible[]; +} +@end +@implementation UnsafeUnretainedArray +@end + +@interface NotObjCLifetimeTypeArray { + char flexible[]; +} +@end +@implementation NotObjCLifetimeTypeArray +@end diff --git a/test/SemaObjC/flexible-array.m b/test/SemaObjC/flexible-array.m new file mode 100644 index 0000000000..68e32d851f --- /dev/null +++ b/test/SemaObjC/flexible-array.m @@ -0,0 +1,288 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -Wno-objc-root-class %s + +// # Flexible array member. +// ## Instance variables only in interface. +@interface LastIvar { + char flexible[]; +} +@end + +@interface NotLastIvar { + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +// ## Instance variables in implementation. +@interface LastIvarInImpl +@end +@implementation LastIvarInImpl { + char flexible[]; // expected-warning {{field 'flexible' with variable sized type 'char []' is not visible to subclasses and can conflict with their instance variables}} +} +@end + +@interface NotLastIvarInImpl +@end +@implementation NotLastIvarInImpl { + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} + // expected-warning@-1 {{field 'flexible' with variable sized type 'char []' is not visible to subclasses and can conflict with their instance variables}} + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +@implementation NotLastIvarInImplWithoutInterface { // expected-warning {{cannot find interface declaration for 'NotLastIvarInImplWithoutInterface'}} + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} + // expected-warning@-1 {{field 'flexible' with variable sized type 'char []' is not visible to subclasses and can conflict with their instance variables}} + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +@interface LastIvarInClass_OtherIvarInImpl { + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} +} +@end +@implementation LastIvarInClass_OtherIvarInImpl { + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +// ## Non-instance variables in implementation. +@interface LastIvarInClass_UnrelatedVarInImpl { + char flexible[]; +} +@end +@implementation LastIvarInClass_UnrelatedVarInImpl +int nonIvar; +@end + +// ## Instance variables in class extension. +@interface LastIvarInExtension +@end +@interface LastIvarInExtension() { + char flexible[]; // expected-warning {{field 'flexible' with variable sized type 'char []' is not visible to subclasses and can conflict with their instance variables}} +} +@end + +@interface NotLastIvarInExtension +@end +@interface NotLastIvarInExtension() { + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} + // expected-warning@-1 {{field 'flexible' with variable sized type 'char []' is not visible to subclasses and can conflict with their instance variables}} + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +@interface LastIvarInClass_OtherIvarInExtension { + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} +} +@end +@interface LastIvarInClass_OtherIvarInExtension() { + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +@interface LastIvarInExtension_OtherIvarInExtension +@end +@interface LastIvarInExtension_OtherIvarInExtension() { + int last; // expected-note {{next instance variable declaration is here}} +} +@end +@interface LastIvarInExtension_OtherIvarInExtension() +// Extension without ivars to test we see through such extensions. +@end +@interface LastIvarInExtension_OtherIvarInExtension() { + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} + // expected-warning@-1 {{field 'flexible' with variable sized type 'char []' is not visible to subclasses and can conflict with their instance variables}} +} +@end + +@interface LastIvarInExtension_OtherIvarInImpl +@end +@interface LastIvarInExtension_OtherIvarInImpl() { + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} + // expected-warning@-1 {{field 'flexible' with variable sized type 'char []' is not visible to subclasses and can conflict with their instance variables}} +} +@end +@implementation LastIvarInExtension_OtherIvarInImpl { + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +// ## Instance variables in named categories. +@interface IvarInNamedCategory +@end +@interface IvarInNamedCategory(Category) { + char flexible[]; // expected-error {{instance variables may not be placed in categories}} +} +@end + +// ## Synthesized instance variable. +@interface LastIvarAndProperty { + char _flexible[]; +} +@property char flexible[]; // expected-error {{property cannot have array or function type 'char []'}} +@end + +// ## Synthesize other instance variables. +@interface LastIvar_ExplicitlyNamedPropertyBackingIvarPreceding { + int _elementsCount; + char flexible[]; +} +@property int count; +@end +@implementation LastIvar_ExplicitlyNamedPropertyBackingIvarPreceding +@synthesize count = _elementsCount; +@end + +@interface LastIvar_ImplicitlyNamedPropertyBackingIvarPreceding { + int count; + char flexible[]; +} +@property int count; +@end +@implementation LastIvar_ImplicitlyNamedPropertyBackingIvarPreceding +@synthesize count; +@end + +@interface NotLastIvar_ExplicitlyNamedPropertyBackingIvarLast { + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} +} +@property int count; +@end +@implementation NotLastIvar_ExplicitlyNamedPropertyBackingIvarLast +@synthesize count = _elementsCount; // expected-note {{next synthesized instance variable is here}} +@end + +@interface NotLastIvar_ImplicitlyNamedPropertyBackingIvarLast { + char flexible[]; // expected-error {{flexible array member 'flexible' with type 'char []' is not at the end of class}} +} +@property int count; // expected-note {{next synthesized instance variable is here}} +@end +@implementation NotLastIvar_ImplicitlyNamedPropertyBackingIvarLast +// Test auto-synthesize. +//@synthesize count; +@end + + +// # Variable sized types. +struct Packet { + unsigned int size; + char data[]; +}; + +// ## Instance variables only in interface. +@interface LastStructIvar { + struct Packet flexible; +} +@end + +@interface NotLastStructIvar { + struct Packet flexible; // expected-error {{field 'flexible' with variable sized type 'struct Packet' is not at the end of class}} + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +// ## Instance variables in implementation. +@interface LastStructIvarInImpl +@end +@implementation LastStructIvarInImpl { + struct Packet flexible; // expected-warning {{field 'flexible' with variable sized type 'struct Packet' is not visible to subclasses and can conflict with their instance variables}} +} +@end + +@interface NotLastStructIvarInImpl +@end +@implementation NotLastStructIvarInImpl { + struct Packet flexible; // expected-error {{field 'flexible' with variable sized type 'struct Packet' is not at the end of class}} + // expected-warning@-1 {{field 'flexible' with variable sized type 'struct Packet' is not visible to subclasses and can conflict with their instance variables}} + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +@interface LastStructIvarInClass_OtherIvarInImpl { + struct Packet flexible; // expected-error {{field 'flexible' with variable sized type 'struct Packet' is not at the end of class}} +} +@end +@implementation LastStructIvarInClass_OtherIvarInImpl { + int last; // expected-note {{next instance variable declaration is here}} +} +@end + +// ## Synthesized instance variable. +@interface LastSynthesizeStructIvar +@property int first; +@property struct Packet flexible; // expected-error {{synthesized property with variable size type 'struct Packet' requires an existing instance variable}} +@end +@implementation LastSynthesizeStructIvar +@end + +@interface NotLastSynthesizeStructIvar +@property struct Packet flexible; // expected-error {{synthesized property with variable size type 'struct Packet' requires an existing instance variable}} +@property int last; +@end +@implementation NotLastSynthesizeStructIvar +@end + +@interface LastStructIvarWithExistingIvarAndSynthesizedProperty { + struct Packet _flexible; +} +@property struct Packet flexible; +@end +@implementation LastStructIvarWithExistingIvarAndSynthesizedProperty +@end + + +// # Subclasses. +@interface FlexibleArrayMemberBase { + char flexible[]; // expected-note6 {{'flexible' declared here}} +} +@end + +@interface NoIvarAdditions : FlexibleArrayMemberBase +@end +@implementation NoIvarAdditions +@end + +@interface AddedIvarInInterface : FlexibleArrayMemberBase { + int last; // expected-warning {{field 'last' can overwrite instance variable 'flexible' with variable sized type 'char []' in superclass 'FlexibleArrayMemberBase'}} +} +@end + +@interface AddedIvarInImplementation : FlexibleArrayMemberBase +@end +@implementation AddedIvarInImplementation { + int last; // expected-warning {{field 'last' can overwrite instance variable 'flexible' with variable sized type 'char []' in superclass 'FlexibleArrayMemberBase'}} +} +@end + +@interface AddedIvarInExtension : FlexibleArrayMemberBase +@end +@interface AddedIvarInExtension() { + int last; // expected-warning {{field 'last' can overwrite instance variable 'flexible' with variable sized type 'char []' in superclass 'FlexibleArrayMemberBase'}} +} +@end + +@interface SynthesizedIvar : FlexibleArrayMemberBase +@property int count; +@end +@implementation SynthesizedIvar +@synthesize count; // expected-warning {{field 'count' can overwrite instance variable 'flexible' with variable sized type 'char []' in superclass 'FlexibleArrayMemberBase'}} +@end + +@interface WarnInSubclassOnlyOnce : FlexibleArrayMemberBase { + int last; // expected-warning {{field 'last' can overwrite instance variable 'flexible' with variable sized type 'char []' in superclass 'FlexibleArrayMemberBase'}} +} +@end +@interface WarnInSubclassOnlyOnce() { + int laster; +} +@end +@implementation WarnInSubclassOnlyOnce { + int lastest; +} +@end + +@interface AddedIvarInSubSubClass : NoIvarAdditions { + int last; // expected-warning {{field 'last' can overwrite instance variable 'flexible' with variable sized type 'char []' in superclass 'FlexibleArrayMemberBase'}} +} +@end diff --git a/test/SemaObjC/ivar-sem-check-1.m b/test/SemaObjC/ivar-sem-check-1.m index de038f5487..48e0a54b62 100644 --- a/test/SemaObjC/ivar-sem-check-1.m +++ b/test/SemaObjC/ivar-sem-check-1.m @@ -6,14 +6,15 @@ typedef int FOO(); @interface INTF { struct F {} JJ; - int arr[]; // expected-error {{field has incomplete type}} + int arr[]; // expected-error {{flexible array member 'arr' with type 'int []' is not at the end of class}} struct S IC; // expected-error {{field has incomplete type}} + // expected-note@-1 {{next instance variable declaration is here}} struct T { // expected-note {{previous definition is here}} struct T {} X; // expected-error {{nested redefinition of 'T'}} }YYY; FOO BADFUNC; // expected-error {{field 'BADFUNC' declared as a function}} int kaka; // expected-note {{previous declaration is here}} int kaka; // expected-error {{duplicate member 'kaka'}} - char ch[]; // expected-error {{field has incomplete type}} + char ch[]; } @end diff --git a/test/SemaObjCXX/flexible-array.mm b/test/SemaObjCXX/flexible-array.mm new file mode 100644 index 0000000000..5537876c30 --- /dev/null +++ b/test/SemaObjCXX/flexible-array.mm @@ -0,0 +1,37 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -Wno-objc-root-class %s + +// Test only flexible array member functionality specific to C++. + +union VariableSizeUnion { + int s; + char c[]; +}; + +@interface LastUnionIvar { + VariableSizeUnion flexible; +} +@end + +@interface NotLastUnionIvar { + VariableSizeUnion flexible; // expected-error {{field 'flexible' with variable sized type 'VariableSizeUnion' is not at the end of class}} + int last; // expected-note {{next instance variable declaration is here}} +} +@end + + +class VariableSizeClass { +public: + int s; + char c[]; +}; + +@interface LastClassIvar { + VariableSizeClass flexible; +} +@end + +@interface NotLastClassIvar { + VariableSizeClass flexible; // expected-error {{field 'flexible' with variable sized type 'VariableSizeClass' is not at the end of class}} + int last; // expected-note {{next instance variable declaration is here}} +} +@end -- 2.40.0