[clang] [Clang] Add -Wcounted-by-addrof to warn when &fam discards __counted_by (PR #206760)

David Meng via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 19 13:00:31 PDT 2026


================
@@ -7195,6 +7195,12 @@ def note_counted_by_consider_using_sized_by : Note<
 def warn_counted_by_attr_elt_type_unknown_size :
   Warning<err_counted_by_attr_pointee_unknown_size.Summary>,
   InGroup<BoundsSafetyCountedByEltTyUnknownSize>;
+def warn_counted_by_addrof_discards_count : Warning<
+  "taking the address of flexible array member %0 discards the "
+  "'%select{counted_by|sized_by|counted_by_or_null|sized_by_or_null}1' bound; "
+  "'__builtin_dynamic_object_size' on the resulting pointer uses the static "
+  "layout of the object, not the annotated count">,
+  InGroup<CountedByAddrof>, DefaultIgnore;
----------------
davidmenggx wrote:

The warning is now default on and fires only for `__builtin_dynamic_object_size(&fam, 1)` where `fam` is a `__counted_by` flexible array member reached through a pointer (not static/local/global storage), rather than at every `&fam`

https://github.com/llvm/llvm-project/pull/206760


More information about the cfe-commits mailing list