Add hasInClassInitializer matcher

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 2 12:06:45 PST 2016


On Wed, Mar 2, 2016 at 2:01 PM, Julian Bangert via cfe-commits
<cfe-commits at lists.llvm.org> wrote:
> This adds a matcher for C++ in Class initializers.
>
> ---
>  include/clang/ASTMatchers/ASTMatchers.h   | 14 ++++++++++++++
>  unittests/ASTMatchers/ASTMatchersTest.cpp |  7 +++++++
>  2 files changed, 21 insertions(+)
>
> diff --git a/include/clang/ASTMatchers/ASTMatchers.h
> b/include/clang/ASTMatchers/ASTMatchers.h

Please also generate the AST matcher documentation by running
dump-ast-matchers.py and register the function in Registry.cpp. Also,
if you can provide an svn patch instead of a git patch, it would be
appreciated (especially if you need someone to commit on your behalf).

> index 21a4969..6b0a5d6 100644
> --- a/include/clang/ASTMatchers/ASTMatchers.h
> +++ b/include/clang/ASTMatchers/ASTMatchers.h
> @@ -2848,6 +2848,20 @@ AST_MATCHER(CXXCtorInitializer, isMemberInitializer)
> {
>    return Node.isMemberInitializer();
>  }
>
> +/// \brief Matches a C++ inClassInitializer matching the given matcher
> +///
> +/// Given:
> +/// \code
> +///   class A { int x = 1; };
> +/// \endcode
> +///
> +/// \c hasInClassInitializer(integerLiteral()) matches int x = 1
> +AST_MATCHER_P(FieldDecl, hasInClassInitializer,
> +              internal::Matcher<Expr>, InnerMatcher) {
> +  return Node.hasInClassInitializer()
> +      && InnerMatcher.matches(*Node.getInClassInitializer(), Finder,
> Builder);
> +}
> +
>  /// \brief Matches any argument of a call expression or a constructor call
>  /// expression.
>  ///
> diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp
> b/unittests/ASTMatchers/ASTMatchersTest.cpp
> index 133dc70..15776d7 100644
> --- a/unittests/ASTMatchers/ASTMatchersTest.cpp
> +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp
> @@ -2402,6 +2402,13 @@ TEST(HasAnyConstructorInitializer, IsBaseInitializer)
> {
>      hasName("E")))));
>  }
>
> +TEST(Matcher, inClassInitializer) {
> +  EXPECT_TRUE(matches("class A{ int x = 1; };",
> +                      fieldDecl(hasInClassInitializer(integerLiteral()))));
> +  EXPECT_FALSE(matches("class A{ int x; void b() { x = 1; } };",

This should use EXPECT_TRUE and notMatches.

~Aaron

> +
> fieldDecl(hasInClassInitializer(integerLiteral()))));
> +}
> +
>  TEST(Matcher, NewExpression) {
>    StatementMatcher New = cxxNewExpr();
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>


More information about the cfe-commits mailing list