[clang] [clang-analysis]Fix false positive in mutation check when using pointer to member function (PR #66846)

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 25 01:54:20 PDT 2023


================
@@ -274,8 +288,8 @@ const Stmt *ExprMutationAnalyzer::findDirectMutation(const Expr *Exp) {
   const auto NonConstMethod = cxxMethodDecl(unless(isConst()));
 
   const auto AsNonConstThis = expr(anyOf(
-      cxxMemberCallExpr(callee(NonConstMethod),
-                        on(canResolveToExpr(equalsNode(Exp)))),
+      cxxMemberCallExpr(on(canResolveToExpr(equalsNode(Exp))),
+                        unless(isConstCallee())),
----------------
mzyKi wrote:

```unless(isConstCallee())``` This condition is unnecessary. I have tested your code in my machine.
In such testcase example,
```cpp
#include <iostream>

class X {
public:
  int a;
  int b;
};

void func(X &x, int X::*m) {
  /*const*/ X &tmp = x;

  tmp.*m = 77;
}

int main() {
  X x;
  x.a = 0;
  x.b = 0;

  std::cout << x.a << " " << x.b << std::endl;

  func(x, &X::a);

  std::cout << x.a << " " << x.b << std::endl;

  return 0;
}
```
 use ```cxxMemberCallExpr(callee(NonConstMethod),
                        on(canResolveToExpr(equalsNode(Exp)))),``` cxxMemberCallExpr matcher do not produce false positive




https://github.com/llvm/llvm-project/pull/66846


More information about the cfe-commits mailing list