Skip to content

Commit f7b434c

Browse files
committed
[ExprMutation] fix false postives on pointer-to-member operator
Fixed: #161913
1 parent 138e0ff commit f7b434c

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ Changes in existing checks
369369
<clang-tidy/checks/misc/const-correctness>` check to avoid false
370370
positives when pointers is transferred to non-const references
371371
and avoid false positives of function pointer and fix false
372-
positives on return of non-const pointer.
372+
positives on return of non-const pointer and fix false positive on
373+
pointer-to-member operator.
373374

374375
- Improved :doc:`misc-header-include-cycle
375376
<clang-tidy/checks/misc/header-include-cycle>` check performance.

clang/lib/Analysis/ExprMutationAnalyzer.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,11 +746,14 @@ ExprMutationAnalyzer::Analyzer::findPointeeMemberMutation(const Expr *Exp) {
746746
Stm, Context));
747747
if (MemberCallExpr)
748748
return MemberCallExpr;
749-
const auto Matches =
750-
match(stmt(forEachDescendant(
751-
memberExpr(hasObjectExpression(canResolveToExprPointee(Exp)))
752-
.bind(NodeID<Expr>::value))),
753-
Stm, Context);
749+
const auto Matches = match(
750+
stmt(forEachDescendant(
751+
expr(anyOf(memberExpr(
752+
hasObjectExpression(canResolveToExprPointee(Exp))),
753+
binaryOperator(hasOperatorName("->*"),
754+
hasLHS(canResolveToExprPointee(Exp)))))
755+
.bind(NodeID<Expr>::value))),
756+
Stm, Context);
754757
return findExprMutation(Matches);
755758
}
756759

clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,4 +2076,20 @@ TEST(ExprMutationAnalyzerTest, PointeeMutatedByReturn) {
20762076
}
20772077
}
20782078

2079+
TEST(ExprMutationAnalyzerTest, PointeeMutatedByPointerToMemberOperator) {
2080+
// GH161913
2081+
const std::string Code = R"(
2082+
struct S { int i; };
2083+
void f(S s) {
2084+
S *x = &s;
2085+
(x->*(&S::i))++;
2086+
}
2087+
)";
2088+
auto AST = buildASTFromCodeWithArgs(Code, {"-Wno-everything"});
2089+
auto Results =
2090+
match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
2091+
EXPECT_TRUE(isPointeeMutated(Results, AST.get()));
2092+
}
2093+
2094+
20792095
} // namespace clang

0 commit comments

Comments
 (0)