From abe922342d67d4ffe05b366a5a2af972185272f8 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 8 Apr 2013 16:44:05 +0000 Subject: [PATCH] Add matcher for NamespaceDecls. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@179027 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/ASTMatchers/ASTMatchers.h | 14 +++++++++++++- unittests/ASTMatchers/ASTMatchersTest.cpp | 12 ++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index f10addcb7a..fdb8709ec2 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -129,7 +129,8 @@ typedef internal::Matcher NestedNameSpecifierLocMatcher; /// \endcode /// /// Usable as: Any Matcher -inline internal::PolymorphicMatcherWithParam0 anything() { +inline internal::PolymorphicMatcherWithParam0 +anything() { return internal::PolymorphicMatcherWithParam0(); } @@ -157,6 +158,17 @@ const internal::VariadicAllOfMatcher decl; /// \endcode const internal::VariadicDynCastAllOfMatcher namedDecl; +/// \brief Matches a declaration of a namespace. +/// +/// Given +/// \code +/// namespace {} +/// namespace test {} +/// \endcode +/// namespaceDecl() +/// matches "namespace {}" and "namespace test {}" +const internal::VariadicDynCastAllOfMatcher namespaceDecl; + /// \brief Matches C++ class declarations. /// /// Example matches \c X, \c Z diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp index 301b4f7c8a..82f349fb0e 100644 --- a/unittests/ASTMatchers/ASTMatchersTest.cpp +++ b/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -337,14 +337,22 @@ TEST(DeclarationMatcher, hasDeclContext) { " class D {};" " }" "}", - recordDecl(hasDeclContext(namedDecl(hasName("M")))))); + recordDecl(hasDeclContext(namespaceDecl(hasName("M")))))); EXPECT_TRUE(notMatches( "namespace N {" " namespace M {" " class D {};" " }" "}", - recordDecl(hasDeclContext(namedDecl(hasName("N")))))); + recordDecl(hasDeclContext(namespaceDecl(hasName("N")))))); + + EXPECT_TRUE(matches("namespace {" + " namespace M {" + " class D {};" + " }" + "}", + recordDecl(hasDeclContext(namespaceDecl( + hasName("M"), hasDeclContext(namespaceDecl())))))); } TEST(ClassTemplate, DoesNotMatchClass) { -- 2.40.0