Skip to content

Commit e18ab2a

Browse files
committed
[clangd] Treat UserDefinedLiteral as a leaf in SelectionTree, sidestepping tokenization issues
Summary: Fixes clangd/clangd#203 Reviewers: kadircet Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D70446
1 parent ea8678d commit e18ab2a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

clang-tools-extra/clangd/Selection.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/AST/ASTTypeTraits.h"
1313
#include "clang/AST/DeclCXX.h"
1414
#include "clang/AST/Expr.h"
15+
#include "clang/AST/ExprCXX.h"
1516
#include "clang/AST/PrettyPrinter.h"
1617
#include "clang/AST/RecursiveASTVisitor.h"
1718
#include "clang/AST/TypeLoc.h"
@@ -245,6 +246,10 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
245246
if (canSafelySkipNode(N))
246247
return false;
247248
push(std::move(N));
249+
if (shouldSkipChildren(X)) {
250+
pop();
251+
return false;
252+
}
248253
return true;
249254
}
250255
bool dataTraverseStmtPost(Stmt *X) {
@@ -355,6 +360,15 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
355360
return true;
356361
}
357362

363+
// There are certain nodes we want to treat as leaves in the SelectionTree,
364+
// although they do have children.
365+
bool shouldSkipChildren(const Stmt *X) const {
366+
// UserDefinedLiteral (e.g. 12_i) has two children (12 and _i).
367+
// Unfortunately TokenBuffer sees 12_i as one token and can't split it.
368+
// So we treat UserDefinedLiteral as a leaf node, owning the token.
369+
return llvm::isa<UserDefinedLiteral>(X);
370+
}
371+
358372
// Pushes a node onto the ancestor stack. Pairs with pop().
359373
// Performs early hit detection for some nodes (on the earlySourceRange).
360374
void push(DynTypedNode Node) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,16 @@ TEST(SelectionTest, CommonAncestor) {
304304
}
305305
)cpp",
306306
"CallExpr"},
307+
308+
// User-defined literals are tricky: is 12_i one token or two?
309+
// For now we treat it as one, and the UserDefinedLiteral as a leaf.
310+
{
311+
R"cpp(
312+
struct Foo{};
313+
Foo operator""_ud(unsigned long long);
314+
Foo x = [[^12_ud]];
315+
)cpp",
316+
"UserDefinedLiteral"},
307317
};
308318
for (const Case &C : Cases) {
309319
Annotations Test(C.Code);

0 commit comments

Comments
 (0)