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 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); 53*9ed4b316SAdam Balogh 54b570195cSAnna Zaks ProgramStateRef SelfAssignState = State->bindLoc(Param, ThisVal, LCtx); 55*9ed4b316SAdam Balogh const NoteTag *SelfAssignTag = 56*9ed4b316SAdam Balogh C.getNoteTag([MD](BugReport &BR) -> std::string { 57*9ed4b316SAdam Balogh SmallString<256> Msg; 58*9ed4b316SAdam Balogh llvm::raw_svector_ostream Out(Msg); 59*9ed4b316SAdam Balogh Out << "Assuming " << MD->getParamDecl(0)->getName() << " == *this"; 60*9ed4b316SAdam Balogh return Out.str(); 61*9ed4b316SAdam Balogh }); 62*9ed4b316SAdam Balogh C.addTransition(SelfAssignState, SelfAssignTag); 63*9ed4b316SAdam Balogh 64b570195cSAnna Zaks ProgramStateRef NonSelfAssignState = State->bindLoc(Param, ParamVal, LCtx); 65*9ed4b316SAdam Balogh const NoteTag *NonSelfAssignTag = 66*9ed4b316SAdam Balogh C.getNoteTag([MD](BugReport &BR) -> std::string { 67*9ed4b316SAdam Balogh SmallString<256> Msg; 68*9ed4b316SAdam Balogh llvm::raw_svector_ostream Out(Msg); 69*9ed4b316SAdam Balogh Out << "Assuming " << MD->getParamDecl(0)->getName() << " != *this"; 70*9ed4b316SAdam Balogh return Out.str(); 71*9ed4b316SAdam Balogh }); 72*9ed4b316SAdam Balogh C.addTransition(NonSelfAssignState, NonSelfAssignTag); 73f57f90dfSDevin Coughlin } 74f57f90dfSDevin Coughlin 75f57f90dfSDevin Coughlin void ento::registerCXXSelfAssignmentChecker(CheckerManager &Mgr) { 76f57f90dfSDevin Coughlin Mgr.registerChecker<CXXSelfAssignmentChecker>(); 77f57f90dfSDevin Coughlin } 78058a7a45SKristof Umann 79058a7a45SKristof Umann bool ento::shouldRegisterCXXSelfAssignmentChecker(const LangOptions &LO) { 80058a7a45SKristof Umann return true; 81058a7a45SKristof Umann } 82