diff --git a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp b/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp --- a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp +++ b/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp @@ -16,16 +16,12 @@ namespace tidy { namespace cert { -AST_MATCHER(CXXNewExpr, isPlacementNew) { - return Node.getNumPlacementArgs() > 0; -} - void DefaultOperatorNewAlignmentCheck::registerMatchers(MatchFinder *Finder) { // Check not applicable in C++17 (or newer). if (getLangOpts().CPlusPlus17) return; - Finder->addMatcher(cxxNewExpr(unless(isPlacementNew())).bind("new"), this); + Finder->addMatcher(cxxNewExpr(unless(isPlacement())).bind("new"), this); } void DefaultOperatorNewAlignmentCheck::check( diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -91,7 +91,7 @@ hasArgument(0, cxxNewExpr(hasType(pointsTo(qualType(hasCanonicalType( equalsBoundNode(PointerType))))), - CanCallCtor) + CanCallCtor, unless(isPlacement())) .bind(NewExpression)), unless(isInTemplateInstantiation())) .bind(ConstructorCall)))), @@ -101,7 +101,8 @@ cxxMemberCallExpr( thisPointerType(getSmartPointerTypeMatcher()), callee(cxxMethodDecl(hasName("reset"))), - hasArgument(0, cxxNewExpr(CanCallCtor).bind(NewExpression)), + hasArgument(0, cxxNewExpr(CanCallCtor, unless(isPlacement())) + .bind(NewExpression)), unless(isInTemplateInstantiation())) .bind(ResetCall), this); @@ -119,8 +120,6 @@ const auto *Type = Result.Nodes.getNodeAs(PointerType); const auto *New = Result.Nodes.getNodeAs(NewExpression); - if (New->getNumPlacementArgs() != 0) - return; // Skip when this is a new-expression with `auto`, e.g. new auto(1) if (New->getType()->getPointeeType()->getContainedAutoType()) return; diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -2547,6 +2547,16 @@ +Matcher<CXXNewExpr>isPlacement +
Matches placement new expressions.
+
+Given:
+  MyClass *p1 = new (Storage) MyClass();
+cxxNewExpr(isPlacement())
+  matches the expression 'new (Storage) MyClass()'.
+
+ + Matcher<CXXOperatorCallExpr>hasOverloadedOperatorNameStringRef Name
Matches overloaded operator names.
 
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -6752,6 +6752,16 @@
   return Node.isArray();
 }
 
+/// Matches placement new expressions.
+///
+/// Given:
+/// \code
+///   MyClass *p1 = new (Storage) MyClass();
+/// \endcode
+/// cxxNewExpr(isPlacement())
+///   matches the expression 'new (Storage) MyClass()'.
+AST_MATCHER(CXXNewExpr, isPlacement) { return Node.getNumPlacementArgs() > 0; }
+
 /// Matches array new expressions with a given array size.
 ///
 /// Given:
diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
--- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -394,6 +394,7 @@
   REGISTER_MATCHER(isNoneKind);
   REGISTER_MATCHER(isOMPStructuredBlock);
   REGISTER_MATCHER(isOverride);
+  REGISTER_MATCHER(isPlacement)
   REGISTER_MATCHER(isPrivate);
   REGISTER_MATCHER(isProtected);
   REGISTER_MATCHER(isPublic);
diff --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -3124,5 +3124,37 @@
   EXPECT_TRUE(notMatches("template class A {};", Matcher));
 }
 
+TEST(CXXNewExpr, Array) {
+  StatementMatcher NewArray = cxxNewExpr(isArray());
+
+  EXPECT_TRUE(matches("void foo() { int *Ptr = new int[10]; }", NewArray));
+  EXPECT_TRUE(notMatches("void foo() { int *Ptr = new int; }", NewArray));
+
+  StatementMatcher NewArraySize10 =
+      cxxNewExpr(hasArraySize(integerLiteral(equals(10))));
+  EXPECT_TRUE(
+      matches("void foo() { int *Ptr = new int[10]; }", NewArraySize10));
+  EXPECT_TRUE(
+      notMatches("void foo() { int *Ptr = new int[20]; }", NewArraySize10));
+}
+
+TEST(CXXNewExpr, IsPlacement) {
+  StatementMatcher PlacementNew = cxxNewExpr(isPlacement());
+
+  EXPECT_TRUE(matches(R"(
+    void* operator new(decltype(sizeof(void*)), void*); 
+    int *foo(void* Storage) {
+      return new (Storage) int; 
+    })",
+                      PlacementNew));
+
+  EXPECT_TRUE(notMatches(R"(
+    void* operator new(decltype(sizeof(void*)), void*); 
+    int *foo(void* Storage) {
+      return new int; 
+    })",
+                         PlacementNew));
+}
+
 } // namespace ast_matchers
 } // namespace clang