================
@@ -0,0 +1,403 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "UseStructuredBindingCheck.h"
+#include "../utils/DeclRefExprUtils.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+static constexpr StringRef PairDeclName = "PairVarD";
+static constexpr StringRef PairVarTypeName = "PairVarType";
+static constexpr StringRef FirstVarDeclName = "FirstVarDecl";
+static constexpr StringRef SecondVarDeclName = "SecondVarDecl";
+static constexpr StringRef BeginDeclStmtName = "BeginDeclStmt";
+static constexpr StringRef EndDeclStmtName = "EndDeclStmt";
+static constexpr StringRef FirstTypeName = "FirstType";
+static constexpr StringRef SecondTypeName = "SecondType";
+static constexpr StringRef ScopeBlockName = "ScopeBlock";
+static constexpr StringRef StdTieAssignStmtName = "StdTieAssign";
+static constexpr StringRef StdTieExprName = "StdTieExpr";
+static constexpr StringRef ForRangeStmtName = "ForRangeStmt";
+static constexpr StringRef InitExprName = "init_expr";
+
+/// Matches a sequence of VarDecls matching the inner matchers, starting from
+/// the \p Iter to \p EndIter and set bindings for the first DeclStmt and the
+/// last DeclStmt if matched.
+///
+/// \p Backwards indicates whether to match the VarDecls in reverse order.
+template <typename Iterator>
+static bool matchNVarDeclStartingWith(
+    Iterator Iter, const Iterator &EndIter,
+    ArrayRef<ast_matchers::internal::Matcher<VarDecl>> InnerMatchers,
+    ast_matchers::internal::ASTMatchFinder *Finder,
+    ast_matchers::internal::BoundNodesTreeBuilder *Builder,
+    bool Backwards = false) {
+  const DeclStmt *BeginDS = nullptr;
+  const DeclStmt *EndDS = nullptr;
+  const size_t N = InnerMatchers.size();
+  size_t Count = 0;
+
+  auto Matches = [&](const Decl *VD) {
+    // We don't want redundant decls in DeclStmt.
+    if (Count == N)
+      return false;
+
+    if (const auto *Var = dyn_cast<VarDecl>(VD);
+        Var && InnerMatchers[Backwards ? N - Count - 1 : Count].matches(
+                   *Var, Finder, Builder)) {
+      ++Count;
+      return true;
+    }
+
+    return false;
+  };
+
+  for (; Iter != EndIter; ++Iter) {
+    EndDS = dyn_cast<DeclStmt>(*Iter);
+    if (!EndDS)
+      break;
+
+    if (!BeginDS)
+      BeginDS = EndDS;
+
+    for (const auto *VD :
+         llvm::reverse_conditionally(EndDS->decls(), Backwards)) {
+      if (!Matches(VD))
+        return false;
+    }
+
+    // All the matchers is satisfied in those DeclStmts.
+    if (Count == N) {
+      Builder->setBinding(
+          BeginDeclStmtName,
+          clang::DynTypedNode::create(Backwards ? *EndDS : *BeginDS));
+      Builder->setBinding(EndDeclStmtName, clang::DynTypedNode::create(
+                                               Backwards ? *BeginDS : *EndDS));
----------------
zwuis wrote:

```diff
+if (Backwards)
+  std::swap(BeginDS, EndDS);
-BeginDeclStmtName, clang::DynTypedNode::create(Backwards ? *EndDS : *BeginDS)
+BeginDeclStmtName, clang::DynTypedNode::create(*BeginDS)
-EndDeclStmtName, clang::DynTypedNode::create(Backwards ? *BeginDS : *EndDS)
+EndDeclStmtName, clang::DynTypedNode::create(*EndDS)
```

https://github.com/llvm/llvm-project/pull/158462
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to