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>isAtPositionunsigned 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/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -2795,6 +2795,10 @@ /// index of the parameter when it exceeds the size of the normal bitfield. unsigned getParameterIndex(const ParmVarDecl *D) const; + // Returns "true" iff the given ParmVarDecl exists in this context at the + // given position. + bool parameterHasIndex(const ParmVarDecl *D, unsigned index) const; + /// Return a string representing the human readable name for the specified /// function declaration or file name. Used by SourceLocExpr and /// PredefinedExpr to cache evaluated results. 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 @@ -4615,6 +4615,27 @@ InnerMatcher.matches(*DeclarationStatement, Finder, Builder); } +/// 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 *ParentContext = Node.getParentFunctionOrMethod(); + return ParentContext != nullptr && + ParentContext->getParentASTContext().parameterHasIndex(&Node, N); +} + /// Matches the index expression of an array subscript expression. /// /// Given diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -10749,6 +10749,13 @@ return I->second; } +bool ASTContext::parameterHasIndex(const ParmVarDecl *D, unsigned index) const { + ParameterIndexTable::const_iterator I = ParamIndices.find(D); + if (I == ParamIndices.end()) + return false; + return I->second == index; +} + QualType ASTContext::getStringLiteralArrayType(QualType EltTy, unsigned Length) const { // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). 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,29 @@ 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(0)))); + EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1)))); + EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1)))); + + // Tests with function-decls + 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)))); + + // Tests with lamdas + 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(IsArray, Basic) { EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];", cxxNewExpr(isArray())));