Devin Coughlin | f57f90d | 2016-07-21 23:42:31 +0000 | [diff] [blame] | 1 | //=== CXXSelfAssignmentChecker.cpp -----------------------------*- C++ -*--===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Devin Coughlin | f57f90d | 2016-07-21 23:42:31 +0000 | [diff] [blame] | 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 | |
Kristof Umann | 76a2150 | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
Devin Coughlin | f57f90d | 2016-07-21 23:42:31 +0000 | [diff] [blame] | 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 | CXXSelfAssignmentChecker::CXXSelfAssignmentChecker() {} |
| 37 | |
| 38 | void CXXSelfAssignmentChecker::checkBeginFunction(CheckerContext &C) const { |
| 39 | if (!C.inTopFrame()) |
| 40 | return; |
| 41 | const auto *LCtx = C.getLocationContext(); |
| 42 | const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl()); |
| 43 | if (!MD) |
| 44 | return; |
| 45 | if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator()) |
| 46 | return; |
| 47 | auto &State = C.getState(); |
| 48 | auto &SVB = C.getSValBuilder(); |
| 49 | auto ThisVal = |
George Karpenkov | dd18b11 | 2018-06-27 01:51:55 +0000 | [diff] [blame] | 50 | State->getSVal(SVB.getCXXThis(MD, LCtx->getStackFrame())); |
Devin Coughlin | f57f90d | 2016-07-21 23:42:31 +0000 | [diff] [blame] | 51 | auto Param = SVB.makeLoc(State->getRegion(MD->getParamDecl(0), LCtx)); |
| 52 | auto ParamVal = State->getSVal(Param); |
Anna Zaks | b570195 | 2017-01-13 00:50:57 +0000 | [diff] [blame] | 53 | ProgramStateRef SelfAssignState = State->bindLoc(Param, ThisVal, LCtx); |
Devin Coughlin | f57f90d | 2016-07-21 23:42:31 +0000 | [diff] [blame] | 54 | C.addTransition(SelfAssignState); |
Anna Zaks | b570195 | 2017-01-13 00:50:57 +0000 | [diff] [blame] | 55 | ProgramStateRef NonSelfAssignState = State->bindLoc(Param, ParamVal, LCtx); |
Devin Coughlin | f57f90d | 2016-07-21 23:42:31 +0000 | [diff] [blame] | 56 | C.addTransition(NonSelfAssignState); |
| 57 | } |
| 58 | |
| 59 | void ento::registerCXXSelfAssignmentChecker(CheckerManager &Mgr) { |
| 60 | Mgr.registerChecker<CXXSelfAssignmentChecker>(); |
| 61 | } |
Kristof Umann | 058a7a4 | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 62 | |
| 63 | bool ento::shouldRegisterCXXSelfAssignmentChecker(const LangOptions &LO) { |
| 64 | return true; |
| 65 | } |