From e6d2a96cc9689be584c3bfc09ac527a03a25b3bc Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 18 Sep 2012 13:36:17 +0000 Subject: [PATCH] Add missing matcher for C-style cast expressions. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Patch by Gábor Horváth. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@164123 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/ASTMatchers/ASTMatchers.h | 10 ++++++++++ unittests/ASTMatchers/ASTMatchersTest.cpp | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index 9fdd9333b5..2a1acdb0e6 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -865,6 +865,16 @@ const internal::VariadicDynCastAllOfMatcher< Stmt, CXXConstCastExpr> constCastExpr; +/// \brief Matches a C-style cast expression. +/// +/// Example: Matches (int*) 2.2f in +/// \code +/// int i = (int) 2.2f; +/// \endcode +const internal::VariadicDynCastAllOfMatcher< + Stmt, + CStyleCastExpr> cStyleCastExpr; + /// \brief Matches explicit cast expressions. /// /// Matches any cast expression written in user code, whether it be a diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index 348034f0ed..b8ffaa4300 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -2229,6 +2229,20 @@ TEST(StaticCast, DoesNotMatchOtherCasts) { staticCastExpr())); } +TEST(CStyleCast, MatchesSimpleCase) { + EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr())); +} + +TEST(CStyleCast, DoesNotMatchOtherCasts) { + EXPECT_TRUE(notMatches("char* p = static_cast(0);" + "char q, *r = const_cast(&q);" + "void* s = reinterpret_cast(&s);" + "struct B { virtual ~B() {} }; struct D : B {};" + "B b;" + "D* t = dynamic_cast(&b);", + cStyleCastExpr())); +} + TEST(HasDestinationType, MatchesSimpleCase) { EXPECT_TRUE(matches("char* p = static_cast(0);", staticCastExpr(hasDestinationType( -- 2.50.1