Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -4170,6 +4170,18 @@ +
Matches the operator new function declaration that the given new expression calls. + +Example matches new expression for B in the last line + (matcher = cxxNewExpr(operatorNew(functionDecl(parameterCountIs(2))))) + void *operator new(size_t SZ, void *P) noexcept; + int *A = new int(); + char Buffer[sizeof(int)]; + int *B = new (&Buffer[0]) int(); +
Matches the first method of a class or struct that satisfies InnerMatcher. Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -4028,6 +4028,24 @@ return Node.isVirtualAsWritten(); } +/// \brief Matches the operator new declaration that the given new expression +/// calls +/// +/// Example matches new expression for B in the last line +/// (matcher = cxxNewExpr(operatorNew(functionDecl(parameterCountIs(2))))) +/// \code +/// void *operator new(size_t SZ, void *P) noexcept; +/// int *A = new int(); +/// char Buffer[sizeof(int)]; +/// int *B = new (&Buffer[0]) int(); +/// \endcode +AST_MATCHER_P(CXXNewExpr, operatorNew, + internal::Matcher, InnerMatcher) { + const FunctionDecl *OperatorNew = Node.getOperatorNew(); + return (OperatorNew != nullptr && + InnerMatcher.matches(*OperatorNew, Finder, Builder)); +} + /// \brief Matches if the given method or class declaration is final. /// /// Given: Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -362,6 +362,7 @@ REGISTER_MATCHER(on); REGISTER_MATCHER(onImplicitObjectArgument); REGISTER_MATCHER(opaqueValueExpr); + REGISTER_MATCHER(operatorNew); REGISTER_MATCHER(parameterCountIs); REGISTER_MATCHER(parenExpr); REGISTER_MATCHER(parenListExpr); Index: unittests/ASTMatchers/ASTMatchersTraversalTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -2189,5 +2189,14 @@ functionDecl(hasName("bar")))))); } +TEST(CXXNewExpr, OperatorNew) { + std::string Fragment = "int *A = new int();"; + EXPECT_TRUE(matches( + Fragment, cxxNewExpr(operatorNew(functionDecl(parameterCountIs(1)))))); + + EXPECT_TRUE(notMatches( + Fragment, cxxNewExpr(operatorNew(functionDecl(parameterCountIs(2)))))); +} + } // namespace ast_matchers } // namespace clang