diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -591,6 +591,15 @@
+
Matcher<Decl> | bindingDecl | Matcher<BindingDecl>... |
+Matches binding declarations
+Example matches foo and bar
+(matcher = bindingDecl()
+
+ auto [foo, bar] = std::make_pair{42, 42};
+ |
+
+
Matcher<Decl> | blockDecl | Matcher<BlockDecl>... |
Matches block declarations.
@@ -6011,6 +6020,24 @@
|
+Matcher<BindingDecl> | forDecomposition | Matcher<ValueDecl> InnerMatcher |
+Matches the DecompositionDecl the binding belongs to.
+
+For example, in:
+void foo()
+{
+ int arr[3];
+ auto &[f, s, t] = arr;
+
+ f = 42;
+}
+The matcher:
+ bindingDecl(hasName("f"),
+ forDecomposition(decompositionDecl())
+matches 'f' in 'auto &[f, s, t]'.
+ |
+
+
Matcher<BlockDecl> | hasAnyParameter | Matcher<ParmVarDecl> InnerMatcher |
Matches any parameter of a function or an ObjC method declaration or a
block.
@@ -7090,6 +7117,40 @@
|
+Matcher<DecompositionDecl> | hasAnyBinding | Matcher<BindingDecl> InnerMatcher |
+Matches any binding of a DecompositionDecl.
+
+For example, in:
+void foo()
+{
+ int arr[3];
+ auto &[f, s, t] = arr;
+
+ f = 42;
+}
+The matcher:
+ decompositionDecl(hasAnyBinding(bindingDecl(hasName("f").bind("fBinding"))))
+matches the decomposition decl with 'f' bound to "fBinding".
+ |
+
+
+Matcher<DecompositionDecl> | hasBinding | unsigned N, Matcher<BindingDecl> InnerMatcher |
+Matches the Nth binding of a DecompositionDecl.
+
+For example, in:
+void foo()
+{
+ int arr[3];
+ auto &[f, s, t] = arr;
+
+ f = 42;
+}
+The matcher:
+ decompositionDecl(hasBinding(0, bindingDecl(hasName("f").bind("fBinding"))))
+matches the decomposition decl with 'f' bound to "fBinding".
+ |
+
+
Matcher<DoStmt> | hasBody | Matcher<Stmt> InnerMatcher |
|
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -347,6 +347,16 @@
extern const internal::VariadicDynCastAllOfMatcher
decompositionDecl;
+/// Matches binding declarations
+/// Example matches \c foo and \c bar
+/// (matcher = bindingDecl()
+///
+/// \code
+/// auto [foo, bar] = std::make_pair{42, 42};
+/// \endcode
+extern const internal::VariadicDynCastAllOfMatcher
+ bindingDecl;
+
/// Matches a declaration of a linkage specification.
///
/// Given
@@ -7379,6 +7389,80 @@
Expr::NPC_ValueDependentIsNull);
}
+/// Matches the DecompositionDecl the binding belongs to.
+///
+/// For example, in:
+/// \code
+/// void foo()
+/// {
+/// int arr[3];
+/// auto &[f, s, t] = arr;
+///
+/// f = 42;
+/// }
+/// \endcode
+/// The matcher:
+/// \code
+/// bindingDecl(hasName("f"),
+/// forDecomposition(decompositionDecl())
+/// \endcode
+/// matches 'f' in 'auto &[f, s, t]'.
+AST_MATCHER_P(BindingDecl, forDecomposition, internal::Matcher,
+ InnerMatcher) {
+ if (const ValueDecl *VD = Node.getDecomposedDecl())
+ return InnerMatcher.matches(*VD, Finder, Builder);
+ return false;
+}
+
+/// Matches the Nth binding of a DecompositionDecl.
+///
+/// For example, in:
+/// \code
+/// void foo()
+/// {
+/// int arr[3];
+/// auto &[f, s, t] = arr;
+///
+/// f = 42;
+/// }
+/// \endcode
+/// The matcher:
+/// \code
+/// decompositionDecl(hasBinding(0,
+/// bindingDecl(hasName("f").bind("fBinding"))))
+/// \endcode
+/// matches the decomposition decl with 'f' bound to "fBinding".
+AST_MATCHER_P2(DecompositionDecl, hasBinding, unsigned, N,
+ internal::Matcher, InnerMatcher) {
+ if (Node.bindings().size() <= N)
+ return false;
+ return InnerMatcher.matches(*Node.bindings()[N], Finder, Builder);
+}
+
+/// Matches any binding of a DecompositionDecl.
+///
+/// For example, in:
+/// \code
+/// void foo()
+/// {
+/// int arr[3];
+/// auto &[f, s, t] = arr;
+///
+/// f = 42;
+/// }
+/// \endcode
+/// The matcher:
+/// \code
+/// decompositionDecl(hasAnyBinding(bindingDecl(hasName("f").bind("fBinding"))))
+/// \endcode
+/// matches the decomposition decl with 'f' bound to "fBinding".
+AST_MATCHER_P(DecompositionDecl, hasAnyBinding, internal::Matcher,
+ InnerMatcher) {
+ return llvm::any_of(Node.bindings(), [&](const auto *Binding) {
+ return InnerMatcher.matches(*Binding, Finder, Builder);
+ });
+}
+
/// Matches declaration of the function the statement belongs to
///
/// Given:
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -144,6 +144,7 @@
REGISTER_MATCHER(binaryConditionalOperator);
REGISTER_MATCHER(binaryOperator);
REGISTER_MATCHER(binaryOperation);
+ REGISTER_MATCHER(bindingDecl);
REGISTER_MATCHER(blockDecl);
REGISTER_MATCHER(blockExpr);
REGISTER_MATCHER(blockPointerType);
@@ -226,6 +227,7 @@
REGISTER_MATCHER(exprWithCleanups);
REGISTER_MATCHER(fieldDecl);
REGISTER_MATCHER(floatLiteral);
+ REGISTER_MATCHER(forDecomposition);
REGISTER_MATCHER(forEach);
REGISTER_MATCHER(forEachArgumentWithParam);
REGISTER_MATCHER(forEachArgumentWithParamType);
@@ -248,6 +250,7 @@
REGISTER_MATCHER(hasAncestor);
REGISTER_MATCHER(hasAnyArgument);
REGISTER_MATCHER(hasAnyBase);
+ REGISTER_MATCHER(hasAnyBinding);
REGISTER_MATCHER(hasAnyClause);
REGISTER_MATCHER(hasAnyConstructorInitializer);
REGISTER_MATCHER(hasAnyDeclaration);
@@ -266,6 +269,7 @@
REGISTER_MATCHER(hasAttr);
REGISTER_MATCHER(hasAutomaticStorageDuration);
REGISTER_MATCHER(hasBase);
+ REGISTER_MATCHER(hasBinding);
REGISTER_MATCHER(hasBitWidth);
REGISTER_MATCHER(hasBody);
REGISTER_MATCHER(hasCanonicalType);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
--- a/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2129,6 +2129,37 @@
EXPECT_TRUE(matchesObjC(ObjCString, objcFinallyStmt()));
}
+TEST(ASTMatchersTest, DecompositionDecl) {
+ StringRef Code = R"cpp(
+void foo()
+{
+ int arr[3];
+ auto &[f, s, t] = arr;
+
+ f = 42;
+}
+ )cpp";
+ EXPECT_TRUE(matchesConditionally(
+ Code, decompositionDecl(hasBinding(0, bindingDecl(hasName("f")))), true,
+ {"-std=c++17"}));
+ EXPECT_FALSE(matchesConditionally(
+ Code, decompositionDecl(hasBinding(42, bindingDecl(hasName("f")))), true,
+ {"-std=c++17"}));
+ EXPECT_FALSE(matchesConditionally(
+ Code, decompositionDecl(hasBinding(0, bindingDecl(hasName("s")))), true,
+ {"-std=c++17"}));
+ EXPECT_TRUE(matchesConditionally(
+ Code, decompositionDecl(hasBinding(1, bindingDecl(hasName("s")))), true,
+ {"-std=c++17"}));
+
+ EXPECT_TRUE(matchesConditionally(
+ Code,
+ bindingDecl(decl().bind("self"), hasName("f"),
+ forDecomposition(decompositionDecl(
+ hasAnyBinding(bindingDecl(equalsBoundNode("self")))))),
+ true, {"-std=c++17"}));
+}
+
TEST(ASTMatchersTestObjC, ObjCAutoreleasePoolStmt) {
StringRef ObjCString = "void f() {"
"@autoreleasepool {"