Skip to content

Commit f5465e7

Browse files
committed
[clangd] Include expression in DecltypeTypeLoc sourcerange while building SelectionTree
Summary: Currently AST only contains the location for `decltype` keyword, therefore we were skipping expressions inside decltype while building selection tree. This patch extends source range in such cases to contain the expression as well. A proper fix would require changes to Sema and DecltypeTypeLoc to contain these location information. Fixes clangd/clangd#250. Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72594
1 parent 15078d7 commit f5465e7

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

clang-tools-extra/clangd/Selection.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,19 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
527527
// don't intersect the selection may be recursively skipped.
528528
bool canSafelySkipNode(const DynTypedNode &N) {
529529
SourceRange S = N.getSourceRange();
530+
if (auto *TL = N.get<TypeLoc>()) {
531+
// DeclTypeTypeLoc::getSourceRange() is incomplete, which would lead to
532+
// failing
533+
// to descend into the child expression.
534+
// decltype(2+2);
535+
// ~~~~~~~~~~~~~ <-- correct range
536+
// ~~~~~~~~ <-- range reported by getSourceRange()
537+
// ~~~~~~~~~~~~ <-- range with this hack(i.e, missing closing paren)
538+
// FIXME: Alter DecltypeTypeLoc to contain parentheses locations and get
539+
// rid of this patch.
540+
if (auto DT = TL->getAs<DecltypeTypeLoc>())
541+
S.setEnd(DT.getUnderlyingExpr()->getEndLoc());
542+
}
530543
if (!SelChecker.mayHit(S)) {
531544
dlog("{1}skip: {0}", printNodeToString(N, PrintPolicy), indent());
532545
dlog("{1}skipped range = {0}", S.printToString(SM), indent(1));

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

+6
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ TEST(SelectionTest, CommonAncestor) {
323323
Foo x = [[^12_ud]];
324324
)cpp",
325325
"UserDefinedLiteral"},
326+
{
327+
R"cpp(
328+
int a;
329+
decltype([[^a]] + a) b;
330+
)cpp",
331+
"DeclRefExpr"},
326332
};
327333
for (const Case &C : Cases) {
328334
Annotations Test(C.Code);

0 commit comments

Comments
 (0)