[clang-tools-extra] 87f03e1 - [clangd] Don't offer to expand auto in structured binding declarations.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 28 10:55:36 PDT 2020


Author: Sam McCall
Date: 2020-10-28T18:55:23+01:00
New Revision: 87f03e13ce0e840d7eb9a4a6d805d117fe165672

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

LOG: [clangd] Don't offer to expand auto in structured binding declarations.

auto must be used for the code to parse.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
index 4dfaf729c892..61f68a688252 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ExpandAutoType.cpp
@@ -53,13 +53,24 @@ REGISTER_TWEAK(ExpandAutoType)
 
 std::string ExpandAutoType::title() const { return "Expand auto type"; }
 
+// Structured bindings must use auto, e.g. `const auto& [a,b,c] = ...;`.
+// Return whether N (an AutoTypeLoc) is such an auto that must not be expanded.
+bool isStructuredBindingType(const SelectionTree::Node *N) {
+  // Walk up the TypeLoc chain, because auto may be qualified.
+  while (N && N->ASTNode.get<TypeLoc>())
+    N = N->Parent;
+  // The relevant type is the only direct type child of a Decomposition.
+  return N && N->ASTNode.get<DecompositionDecl>();
+}
+
 bool ExpandAutoType::prepare(const Selection& Inputs) {
   CachedLocation = llvm::None;
   if (auto *Node = Inputs.ASTSelection.commonAncestor()) {
     if (auto *TypeNode = Node->ASTNode.get<TypeLoc>()) {
       if (const AutoTypeLoc Result = TypeNode->getAs<AutoTypeLoc>()) {
         // Code in apply() does handle 'decltype(auto)' yet.
-        if (!Result.getTypePtr()->isDecltypeAuto())
+        if (!Result.getTypePtr()->isDecltypeAuto() &&
+            !isStructuredBindingType(Node))
           CachedLocation = Result;
       }
     }

diff  --git a/clang-tools-extra/clangd/unittests/TweakTests.cpp b/clang-tools-extra/clangd/unittests/TweakTests.cpp
index 8488c423dcdd..563be79c4c0d 100644
--- a/clang-tools-extra/clangd/unittests/TweakTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TweakTests.cpp
@@ -572,6 +572,8 @@ TEST_F(ExpandAutoTypeTest, Test) {
             R"cpp(const char * x = "test";)cpp");
 
   EXPECT_UNAVAILABLE("dec^ltype(au^to) x = 10;");
+  // expanding types in structured bindings is syntactically invalid.
+  EXPECT_UNAVAILABLE("const ^auto &[x,y] = (int[]){1,2};");
 
   // FIXME: Auto-completion in a template requires disabling delayed template
   // parsing.


        


More information about the cfe-commits mailing list