diff options
| author | Manuel Klimek <klimek@google.com> | 2012-07-25 10:02:02 +0000 |
|---|---|---|
| committer | Manuel Klimek <klimek@google.com> | 2012-07-25 10:02:02 +0000 |
| commit | e923569b1b2289b53e7bb10c0fd734672b5c0bbc (patch) | |
| tree | baf6e84db5b4515fc159425b9f4937636582f027 | |
| parent | e1efe31948970c6f54c8f1ddaa3286c29394965a (diff) | |
| download | bcm5719-llvm-e923569b1b2289b53e7bb10c0fd734672b5c0bbc.tar.gz bcm5719-llvm-e923569b1b2289b53e7bb10c0fd734672b5c0bbc.zip | |
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".
llvm-svn: 160716
| -rw-r--r-- | clang/include/clang/ASTMatchers/ASTMatchers.h | 16 | ||||
| -rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersTest.cpp | 15 |
2 files changed, 27 insertions, 4 deletions
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 61571e5fa0b..a5348d0baa8 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -130,6 +130,17 @@ inline internal::PolymorphicMatcherWithParam0<internal::TrueMatcher> anything() 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 @@ AST_MATCHER_P(UnaryOperator, hasUnaryOperand, 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 && diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp index f76a59696f8..91095eb3ff1 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -39,6 +39,12 @@ TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) { } #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 @@ TEST(HasDestinationType, MatchesSimpleCase) { 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())); } |

