[cfe-commits] [PATCH] ThisExpr matcher
Gábor Horváth
xazax.hun at gmail.com
Sun Oct 21 12:41:43 PDT 2012
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 --------------
A non-text attachment was scrubbed...
Name: D70.1.patch
Type: text/x-patch
Size: 1293 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121021/c3defcc2/attachment.bin>
More information about the cfe-commits
mailing list