Index: cfe/trunk/docs/LibASTMatchersReference.html =================================================================== --- cfe/trunk/docs/LibASTMatchersReference.html +++ cfe/trunk/docs/LibASTMatchersReference.html @@ -307,6 +307,19 @@ +Matcher<Decl>translationUnitDeclMatcher<TranslationUnitDecl>... +
Matches the top declaration context.
+
+Given
+  int X;
+  namespace NS {
+  int Y;
+  }  namespace NS
+decl(hasDeclContext(translationUnitDecl()))
+  matches "int X", but not "int Y".
+
+ + Matcher<Decl>typedefDeclMatcher<TypedefDecl>...
Matches typedef declarations.
 
@@ -1525,7 +1538,7 @@
 
-Matcher<CXXRecordDecl>isDerivedFromStringRef BaseName +Matcher<CXXRecordDecl>isDerivedFromstd::string BaseName
Overloaded method as shortcut for isDerivedFrom(hasName(...)).
 
@@ -1544,7 +1557,7 @@ -Matcher<CXXRecordDecl>isSameOrDerivedFromStringRef BaseName +Matcher<CXXRecordDecl>isSameOrDerivedFromstd::string BaseName
Overloaded method as shortcut for
 isSameOrDerivedFrom(hasName(...)).
 
@@ -1721,19 +1734,6 @@ -Matcher<Decl>isInstantiated -
Matches declarations that are template instantiations or are inside
-template instantiations.
-
-Given
-  template<typename T> void A(T t) { T i; }
-  A(0);
-  A(0U);
-functionDecl(isInstantiated())
-  matches 'A(int) {...};' and 'A(unsigned) {...}'.
-
- - Matcher<Decl>isPrivate
Matches private C++ declarations.
 
@@ -2093,22 +2093,6 @@
 
-Matcher<Stmt>isInTemplateInstantiation -
Matches statements inside of a template instantiation.
-
-Given
-  int j;
-  template<typename T> void A(T t) { T i; j += 42;}
-  A(0);
-  A(0U);
-declStmt(isInTemplateInstantiation())
-  matches 'int i;' and 'unsigned i'.
-unless(stmt(isInTemplateInstantiation()))
-  will NOT match j += 42; as it's shared between the template definition and
-  instantiation.
-
- - Matcher<TagDecl>isDefinition
Matches if a declaration has a body attached.
 
@@ -2229,6 +2213,16 @@
 
+Matcher<Type>voidType +
Matches type void.
+
+Given
+ struct S { void func(); };
+functionDecl(returns(voidType()))
+  matches "void func();"
+
+ + Matcher<UnaryExprOrTypeTraitExpr>ofKindUnaryExprOrTypeTrait Kind
Matches unary expressions of a certain kind.
 
@@ -2323,6 +2317,35 @@
 Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
 
+ +Matcher<internal::Matcher<Decl>>isInstantiated +
Matches declarations that are template instantiations or are inside
+template instantiations.
+
+Given
+  template<typename T> void A(T t) { T i; }
+  A(0);
+  A(0U);
+functionDecl(isInstantiated())
+  matches 'A(int) {...};' and 'A(unsigned) {...}'.
+
+ + +Matcher<internal::Matcher<Stmt>>isInTemplateInstantiation +
Matches statements inside of a template instantiation.
+
+Given
+  int j;
+  template<typename T> void A(T t) { T i; j += 42;}
+  A(0);
+  A(0U);
+declStmt(isInTemplateInstantiation())
+  matches 'int i;' and 'unsigned i'.
+unless(stmt(isInTemplateInstantiation()))
+  will NOT match j += 42; as it's shared between the template definition and
+  instantiation.
+
+ Index: cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h +++ cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h @@ -143,6 +143,20 @@ /// Usable as: Any Matcher inline internal::TrueMatcher anything() { return internal::TrueMatcher(); } +/// \brief Matches the top declaration context. +/// +/// Given +/// \code +/// int X; +/// namespace NS { +/// int Y; +/// } // namespace NS +/// \endcode +/// decl(hasDeclContext(translationUnitDecl())) +/// matches "int X", but not "int Y". +const internal::VariadicDynCastAllOfMatcher + translationUnitDecl; + /// \brief Matches typedef declarations. /// /// Given Index: cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp +++ cfe/trunk/lib/ASTMatchers/Dynamic/Registry.cpp @@ -315,6 +315,7 @@ REGISTER_MATCHER(throughUsingDecl); REGISTER_MATCHER(throwExpr); REGISTER_MATCHER(to); + REGISTER_MATCHER(translationUnitDecl); REGISTER_MATCHER(tryStmt); REGISTER_MATCHER(type); REGISTER_MATCHER(typedefDecl); Index: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp =================================================================== --- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp +++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp @@ -379,6 +379,21 @@ EXPECT_TRUE(matches("class D{};", decl(hasDeclContext(decl())))); } +TEST(DeclarationMatcher, translationUnitDecl) { + const std::string Code = "int MyVar1;\n" + "namespace NameSpace {\n" + "int MyVar2;\n" + "} // namespace NameSpace\n"; + EXPECT_TRUE(matches( + Code, varDecl(hasName("MyVar1"), hasDeclContext(translationUnitDecl())))); + EXPECT_FALSE(matches( + Code, varDecl(hasName("MyVar2"), hasDeclContext(translationUnitDecl())))); + EXPECT_TRUE(matches( + Code, + varDecl(hasName("MyVar2"), + hasDeclContext(decl(hasDeclContext(translationUnitDecl())))))); +} + TEST(DeclarationMatcher, LinkageSpecification) { EXPECT_TRUE(matches("extern \"C\" { void foo() {}; }", linkageSpecDecl())); EXPECT_TRUE(notMatches("void foo() {};", linkageSpecDecl()));