[PATCH] D53347: [clangd] Simplify auto hover

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 16 16:16:08 PDT 2018


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

Use helper from clang. Also fixes some weird corner cases, e.g.

  auto (*foo)() = bar;


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D53347

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


Index: unittests/clangd/XRefsTests.cpp
===================================================================
--- unittests/clangd/XRefsTests.cpp
+++ unittests/clangd/XRefsTests.cpp
@@ -999,6 +999,13 @@
           )cpp",
           "",
       },
+      {
+          R"cpp(// More compilcated structured types.
+            int bar();
+            ^auto (*foo)() = bar;
+          )cpp",
+          "int",
+      },
   };
 
   for (const OneTest &Test : Tests) {
Index: clangd/XRefs.cpp
===================================================================
--- clangd/XRefs.cpp
+++ clangd/XRefs.cpp
@@ -579,17 +579,6 @@
 
   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;
@@ -600,18 +589,9 @@
         D->getTypeSourceInfo()->getTypeLoc().getBeginLoc() != SearchedLocation)
       return true;
 
-    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
-      // represented twice: written in the code and in the hover.
-      // Example: "const auto I = 1", we only want "int" when hovering on auto,
-      // not "const int".
-      //
-      // For decltype(auto), take the type as is because it cannot be written
-      // with qualifiers or references but its decuded type can be const-ref.
-      DeducedType = AT->isDecltypeAuto() ? DeclT : DeclT.getUnqualifiedType();
-    }
+    auto AT = D->getType()->getContainedAutoType();
+    if (AT && !AT->getDeducedType().isNull())
+      DeducedType = AT->getDeducedType();
     return true;
   }
 
@@ -634,12 +614,11 @@
     if (CurLoc != SearchedLocation)
       return true;
 
-    auto T = UnwrapReferenceOrPointer(D->getReturnType());
-    const AutoType *AT = dyn_cast<AutoType>(T.getTypePtr());
+    const AutoType *AT = D->getReturnType()->getContainedAutoType();
     if (AT && !AT->getDeducedType().isNull()) {
-      DeducedType = T.getUnqualifiedType();
+      DeducedType = AT->getDeducedType();
     } else { // auto in a trailing return type just points to a DecltypeType.
-      const DecltypeType *DT = dyn_cast<DecltypeType>(T.getTypePtr());
+      const DecltypeType *DT = dyn_cast<DecltypeType>(D->getReturnType());
       if (!DT->getUnderlyingType().isNull())
         DeducedType = DT->getUnderlyingType();
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53347.169917.patch
Type: text/x-patch
Size: 2987 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181016/989ecf9b/attachment.bin>


More information about the cfe-commits mailing list