Add an AST matcher for hasDefaultArgument() to match on parameter declarations that have a default value.

Patch by Julie Hockett.

llvm-svn: 318794
This commit is contained in:
Aaron Ballman 2017-11-21 19:22:34 +00:00
parent b67ce00e4d
commit 5f8980a995
4 changed files with 28 additions and 0 deletions

View File

@ -3183,6 +3183,15 @@ Usable as: Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1TagDe
</pre></td></tr>
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1ParmVarDecl.html">ParmVarDecl</a>&gt;</td><td class="name" onclick="toggle('hasDefaultArgument0')"><a name="hasDefaultArgument0Anchor">hasDefaultArgument</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="hasDefaultArgument0"><pre>Matches a declaration that has default arguments.
Example matches y (matcher = parmVarDecl(hasDefaultArgument()))
void x(int val) {}
void y(int val = 0) {}
</pre></td></tr>
<tr><td>Matcher&lt;<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>&gt;</td><td class="name" onclick="toggle('asString0')"><a name="asString0Anchor">asString</a></td><td>std::string Name</td></tr>
<tr><td colspan="4" class="doc" id="asString0"><pre>Matches if the matched type is represented by the given string.

View File

@ -5817,6 +5817,17 @@ AST_MATCHER(NamedDecl, hasExternalFormalLinkage) {
return Node.hasExternalFormalLinkage();
}
/// \brief Matches a declaration that has default arguments.
///
/// Example matches y (matcher = parmVarDecl(hasDefaultArgument()))
/// \code
/// void x(int val) {}
/// void y(int val = 0) {}
/// \endcode
AST_MATCHER(ParmVarDecl, hasDefaultArgument) {
return Node.hasDefaultArg();
}
} // namespace ast_matchers
} // namespace clang

View File

@ -248,6 +248,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(hasDeclaration);
REGISTER_MATCHER(hasDeclContext);
REGISTER_MATCHER(hasDeducedType);
REGISTER_MATCHER(hasDefaultArgument);
REGISTER_MATCHER(hasDescendant);
REGISTER_MATCHER(hasDestinationType);
REGISTER_MATCHER(hasDynamicExceptionSpec);

View File

@ -1991,5 +1991,12 @@ TEST(HasExternalFormalLinkage, Basic) {
namedDecl(hasExternalFormalLinkage())));
}
TEST(HasDefaultArgument, Basic) {
EXPECT_TRUE(matches("void x(int val = 0) {}",
parmVarDecl(hasDefaultArgument())));
EXPECT_TRUE(notMatches("void x(int val) {}",
parmVarDecl(hasDefaultArgument())));
}
} // namespace ast_matchers
} // namespace clang