37#include "llvm/ADT/DenseMap.h"
38#include "llvm/ADT/STLExtras.h"
39#include "llvm/ADT/SetVector.h"
40#include "llvm/ADT/StringRef.h"
48 bool check_MissingInvalidationMethod =
false;
50 bool check_InstanceVariableInvalidation =
false;
52 CheckerNameRef checkName_MissingInvalidationMethod;
53 CheckerNameRef checkName_InstanceVariableInvalidation;
56class IvarInvalidationCheckerImpl {
57 typedef llvm::SmallSetVector<const ObjCMethodDecl*, 2> MethodSet;
58 typedef llvm::DenseMap<
const ObjCMethodDecl*,
59 const ObjCIvarDecl*> MethToIvarMapTy;
60 typedef llvm::DenseMap<
const ObjCPropertyDecl*,
61 const ObjCIvarDecl*> PropToIvarMapTy;
62 typedef llvm::DenseMap<
const ObjCIvarDecl*,
63 const ObjCPropertyDecl*> IvarToPropMapTy;
65 struct InvalidationInfo {
67 bool IsInvalidated =
false;
70 MethodSet InvalidationMethods;
72 InvalidationInfo() =
default;
73 void addInvalidationMethod(
const ObjCMethodDecl *MD) {
74 InvalidationMethods.insert(MD);
77 bool needsInvalidation()
const {
78 return !InvalidationMethods.empty();
81 bool hasMethod(
const ObjCMethodDecl *MD) {
84 if (llvm::is_contained(InvalidationMethods, MD)) {
92 typedef llvm::DenseMap<const ObjCIvarDecl*, InvalidationInfo> IvarSet;
96 class MethodCrawler :
public ConstStmtVisitor<MethodCrawler> {
102 bool &CalledAnotherInvalidationMethod;
105 const MethToIvarMapTy &PropertySetterToIvarMap;
108 const MethToIvarMapTy &PropertyGetterToIvarMap;
111 const PropToIvarMapTy &PropertyToIvarMap;
114 const ObjCMethodDecl *InvalidationMethod;
119 const Expr *peel(
const Expr *E)
const;
122 bool isZero(
const Expr *E)
const;
125 void markInvalidated(
const ObjCIvarDecl *Iv);
129 void checkObjCIvarRefExpr(
const ObjCIvarRefExpr *IvarRef);
133 void checkObjCPropertyRefExpr(
const ObjCPropertyRefExpr *PA);
137 void checkObjCMessageExpr(
const ObjCMessageExpr *ME);
140 void check(
const Expr *E);
143 MethodCrawler(IvarSet &InIVars,
144 bool &InCalledAnotherInvalidationMethod,
145 const MethToIvarMapTy &InPropertySetterToIvarMap,
146 const MethToIvarMapTy &InPropertyGetterToIvarMap,
147 const PropToIvarMapTy &InPropertyToIvarMap,
150 CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
151 PropertySetterToIvarMap(InPropertySetterToIvarMap),
152 PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
153 PropertyToIvarMap(InPropertyToIvarMap),
157 void VisitStmt(
const Stmt *S) { VisitChildren(S); }
159 void VisitBinaryOperator(
const BinaryOperator *BO);
161 void VisitObjCMessageExpr(
const ObjCMessageExpr *ME);
163 void VisitChildren(
const Stmt *S) {
164 for (
const auto *Child : S->
children()) {
167 if (CalledAnotherInvalidationMethod)
177 static void containsInvalidationMethod(
const ObjCContainerDecl *D,
178 InvalidationInfo &Out,
179 bool LookForPartial);
183 static bool trackIvar(
const ObjCIvarDecl *Iv, IvarSet &TrackedIvars,
184 const ObjCIvarDecl **FirstIvarDecl);
189 static const ObjCIvarDecl *findPropertyBackingIvar(
190 const ObjCPropertyDecl *Prop,
191 const ObjCInterfaceDecl *InterfaceD,
192 IvarSet &TrackedIvars,
193 const ObjCIvarDecl **FirstIvarDecl);
196 static void printIvar(llvm::raw_svector_ostream &os,
197 const ObjCIvarDecl *IvarDecl,
198 const IvarToPropMapTy &IvarToPopertyMap);
200 void reportNoInvalidationMethod(CheckerNameRef CheckName,
201 const ObjCIvarDecl *FirstIvarDecl,
202 const IvarToPropMapTy &IvarToPopertyMap,
203 const ObjCInterfaceDecl *InterfaceD,
204 bool MissingDeclaration)
const;
206 void reportIvarNeedsInvalidation(
const ObjCIvarDecl *IvarD,
207 const IvarToPropMapTy &IvarToPopertyMap,
208 const ObjCMethodDecl *MethodD)
const;
210 AnalysisManager& Mgr;
213 const ChecksFilter &Filter;
216 IvarInvalidationCheckerImpl(AnalysisManager& InMgr,
218 const ChecksFilter &InFilter) :
219 Mgr (InMgr), BR(InBR), Filter(InFilter) {}
221 void visit(
const ObjCImplementationDecl *D)
const;
227 ? llvm::StringLiteral(
"objc_instance_variable_invalidator_partial")
229 auto IsWantedAnnotation = [Wanted](
const AnnotateAttr *Ann) {
230 return Ann->getAnnotation() == Wanted;
232 return llvm::any_of(M->
specific_attrs<AnnotateAttr>(), IsWantedAnnotation);
235void IvarInvalidationCheckerImpl::containsInvalidationMethod(
245 for (
const auto *MDI : D->
methods())
247 OutInfo.addInvalidationMethod(
251 if (
const ObjCInterfaceDecl *InterfD = dyn_cast<ObjCInterfaceDecl>(D)) {
254 for (
const auto *I : InterfD->protocols())
255 containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
259 for (
const auto *Ext : InterfD->visible_extensions())
260 containsInvalidationMethod(Ext, OutInfo, Partial);
262 containsInvalidationMethod(InterfD->getSuperClass(), OutInfo, Partial);
267 if (
const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
268 for (
const auto *I : ProtD->protocols()) {
269 containsInvalidationMethod(I->getDefinition(), OutInfo, Partial);
275bool IvarInvalidationCheckerImpl::trackIvar(
const ObjCIvarDecl *Iv,
276 IvarSet &TrackedIvars,
277 const ObjCIvarDecl **FirstIvarDecl) {
278 QualType IvQTy = Iv->
getType();
279 const ObjCObjectPointerType *IvTy = IvQTy->
getAs<ObjCObjectPointerType>();
284 InvalidationInfo Info;
285 containsInvalidationMethod(IvInterf, Info,
false);
286 if (Info.needsInvalidation()) {
288 TrackedIvars[I] = std::move(Info);
296const ObjCIvarDecl *IvarInvalidationCheckerImpl::findPropertyBackingIvar(
297 const ObjCPropertyDecl *Prop,
298 const ObjCInterfaceDecl *InterfaceD,
299 IvarSet &TrackedIvars,
300 const ObjCIvarDecl **FirstIvarDecl) {
301 const ObjCIvarDecl *IvarD =
nullptr;
308 if (TrackedIvars.count(IvarD)) {
312 if (trackIvar(IvarD, TrackedIvars, FirstIvarDecl))
318 for (
const ObjCIvarDecl *Iv : llvm::make_first_range(TrackedIvars)) {
319 StringRef IvarName = Iv->
getName();
321 if (IvarName == PropName)
324 SmallString<128> PropNameWithUnderscore;
326 llvm::raw_svector_ostream os(PropNameWithUnderscore);
327 os <<
'_' << PropName;
329 if (IvarName == PropNameWithUnderscore)
339void IvarInvalidationCheckerImpl::printIvar(llvm::raw_svector_ostream &os,
340 const ObjCIvarDecl *IvarDecl,
341 const IvarToPropMapTy &IvarToPopertyMap) {
343 const ObjCPropertyDecl *PD = IvarToPopertyMap.lookup(IvarDecl);
344 assert(PD &&
"Do we synthesize ivars for something other than properties?");
345 os <<
"Property "<< PD->
getName() <<
" ";
347 os <<
"Instance variable "<< IvarDecl->
getName() <<
" ";
353void IvarInvalidationCheckerImpl::
354visit(
const ObjCImplementationDecl *ImplD)
const {
360 const ObjCIvarDecl *FirstIvarDecl =
nullptr;
364 ObjCInterfaceDecl *IDecl =
const_cast<ObjCInterfaceDecl *
>(InterfaceD);
367 trackIvar(Iv, Ivars, &FirstIvarDecl);
371 MethToIvarMapTy PropSetterToIvarMap;
372 MethToIvarMapTy PropGetterToIvarMap;
373 PropToIvarMapTy PropertyToIvarMap;
374 IvarToPropMapTy IvarToPopertyMap;
376 ObjCInterfaceDecl::PropertyMap PropMap;
379 for (
const ObjCPropertyDecl *PD : llvm::make_second_range(PropMap)) {
383 const ObjCIvarDecl *
ID = findPropertyBackingIvar(PD, InterfaceD, Ivars,
390 PropertyToIvarMap[PD] =
ID;
391 IvarToPopertyMap[
ID] = PD;
397 PropSetterToIvarMap[SetterD] =
ID;
403 PropGetterToIvarMap[GetterD] =
ID;
412 InvalidationInfo PartialInfo;
413 containsInvalidationMethod(InterfaceD, PartialInfo,
true);
417 bool AtImplementationContainsAtLeastOnePartialInvalidationMethod =
false;
418 for (
const ObjCMethodDecl *InterfD : PartialInfo.InvalidationMethods) {
422 if (Stmt *Body; D && (Body = D->
getBody())) {
423 AtImplementationContainsAtLeastOnePartialInvalidationMethod =
true;
425 bool CalledAnotherInvalidationMethod =
false;
428 CalledAnotherInvalidationMethod,
435 if (CalledAnotherInvalidationMethod)
446 InvalidationInfo Info;
447 containsInvalidationMethod(InterfaceD, Info,
false);
450 if (!Info.needsInvalidation() && !PartialInfo.needsInvalidation()) {
451 if (Filter.check_MissingInvalidationMethod)
452 reportNoInvalidationMethod(Filter.checkName_MissingInvalidationMethod,
453 FirstIvarDecl, IvarToPopertyMap, InterfaceD,
462 if (!Filter.check_InstanceVariableInvalidation)
466 bool AtImplementationContainsAtLeastOneInvalidationMethod =
false;
467 for (
const ObjCMethodDecl *InterfD : Info.InvalidationMethods) {
471 if (Stmt *Body; D && (Body = D->
getBody())) {
472 AtImplementationContainsAtLeastOneInvalidationMethod =
true;
475 IvarSet IvarsI = Ivars;
477 bool CalledAnotherInvalidationMethod =
false;
478 MethodCrawler(IvarsI,
479 CalledAnotherInvalidationMethod,
486 if (CalledAnotherInvalidationMethod)
490 for (
const ObjCIvarDecl *Ivar : llvm::make_first_range(IvarsI))
491 reportIvarNeedsInvalidation(Ivar, IvarToPopertyMap, D);
496 if (!AtImplementationContainsAtLeastOneInvalidationMethod) {
497 if (AtImplementationContainsAtLeastOnePartialInvalidationMethod) {
500 for (
const ObjCIvarDecl *Ivar : llvm::make_first_range(Ivars))
501 reportIvarNeedsInvalidation(Ivar, IvarToPopertyMap,
nullptr);
504 reportNoInvalidationMethod(Filter.checkName_InstanceVariableInvalidation,
505 FirstIvarDecl, IvarToPopertyMap, InterfaceD,
511void IvarInvalidationCheckerImpl::reportNoInvalidationMethod(
512 CheckerNameRef CheckName,
const ObjCIvarDecl *FirstIvarDecl,
513 const IvarToPropMapTy &IvarToPopertyMap,
514 const ObjCInterfaceDecl *InterfaceD,
bool MissingDeclaration)
const {
515 SmallString<128> sbuf;
516 llvm::raw_svector_ostream os(sbuf);
517 assert(FirstIvarDecl);
518 printIvar(os, FirstIvarDecl, IvarToPopertyMap);
519 os <<
"needs to be invalidated; ";
520 if (MissingDeclaration)
521 os <<
"no invalidation method is declared for ";
523 os <<
"no invalidation method is defined in the @implementation for ";
526 PathDiagnosticLocation IvarDecLocation =
529 BR.
EmitBasicReport(FirstIvarDecl, CheckName,
"Incomplete invalidation",
534void IvarInvalidationCheckerImpl::
535reportIvarNeedsInvalidation(
const ObjCIvarDecl *IvarD,
536 const IvarToPropMapTy &IvarToPopertyMap,
537 const ObjCMethodDecl *MethodD)
const {
538 SmallString<128> sbuf;
539 llvm::raw_svector_ostream os(sbuf);
540 printIvar(os, IvarD, IvarToPopertyMap);
541 os <<
"needs to be invalidated or set to nil";
543 PathDiagnosticLocation MethodDecLocation =
547 BR.
EmitBasicReport(MethodD, Filter.checkName_InstanceVariableInvalidation,
548 "Incomplete invalidation",
553 IvarD, Filter.checkName_InstanceVariableInvalidation,
560void IvarInvalidationCheckerImpl::MethodCrawler::markInvalidated(
561 const ObjCIvarDecl *Iv) {
562 IvarSet::iterator I = IVars.find(Iv);
563 if (I != IVars.end()) {
567 if (!InvalidationMethod || I->second.hasMethod(InvalidationMethod))
572const Expr *IvarInvalidationCheckerImpl::MethodCrawler::peel(
const Expr *E)
const {
574 if (
const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
576 if (
const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
581void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCIvarRefExpr(
582 const ObjCIvarRefExpr *IvarRef) {
583 if (
const Decl *D = IvarRef->
getDecl())
587void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCMessageExpr(
588 const ObjCMessageExpr *ME) {
592 MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
593 if (IvI != PropertyGetterToIvarMap.end())
594 markInvalidated(IvI->second);
598void IvarInvalidationCheckerImpl::MethodCrawler::checkObjCPropertyRefExpr(
599 const ObjCPropertyRefExpr *PA) {
605 PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
606 if (IvI != PropertyToIvarMap.end())
607 markInvalidated(IvI->second);
616 MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
617 if (IvI != PropertyGetterToIvarMap.end())
618 markInvalidated(IvI->second);
624bool IvarInvalidationCheckerImpl::MethodCrawler::isZero(
const Expr *E)
const {
631void IvarInvalidationCheckerImpl::MethodCrawler::check(
const Expr *E) {
634 if (
const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
635 checkObjCIvarRefExpr(IvarRef);
639 if (
const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
640 checkObjCPropertyRefExpr(PropRef);
644 if (
const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
645 checkObjCMessageExpr(MsgExpr);
650void IvarInvalidationCheckerImpl::MethodCrawler::VisitBinaryOperator(
651 const BinaryOperator *BO) {
657 if (Opcode != BO_Assign &&
662 if (isZero(BO->
getRHS())) {
667 if (Opcode != BO_Assign && isZero(BO->
getLHS())) {
673void IvarInvalidationCheckerImpl::MethodCrawler::VisitObjCMessageExpr(
674 const ObjCMessageExpr *ME) {
681 CalledAnotherInvalidationMethod =
true;
688 MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
689 if (IvI != PropertySetterToIvarMap.end()) {
690 markInvalidated(IvI->second);
697 InvalidationMethod = MD;
699 InvalidationMethod =
nullptr;
708class IvarInvalidationChecker :
709 public Checker<check::ASTDecl<ObjCImplementationDecl> > {
713 void checkASTDecl(
const ObjCImplementationDecl *D, AnalysisManager& Mgr,
714 BugReporter &BR)
const {
715 IvarInvalidationCheckerImpl Walker(Mgr, BR, Filter);
721void ento::registerIvarInvalidationModeling(CheckerManager &mgr) {
725bool ento::shouldRegisterIvarInvalidationModeling(
const CheckerManager &mgr) {
729#define REGISTER_CHECKER(name) \
730 void ento::register##name(CheckerManager &mgr) { \
731 IvarInvalidationChecker *checker = \
732 mgr.getChecker<IvarInvalidationChecker>(); \
733 checker->Filter.check_##name = true; \
734 checker->Filter.checkName_##name = mgr.getCurrentCheckerName(); \
737 bool ento::shouldRegister##name(const CheckerManager &mgr) { return true; }
742#undef REGISTER_CHECKER
#define REGISTER_CHECKER(name)
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
bool isObjCSelfExpr() const
Check if this expression is the ObjC 'self' implicit parameter.
@ NPC_ValueDependentIsNotNull
Specifies that a value-dependent expression should be considered to never be a null pointer constant.
@ NPCK_NotNull
Expression is not a Null pointer constant.
NullPointerConstantKind isNullPointerConstant(ASTContext &Ctx, NullPointerConstantValueDependence NPC) const
isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to a Null pointer constant.
StringRef getName() const
Return the actual identifier string.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
ObjCContainerDecl - Represents a container for method declarations.
ObjCMethodDecl * getMethod(Selector Sel, bool isInstance, bool AllowHidden=false) const
method_range methods() const
const ObjCInterfaceDecl * getClassInterface() const
ObjCIvarDecl * all_declared_ivar_begin()
all_declared_ivar_begin - return first ivar declared in this class, its extensions and its implementa...
void collectPropertiesToImplement(PropertyMap &PM) const override
This routine collects list of properties to be implemented in the class.
bool getSynthesize() const
ObjCInterfaceDecl * getContainingInterface()
Return the class interface that this ivar is logically contained in; this is either the interface whe...
ObjCIvarDecl * getNextIvar()
ObjCIvarDecl * getCanonicalDecl() override
Retrieves the canonical declaration of this field.
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Expr * getInstanceReceiver()
Returns the object expression (receiver) for an instance message, or null for a message that is not a...
const ObjCMethodDecl * getMethodDecl() const
unsigned getNumArgs() const
Return the number of actual arguments in this message, not counting the receiver.
ObjCMethodDecl - Represents an instance or class method declaration.
Stmt * getBody() const override
Retrieve the body of this method, if it has one.
ObjCMethodDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Selector getSelector() const
bool isInstanceMethod() const
ObjCInterfaceDecl * getInterfaceDecl() const
If this pointer points to an Objective @interface type, gets the declaration for that interface.
bool isClassProperty() const
ObjCMethodDecl * getGetterMethodDecl() const
ObjCMethodDecl * getSetterMethodDecl() const
ObjCIvarDecl * getPropertyIvarDecl() const
ObjCPropertyDecl * getExplicitProperty() const
bool isExplicitProperty() const
bool isImplicitProperty() const
ObjCMethodDecl * getImplicitPropertySetter() const
StringLiteral - This represents a string literal expression, e.g.
const T * getAs() const
Member-template getAs<specific type>'.
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
const SourceManager & getSourceManager()
ASTContext & getContext()
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerFrontend *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges={}, ArrayRef< FixItHint > Fixits={})
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
static PathDiagnosticLocation createEnd(const Stmt *S, const SourceManager &SM, const StackFrameOrAnalysisDeclContext SFAC)
Create a location for the end of the statement.
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
bool isInvalidationMethod(const CXXMethodDecl &MD)
const char *const CoreFoundationObjectiveC
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
U cast(CodeGen::Address addr)
Diagnostic wrappers for TextAPI types for error reporting.