diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -4671,6 +4671,23 @@
Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
+
+
Matcher<clang::ParmVarDecl> | isAtPosition | unsigned N |
+Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+list. The parameter list could be that of either a block, function, or
+objc-method.
+
+
+Given
+
+void f(int a, int b, int c) {
+}
+
+``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+
+``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+ |
+
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
@@ -4257,6 +4257,34 @@
return Matched;
}
+/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
+/// list. The parameter list could be that of either a block, function, or
+/// objc-method.
+///
+///
+/// Given
+///
+/// \code
+/// void f(int a, int b, int c) {
+/// }
+/// \endcode
+///
+/// ``parmVarDecl(isAtPosition(0))`` matches ``int a``.
+///
+/// ``parmVarDecl(isAtPosition(1))`` matches ``int b``.
+AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) {
+ const clang::DeclContext *Context = Node.getParentFunctionOrMethod();
+
+ if (const auto *Decl = dyn_cast_or_null(Context))
+ return N < Decl->param_size() && Decl->getParamDecl(N) == &Node;
+ if (const auto *Decl = dyn_cast_or_null(Context))
+ return N < Decl->param_size() && Decl->getParamDecl(N) == &Node;
+ if (const auto *Decl = dyn_cast_or_null(Context))
+ return N < Decl->param_size() && Decl->getParamDecl(N) == &Node;
+
+ return false;
+}
+
/// Matches any parameter of a function or an ObjC method declaration or a
/// block.
///
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
@@ -219,6 +219,7 @@
REGISTER_MATCHER(floatLiteral);
REGISTER_MATCHER(forEach);
REGISTER_MATCHER(forEachArgumentWithParam);
+ REGISTER_MATCHER(isAtPosition);
REGISTER_MATCHER(forEachConstructorInitializer);
REGISTER_MATCHER(forEachDescendant);
REGISTER_MATCHER(forEachOverridden);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -2643,6 +2643,45 @@
parmVarDecl(hasDefaultArgument())));
}
+TEST(IsAtPosition, Basic) {
+ EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1))));
+ EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0))));
+ EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1))));
+ EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1))));
+}
+
+TEST(IsAtPosition, FunctionDecl) {
+ EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0))));
+ EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0))));
+ EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1))));
+ EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1))));
+}
+
+TEST(IsAtPosition, Lambda) {
+ EXPECT_TRUE(
+ matches("void x() { [](int a) {}; }", parmVarDecl(isAtPosition(0))));
+ EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+ parmVarDecl(isAtPosition(0))));
+ EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }",
+ parmVarDecl(isAtPosition(1))));
+ EXPECT_TRUE(
+ notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1))));
+}
+
+TEST(IsAtPosition, BlockDecl) {
+ EXPECT_TRUE(matchesObjC(
+ "void func() { void (^my_block)(int arg) = ^void(int arg) {}; } ",
+ parmVarDecl(isAtPosition(0))));
+
+ EXPECT_TRUE(matchesObjC("void func() { void (^my_block)(int x, int y) = "
+ "^void(int x, int y) {}; } ",
+ parmVarDecl(isAtPosition(1))));
+
+ EXPECT_TRUE(notMatchesObjC(
+ "void func() { void (^my_block)(int arg) = ^void(int arg) {}; } ",
+ parmVarDecl(isAtPosition(1))));
+}
+
TEST(IsArray, Basic) {
EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];",
cxxNewExpr(isArray())));