[PATCH] D65067: [clangd] Fix auto expand not work on "const au^to s = 123;"

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 22 02:29:32 PDT 2019


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

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D65067

Files:
  clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
  clang-tools-extra/clangd/unittests/TweakTests.cpp


Index: clang-tools-extra/clangd/unittests/TweakTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -663,6 +663,35 @@
     const char * x = "test";
   )cpp";
   checkTransform(ID, Input, Output);
+
+  // qualified auto type
+  Input = R"cpp(
+    class Foo {};
+    const au^to x = Foo();
+  )cpp";
+  Output = R"cpp(
+    class Foo {};
+    const Foo x = Foo();
+  )cpp";
+  checkTransform(ID, Input, Output);
+
+  Input = R"cpp(
+    volatile au^to x = 1;
+  )cpp";
+  Output = R"cpp(
+    volatile int x = 1;
+  )cpp";
+  checkTransform(ID, Input, Output);
+
+  Input = R"cpp(
+    class Foo {};
+    const au^to *x = new Foo();
+  )cpp";
+  Output = R"cpp(
+    class Foo {};
+    const Foo *x = new Foo();
+  )cpp";
+  checkTransform(ID, Input, Output);
 }
 
 } // namespace
Index: clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
+++ clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
@@ -60,9 +60,12 @@
   CachedLocation = llvm::None;
   if (auto *Node = Inputs.ASTSelection.commonAncestor()) {
     if (auto *TypeNode = Node->ASTNode.get<TypeLoc>()) {
-      if (const AutoTypeLoc Result = TypeNode->getAs<AutoTypeLoc>()) {
+      auto Node = *TypeNode;
+      // Skip any qualifiers (e.g. const) around auto.
+      if (const auto QTL = Node.getAs<QualifiedTypeLoc>())
+        Node = QTL.getUnqualifiedLoc();
+      if (const AutoTypeLoc Result = Node.getAs<AutoTypeLoc>())
         CachedLocation = Result;
-      }
     }
   }
   return (bool) CachedLocation;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65067.211032.patch
Type: text/x-patch
Size: 1760 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190722/9ee872d9/attachment.bin>


More information about the cfe-commits mailing list