Skip to content

Commit cb9ccd3

Browse files
committed
[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
1 parent ca044f5 commit cb9ccd3

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

clang-tools-extra/clangd/Selection.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ void recordMetrics(const SelectionTree &S, const LangOptions &Lang) {
6060

6161
// Return the range covering a node and all its children.
6262
SourceRange getSourceRange(const DynTypedNode &N) {
63+
// DeclTypeTypeLoc::getSourceRange() is incomplete, which would lead to
64+
// failing to descend into the child expression.
65+
// decltype(2+2);
66+
// ~~~~~~~~~~~~~ <-- correct range
67+
// ~~~~~~~~ <-- range reported by getSourceRange()
68+
// ~~~~~~~~~~~~ <-- range with this hack(i.e, missing closing paren)
69+
// FIXME: Alter DecltypeTypeLoc to contain parentheses locations and get
70+
// rid of this patch.
71+
if (const auto *TL = N.get<TypeLoc>()) {
72+
if (auto DT = TL->getAs<DecltypeTypeLoc>()) {
73+
SourceRange S = DT.getSourceRange();
74+
S.setEnd(DT.getUnderlyingExpr()->getEndLoc());
75+
return S;
76+
}
77+
}
6378
// MemberExprs to implicitly access anonymous fields should not claim any
6479
// tokens for themselves. Given:
6580
// struct A { struct { int b; }; };
@@ -647,17 +662,6 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
647662
// heuristics. We should consider only pruning critical TypeLoc nodes, to
648663
// be more robust.
649664

650-
// DeclTypeTypeLoc::getSourceRange() is incomplete, which would lead to
651-
// failing
652-
// to descend into the child expression.
653-
// decltype(2+2);
654-
// ~~~~~~~~~~~~~ <-- correct range
655-
// ~~~~~~~~ <-- range reported by getSourceRange()
656-
// ~~~~~~~~~~~~ <-- range with this hack(i.e, missing closing paren)
657-
// FIXME: Alter DecltypeTypeLoc to contain parentheses locations and get
658-
// rid of this patch.
659-
if (auto DT = TL->getAs<DecltypeTypeLoc>())
660-
S.setEnd(DT.getUnderlyingExpr()->getEndLoc());
661665
// AttributedTypeLoc may point to the attribute's range, NOT the modified
662666
// type's range.
663667
if (auto AT = TL->getAs<AttributedTypeLoc>())

clang-tools-extra/clangd/unittests/SelectionTests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ TEST(SelectionTest, CommonAncestor) {
385385
decltype([[^a]] + a) b;
386386
)cpp",
387387
"DeclRefExpr"},
388+
{"[[decltype]]^(1) b;", "DecltypeTypeLoc"}, // Not the VarDecl.
388389

389390
// Objective-C nullability attributes.
390391
{

0 commit comments

Comments
 (0)