[clang-tools-extra] Improve attribute range handling for attributed function types in sel… (PR #163926)
Quan Zhuo via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 17 02:27:47 PDT 2025
================
@@ -958,6 +958,18 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
claimRange(SourceRange(FTL.getLParenLoc(), FTL.getEndLoc()), Result);
return;
}
+ if (auto ATL = TL->getAs<AttributedTypeLoc>()) {
+ // For attributed function types like `int foo() [[attr]]`, the
+ // AttributedTypeLoc's range includes the function name. We want to
+ // allow the function name to be associated with the FunctionDecl
+ // rather than the AttributedTypeLoc, so we only claim the attribute
+ // range itself.
+ if (ATL.getModifiedLoc().getAs<FunctionTypeLoc>()) {
+ // Only claim the attribute's source range, not the whole type.
+ claimRange(ATL.getLocalSourceRange(), Result);
----------------
quanzhuo wrote:
The `RecursiveASTVisitor` automatically traverses the inner type of `AttributedTypeLoc`. Looking at the RAV implementation in RecursiveASTVisitor.h:
```cpp
DEF_TRAVERSE_TYPELOC(AttributedType,
{ TRY_TO(TraverseTypeLoc(TL.getModifiedLoc())); })
```
This means when we visit an `AttributedTypeLoc`, the RAV will automatically traverse its `ModifiedLoc` (the inner `FunctionTypeLoc`).
So the flow is:
1. We encounter `AttributedTypeLoc` and only claim the attribute's source range
2. RAV automatically traverses the inner `FunctionTypeLoc`
3. The existing special handling for `FunctionTypeLoc` (lines 957-959) claims the parameter list range
If we claim the function's parameter list in the `AttributedTypeLoc` handler as suggested, it would be claimed twice: once in the `AttributedTypeLoc` case and again when the inner `FunctionTypeLoc` is traversed.
https://github.com/llvm/llvm-project/pull/163926
More information about the cfe-commits
mailing list