Index: clang-tools-extra/trunk/clangd/XRefs.cpp =================================================================== --- clang-tools-extra/trunk/clangd/XRefs.cpp +++ clang-tools-extra/trunk/clangd/XRefs.cpp @@ -579,20 +579,28 @@ llvm::Optional getDeducedType() { return DeducedType; } + // Remove the surrounding Reference or Pointer type of the given type T. + QualType UnwrapReferenceOrPointer(QualType T) { + // "auto &" is represented as a ReferenceType containing an AutoType + if (const ReferenceType *RT = dyn_cast(T.getTypePtr())) + return RT->getPointeeType(); + // "auto *" is represented as a PointerType containing an AutoType + if (const PointerType *PT = dyn_cast(T.getTypePtr())) + return PT->getPointeeType(); + return T; + } + // Handle auto initializers: //- auto i = 1; //- decltype(auto) i = 1; //- auto& i = 1; + //- auto* i = &a; bool VisitDeclaratorDecl(DeclaratorDecl *D) { if (!D->getTypeSourceInfo() || D->getTypeSourceInfo()->getTypeLoc().getBeginLoc() != SearchedLocation) return true; - auto DeclT = D->getType(); - // "auto &" is represented as a ReferenceType containing an AutoType - if (const ReferenceType *RT = dyn_cast(DeclT.getTypePtr())) - DeclT = RT->getPointeeType(); - + auto DeclT = UnwrapReferenceOrPointer(D->getType()); const AutoType *AT = dyn_cast(DeclT.getTypePtr()); if (AT && !AT->getDeducedType().isNull()) { // For auto, use the underlying type because the const& would be @@ -626,11 +634,7 @@ if (CurLoc != SearchedLocation) return true; - auto T = D->getReturnType(); - // "auto &" is represented as a ReferenceType containing an AutoType. - if (const ReferenceType *RT = dyn_cast(T.getTypePtr())) - T = RT->getPointeeType(); - + auto T = UnwrapReferenceOrPointer(D->getReturnType()); const AutoType *AT = dyn_cast(T.getTypePtr()); if (AT && !AT->getDeducedType().isNull()) { DeducedType = T.getUnqualifiedType(); Index: clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp =================================================================== --- clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp +++ clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp @@ -756,6 +756,15 @@ "int", }, { + R"cpp(// Simple initialization with auto* + void foo() { + int a = 1; + ^auto* i = &a; + } + )cpp", + "int", + }, + { R"cpp(// Auto with initializer list. namespace std { @@ -863,6 +872,16 @@ "struct Bar", }, { + R"cpp(// auto* in function return + struct Bar {}; + ^auto* test() { + Bar* bar; + return bar; + } + )cpp", + "struct Bar", + }, + { R"cpp(// const auto& in function return struct Bar {}; const ^auto& test() {