]> granicus.if.org Git - clang/commitdiff
Add parameterCountIs() matcher.
authorDaniel Jasper <djasper@google.com>
Tue, 4 Dec 2012 11:54:27 +0000 (11:54 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 4 Dec 2012 11:54:27 +0000 (11:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169257 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/ASTMatchers/ASTMatchers.h
unittests/ASTMatchers/ASTMatchersTest.cpp

index a70dd5c378bd82f38159823d68f5eded41a4c4fd..71ce10f89329d84f9e8b5e325c9b6b01b1580d58 100644 (file)
@@ -1974,6 +1974,19 @@ AST_MATCHER_P(FunctionDecl, hasAnyParameter,
   return false;
 }
 
+/// \brief Matches \c FunctionDecls that have a specific parameter count.
+///
+/// Given
+/// \code
+///   void f(int i) {}
+///   void g(int i, int j) {}
+/// \endcode
+/// functionDecl(parameterCountIs(2))
+///   matches g(int i, int j) {}
+AST_MATCHER_P(FunctionDecl, parameterCountIs, unsigned, N) {
+  return Node.getNumParams() == N;
+}
+
 /// \brief Matches the return type of a function declaration.
 ///
 /// Given:
index ad072aa7ce359540ec64711ef24fbb939aa62c88..f3d377b771ffcc41c31f06b10b745edcc4bb0499 100644 (file)
@@ -1238,6 +1238,14 @@ TEST(Matcher, ArgumentCount) {
   EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg));
 }
 
+TEST(Matcher, ParameterCount) {
+  DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1));
+  EXPECT_TRUE(matches("void f(int i) {}", Function1Arg));
+  EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg));
+  EXPECT_TRUE(notMatches("void f() {}", Function1Arg));
+  EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg));
+}
+
 TEST(Matcher, References) {
   DeclarationMatcher ReferenceClassX = varDecl(
       hasType(references(recordDecl(hasName("X")))));