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 @@ -7001,6 +7001,26 @@ return InnerMatcher.matches(Node, Finder, Builder); } +/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter +/// list. +/// +/// 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) { + return parmVarDecl(hasAncestor(functionDecl( + hasParameter(N, parmVarDecl().bind("this_decl")))), + equalsBoundNode("this_decl")) + .matches(Node, Finder, Builder); +} + //----------------------------------------------------------------------------// // OpenMP handling. //----------------------------------------------------------------------------// 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,13 @@ parmVarDecl(hasDefaultArgument()))); } +TEST(IsAtPosition, Basic) { + EXPECT_TRUE(matches("void x(int a) {}", parmVarDecl(isAtPosition(0)))); + EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1)))); + EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1)))); + EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1)))); +} + TEST(IsArray, Basic) { EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];", cxxNewExpr(isArray())));