diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -4158,3 +4158,11 @@ let Subjects = SubjectList<[Record]>; let Documentation = [ReadOnlyPlacementDocs]; } + +def ElementCount : InheritableAttr { + let Spellings = [Clang<"element_count">]; + let Subjects = SubjectList<[Field]>; + let Args = [IdentifierArgument<"ElementCountField">]; + let Documentation = [ElementCountDocs]; + let LangOpts = [COnly]; +} diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -6950,3 +6950,10 @@ its underlying representation to be a WebAssembly ``funcref``. }]; } + +def ElementCountDocs : Documentation { + let Category = DocCatField; + let Content = [{ +Clang supports the ``element_count`` attribute for flexible array members. + }]; +} diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -8979,6 +8979,11 @@ From->args_size()); break; } + case attr::ElementCount: { + const auto *From = cast(FromAttr); + AI.importAttr(From, From->getElementCountField()); + break; + } default: { // The default branch works for attributes that have no arguments to import. diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -946,6 +946,30 @@ return CGF.getVLASize(VAT).NumElts; // Ignore pass_object_size here. It's not applicable on decayed pointers. } + + if (auto *ME = dyn_cast(CE->getSubExpr())) { + if (ME->isFlexibleArrayMemberLike(CGF.getContext(), + StrictFlexArraysLevel, true)) { + if (auto *MD = dyn_cast(ME->getMemberDecl())) { + if (auto ECA = MD->getAttr()) { + RecordDecl *RD = MD->getParent(); + IdentifierInfo *CountField = ECA->getElementCountField(); + + for (FieldDecl *FD : RD->fields()) { + if (FD->getName() != CountField->getName()) + continue; + + auto *Mem = MemberExpr::CreateImplicit( + CGF.getContext(), const_cast(ME->getBase()), true, FD, + FD->getType(), VK_LValue, OK_Ordinary); + + IndexedType = Base->getType(); + return CGF.EmitAnyExprToTemp(Mem).getScalarVal(); + } + } + } + } + } } QualType EltTy{Base->getType()->getPointeeOrArrayElementType(), 0}; diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -8238,6 +8238,12 @@ D->addAttr(ZeroCallUsedRegsAttr::Create(S.Context, Kind, AL)); } +static void handleElementCountAttr(Sema &S, Decl *D, const ParsedAttr &AL) { + // TODO: Probably needs more processing here. See Sema::AddAlignValueAttr. + IdentifierInfo *Name = AL.getArgAsIdent(0)->Ident; + D->addAttr(::new (S.Context) ElementCountAttr(S.Context, AL, Name)); +} + static void handleFunctionReturnThunksAttr(Sema &S, Decl *D, const ParsedAttr &AL) { StringRef KindStr; @@ -9142,6 +9148,9 @@ case ParsedAttr::AT_FunctionReturnThunks: handleFunctionReturnThunksAttr(S, D, AL); break; + case ParsedAttr::AT_ElementCount: + handleElementCountAttr(S, D, AL); + break; // Microsoft attributes: case ParsedAttr::AT_LayoutVersion: diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test b/clang/test/Misc/pragma-attribute-supported-attributes-list.test --- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test +++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test @@ -61,6 +61,7 @@ // CHECK-NEXT: DiagnoseAsBuiltin (SubjectMatchRule_function) // CHECK-NEXT: DisableSanitizerInstrumentation (SubjectMatchRule_function, SubjectMatchRule_objc_method, SubjectMatchRule_variable_is_global) // CHECK-NEXT: DisableTailCalls (SubjectMatchRule_function, SubjectMatchRule_objc_method) +// CHECK-NEXT: ElementCount (SubjectMatchRule_field) // CHECK-NEXT: EnableIf (SubjectMatchRule_function) // CHECK-NEXT: EnforceTCB (SubjectMatchRule_function, SubjectMatchRule_objc_method) // CHECK-NEXT: EnforceTCBLeaf (SubjectMatchRule_function, SubjectMatchRule_objc_method)