Skip to content

Commit 8bd7fc7

Browse files
[ExprMutation] fix false postives on pointer-to-member operator (#166069)
Fixed: #161913 --------- Co-authored-by: Baranov Victor <[email protected]>
1 parent ecc70fd commit 8bd7fc7

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ Changes in existing checks
379379
<clang-tidy/checks/misc/const-correctness>` check to avoid false
380380
positives when pointers is transferred to non-const references
381381
and avoid false positives of function pointer and fix false
382-
positives on return of non-const pointer.
382+
positives on return of non-const pointer and fix false positives on
383+
pointer-to-member operator.
383384

384385
- Improved :doc:`misc-header-include-cycle
385386
<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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,4 +2076,19 @@ 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+
20792094
} // namespace clang

0 commit comments

Comments
 (0)