Skip to content

Commit af071f0

Browse files
committed
[clangd] Improve ObjC property handling in SelectionTree.
Fixes clangd/clangd#233 Reviewers: dgoldman Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72634
1 parent 88d6f18 commit af071f0

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

clang-tools-extra/clangd/Selection.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,14 @@ class SelectionVisitor : public RecursiveASTVisitor<SelectionVisitor> {
462462
TraverseStmt(S->getRangeInit()) && TraverseStmt(S->getBody());
463463
});
464464
}
465+
// OpaqueValueExpr blocks traversal, we must explicitly traverse it.
466+
bool TraverseOpaqueValueExpr(OpaqueValueExpr *E) {
467+
return traverseNode(E, [&] { return TraverseStmt(E->getSourceExpr()); });
468+
}
469+
// We only want to traverse the *syntactic form* to understand the selection.
470+
bool TraversePseudoObjectExpr(PseudoObjectExpr *E) {
471+
return traverseNode(E, [&] { return TraverseStmt(E->getSyntacticForm()); });
472+
}
465473

466474
private:
467475
using Base = RecursiveASTVisitor<SelectionVisitor>;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ TEST_F(TargetDeclTest, ObjC) {
536536
[[f.x]].y = 0;
537537
}
538538
)cpp";
539-
EXPECT_DECLS("OpaqueValueExpr", "@property(atomic, retain, readwrite) I *x");
539+
EXPECT_DECLS("ObjCPropertyRefExpr",
540+
"@property(atomic, retain, readwrite) I *x");
540541

541542
Code = R"cpp(
542543
@protocol Foo

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,52 @@ TEST(SelectionTest, CommonAncestor) {
323323
Foo x = [[^12_ud]];
324324
)cpp",
325325
"UserDefinedLiteral"},
326+
326327
{
327328
R"cpp(
328329
int a;
329330
decltype([[^a]] + a) b;
330331
)cpp",
331332
"DeclRefExpr"},
333+
334+
// Objective-C OpaqueValueExpr/PseudoObjectExpr has weird ASTs.
335+
// Need to traverse the contents of the OpaqueValueExpr to the POE,
336+
// and ensure we traverse only the syntactic form of the PseudoObjectExpr.
337+
{
338+
R"cpp(
339+
@interface I{}
340+
@property(retain) I*x;
341+
@property(retain) I*y;
342+
@end
343+
void test(I *f) { [[^f]].x.y = 0; }
344+
)cpp",
345+
"DeclRefExpr"},
346+
{
347+
R"cpp(
348+
@interface I{}
349+
@property(retain) I*x;
350+
@property(retain) I*y;
351+
@end
352+
void test(I *f) { [[f.^x]].y = 0; }
353+
)cpp",
354+
"ObjCPropertyRefExpr"},
355+
// Examples with implicit properties.
356+
{
357+
R"cpp(
358+
@interface I{}
359+
-(int)foo;
360+
@end
361+
int test(I *f) { return 42 + [[^f]].foo; }
362+
)cpp",
363+
"DeclRefExpr"},
364+
{
365+
R"cpp(
366+
@interface I{}
367+
-(int)foo;
368+
@end
369+
int test(I *f) { return 42 + [[f.^foo]]; }
370+
)cpp",
371+
"ObjCPropertyRefExpr"},
332372
};
333373
for (const Case &C : Cases) {
334374
Annotations Test(C.Code);
@@ -339,6 +379,7 @@ TEST(SelectionTest, CommonAncestor) {
339379
// FIXME: Auto-completion in a template requires disabling delayed template
340380
// parsing.
341381
TU.ExtraArgs.push_back("-fno-delayed-template-parsing");
382+
TU.ExtraArgs.push_back("-xobjective-c++");
342383

343384
auto AST = TU.build();
344385
auto T = makeSelectionTree(C.Code, AST);

0 commit comments

Comments
 (0)