From 90ecfe136730918c6d80148ad8e2a870ac86fda5 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Sun, 2 Nov 2025 21:00:31 +0800 Subject: [PATCH 1/2] [ExprMutation] fix false postives on pointer-to-member operator Fixed: #161913 --- clang-tools-extra/docs/ReleaseNotes.rst | 3 ++- clang/lib/Analysis/ExprMutationAnalyzer.cpp | 13 ++++++++----- .../Analysis/ExprMutationAnalyzerTest.cpp | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index ab7dc87d9d5f3..7fa096a9495f2 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -369,7 +369,8 @@ Changes in existing checks ` check to avoid false positives when pointers is transferred to non-const references and avoid false positives of function pointer and fix false - positives on return of non-const pointer. + positives on return of non-const pointer and fix false positive on + pointer-to-member operator. - Improved :doc:`misc-header-include-cycle ` check performance. diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp index 75b17c545bb78..54c30c05c3e19 100644 --- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp +++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp @@ -746,11 +746,14 @@ ExprMutationAnalyzer::Analyzer::findPointeeMemberMutation(const Expr *Exp) { Stm, Context)); if (MemberCallExpr) return MemberCallExpr; - const auto Matches = - match(stmt(forEachDescendant( - memberExpr(hasObjectExpression(canResolveToExprPointee(Exp))) - .bind(NodeID::value))), - Stm, Context); + const auto Matches = match( + stmt(forEachDescendant( + expr(anyOf(memberExpr( + hasObjectExpression(canResolveToExprPointee(Exp))), + binaryOperator(hasOperatorName("->*"), + hasLHS(canResolveToExprPointee(Exp))))) + .bind(NodeID::value))), + Stm, Context); return findExprMutation(Matches); } diff --git a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp index ef229606de0f0..8fc9a66dbda7e 100644 --- a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp +++ b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp @@ -2076,4 +2076,19 @@ TEST(ExprMutationAnalyzerTest, PointeeMutatedByReturn) { } } +TEST(ExprMutationAnalyzerTest, PointeeMutatedByPointerToMemberOperator) { + // GH161913 + const std::string Code = R"( + struct S { int i; }; + void f(S s) { + S *x = &s; + (x->*(&S::i))++; + } + )"; + auto AST = buildASTFromCodeWithArgs(Code, {"-Wno-everything"}); + auto Results = + match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); + EXPECT_TRUE(isPointeeMutated(Results, AST.get())); +} + } // namespace clang From 460c2f90732ee0066193b31b90c5ab080f4c5339 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Mon, 3 Nov 2025 21:10:15 +0800 Subject: [PATCH 2/2] Update clang-tools-extra/docs/ReleaseNotes.rst Co-authored-by: Baranov Victor --- clang-tools-extra/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7fa096a9495f2..f02f932c93058 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -369,7 +369,7 @@ Changes in existing checks ` check to avoid false positives when pointers is transferred to non-const references and avoid false positives of function pointer and fix false - positives on return of non-const pointer and fix false positive on + positives on return of non-const pointer and fix false positives on pointer-to-member operator. - Improved :doc:`misc-header-include-cycle