clang 23.0.0git
CXXSelfAssignmentChecker.cpp
Go to the documentation of this file.
1//=== CXXSelfAssignmentChecker.cpp -----------------------------*- C++ -*--===//
2//
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
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
23
24using namespace clang;
25using namespace ento;
26
27namespace {
28
29class CXXSelfAssignmentChecker : public Checker<check::BeginFunction> {
30public:
31 CXXSelfAssignmentChecker();
32 void checkBeginFunction(CheckerContext &C) const;
33};
34}
35
36CXXSelfAssignmentChecker::CXXSelfAssignmentChecker() {}
37
38void CXXSelfAssignmentChecker::checkBeginFunction(CheckerContext &C) const {
39 if (!C.inTopFrame())
40 return;
41 const auto *SF = C.getStackFrame();
42 const auto *MD = dyn_cast<CXXMethodDecl>(SF->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 = State->getSVal(SVB.getCXXThis(MD, SF));
50 auto Param = SVB.makeLoc(State->getRegion(MD->getParamDecl(0), SF));
51 auto ParamVal = State->getSVal(Param);
52
53 ProgramStateRef SelfAssignState = State->bindLoc(Param, ThisVal, SF);
54 const NoteTag *SelfAssignTag =
55 C.getNoteTag([MD](PathSensitiveBugReport &BR) -> std::string {
56 SmallString<256> Msg;
57 llvm::raw_svector_ostream Out(Msg);
58 Out << "Assuming " << MD->getParamDecl(0)->getName() << " == *this";
59 return std::string(Out.str());
60 });
61 C.addTransition(SelfAssignState, SelfAssignTag);
62
63 ProgramStateRef NonSelfAssignState = State->bindLoc(Param, ParamVal, SF);
64 const NoteTag *NonSelfAssignTag =
65 C.getNoteTag([MD](PathSensitiveBugReport &BR) -> std::string {
66 SmallString<256> Msg;
67 llvm::raw_svector_ostream Out(Msg);
68 Out << "Assuming " << MD->getParamDecl(0)->getName() << " != *this";
69 return std::string(Out.str());
70 });
71 C.addTransition(NonSelfAssignState, NonSelfAssignTag);
72}
73
74void ento::registerCXXSelfAssignmentChecker(CheckerManager &Mgr) {
75 Mgr.registerChecker<CXXSelfAssignmentChecker>();
76}
77
78bool ento::shouldRegisterCXXSelfAssignmentChecker(const CheckerManager &mgr) {
79 return true;
80}
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
Definition Checker.h:550
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
The JSON file list parser is used to communicate input to InstallAPI.