xref: /llvm-project/clang/lib/StaticAnalyzer/Checkers/CXXSelfAssignmentChecker.cpp (revision bda3dd0d986b33c3a327c0ee0eb8ba43aa140699)
1f57f90dfSDevin Coughlin //=== CXXSelfAssignmentChecker.cpp -----------------------------*- C++ -*--===//
2f57f90dfSDevin Coughlin //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler 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 
CXXSelfAssignmentChecker()36f57f90dfSDevin Coughlin CXXSelfAssignmentChecker::CXXSelfAssignmentChecker() {}
37f57f90dfSDevin Coughlin 
checkBeginFunction(CheckerContext & C) const38f57f90dfSDevin 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);
539ed4b316SAdam Balogh 
54b570195cSAnna Zaks   ProgramStateRef SelfAssignState = State->bindLoc(Param, ThisVal, LCtx);
559ed4b316SAdam Balogh   const NoteTag *SelfAssignTag =
5620a3d64cSAdam Balogh     C.getNoteTag([MD](PathSensitiveBugReport &BR) -> std::string {
579ed4b316SAdam Balogh         SmallString<256> Msg;
589ed4b316SAdam Balogh         llvm::raw_svector_ostream Out(Msg);
599ed4b316SAdam Balogh         Out << "Assuming " << MD->getParamDecl(0)->getName() << " == *this";
60adcd0268SBenjamin Kramer         return std::string(Out.str());
619ed4b316SAdam Balogh       });
629ed4b316SAdam Balogh   C.addTransition(SelfAssignState, SelfAssignTag);
639ed4b316SAdam Balogh 
64b570195cSAnna Zaks   ProgramStateRef NonSelfAssignState = State->bindLoc(Param, ParamVal, LCtx);
659ed4b316SAdam Balogh   const NoteTag *NonSelfAssignTag =
6620a3d64cSAdam Balogh     C.getNoteTag([MD](PathSensitiveBugReport &BR) -> std::string {
679ed4b316SAdam Balogh         SmallString<256> Msg;
689ed4b316SAdam Balogh         llvm::raw_svector_ostream Out(Msg);
699ed4b316SAdam Balogh         Out << "Assuming " << MD->getParamDecl(0)->getName() << " != *this";
70adcd0268SBenjamin Kramer         return std::string(Out.str());
719ed4b316SAdam Balogh       });
729ed4b316SAdam Balogh   C.addTransition(NonSelfAssignState, NonSelfAssignTag);
73f57f90dfSDevin Coughlin }
74f57f90dfSDevin Coughlin 
registerCXXSelfAssignmentChecker(CheckerManager & Mgr)75f57f90dfSDevin Coughlin void ento::registerCXXSelfAssignmentChecker(CheckerManager &Mgr) {
76f57f90dfSDevin Coughlin   Mgr.registerChecker<CXXSelfAssignmentChecker>();
77f57f90dfSDevin Coughlin }
78058a7a45SKristof Umann 
shouldRegisterCXXSelfAssignmentChecker(const CheckerManager & mgr)79*bda3dd0dSKirstóf Umann bool ento::shouldRegisterCXXSelfAssignmentChecker(const CheckerManager &mgr) {
80058a7a45SKristof Umann   return true;
81058a7a45SKristof Umann }
82