Add an AST matcher to match member intializers of a CXXCtorInitializer.

llvm-svn: 244662
This commit is contained in:
Aaron Ballman 2015-08-11 20:42:00 +00:00
parent ed483770b1
commit ed455d4062
4 changed files with 44 additions and 0 deletions

View File

@ -1512,6 +1512,23 @@ constructorDecl(hasAnyConstructorInitializer(isBaseInitializer()))
</pre></td></tr>
<tr><td>Matcher&lt<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXCtorInitializer.html">CXXCtorInitializer</a>&gt;</td><td class="name" onclick="toggle('isMemberInitializer0')"><a name="isMemberInitializer0Anchor">isMemberInitializer</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isMemberInitializer0"><pre>Matches a constructor initializer if it is initializing a direct member, as opposed to a base.
Given
struct B {};
struct D : B {
int I;
D(int i) : I(i) {}
};
struct E : B {
E() : B() {}
};
constructorDecl(hasAnyConstructorInitializer(isMemberInitializer()))
will match D(int), but not match E().
</pre></td></tr>
<tr><td>Matcher&lt<a href="http://clang.llvm.org/doxygen/classclang_1_1CXXCtorInitializer.html">CXXCtorInitializer</a>&gt;</td><td class="name" onclick="toggle('isWritten0')"><a name="isWritten0Anchor">isWritten</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="isWritten0"><pre>Matches a constructor initializer if it is explicitly written in
code (as opposed to implicitly added by the compiler).

View File

@ -2652,6 +2652,26 @@ AST_MATCHER(CXXCtorInitializer, isBaseInitializer) {
return Node.isBaseInitializer();
}
/// \brief Matches a constructor initializer if it is initializing a member, as
/// opposed to a base.
///
/// Given
/// \code
/// struct B {};
/// struct D : B {
/// int I;
/// D(int i) : I(i) {}
/// };
/// struct E : B {
/// E() : B() {}
/// };
/// \endcode
/// constructorDecl(hasAnyConstructorInitializer(isMemberInitializer()))
/// will match D(int), but not match E().
AST_MATCHER(CXXCtorInitializer, isMemberInitializer) {
return Node.isMemberInitializer();
}
/// \brief Matches any argument of a call expression or a constructor call
/// expression.
///

View File

@ -262,6 +262,7 @@ RegistryMaps::RegistryMaps() {
REGISTER_MATCHER(isIntegral);
REGISTER_MATCHER(isInTemplateInstantiation);
REGISTER_MATCHER(isListInitialization);
REGISTER_MATCHER(isMemberInitializer);
REGISTER_MATCHER(isMoveConstructor);
REGISTER_MATCHER(isOverride);
REGISTER_MATCHER(isPrivate);

View File

@ -2097,6 +2097,12 @@ TEST(HasAnyConstructorInitializer, IsBaseInitializer) {
EXPECT_TRUE(notMatches(Code, constructorDecl(allOf(
hasAnyConstructorInitializer(allOf(isBaseInitializer(), isWritten())),
hasName("D")))));
EXPECT_TRUE(matches(Code, constructorDecl(allOf(
hasAnyConstructorInitializer(allOf(isMemberInitializer(), isWritten())),
hasName("D")))));
EXPECT_TRUE(notMatches(Code, constructorDecl(allOf(
hasAnyConstructorInitializer(allOf(isMemberInitializer(), isWritten())),
hasName("E")))));
}
TEST(Matcher, NewExpression) {