summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaojian Wu <hokein.wu@gmail.com>2022-01-04 12:07:37 +0100
committerHaojian Wu <hokein.wu@gmail.com>2022-01-04 12:11:58 +0100
commitcb9ccd38c55f729a6bd7986bbdcb34755b774240 (patch)
tree9ebb9fb10e712710b4ad248e3f03b66e8f1ad6a0
parentca044f5369c7c156c1c7d35601b09fe610cc73d3 (diff)
downloadllvm-cb9ccd38c55f729a6bd7986bbdcb34755b774240.tar.gz
[clangd] Move the selection decltype hack to getSourceRange.
Previously, it was in canSafelySkipNode, which is only used to decide whether we should descend into it and its children, and we still used the incomplete Decltypeloc.getSourceRange() to claim tokens, which will cause some tokens were not claimed correctly. Separate a change of https://reviews.llvm.org/D116536 Reviewed By: sammccall Differential Revision: https://reviews.llvm.org/D116586
-rw-r--r--clang-tools-extra/clangd/Selection.cpp26
-rw-r--r--clang-tools-extra/clangd/unittests/SelectionTests.cpp1
2 files changed, 16 insertions, 11 deletions
diff --git a/clang-tools-extra/clangd/Selection.cpp b/clang-tools-extra/clangd/Selection.cpp
index 2024228e2b58..40021c62d9e2 100644
--- a/clang-tools-extra/clangd/Selection.cpp
+++ b/clang-tools-extra/clangd/Selection.cpp
@@ -60,6 +60,21 @@ void recordMetrics(const SelectionTree &S, const LangOptions &Lang) {
// Return the range covering a node and all its children.
SourceRange getSourceRange(const DynTypedNode &N) {
+ // DeclTypeTypeLoc::getSourceRange() is incomplete, which would lead to
+ // failing to descend into the child expression.
+ // decltype(2+2);
+ // ~~~~~~~~~~~~~ <-- correct range
+ // ~~~~~~~~ <-- range reported by getSourceRange()
+ // ~~~~~~~~~~~~ <-- range with this hack(i.e, missing closing paren)
+ // FIXME: Alter DecltypeTypeLoc to contain parentheses locations and get
+ // rid of this patch.
+ if (const auto *TL = N.get<TypeLoc>()) {
+ if (auto DT = TL->getAs<DecltypeTypeLoc>()) {
+ SourceRange S = DT.getSourceRange();
+ S.setEnd(DT.getUnderlyingExpr()->getEndLoc());
+ return S;
+ }
+ }
// MemberExprs to implicitly access anonymous fields should not claim any
// tokens for themselves. Given:
// struct A { struct { int b; }; };
@@ -647,17 +662,6 @@ private:
// heuristics. We should consider only pruning critical TypeLoc nodes, to
// be more robust.
- // DeclTypeTypeLoc::getSourceRange() is incomplete, which would lead to
- // failing
- // to descend into the child expression.
- // decltype(2+2);
- // ~~~~~~~~~~~~~ <-- correct range
- // ~~~~~~~~ <-- range reported by getSourceRange()
- // ~~~~~~~~~~~~ <-- range with this hack(i.e, missing closing paren)
- // FIXME: Alter DecltypeTypeLoc to contain parentheses locations and get
- // rid of this patch.
- if (auto DT = TL->getAs<DecltypeTypeLoc>())
- S.setEnd(DT.getUnderlyingExpr()->getEndLoc());
// AttributedTypeLoc may point to the attribute's range, NOT the modified
// type's range.
if (auto AT = TL->getAs<AttributedTypeLoc>())
diff --git a/clang-tools-extra/clangd/unittests/SelectionTests.cpp b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
index 971487c9cd27..6583d89af695 100644
--- a/clang-tools-extra/clangd/unittests/SelectionTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SelectionTests.cpp
@@ -385,6 +385,7 @@ TEST(SelectionTest, CommonAncestor) {
decltype([[^a]] + a) b;
)cpp",
"DeclRefExpr"},
+ {"[[decltype]]^(1) b;", "DecltypeTypeLoc"}, // Not the VarDecl.
// Objective-C nullability attributes.
{