[cfe-commits] [PATCH] C++11 matchers
Gábor Horváth
reviews at llvm-reviews.chandlerc.com
Thu Sep 20 06:28:54 PDT 2012
Hi djasper, klimek,
Added a few C++11 matchers, that were trivial to implement.
http://llvm-reviews.chandlerc.com/D46
Files:
include/clang/ASTMatchers/ASTMatchers.h
include/clang/ASTMatchers/ASTMatchersInternal.h
unittests/ASTMatchers/ASTMatchersTest.cpp
unittests/ASTMatchers/ASTMatchersTest.h
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -489,6 +489,14 @@
/// \endcode
const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr;
+/// \brief Matches lambda expressions.
+///
+/// Example matches [&](){return 5;}
+/// \code
+/// [&](){return 5;}
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr;
+
/// \brief Matches member call expressions.
///
/// Example matches x.y()
@@ -667,6 +675,14 @@
/// \endcode
const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt;
+/// \brief Matches C++11 range for statements.
+///
+/// forRangeStmt() matches 'for (auto a : i)'
+/// \code
+/// int i[] = {1, 2, 3}; for (auto a : i);
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt> forRangeStmt;
+
/// \brief Matches the increment statement of a for loop.
///
/// Example:
@@ -776,6 +792,18 @@
Stmt,
IntegerLiteral> integerLiteral;
+/// \brief Matches (C++11) call to user defined literal operator call.
+///
+/// Example match: "foo"s
+const internal::VariadicDynCastAllOfMatcher<
+ Stmt,
+ UserDefinedLiteral> userDefinedLiteral;
+
+/// \brief Matches (C++11) nullptr literal.
+const internal::VariadicDynCastAllOfMatcher<
+ Stmt,
+ CXXNullPtrLiteralExpr> nullPtrLiteralExpr;
+
/// \brief Matches binary operator expressions.
///
/// Example matches a || b
Index: include/clang/ASTMatchers/ASTMatchersInternal.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchersInternal.h
+++ include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -39,6 +39,7 @@
#include "clang/AST/DeclCXX.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/Stmt.h"
+#include "clang/AST/StmtCXX.h"
#include "clang/AST/Type.h"
#include "clang/ASTMatchers/ASTTypeTraits.h"
#include "llvm/ADT/VariadicFunction.h"
Index: unittests/ASTMatchers/ASTMatchersTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -805,6 +805,23 @@
MethodOnYPointer));
}
+TEST(Matcher, Lambda) {
+ EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };",
+ lambdaExpr()));
+}
+
+TEST(Matcher, ForRange) {
+ EXPECT_TRUE(matches("#include <initializer_list>\n"
+ "void f() { for (auto &a : {1, 2, 3}); }",
+ forRangeStmt()));
+}
+
+TEST(Matcher, UserDefinedLiteral) {
+ EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i)"
+ "{ return i + 1; } char c = 'a'_inc;",
+ userDefinedLiteral()));
+}
+
TEST(HasType, MatchesAsString) {
EXPECT_TRUE(
matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }",
@@ -1541,6 +1558,10 @@
EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
}
+TEST(Matcher, NullPtrLiteral) {
+ EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr()));
+}
+
TEST(Matcher, Conditions) {
StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true))));
@@ -2086,15 +2107,15 @@
}
TEST(Member, MatchesMemberAllocationFunction) {
- EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }"
+ EXPECT_TRUE(matches("namespace std { typedef decltype(sizeof(int)) size_t; }"
"class X { void *operator new(std::size_t); };",
methodDecl(ofClass(hasName("X")))));
EXPECT_TRUE(matches("class X { void operator delete(void*); };",
methodDecl(ofClass(hasName("X")))));
EXPECT_TRUE(matches(
- "namespace std { typedef typeof(sizeof(int)) size_t; }"
+ "namespace std { typedef decltype(sizeof(int)) size_t; }"
"class X { void operator delete[](void*, std::size_t); };",
methodDecl(ofClass(hasName("X")))));
}
Index: unittests/ASTMatchers/ASTMatchersTest.h
===================================================================
--- unittests/ASTMatchers/ASTMatchersTest.h
+++ unittests/ASTMatchers/ASTMatchersTest.h
@@ -57,7 +57,7 @@
Finder.addMatcher(AMatcher, new VerifyMatch(0, &Found));
OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder));
// Some tests use typeof, which is a gnu extension.
- std::vector<std::string> Args(1, "-std=gnu++98");
+ std::vector<std::string> Args(1, "-std=c++11");
if (!runToolOnCodeWithArgs(Factory->create(), Code, Args)) {
return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46.1.patch
Type: text/x-patch
Size: 4716 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20120920/3d2eee9a/attachment.bin>
More information about the cfe-commits
mailing list