1f57f90dfSDevin Coughlin //=== CXXSelfAssignmentChecker.cpp -----------------------------*- C++ -*--===//
2f57f90dfSDevin Coughlin //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f57f90dfSDevin Coughlin //
7f57f90dfSDevin Coughlin //===----------------------------------------------------------------------===//
8f57f90dfSDevin Coughlin //
9f57f90dfSDevin Coughlin // This file defines CXXSelfAssignmentChecker, which tests all custom defined
10f57f90dfSDevin Coughlin // copy and move assignment operators for the case of self assignment, thus
11f57f90dfSDevin Coughlin // where the parameter refers to the same location where the this pointer
12f57f90dfSDevin Coughlin // points to. The checker itself does not do any checks at all, but it
13f57f90dfSDevin Coughlin // causes the analyzer to check every copy and move assignment operator twice:
14f57f90dfSDevin Coughlin // once for when 'this' aliases with the parameter and once for when it may not.
15f57f90dfSDevin Coughlin // It is the task of the other enabled checkers to find the bugs in these two
16f57f90dfSDevin Coughlin // different cases.
17f57f90dfSDevin Coughlin //
18f57f90dfSDevin Coughlin //===----------------------------------------------------------------------===//
19f57f90dfSDevin Coughlin 
2076a21502SKristof Umann #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
21f57f90dfSDevin Coughlin #include "clang/StaticAnalyzer/Core/Checker.h"
22f57f90dfSDevin Coughlin #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23f57f90dfSDevin Coughlin 
24f57f90dfSDevin Coughlin using namespace clang;
25f57f90dfSDevin Coughlin using namespace ento;
26f57f90dfSDevin Coughlin 
27f57f90dfSDevin Coughlin namespace {
28f57f90dfSDevin Coughlin 
29f57f90dfSDevin Coughlin class CXXSelfAssignmentChecker : public Checker<check::BeginFunction> {
30f57f90dfSDevin Coughlin public:
31f57f90dfSDevin Coughlin   CXXSelfAssignmentChecker();
32f57f90dfSDevin Coughlin   void checkBeginFunction(CheckerContext &C) const;
33f57f90dfSDevin Coughlin };
34f57f90dfSDevin Coughlin }
35f57f90dfSDevin Coughlin 
36f57f90dfSDevin Coughlin CXXSelfAssignmentChecker::CXXSelfAssignmentChecker() {}
37f57f90dfSDevin Coughlin 
38f57f90dfSDevin Coughlin void CXXSelfAssignmentChecker::checkBeginFunction(CheckerContext &C) const {
39f57f90dfSDevin Coughlin   if (!C.inTopFrame())
40f57f90dfSDevin Coughlin     return;
41f57f90dfSDevin Coughlin   const auto *LCtx = C.getLocationContext();
42f57f90dfSDevin Coughlin   const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl());
43f57f90dfSDevin Coughlin   if (!MD)
44f57f90dfSDevin Coughlin     return;
45f57f90dfSDevin Coughlin   if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator())
46f57f90dfSDevin Coughlin     return;
47f57f90dfSDevin Coughlin   auto &State = C.getState();
48f57f90dfSDevin Coughlin   auto &SVB = C.getSValBuilder();
49f57f90dfSDevin Coughlin   auto ThisVal =
50dd18b11bSGeorge Karpenkov       State->getSVal(SVB.getCXXThis(MD, LCtx->getStackFrame()));
51f57f90dfSDevin Coughlin   auto Param = SVB.makeLoc(State->getRegion(MD->getParamDecl(0), LCtx));
52f57f90dfSDevin Coughlin   auto ParamVal = State->getSVal(Param);
53b570195cSAnna Zaks   ProgramStateRef SelfAssignState = State->bindLoc(Param, ThisVal, LCtx);
54f57f90dfSDevin Coughlin   C.addTransition(SelfAssignState);
55b570195cSAnna Zaks   ProgramStateRef NonSelfAssignState = State->bindLoc(Param, ParamVal, LCtx);
56f57f90dfSDevin Coughlin   C.addTransition(NonSelfAssignState);
57f57f90dfSDevin Coughlin }
58f57f90dfSDevin Coughlin 
59f57f90dfSDevin Coughlin void ento::registerCXXSelfAssignmentChecker(CheckerManager &Mgr) {
60f57f90dfSDevin Coughlin   Mgr.registerChecker<CXXSelfAssignmentChecker>();
61f57f90dfSDevin Coughlin }
62