[PATCH] D53186: [clangd] Support hover on "aut^o *".

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 12 01:24:37 PDT 2018


hokein created this revision.
hokein added a reviewer: kadircet.
Herald added subscribers: arphaman, jkorous, MaskRay, ioeric, ilya-biryukov.

Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D53186

Files:
  clangd/XRefs.cpp
  unittests/clangd/XRefsTests.cpp


Index: unittests/clangd/XRefsTests.cpp
===================================================================
--- unittests/clangd/XRefsTests.cpp
+++ unittests/clangd/XRefsTests.cpp
@@ -755,6 +755,15 @@
           )cpp",
           "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
@@ -862,6 +871,16 @@
           )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
             struct Bar {};
Index: clangd/XRefs.cpp
===================================================================
--- clangd/XRefs.cpp
+++ clangd/XRefs.cpp
@@ -579,20 +579,28 @@
 
   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 @@
     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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53186.169357.patch
Type: text/x-patch
Size: 2967 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181012/cf27b547/attachment.bin>


More information about the cfe-commits mailing list