[clang-tools-extra] aeaeb9e - [clangd] Make ExpandAutoType not available on template params.

Adam Czachorowski via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 15 05:25:58 PST 2021


Author: Adam Czachorowski
Date: 2021-01-15T14:19:05+01:00
New Revision: aeaeb9e6bdc90d9c4b839ac0e4edc6255021cced

URL: https://github.com/llvm/llvm-project/commit/aeaeb9e6bdc90d9c4b839ac0e4edc6255021cced
DIFF: https://github.com/llvm/llvm-project/commit/aeaeb9e6bdc90d9c4b839ac0e4edc6255021cced.diff

LOG: [clangd] Make ExpandAutoType not available on template params.

We cannot expand auto when used inside a template param (C++17 feature),
so do not offer it there.

Differential Revision: https://reviews.llvm.org/D94719

Added: 
    

Modified: 
    clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
    clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
index cb87cc764bc3..3776e1c3505d 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
@@ -82,6 +82,15 @@ bool isDeducedAsLambda(const SelectionTree::Node *Node, SourceLocation Loc) {
   return false;
 }
 
+// Returns true iff "auto" in Node is really part of the template parameter,
+// which we cannot expand.
+bool isTemplateParam(const SelectionTree::Node *Node) {
+  if (Node->Parent)
+    if (Node->Parent->ASTNode.get<NonTypeTemplateParmDecl>())
+      return true;
+  return false;
+}
+
 bool ExpandAutoType::prepare(const Selection& Inputs) {
   CachedLocation = llvm::None;
   if (auto *Node = Inputs.ASTSelection.commonAncestor()) {
@@ -90,7 +99,8 @@ bool ExpandAutoType::prepare(const Selection& Inputs) {
         // Code in apply() does handle 'decltype(auto)' yet.
         if (!Result.getTypePtr()->isDecltypeAuto() &&
             !isStructuredBindingType(Node) &&
-            !isDeducedAsLambda(Node, Result.getBeginLoc()))
+            !isDeducedAsLambda(Node, Result.getBeginLoc()) &&
+            !isTemplateParam(Node))
           CachedLocation = Result;
       }
     }

diff  --git a/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp b/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
index 6c96ecc2332f..5554b68b31c7 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/ExpandAutoTypeTests.cpp
@@ -80,6 +80,9 @@ TEST_F(ExpandAutoTypeTest, Test) {
   // unknown types in a template should not be replaced
   EXPECT_THAT(apply("template <typename T> void x() { ^auto y = T::z(); }"),
               StartsWith("fail: Could not deduce type for 'auto' type"));
+
+  ExtraArgs.push_back("-std=c++17");
+  EXPECT_UNAVAILABLE("template <au^to X> class Y;");
 }
 
 } // namespace


        


More information about the cfe-commits mailing list