[cfe-commits] r160716 - in /cfe/trunk: include/clang/ASTMatchers/ASTMatchers.h unittests/ASTMatchers/ASTMatchersTest.cpp
Manuel Klimek
klimek at google.com
Wed Jul 25 03:02:03 PDT 2012
Author: klimek
Date: Wed Jul 25 05:02:02 2012
New Revision: 160716
URL: http://llvm.org/viewvc/llvm-project?rev=160716&view=rev
Log:
Introduces the 'decl' matcher which was missing for a while
and became necessary with the change to require BindableMatchers
for binding.
Also fixes PR 13445: "hasSourceExpression only works for implicit casts".
Modified:
cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp
Modified: cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h?rev=160716&r1=160715&r2=160716&view=diff
==============================================================================
--- cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h (original)
+++ cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h Wed Jul 25 05:02:02 2012
@@ -130,6 +130,17 @@
return internal::PolymorphicMatcherWithParam0<internal::TrueMatcher>();
}
+/// \brief Matches declarations.
+///
+/// Examples matches \c X, \c C, and the friend declaration inside \c C;
+/// \code
+/// void X();
+/// class C {
+/// friend X;
+/// };
+/// \endcode
+const internal::VariadicDynCastAllOfMatcher<Decl, Decl> decl;
+
/// \brief Matches a declaration of anything that could have a name.
///
/// Example matches X, S, the anonymous union type, i, and U;
@@ -1549,15 +1560,14 @@
InnerMatcher.matches(*Operand, Finder, Builder));
}
-/// \brief Matches if the implicit cast's source expression matches the given
-/// matcher.
+/// \brief Matches if the cast's source expression matches the given matcher.
///
/// Example: matches "a string" (matcher =
/// hasSourceExpression(constructorCall()))
///
/// class URL { URL(string); };
/// URL url = "a string";
-AST_MATCHER_P(ImplicitCastExpr, hasSourceExpression,
+AST_MATCHER_P(CastExpr, hasSourceExpression,
internal::Matcher<Expr>, InnerMatcher) {
const Expr* const SubExpression = Node.getSubExpr();
return (SubExpression != NULL &&
Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp?rev=160716&r1=160715&r2=160716&view=diff
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp (original)
+++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp Wed Jul 25 05:02:02 2012
@@ -39,6 +39,12 @@
}
#endif
+TEST(Decl, MatchesDeclarations) {
+ EXPECT_TRUE(notMatches("", decl(usingDecl())));
+ EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
+ decl(usingDecl())));
+}
+
TEST(NameableDeclaration, MatchesVariousDecls) {
DeclarationMatcher NamedX = nameableDeclaration(hasName("X"));
EXPECT_TRUE(matches("typedef int X;", NamedX));
@@ -2037,13 +2043,20 @@
pointsTo(TypeMatcher(anything())))))));
}
-TEST(HasSourceExpression, MatchesSimpleCase) {
+TEST(HasSourceExpression, MatchesImplicitCasts) {
EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };"
"void r() {string a_string; URL url = a_string; }",
expression(implicitCast(
hasSourceExpression(constructorCall())))));
}
+TEST(HasSourceExpression, MatchesExplicitCasts) {
+ EXPECT_TRUE(matches("float x = static_cast<float>(42);",
+ expression(explicitCast(
+ hasSourceExpression(hasDescendant(
+ expression(integerLiteral())))))));
+}
+
TEST(Statement, DoesNotMatchDeclarations) {
EXPECT_TRUE(notMatches("class X {};", statement()));
}
More information about the cfe-commits
mailing list