2016-07-22 07:42:31 +08:00
|
|
|
//=== CXXSelfAssignmentChecker.cpp -----------------------------*- C++ -*--===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-07-22 07:42:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines CXXSelfAssignmentChecker, which tests all custom defined
|
|
|
|
// copy and move assignment operators for the case of self assignment, thus
|
|
|
|
// where the parameter refers to the same location where the this pointer
|
|
|
|
// points to. The checker itself does not do any checks at all, but it
|
|
|
|
// causes the analyzer to check every copy and move assignment operator twice:
|
|
|
|
// once for when 'this' aliases with the parameter and once for when it may not.
|
|
|
|
// It is the task of the other enabled checkers to find the bugs in these two
|
|
|
|
// different cases.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2016-07-22 07:42:31 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CXXSelfAssignmentChecker : public Checker<check::BeginFunction> {
|
|
|
|
public:
|
|
|
|
CXXSelfAssignmentChecker();
|
|
|
|
void checkBeginFunction(CheckerContext &C) const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
CXXSelfAssignmentChecker::CXXSelfAssignmentChecker() {}
|
|
|
|
|
|
|
|
void CXXSelfAssignmentChecker::checkBeginFunction(CheckerContext &C) const {
|
|
|
|
if (!C.inTopFrame())
|
|
|
|
return;
|
|
|
|
const auto *LCtx = C.getLocationContext();
|
|
|
|
const auto *MD = dyn_cast<CXXMethodDecl>(LCtx->getDecl());
|
|
|
|
if (!MD)
|
|
|
|
return;
|
|
|
|
if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator())
|
|
|
|
return;
|
|
|
|
auto &State = C.getState();
|
|
|
|
auto &SVB = C.getSValBuilder();
|
|
|
|
auto ThisVal =
|
2018-06-27 09:51:55 +08:00
|
|
|
State->getSVal(SVB.getCXXThis(MD, LCtx->getStackFrame()));
|
2016-07-22 07:42:31 +08:00
|
|
|
auto Param = SVB.makeLoc(State->getRegion(MD->getParamDecl(0), LCtx));
|
|
|
|
auto ParamVal = State->getSVal(Param);
|
2019-05-28 21:07:09 +08:00
|
|
|
|
2017-01-13 08:50:57 +08:00
|
|
|
ProgramStateRef SelfAssignState = State->bindLoc(Param, ThisVal, LCtx);
|
2019-05-28 21:07:09 +08:00
|
|
|
const NoteTag *SelfAssignTag =
|
2020-03-10 16:05:16 +08:00
|
|
|
C.getNoteTag([MD](PathSensitiveBugReport &BR) -> std::string {
|
2019-05-28 21:07:09 +08:00
|
|
|
SmallString<256> Msg;
|
|
|
|
llvm::raw_svector_ostream Out(Msg);
|
|
|
|
Out << "Assuming " << MD->getParamDecl(0)->getName() << " == *this";
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(Out.str());
|
2019-05-28 21:07:09 +08:00
|
|
|
});
|
|
|
|
C.addTransition(SelfAssignState, SelfAssignTag);
|
|
|
|
|
2017-01-13 08:50:57 +08:00
|
|
|
ProgramStateRef NonSelfAssignState = State->bindLoc(Param, ParamVal, LCtx);
|
2019-05-28 21:07:09 +08:00
|
|
|
const NoteTag *NonSelfAssignTag =
|
2020-03-10 16:05:16 +08:00
|
|
|
C.getNoteTag([MD](PathSensitiveBugReport &BR) -> std::string {
|
2019-05-28 21:07:09 +08:00
|
|
|
SmallString<256> Msg;
|
|
|
|
llvm::raw_svector_ostream Out(Msg);
|
|
|
|
Out << "Assuming " << MD->getParamDecl(0)->getName() << " != *this";
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(Out.str());
|
2019-05-28 21:07:09 +08:00
|
|
|
});
|
|
|
|
C.addTransition(NonSelfAssignState, NonSelfAssignTag);
|
2016-07-22 07:42:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerCXXSelfAssignmentChecker(CheckerManager &Mgr) {
|
|
|
|
Mgr.registerChecker<CXXSelfAssignmentChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterCXXSelfAssignmentChecker(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|