Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/CXXSelfAssignmentChecker.cpp
Line
Count
Source
1
//=== CXXSelfAssignmentChecker.cpp -----------------------------*- C++ -*--===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file defines CXXSelfAssignmentChecker, which tests all custom defined
10
// copy and move assignment operators for the case of self assignment, thus
11
// where the parameter refers to the same location where the this pointer
12
// points to. The checker itself does not do any checks at all, but it
13
// causes the analyzer to check every copy and move assignment operator twice:
14
// once for when 'this' aliases with the parameter and once for when it may not.
15
// It is the task of the other enabled checkers to find the bugs in these two
16
// different cases.
17
//
18
//===----------------------------------------------------------------------===//
19
20
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
21
#include "clang/StaticAnalyzer/Core/Checker.h"
22
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23
24
using namespace clang;
25
using namespace ento;
26
27
namespace {
28
29
class CXXSelfAssignmentChecker : public Checker<check::BeginFunction> {
30
public:
31
  CXXSelfAssignmentChecker();
32
  void checkBeginFunction(CheckerContext &C) const;
33
};
34
}
35
36
65
CXXSelfAssignmentChecker::CXXSelfAssignmentChecker() {}
37
38
9.26k
void CXXSelfAssignmentChecker::checkBeginFunction(CheckerContext &C) const {
39
9.26k
  if (!C.inTopFrame())
40
7.45k
    return;
41
1.80k
  const auto *LCtx = C.getLocationContext();
42
1.80k
  const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl());
43
1.80k
  if (!MD)
44
1.67k
    return;
45
135
  if (!MD->isCopyAssignmentOperator() && 
!MD->isMoveAssignmentOperator()132
)
46
127
    return;
47
8
  auto &State = C.getState();
48
8
  auto &SVB = C.getSValBuilder();
49
8
  auto ThisVal =
50
8
      State->getSVal(SVB.getCXXThis(MD, LCtx->getStackFrame()));
51
8
  auto Param = SVB.makeLoc(State->getRegion(MD->getParamDecl(0), LCtx));
52
8
  auto ParamVal = State->getSVal(Param);
53
54
8
  ProgramStateRef SelfAssignState = State->bindLoc(Param, ThisVal, LCtx);
55
8
  const NoteTag *SelfAssignTag =
56
8
    C.getNoteTag([MD](PathSensitiveBugReport &BR) -> std::string {
57
6
        SmallString<256> Msg;
58
6
        llvm::raw_svector_ostream Out(Msg);
59
6
        Out << "Assuming " << MD->getParamDecl(0)->getName() << " == *this";
60
6
        return std::string(Out.str());
61
6
      });
62
8
  C.addTransition(SelfAssignState, SelfAssignTag);
63
64
8
  ProgramStateRef NonSelfAssignState = State->bindLoc(Param, ParamVal, LCtx);
65
8
  const NoteTag *NonSelfAssignTag =
66
8
    C.getNoteTag([MD](PathSensitiveBugReport &BR) -> std::string {
67
4
        SmallString<256> Msg;
68
4
        llvm::raw_svector_ostream Out(Msg);
69
4
        Out << "Assuming " << MD->getParamDecl(0)->getName() << " != *this";
70
4
        return std::string(Out.str());
71
4
      });
72
8
  C.addTransition(NonSelfAssignState, NonSelfAssignTag);
73
8
}
74
75
65
void ento::registerCXXSelfAssignmentChecker(CheckerManager &Mgr) {
76
65
  Mgr.registerChecker<CXXSelfAssignmentChecker>();
77
65
}
78
79
130
bool ento::shouldRegisterCXXSelfAssignmentChecker(const CheckerManager &mgr) {
80
130
  return true;
81
130
}