[clang] [Bounds-Safety] Add sized_by, counted_by_or_null & sized_by_or_null (PR #93231)
Dan Liew via cfe-commits
cfe-commits at lists.llvm.org
Fri May 24 15:10:03 PDT 2024
================
@@ -8641,22 +8641,33 @@ enum class CountedByInvalidPointeeTypeKind {
VALID,
};
-static bool CheckCountedByAttrOnField(
- Sema &S, FieldDecl *FD, Expr *E,
- llvm::SmallVectorImpl<TypeCoupledDeclRefInfo> &Decls) {
+static bool
+CheckCountedByAttrOnField(Sema &S, FieldDecl *FD, Expr *E,
+ llvm::SmallVectorImpl<TypeCoupledDeclRefInfo> &Decls,
+ bool CountInBytes, bool OrNull) {
// Check the context the attribute is used in
+ unsigned Kind = CountInBytes;
+ if (OrNull)
+ Kind += 2;
+
if (FD->getParent()->isUnion()) {
S.Diag(FD->getBeginLoc(), diag::err_counted_by_attr_in_union)
- << FD->getSourceRange();
+ << Kind << FD->getSourceRange();
return true;
}
const auto FieldTy = FD->getType();
+ if (FieldTy->isArrayType() && (CountInBytes || OrNull)) {
+ S.Diag(FD->getBeginLoc(),
+ diag::err_counted_by_attr_not_on_ptr_or_flexible_array_member)
----------------
delcypher wrote:
> > The diagnostic name is a little misleading here because CountInBytes suggested __sized_by but the diagnostic name has counted_by in its name
>
> I kept it because it's the same family of attributes. Do you have a suggestion for a name that would imply that it's not just for `counted_by`, but more specific than `bounds_attribute`?
We use `CountAttributedType` to represent both `counted_by` and `sized_by` so how about `err_count_attr_...` as the diagnostic prefix? I don't have strong opinions on exactly what the name should be.
https://github.com/llvm/llvm-project/pull/93231
More information about the cfe-commits
mailing list