Author: Berkay Sahin Date: 2026-03-22T12:22:17+01:00 New Revision: b4084bd213224799d350359a89235a41b3297f4a
URL: https://github.com/llvm/llvm-project/commit/b4084bd213224799d350359a89235a41b3297f4a DIFF: https://github.com/llvm/llvm-project/commit/b4084bd213224799d350359a89235a41b3297f4a.diff LOG: [clang] Detect pointee mutations in placement new expressions (#187508) Fixes #187012 which is a false positive on clang-tidy end. Added: Modified: clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp clang/lib/Analysis/ExprMutationAnalyzer.cpp clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index ef4a771d5a0f6..0085ce87174b2 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -272,6 +272,9 @@ Changes in existing checks - Fixed false positive where an array of pointers to ``const`` was incorrectly diagnosed as allowing the pointee to be made ``const``. + - Fixed false positive where a pointer used with placement new was + incorrectly diagnosed as allowing the pointee to be made ``const``. + - Improved :doc:`misc-multiple-inheritance <clang-tidy/checks/misc/multiple-inheritance>` by avoiding false positives when virtual inheritance causes concrete bases to be counted more than once. diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp index 9ecd804cf9235..4c42743af8f11 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-pointer-as-pointers.cpp @@ -115,3 +115,11 @@ void multi_level_pointer() { // CHECK-FIXES: const char * const* foo[] = {&s}; const char * p = *foo[0]; } + +void* operator new(decltype(sizeof(void*)), void*) noexcept; + +void pointer_in_emplacement_new() { + int* ptr = nullptr; + // CHECK-NOT: warning + new(ptr) int {123}; +} 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..c63479dc26e0b 100644 --- a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp +++ b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp @@ -1801,6 +1801,16 @@ TEST(ExprMutationAnalyzerTest, PointeeMutatedByPassAsArgument) { match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); EXPECT_TRUE(isPointeeMutated(Results, AST.get())); } + { + const std::string Code = + "namespace std { typedef decltype(sizeof(int)) size_t; }" + "void* operator new(std::size_t, void*) 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
