[cfe-commits] [PATCH] ThisExpr matcher

Manuel Klimek klimek at google.com
Mon Oct 22 01:39:42 PDT 2012


And while I approve of the new matcher, can't your original problem be
solved with the memberExpr() matcher?


On Sun, Oct 21, 2012 at 8:41 PM, Gábor Horváth <xazax.hun at gmail.com> wrote:

> Hi djasper, klimek,
>
> Given a snippet like :
> struct foo {
>   int i;
>   int f() { return i; }
> };
>
> I know no way to determine if we match where i is used determine if i is a
> member of foo or not. Or if we match the fieldDecl of foo, we can not match
> where i is used.
>
> For other similar cases there is declRefExpr, but it is not usable in this
> scenario, however thisExpr can be a solution for this case.
>
> For example to match functor predicates that are not stateless I'm using
> something like:
> thisExpr(hasAncestor(methodDecl(allOf(hasName("operator()"),
> returns(asString("_Bool")))))).bind("id")
>
> http://llvm-reviews.chandlerc.com/D70
>
> Files:
>   include/clang/ASTMatchers/ASTMatchers.h
>   unittests/ASTMatchers/ASTMatchersTest.cpp
>
> Index: include/clang/ASTMatchers/ASTMatchers.h
> ===================================================================
> --- include/clang/ASTMatchers/ASTMatchers.h
> +++ include/clang/ASTMatchers/ASTMatchers.h
> @@ -546,6 +546,18 @@
>    Stmt,
>    CXXConstructExpr> constructExpr;
>
> +/// \brief Matches implicit and explicit this expressions.
> +///
> +/// Example matches "this" before i.
> +///     (matcher = thisExpr())
> +/// \code
> +/// struct foo {
> +///   int i;
> +///   int f() { return i; }
> +/// };
> +/// \endcode
> +const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr> thisExpr;
> +
>  /// \brief Matches nodes where temporaries are created.
>  ///
>  /// Example matches FunctionTakesString(GetStringByValue())
> Index: unittests/ASTMatchers/ASTMatchersTest.cpp
> ===================================================================
> --- unittests/ASTMatchers/ASTMatchersTest.cpp
> +++ unittests/ASTMatchers/ASTMatchersTest.cpp
> @@ -1333,6 +1333,13 @@
>                   Constructor1Arg));
>  }
>
> +TEST(Matcher,ThisExpr) {
> +  EXPECT_TRUE(
> +      matches("struct X { int a; int f () { return a; } };", thisExpr()));
> +  EXPECT_TRUE(
> +      notMatches("struct X { int f () { int a; return a; } };",
> thisExpr()));
> +}
> +
>  TEST(Matcher, BindTemporaryExpression) {
>    StatementMatcher TempExpression = bindTemporaryExpr();
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121022/b9d56757/attachment.html>


More information about the cfe-commits mailing list