[clang-tools-extra] [clangd] Do not skip Attr nodes in SelectionTree traversal (PR #174199)
Quan Zhuo via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 2 03:54:27 PST 2026
https://github.com/quanzhuo created https://github.com/llvm/llvm-project/pull/174199
SelectionTree attempts to skip nodes that don't intersect with the selection for performance. However, Attr nodes (like AlignedAttr) often have inaccurate source ranges (e.g. pointing to a single location).
Previously, canSafelySkipNode handled nodes that *have* attributes attached, but failed to account for the case where the node being visited *is* the attribute itself. This caused `alignas(WALDO)` to be skipped, breaking features like go-to-definition on WALDO.
This commit adds a check to ensure explicit Attr nodes are visited, fixing the issue.
Fixes https://github.com/clangd/clangd/issues/2502
>From 2f9ccb917fc5e29612117836d2530b4a92fbc701 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=85=A8=E5=8D=93?= <quanzhuo at kylinos.cn>
Date: Fri, 2 Jan 2026 19:49:59 +0800
Subject: [PATCH] [clangd] Do not skip Attr nodes in SelectionTree traversal
This commit adds a check to ensure explicit Attr nodes are visited,
Fixes https://github.com/clangd/clangd/issues/2502
---
clang-tools-extra/clangd/Selection.cpp | 5 +++++
clang-tools-extra/clangd/unittests/SelectionTests.cpp | 5 +++++
2 files changed, 10 insertions(+)
diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp
index faa00d20497fa..01f1470f7fac3 100644
--- a/clang-tools-extra/clangd/Selection.cpp
+++ b/clang-tools-extra/clangd/Selection.cpp
@@ -817,6 +817,11 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
if (llvm::any_of(getAttributes(N),
[](const Attr *A) { return !A->isImplicit(); }))
return false;
+ // Attributes themselves also often have bad source ranges.
+ if (const auto *A = N.get<Attr>()) {
+ if (!A->isImplicit())
+ return false;
+ }
if (!SelChecker.mayHit(S)) {
dlog("{2}skip: {0} {1}", printNodeToString(N, PrintPolicy),
S.printToString(SM), indent());
diff --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
index 63c0403ab2e70..5e897fae79df4 100644
--- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -608,6 +608,11 @@ TEST(SelectionTest, CommonAncestor) {
template <typename T> void g(D<[[^T]]> auto abc) {}
)cpp",
"TemplateTypeParmTypeLoc"},
+ {R"cpp(
+ const unsigned WALDO = 64;
+ struct alignas([[WA^LDO]]) foo {};
+ )cpp",
+ "DeclRefExpr"},
};
for (const Case &C : Cases) {
More information about the cfe-commits
mailing list