Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -2232,6 +2232,16 @@ +
Matches array new expressions. + +Given: + MyClass *p1 = new MyClass[10]; +cxxNewExpr(isArray()) + matches the expression 'new MyClass[10]'. +
Matches overloaded operator names. @@ -4380,6 +4390,16 @@
Matches array new expressions with a given array size. + +Given: + MyClass *p1 = new MyClass[10]; +cxxNewExpr(hasArraySize(intgerLiteral(equals(10)))) + matches the expression 'new MyClass[10]'. +
Matches a node if the declaration associated with that node matches the given matcher. Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -5761,6 +5761,31 @@ return Node.hasExternalFormalLinkage(); } +/// \brief Matches array new expressions. +/// +/// Given: +/// \code +/// MyClass *p1 = new MyClass[10]; +/// \endcode +/// cxxNewExpr(isArray()) +/// matches the expression 'new MyClass[10]'. +AST_MATCHER(CXXNewExpr, isArray) { + return Node.isArray(); +} + +/// \brief Matches array new expressions with a given array size. +/// +/// Given: +/// \code +/// MyClass *p1 = new MyClass[10]; +/// \endcode +/// cxxNewExpr(hasArraySize(intgerLiteral(equals(10)))) +/// matches the expression 'new MyClass[10]'. +AST_MATCHER_P(CXXNewExpr, hasArraySize, internal::Matcher, InnerMatcher) { + return Node.isArray() && + InnerMatcher.matches(*Node.getArraySize(), Finder, Builder); +} + } // end namespace ast_matchers } // end namespace clang Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -220,6 +220,7 @@ REGISTER_MATCHER(hasAnyUsingShadowDecl); REGISTER_MATCHER(hasArgument); REGISTER_MATCHER(hasArgumentOfType); + REGISTER_MATCHER(hasArraySize); REGISTER_MATCHER(hasAttr); REGISTER_MATCHER(hasAutomaticStorageDuration); REGISTER_MATCHER(hasBase); @@ -302,6 +303,7 @@ REGISTER_MATCHER(isAnonymous); REGISTER_MATCHER(isAnyCharacter); REGISTER_MATCHER(isAnyPointer); + REGISTER_MATCHER(isArray); REGISTER_MATCHER(isArrow); REGISTER_MATCHER(isBaseInitializer); REGISTER_MATCHER(isBitField); Index: unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -1983,5 +1983,15 @@ namedDecl(hasExternalFormalLinkage()))); } +TEST(IsArray, Basic) { + EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];", + cxxNewExpr(isArray()))); +} + +TEST(HasArraySize, Basic) { + EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];", + cxxNewExpr(hasArraySize(integerLiteral(equals(10)))))); +} + } // namespace ast_matchers } // namespace clang