Author: hokein Date: Fri Oct 12 03:11:02 2018 New Revision: 344330 URL: http://llvm.org/viewvc/llvm-project?rev=344330&view=rev Log: [clangd] Support hover on "aut^o *".
Reviewers: kadircet Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, arphaman, cfe-commits Differential Revision: https://reviews.llvm.org/D53186 Modified: clang-tools-extra/trunk/clangd/XRefs.cpp clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp Modified: clang-tools-extra/trunk/clangd/XRefs.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/XRefs.cpp?rev=344330&r1=344329&r2=344330&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/XRefs.cpp (original) +++ clang-tools-extra/trunk/clangd/XRefs.cpp Fri Oct 12 03:11:02 2018 @@ -579,20 +579,28 @@ public: llvm::Optional<QualType> 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<ReferenceType>(T.getTypePtr())) + return RT->getPointeeType(); + // "auto *" is represented as a PointerType containing an AutoType + if (const PointerType *PT = dyn_cast<PointerType>(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<ReferenceType>(DeclT.getTypePtr())) - DeclT = RT->getPointeeType(); - + auto DeclT = UnwrapReferenceOrPointer(D->getType()); const AutoType *AT = dyn_cast<AutoType>(DeclT.getTypePtr()); if (AT && !AT->getDeducedType().isNull()) { // For auto, use the underlying type because the const& would be @@ -626,11 +634,7 @@ public: if (CurLoc != SearchedLocation) return true; - auto T = D->getReturnType(); - // "auto &" is represented as a ReferenceType containing an AutoType. - if (const ReferenceType *RT = dyn_cast<ReferenceType>(T.getTypePtr())) - T = RT->getPointeeType(); - + auto T = UnwrapReferenceOrPointer(D->getReturnType()); const AutoType *AT = dyn_cast<AutoType>(T.getTypePtr()); if (AT && !AT->getDeducedType().isNull()) { DeducedType = T.getUnqualifiedType(); Modified: clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp?rev=344330&r1=344329&r2=344330&view=diff ============================================================================== --- clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp (original) +++ clang-tools-extra/trunk/unittests/clangd/XRefsTests.cpp Fri Oct 12 03:11:02 2018 @@ -756,6 +756,15 @@ TEST(Hover, All) { "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 { @@ -861,6 +870,16 @@ TEST(Hover, All) { } )cpp", "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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits