================ @@ -230,143 +246,55 @@ bool Sema::ValidateBoundsAttrTypeForTypePosition( // void f(int *__counted_by(*len) *buf, int *len); // See https://github.com/llvm/llvm-project/issues/166411. if (PointerNestLevel > 0) { - Diag(AttrLoc, diag::err_counted_by_on_nested_pointer) - << getBoundsAttrKind(Flags); + Diag(AttrLoc, diag::err_counted_by_on_nested_pointer) << Kind; return false; } return true; } -bool Sema::CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes, - bool OrNull) { - // Check the context the attribute is used in +bool Sema::ValidateBoundsAttrTypeForTypePosition( + QualType Ty, AttributeCommonInfo::Kind AttrKind, SourceLocation AttrLoc, + SourceRange AttrRange, unsigned PointerNestLevel, bool OnReturnType, + BoundsAttrFlags &Flags) { + Flags = BoundsAttrFlags::get(AttrKind); - unsigned Kind = getCountAttrKind(CountInBytes, OrNull); + if (!ValidateBoundsAttrTypeShape(Ty, AttrLoc, AttrRange, Flags, + /*AttrSpelling=*/{}, /*AllowRedecl=*/false, + /*AttrArg=*/nullptr, /*UpdateFlags=*/false)) + return false; - if (FD->getParent()->isUnion()) { - Diag(FD->getBeginLoc(), diag::err_count_attr_in_union) - << Kind << FD->getSourceRange(); - return true; - } + if (!ValidateBoundsAttrPosition(getBoundsAttrKind(Flags), AttrLoc, AttrRange, + PointerNestLevel, OnReturnType)) + return false; - const auto FieldTy = FD->getType(); - if (FieldTy->isArrayType() && (CountInBytes || OrNull)) { - Diag(FD->getBeginLoc(), - diag::err_count_attr_not_on_ptr_or_flexible_array_member) - << Kind << FD->getLocation() << /* suggest counted_by */ 1; - return true; - } - if (!FieldTy->isArrayType() && !FieldTy->isPointerType()) { - Diag(FD->getBeginLoc(), - diag::err_count_attr_not_on_ptr_or_flexible_array_member) - << Kind << FD->getLocation() << /* do not suggest counted_by */ 0; - return true; - } + return true; +} - LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel = - LangOptions::StrictFlexArraysLevelKind::IncompleteOnly; - if (FieldTy->isArrayType() && - !Decl::isFlexibleArrayMemberLike(getASTContext(), FD, FieldTy, - StrictFlexArraysLevel, true)) { - Diag(FD->getBeginLoc(), - diag::err_counted_by_attr_on_array_not_flexible_array_member) - << Kind << FD->getLocation(); - return true; - } +bool Sema::CheckCountedByAttrInRecord(const DeclContext *DC, QualType Ty, ---------------- rapidsna wrote:
> Why do we even need to provide DC when we can get it by reading the FieldDecl > that E is referring to? The annotated field's `FieldDecl` doesn't exist yet when this runs. The type is built in `GetTypeForDeclarator`, before `ActOnField` creates the `FieldDecl`, so `DC` is the record (or ObjC ivar container) currently being defined. The `FieldDecl` that `E` refers to is the count field, which is a different field. This function checks whether the two fields are in the same context, so the annotated field's context has to be passed separately. I can clarify these in the comment of the function decl. https://github.com/llvm/llvm-project/pull/233582 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
