https://github.com/berkaysahiin created https://github.com/llvm/llvm-project/pull/187508
Fixes #187012 which is a false negative on clang-tidy end. >From 69b92279b9d32a9249965dbac9eb2c9f00c6d150 Mon Sep 17 00:00:00 2001 From: Berkay Sahin <[email protected]> Date: Thu, 19 Mar 2026 16:47:08 +0300 Subject: [PATCH 1/2] [clang] Detect pointee mutations in placement new expressions --- clang/lib/Analysis/ExprMutationAnalyzer.cpp | 2 ++ clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp index 86d7dcab807d3..5def6ba3cac5a 100644 --- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp +++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp @@ -787,6 +787,8 @@ ExprMutationAnalyzer::Analyzer::findPointeeToNonConst(const Expr *Exp) { anyOf(ArgOfNonConstParameter, ArgOfInstantiationDependent); const auto PassAsNonConstArg = expr(anyOf(cxxUnresolvedConstructExpr(ArgOfInstantiationDependent), + cxxNewExpr(hasAnyPlacementArg( + ignoringParenImpCasts(canResolveToExprPointee(Exp)))), cxxConstructExpr(CallLikeMatcher), callExpr(CallLikeMatcher), parenListExpr(has(canResolveToExprPointee(Exp))), initListExpr(hasAnyInit(canResolveToExprPointee(Exp))))); diff --git a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp index d171d47ac1fef..82b12821e39f9 100644 --- a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp +++ b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp @@ -1803,6 +1803,16 @@ TEST(ExprMutationAnalyzerTest, PointeeMutatedByPassAsArgument) { } } +TEST(ExprMutationAnalyzerTest, PointeeMutatedByPassAsArgumentInNew) { + const std::string Code = + "void* operator new(unsigned long, void* p) noexcept;" + "void f() { int* x = nullptr; new(x) int{311}; }"; + auto AST = buildASTFromCodeWithArgs(Code, {}); + auto Results = + match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); + EXPECT_TRUE(isPointeeMutated(Results, AST.get())); +} + TEST(ExprMutationAnalyzerTest, PointeeMutatedByPassAsArgumentInConstruct) { { const std::string Code = "struct A { A(int const*); };" >From 84d35eadca40ef631cfbbb723d04741cf571c8e0 Mon Sep 17 00:00:00 2001 From: Berkay Sahin <[email protected]> Date: Thu, 19 Mar 2026 17:10:46 +0300 Subject: [PATCH 2/2] [clang] Move the test --- .../Analysis/ExprMutationAnalyzerTest.cpp | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp index 82b12821e39f9..27c0be6b74fcc 100644 --- a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp +++ b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp @@ -1801,16 +1801,15 @@ TEST(ExprMutationAnalyzerTest, PointeeMutatedByPassAsArgument) { match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); EXPECT_TRUE(isPointeeMutated(Results, AST.get())); } -} - -TEST(ExprMutationAnalyzerTest, PointeeMutatedByPassAsArgumentInNew) { - const std::string Code = - "void* operator new(unsigned long, void* p) noexcept;" - "void f() { int* x = nullptr; new(x) int{311}; }"; - auto AST = buildASTFromCodeWithArgs(Code, {}); - auto Results = - match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); - EXPECT_TRUE(isPointeeMutated(Results, AST.get())); + { + const std::string Code = + "void* operator new(unsigned long, void* p) noexcept;" + "void f() { int* x = nullptr; new(x) int{311}; }"; + auto AST = buildASTFromCodeWithArgs(Code, {}); + auto Results = + match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); + EXPECT_TRUE(isPointeeMutated(Results, AST.get())); + } } TEST(ExprMutationAnalyzerTest, PointeeMutatedByPassAsArgumentInConstruct) { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
