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