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

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rG87f03e13ce0e: [clangd] Don't offer to expand auto in structured binding declarations. (authored by sammccall).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89700/new/

https://reviews.llvm.org/D89700

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
@@ -572,6 +572,8 @@
             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.
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
@@ -53,13 +53,24 @@
 
 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;
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D89700.301339.patch
Type: text/x-patch
Size: 1966 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201028/ca9f0baf/attachment.bin>


More information about the cfe-commits mailing list