[clang] [Clang][Sema] Allow counted_by on void* in GNU mode (PR #164737)

Dan Liew via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 24 16:57:26 PDT 2025


================
@@ -132,9 +132,20 @@ bool Sema::CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes,
     // `BoundsSafetyCheckUseOfCountAttrPtr`
     //
     // * When the pointee type is always an incomplete type (e.g.
-    // `void`) the attribute is disallowed by this method because we know the
-    // type can never be completed so there's no reason to allow it.
-    InvalidTypeKind = CountedByInvalidPointeeTypeKind::INCOMPLETE;
+    // `void` in strict C mode) the attribute is disallowed by this method
+    // because we know the type can never be completed so there's no reason
+    // to allow it.
+    //
+    // Exception: In GNU mode, void has an implicit size of 1 byte for pointer
+    // arithmetic. Therefore, counted_by on void* is allowed as a GNU extension
+    // and behaves equivalently to sized_by (treating the count as bytes).
+    bool IsVoidPtrInGNUMode = PointeeTy->isVoidType() && getLangOpts().GNUMode;
+    if (IsVoidPtrInGNUMode) {
+      // Emit a warning that this is a GNU extension
+      Diag(FD->getBeginLoc(), diag::ext_gnu_counted_by_void_ptr) << Kind;
----------------
delcypher wrote:

I'm not suggesting you modify the wording here. I'm suggesting you **also** emit a note diagnostic that tells the user how to suppress the warning by using the appropriate attribute instead of `__counted_by`.

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


More information about the cfe-commits mailing list