[clang] [BoundsSafety][NFC] Add counted_by type-shape validation helper (PR #224846)

2026-10-02 Thread Yeoul Na via cfe-commits

rapidsna wrote:

Superseded by https://github.com/llvm/llvm-project/pull/227774

https://github.com/llvm/llvm-project/pull/224846
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [BoundsSafety][NFC] Add counted_by type-shape validation helper (PR #224846)

2026-10-02 Thread Yeoul Na via cfe-commits

https://github.com/rapidsna closed 
https://github.com/llvm/llvm-project/pull/224846
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [BoundsSafety][NFC] Add counted_by type-shape validation helper (PR #224846)

2026-09-19 Thread Yeoul Na via cfe-commits

https://github.com/rapidsna updated 
https://github.com/llvm/llvm-project/pull/224846

>From b11b1745e1f15b3d771c7c78ef2322c7875c0de0 Mon Sep 17 00:00:00 2001
From: Yeoul Na 
Date: Thu, 10 Sep 2026 07:30:15 -0700
Subject: [PATCH] [BoundsSafety][NFC] Add counted_by type-shape validation
 helper

Introduce the single "is this type valid for a counted_by-family attribute in
type position" leaf that both the eager type-attribute path and the
late-parsed path will call:

  - Sema::ValidateBoundsAttrTypeShape holds the type-shape checks -- pointer
or flexible array member, void and function pointee, pointee that is a
struct with a flexible array member -- and their diagnostics.
  - validateBoundsAttrTypeForTypePosition wraps it for type position and adds
the nested-pointer rejection, reported with the new
err_counted_by_on_nested_pointer diagnostic.

Supporting pieces: Sema::BoundsAttrFlags and Sema::getBoundsAttrKind,
getCountAttrKind, getPointerNestLevel, the CountedByInvalidPointeeTypeKind
enum, and LangOptions::hasBoundsSafetyAttributes(), a stub returning false so
the shared leaf can gate its -fbounds-safety-only branches with the same
predicate used downstream.

Unused at this point -- nothing invokes it yet -- so this is NFC. The callers
are added in the following commits.
---
 .../clang/Basic/DiagnosticSemaKinds.td|   3 +
 clang/include/clang/Basic/LangOptions.h   |   7 ++
 clang/include/clang/Sema/Sema.h   |  24 
 clang/lib/Sema/SemaBoundsSafety.cpp   | 112 ++
 clang/lib/Sema/SemaType.cpp   |  83 +
 5 files changed, 229 insertions(+)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index fca68f292f667..205d68b0979fe 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7266,6 +7266,9 @@ def err_builtin_counted_by_ref_invalid_use : Error<
   "value returned by '__builtin_counted_by_ref' cannot be used in "
   "%select{an array subscript|a binary}0 expression">;
 
+def err_counted_by_on_nested_pointer : Error<
+  "'%select{counted_by|sized_by|counted_by_or_null|sized_by_or_null}0' 
attribute on nested pointer type is not allowed">;
+
 let CategoryName = "ARC Semantic Issue" in {
 
 // ARC-mode diagnostics.
diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index 7539e000d03f9..0d615824ecc5b 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -710,6 +710,13 @@ class LangOptions : public LangOptionsBase {
 return ConvergentFunctions;
   }
 
+  /// Returns true when the -fbounds-safety attribute programming model is in
+  /// effect. There is no attributes-only mode on this base, so this is always
+  /// false; it exists so the shared Sema::ValidateBoundsAttrTypeShape leaf can
+  /// gate its -fbounds-safety-only branches with the same predicate used
+  /// downstream (where those branches carry the extra diagnostics).
+  bool hasBoundsSafetyAttributes() const { return false; }
+
   /// Return true if atomicrmw operations targeting allocations in private
   /// memory are undefined.
   bool threadPrivateMemoryAtomicsAreUndefined() const {
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 5becfc9fae152..6e8a75bdf2bd5 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -2493,6 +2493,30 @@ class Sema final : public SemaBase {
   /// Implementations are in SemaBoundsSafety.cpp
   ///@{
 public:
+  struct BoundsAttrFlags {
+bool CountInBytes = false;
+bool OrNull = false;
+bool IsEndedBy = false;
+  };
+  static BoundsAttrFlags getBoundsAttrFlags(AttributeCommonInfo::Kind K);
+  static BoundsAttributedType::BoundsAttrKind
+  getBoundsAttrKind(const BoundsAttrFlags &);
+
+  /// Validates that a type is eligible for an "externally counted" bounds
+  /// attribute (counted_by/sized_by and their _or_null variants).
+  ///
+  /// \p Flags selects the attribute variant. \returns true if the type is
+  /// valid, false on error (diagnostics emitted). For `void *__counted_by(n)`
+  /// it warns that the count is treated as a byte size and sets
+  /// \p Flags.CountInBytes; callers that want to preserve a counted_by node
+  /// pass a scratch copy (see validateBoundsAttrTypeForTypePosition).
+  bool ValidateBoundsAttrTypeShape(QualType Ty, SourceLocation AttrLoc,
+   SourceRange AttrRange,
+   BoundsAttrFlags &Flags,
+   StringRef AttrSpelling = {},
+   bool AllowRedecl = false,
+   Expr *AttrArg = nullptr);
+
   /// Check if applying the specified attribute variant from the "counted by"
   /// family of attributes to FieldDecl \p FD is semantically valid. If
   

[clang] [BoundsSafety][NFC] Add counted_by type-shape validation helper (PR #224846)

2026-09-19 Thread Yeoul Na via cfe-commits

https://github.com/rapidsna edited 
https://github.com/llvm/llvm-project/pull/224846
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits