[clang] [BoundsSafety] Support late parsing for `counted_by` in type positions (PR #166491)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 20 04:50:07 PST 2025


================
@@ -1161,10 +1161,13 @@ class Parser : public CodeCompletionHandler {
     IdentifierInfo *MacroII = nullptr;
     SourceLocation AttrNameLoc;
     SmallVector<Decl *, 2> Decls;
+    unsigned NestedTypeLevel;
----------------
AaronBallman wrote:

Most attributes don't need to care about the specific type position; as we're converting the specifiers into a type, we're applying the attribute at the proper level of the type. So if the attribute only applies to an `int *`, then it's fine to write `int * [[foo]] *` because we first form the `int` type, then the `int *` type, then the `int * [[foo]]` attributed type, then the final type. So when we go to check for application of `foo`, everything is fine.

Your case is different because you want to have further restrictions on the entire type, not just the piece of the type the attribute it written on. I was thinking about this a bit last night because who needs to sleep when they can think about type attributes, and I think a potentially cleaner way to handle this is from `GetFullTypeForDeclarator()` (in SemaType.cpp) when we're forming the full type. At that point, we can form the full `int * __counted_by(size) *` type by piecing each of the types together as normal, and once we've got the full type, decide that's not valid and emit a diagnostic at that point. Do you think something like that might work?

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


More information about the cfe-commits mailing list