2012-09-28 03:45:15 +08:00
|
|
|
//=- DirectIvarAssignment.cpp - Check rules on ObjC properties -*- 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
|
2012-09-28 03:45:15 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2013-01-18 07:24:58 +08:00
|
|
|
// Check that Objective C properties are set with the setter, not though a
|
|
|
|
// direct assignment.
|
|
|
|
//
|
|
|
|
// Two versions of a checker exist: one that checks all methods and the other
|
|
|
|
// that only checks the methods annotated with
|
|
|
|
// __attribute__((annotate("objc_no_direct_instance_variable_assignment")))
|
|
|
|
//
|
|
|
|
// The checker does not warn about assignments to Ivars, annotated with
|
|
|
|
// __attribute__((objc_allow_direct_instance_variable_assignment"))). This
|
|
|
|
// annotation serves as a false positive suppression mechanism for the
|
|
|
|
// checker. The annotation is allowed on properties and Ivars.
|
2012-09-28 03:45:15 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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"
|
2012-12-05 09:14:37 +08:00
|
|
|
#include "clang/AST/Attr.h"
|
2012-09-28 03:45:15 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
2012-09-28 03:45:15 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-12-05 09:14:37 +08:00
|
|
|
/// The default method filter, which is used to filter out the methods on which
|
|
|
|
/// the check should not be performed.
|
|
|
|
///
|
|
|
|
/// Checks for the init, dealloc, and any other functions that might be allowed
|
|
|
|
/// to perform direct instance variable assignment based on their name.
|
2013-08-10 01:17:42 +08:00
|
|
|
static bool DefaultMethodFilter(const ObjCMethodDecl *M) {
|
2015-12-28 21:06:58 +08:00
|
|
|
return M->getMethodFamily() == OMF_init ||
|
|
|
|
M->getMethodFamily() == OMF_dealloc ||
|
|
|
|
M->getMethodFamily() == OMF_copy ||
|
|
|
|
M->getMethodFamily() == OMF_mutableCopy ||
|
2021-10-20 23:02:36 +08:00
|
|
|
M->getSelector().getNameForSlot(0).contains("init") ||
|
|
|
|
M->getSelector().getNameForSlot(0).contains("Init");
|
2013-08-10 01:17:42 +08:00
|
|
|
}
|
2012-12-05 09:14:37 +08:00
|
|
|
|
2012-09-28 03:45:15 +08:00
|
|
|
class DirectIvarAssignment :
|
|
|
|
public Checker<check::ASTDecl<ObjCImplementationDecl> > {
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<const ObjCIvarDecl*,
|
|
|
|
const ObjCPropertyDecl*> IvarToPropertyMapTy;
|
|
|
|
|
|
|
|
/// A helper class, which walks the AST and locates all assignments to ivars
|
|
|
|
/// in the given function.
|
|
|
|
class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
|
|
|
|
const IvarToPropertyMapTy &IvarToPropMap;
|
|
|
|
const ObjCMethodDecl *MD;
|
|
|
|
const ObjCInterfaceDecl *InterfD;
|
|
|
|
BugReporter &BR;
|
2014-02-12 05:49:21 +08:00
|
|
|
const CheckerBase *Checker;
|
2012-09-28 03:45:15 +08:00
|
|
|
LocationOrAnalysisDeclContext DCtx;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MethodCrawler(const IvarToPropertyMapTy &InMap, const ObjCMethodDecl *InMD,
|
2014-02-12 05:49:21 +08:00
|
|
|
const ObjCInterfaceDecl *InID, BugReporter &InBR,
|
|
|
|
const CheckerBase *Checker, AnalysisDeclContext *InDCtx)
|
|
|
|
: IvarToPropMap(InMap), MD(InMD), InterfD(InID), BR(InBR),
|
|
|
|
Checker(Checker), DCtx(InDCtx) {}
|
2012-09-28 03:45:15 +08:00
|
|
|
|
|
|
|
void VisitStmt(const Stmt *S) { VisitChildren(S); }
|
|
|
|
|
|
|
|
void VisitBinaryOperator(const BinaryOperator *BO);
|
|
|
|
|
|
|
|
void VisitChildren(const Stmt *S) {
|
2015-07-03 05:03:14 +08:00
|
|
|
for (const Stmt *Child : S->children())
|
|
|
|
if (Child)
|
|
|
|
this->Visit(Child);
|
2012-09-28 03:45:15 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2013-08-10 01:17:42 +08:00
|
|
|
bool (*ShouldSkipMethod)(const ObjCMethodDecl *);
|
2012-12-05 09:14:37 +08:00
|
|
|
|
|
|
|
DirectIvarAssignment() : ShouldSkipMethod(&DefaultMethodFilter) {}
|
|
|
|
|
2012-09-28 03:45:15 +08:00
|
|
|
void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
|
|
|
|
BugReporter &BR) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const ObjCIvarDecl *findPropertyBackingIvar(const ObjCPropertyDecl *PD,
|
2012-09-28 05:57:17 +08:00
|
|
|
const ObjCInterfaceDecl *InterD,
|
|
|
|
ASTContext &Ctx) {
|
2012-09-28 03:45:15 +08:00
|
|
|
// Check for synthesized ivars.
|
|
|
|
ObjCIvarDecl *ID = PD->getPropertyIvarDecl();
|
|
|
|
if (ID)
|
|
|
|
return ID;
|
|
|
|
|
2012-09-28 05:57:17 +08:00
|
|
|
ObjCInterfaceDecl *NonConstInterD = const_cast<ObjCInterfaceDecl*>(InterD);
|
|
|
|
|
2012-09-28 03:45:15 +08:00
|
|
|
// Check for existing "_PropName".
|
2012-09-28 05:57:17 +08:00
|
|
|
ID = NonConstInterD->lookupInstanceVariable(PD->getDefaultSynthIvarName(Ctx));
|
2012-09-28 03:45:15 +08:00
|
|
|
if (ID)
|
|
|
|
return ID;
|
|
|
|
|
|
|
|
// Check for existing "PropName".
|
|
|
|
IdentifierInfo *PropIdent = PD->getIdentifier();
|
2012-09-28 05:57:17 +08:00
|
|
|
ID = NonConstInterD->lookupInstanceVariable(PropIdent);
|
2012-09-28 03:45:15 +08:00
|
|
|
|
|
|
|
return ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectIvarAssignment::checkASTDecl(const ObjCImplementationDecl *D,
|
|
|
|
AnalysisManager& Mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
const ObjCInterfaceDecl *InterD = D->getClassInterface();
|
|
|
|
|
|
|
|
|
|
|
|
IvarToPropertyMapTy IvarToPropMap;
|
|
|
|
|
|
|
|
// Find all properties for this class.
|
2016-01-27 02:05:23 +08:00
|
|
|
for (const auto *PD : InterD->instance_properties()) {
|
2012-09-28 03:45:15 +08:00
|
|
|
// Find the corresponding IVar.
|
2012-09-28 05:57:17 +08:00
|
|
|
const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterD,
|
|
|
|
Mgr.getASTContext());
|
2012-09-28 03:45:15 +08:00
|
|
|
|
|
|
|
if (!ID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Store the IVar to property mapping.
|
|
|
|
IvarToPropMap[ID] = PD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IvarToPropMap.empty())
|
|
|
|
return;
|
|
|
|
|
2014-03-14 03:50:17 +08:00
|
|
|
for (const auto *M : D->instance_methods()) {
|
2012-09-28 03:45:15 +08:00
|
|
|
AnalysisDeclContext *DCtx = Mgr.getAnalysisDeclContext(M);
|
|
|
|
|
2012-12-05 09:14:37 +08:00
|
|
|
if ((*ShouldSkipMethod)(M))
|
2012-09-28 03:45:15 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const Stmt *Body = M->getBody();
|
2019-11-05 06:28:14 +08:00
|
|
|
if (M->isSynthesizedAccessorStub())
|
|
|
|
continue;
|
2012-09-28 05:57:17 +08:00
|
|
|
assert(Body);
|
2012-09-28 03:45:15 +08:00
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
MethodCrawler MC(IvarToPropMap, M->getCanonicalDecl(), InterD, BR, this,
|
|
|
|
DCtx);
|
2012-09-28 03:45:15 +08:00
|
|
|
MC.VisitStmt(Body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-18 07:24:58 +08:00
|
|
|
static bool isAnnotatedToAllowDirectAssignment(const Decl *D) {
|
2014-03-11 01:08:28 +08:00
|
|
|
for (const auto *Ann : D->specific_attrs<AnnotateAttr>())
|
2013-01-16 09:36:00 +08:00
|
|
|
if (Ann->getAnnotation() ==
|
|
|
|
"objc_allow_direct_instance_variable_assignment")
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-28 03:45:15 +08:00
|
|
|
void DirectIvarAssignment::MethodCrawler::VisitBinaryOperator(
|
|
|
|
const BinaryOperator *BO) {
|
|
|
|
if (!BO->isAssignmentOp())
|
|
|
|
return;
|
|
|
|
|
2012-09-28 05:57:17 +08:00
|
|
|
const ObjCIvarRefExpr *IvarRef =
|
|
|
|
dyn_cast<ObjCIvarRefExpr>(BO->getLHS()->IgnoreParenCasts());
|
2012-09-28 03:45:15 +08:00
|
|
|
|
|
|
|
if (!IvarRef)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (const ObjCIvarDecl *D = IvarRef->getDecl()) {
|
|
|
|
IvarToPropertyMapTy::const_iterator I = IvarToPropMap.find(D);
|
2013-01-16 09:36:00 +08:00
|
|
|
|
2012-09-28 03:45:15 +08:00
|
|
|
if (I != IvarToPropMap.end()) {
|
|
|
|
const ObjCPropertyDecl *PD = I->second;
|
2013-01-18 07:24:58 +08:00
|
|
|
// Skip warnings on Ivars, annotated with
|
2013-01-16 09:36:00 +08:00
|
|
|
// objc_allow_direct_instance_variable_assignment. This annotation serves
|
2013-01-18 07:24:58 +08:00
|
|
|
// as a false positive suppression mechanism for the checker. The
|
|
|
|
// annotation is allowed on properties and ivars.
|
|
|
|
if (isAnnotatedToAllowDirectAssignment(PD) ||
|
|
|
|
isAnnotatedToAllowDirectAssignment(D))
|
2013-01-16 09:36:00 +08:00
|
|
|
return;
|
2012-09-28 03:45:15 +08:00
|
|
|
|
|
|
|
ObjCMethodDecl *GetterMethod =
|
|
|
|
InterfD->getInstanceMethod(PD->getGetterName());
|
|
|
|
ObjCMethodDecl *SetterMethod =
|
|
|
|
InterfD->getInstanceMethod(PD->getSetterName());
|
|
|
|
|
|
|
|
if (SetterMethod && SetterMethod->getCanonicalDecl() == MD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (GetterMethod && GetterMethod->getCanonicalDecl() == MD)
|
|
|
|
return;
|
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
BR.EmitBasicReport(
|
|
|
|
MD, Checker, "Property access", categories::CoreFoundationObjectiveC,
|
2012-09-28 03:45:15 +08:00
|
|
|
"Direct assignment to an instance variable backing a property; "
|
2014-02-12 05:49:21 +08:00
|
|
|
"use the setter instead",
|
|
|
|
PathDiagnosticLocation(IvarRef, BR.getSourceManager(), DCtx));
|
2012-09-28 03:45:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-09-28 03:45:15 +08:00
|
|
|
|
2012-12-05 09:14:37 +08:00
|
|
|
// Register the checker that checks for direct accesses in functions annotated
|
2012-12-22 08:34:48 +08:00
|
|
|
// with __attribute__((annotate("objc_no_direct_instance_variable_assignment"))).
|
2013-08-10 01:17:42 +08:00
|
|
|
static bool AttrFilter(const ObjCMethodDecl *M) {
|
2014-03-11 01:08:28 +08:00
|
|
|
for (const auto *Ann : M->specific_attrs<AnnotateAttr>())
|
2013-08-10 01:17:42 +08:00
|
|
|
if (Ann->getAnnotation() == "objc_no_direct_instance_variable_assignment")
|
|
|
|
return false;
|
|
|
|
return true;
|
2012-12-05 09:14:37 +08:00
|
|
|
}
|
|
|
|
|
2019-01-26 22:23:08 +08:00
|
|
|
// Register the checker that checks for direct accesses in all functions,
|
|
|
|
// except for the initialization and copy routines.
|
|
|
|
void ento::registerDirectIvarAssignment(CheckerManager &mgr) {
|
2020-04-14 23:13:41 +08:00
|
|
|
auto Chk = mgr.registerChecker<DirectIvarAssignment>();
|
|
|
|
if (mgr.getAnalyzerOptions().getCheckerBooleanOption(Chk,
|
|
|
|
"AnnotatedFunctions"))
|
|
|
|
Chk->ShouldSkipMethod = &AttrFilter;
|
2019-01-26 22:23:08 +08:00
|
|
|
}
|
|
|
|
|
2020-03-27 21:29:31 +08:00
|
|
|
bool ento::shouldRegisterDirectIvarAssignment(const CheckerManager &mgr) {
|
2019-01-26 22:23:08 +08:00
|
|
|
return true;
|
|
|
|
}
|