diff --git a/clang-tools-extra/clangd/FindTarget.cpp b/clang-tools-extra/clangd/FindTarget.cpp --- a/clang-tools-extra/clangd/FindTarget.cpp +++ b/clang-tools-extra/clangd/FindTarget.cpp @@ -619,7 +619,7 @@ llvm::SmallVector refInExpr(const Expr *E) { struct Visitor : ConstStmtVisitor { - // FIXME: handle more complicated cases, e.g. ObjC, designated initializers. + // FIXME: handle more complicated cases: more ObjC, designated initializers. llvm::SmallVector Refs; void VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E) { @@ -660,6 +660,14 @@ /*IsDecl=*/false, {E->getPack()}}); } + + void VisitObjCPropertyRefExpr(const ObjCPropertyRefExpr *E) { + Refs.push_back(ReferenceLoc{ + NestedNameSpecifierLoc(), E->getLocation(), + /*IsDecl=*/false, + // Select the getter, setter, or @property depending on the call. + explicitReferenceTargets(DynTypedNode::create(*E), {})}); + } }; Visitor V; @@ -780,6 +788,19 @@ return true; } + bool TraverseOpaqueValueExpr(OpaqueValueExpr *OVE) { + visitNode(DynTypedNode::create(*OVE)); + // Not clear why the source expression is skipped by default... + return RecursiveASTVisitor::TraverseStmt(OVE->getSourceExpr()); + } + + bool TraversePseudoObjectExpr(PseudoObjectExpr *POE) { + visitNode(DynTypedNode::create(*POE)); + // Traverse only the syntactic form to find the *written* references. + // (The semantic form also contains lots of duplication) + return RecursiveASTVisitor::TraverseStmt(POE->getSyntacticForm()); + } + // We re-define Traverse*, since there's no corresponding Visit*. // TemplateArgumentLoc is the only way to get locations for references to // template template parameters. diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -589,6 +589,7 @@ // parsing. TU.ExtraArgs.push_back("-fno-delayed-template-parsing"); TU.ExtraArgs.push_back("-std=c++2a"); + TU.ExtraArgs.push_back("-xobjective-c++"); auto AST = TU.build(); auto *TestDecl = &findDecl(AST, "foo"); @@ -696,7 +697,7 @@ void foo() { $0^Struct $1^x; $2^Typedef $3^y; - static_cast<$4^Struct*>(0); + (void) static_cast<$4^Struct*>(0); } )cpp", "0: targets = {Struct}\n" @@ -707,10 +708,10 @@ // Name qualifiers. {R"cpp( namespace a { namespace b { struct S { typedef int type; }; } } - void foo() { + int foo() { $0^a::$1^b::$2^S $3^x; using namespace $4^a::$5^b; - $6^S::$7^type $8^y; + return $6^S::$7^type(5); } )cpp", "0: targets = {a}\n" @@ -720,8 +721,7 @@ "4: targets = {a}\n" "5: targets = {a::b}, qualifier = 'a::'\n" "6: targets = {a::b::S}\n" - "7: targets = {a::b::S::type}, qualifier = 'struct S::'\n" - "8: targets = {y}, decl\n"}, + "7: targets = {a::b::S::type}, qualifier = 'struct S::'\n"}, // Simple templates. {R"cpp( template struct vector { using value_type = T; }; @@ -791,8 +791,8 @@ #define FOO a #define BAR b - void foo(int a, int b) { - $0^FOO+$1^BAR; + int foo(int a, int b) { + return $0^FOO+$1^BAR; } )cpp", "0: targets = {a}\n" @@ -989,7 +989,7 @@ }; // delegating initializer class $10^Foo { - $11^Foo(int); + $11^Foo(int) {} $12^Foo(): $13^Foo(111) {} }; } @@ -1123,7 +1123,37 @@ "3: targets = {foo::bar}, decl\n" "4: targets = {T}\n" "5: targets = {t}, decl\n" - "6: targets = {t}\n"}}; + "6: targets = {t}\n"}, + // Objective-C: properties + { + R"cpp( + @interface I {} + @property(retain) I* x; + @property(retain) I* y; + @end + I *f; + void foo() { + $0^f.$1^x.$2^y = 0; + } + )cpp", + "0: targets = {f}\n" + "1: targets = {I::x}\n" + "2: targets = {I::y}\n"}, + // Objective-C: implicit properties + { + R"cpp( + @interface I {} + -(I*)x; + -(void)setY:(I*)y; + @end + I *f; + void foo() { + $0^f.$1^x.$2^y = 0; + } + )cpp", + "0: targets = {f}\n" + "1: targets = {I::x}\n" + "2: targets = {I::setY:}\n"}}; for (const auto &C : Cases) { llvm::StringRef ExpectedCode = C.first;