blob: 2cb511cbeaefeb3fb783b7d0129c254deb105f80 [file] [log] [blame]
Anna Zaks88a83e32012-09-27 19:45:15 +00001//=- DirectIvarAssignment.cpp - Check rules on ObjC properties -*- C++ ----*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Check that Objective C properties follow the following rules:
11// - The property should be set with the setter, not though a direct
12// assignment.
13//
14//===----------------------------------------------------------------------===//
15
16#include "ClangSACheckers.h"
Anna Zaks39a62fc2012-12-05 01:14:37 +000017#include "clang/AST/Attr.h"
Anna Zaks88a83e32012-09-27 19:45:15 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/StmtVisitor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
21#include "clang/StaticAnalyzer/Core/Checker.h"
22#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Anna Zaks88a83e32012-09-27 19:45:15 +000023#include "llvm/ADT/DenseMap.h"
24
25using namespace clang;
26using namespace ento;
27
28namespace {
29
Anna Zaks39a62fc2012-12-05 01:14:37 +000030/// The default method filter, which is used to filter out the methods on which
31/// the check should not be performed.
32///
33/// Checks for the init, dealloc, and any other functions that might be allowed
34/// to perform direct instance variable assignment based on their name.
35struct MethodFilter {
Daniel Jasperaacadfe2012-12-05 09:23:48 +000036 virtual ~MethodFilter() {}
Anna Zaks39a62fc2012-12-05 01:14:37 +000037 virtual bool operator()(ObjCMethodDecl *M) {
38 if (M->getMethodFamily() == OMF_init ||
39 M->getMethodFamily() == OMF_dealloc ||
40 M->getMethodFamily() == OMF_copy ||
41 M->getMethodFamily() == OMF_mutableCopy ||
42 M->getSelector().getNameForSlot(0).find("init") != StringRef::npos ||
43 M->getSelector().getNameForSlot(0).find("Init") != StringRef::npos)
44 return true;
45 return false;
46 }
47};
48
49static MethodFilter DefaultMethodFilter;
50
Anna Zaks88a83e32012-09-27 19:45:15 +000051class DirectIvarAssignment :
52 public Checker<check::ASTDecl<ObjCImplementationDecl> > {
53
54 typedef llvm::DenseMap<const ObjCIvarDecl*,
55 const ObjCPropertyDecl*> IvarToPropertyMapTy;
56
57 /// A helper class, which walks the AST and locates all assignments to ivars
58 /// in the given function.
59 class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
60 const IvarToPropertyMapTy &IvarToPropMap;
61 const ObjCMethodDecl *MD;
62 const ObjCInterfaceDecl *InterfD;
63 BugReporter &BR;
64 LocationOrAnalysisDeclContext DCtx;
65
66 public:
67 MethodCrawler(const IvarToPropertyMapTy &InMap, const ObjCMethodDecl *InMD,
68 const ObjCInterfaceDecl *InID,
69 BugReporter &InBR, AnalysisDeclContext *InDCtx)
70 : IvarToPropMap(InMap), MD(InMD), InterfD(InID), BR(InBR), DCtx(InDCtx) {}
71
72 void VisitStmt(const Stmt *S) { VisitChildren(S); }
73
74 void VisitBinaryOperator(const BinaryOperator *BO);
75
76 void VisitChildren(const Stmt *S) {
77 for (Stmt::const_child_range I = S->children(); I; ++I)
78 if (*I)
79 this->Visit(*I);
80 }
81 };
82
83public:
Anna Zaks39a62fc2012-12-05 01:14:37 +000084 MethodFilter *ShouldSkipMethod;
85
86 DirectIvarAssignment() : ShouldSkipMethod(&DefaultMethodFilter) {}
87
Anna Zaks88a83e32012-09-27 19:45:15 +000088 void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& Mgr,
89 BugReporter &BR) const;
90};
91
92static const ObjCIvarDecl *findPropertyBackingIvar(const ObjCPropertyDecl *PD,
Anna Zaksbf247922012-09-27 21:57:17 +000093 const ObjCInterfaceDecl *InterD,
94 ASTContext &Ctx) {
Anna Zaks88a83e32012-09-27 19:45:15 +000095 // Check for synthesized ivars.
96 ObjCIvarDecl *ID = PD->getPropertyIvarDecl();
97 if (ID)
98 return ID;
99
Anna Zaksbf247922012-09-27 21:57:17 +0000100 ObjCInterfaceDecl *NonConstInterD = const_cast<ObjCInterfaceDecl*>(InterD);
101
Anna Zaks88a83e32012-09-27 19:45:15 +0000102 // Check for existing "_PropName".
Anna Zaksbf247922012-09-27 21:57:17 +0000103 ID = NonConstInterD->lookupInstanceVariable(PD->getDefaultSynthIvarName(Ctx));
Anna Zaks88a83e32012-09-27 19:45:15 +0000104 if (ID)
105 return ID;
106
107 // Check for existing "PropName".
108 IdentifierInfo *PropIdent = PD->getIdentifier();
Anna Zaksbf247922012-09-27 21:57:17 +0000109 ID = NonConstInterD->lookupInstanceVariable(PropIdent);
Anna Zaks88a83e32012-09-27 19:45:15 +0000110
111 return ID;
112}
113
114void DirectIvarAssignment::checkASTDecl(const ObjCImplementationDecl *D,
115 AnalysisManager& Mgr,
116 BugReporter &BR) const {
117 const ObjCInterfaceDecl *InterD = D->getClassInterface();
118
119
120 IvarToPropertyMapTy IvarToPropMap;
121
122 // Find all properties for this class.
123 for (ObjCInterfaceDecl::prop_iterator I = InterD->prop_begin(),
124 E = InterD->prop_end(); I != E; ++I) {
125 ObjCPropertyDecl *PD = *I;
126
127 // Find the corresponding IVar.
Anna Zaksbf247922012-09-27 21:57:17 +0000128 const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterD,
129 Mgr.getASTContext());
Anna Zaks88a83e32012-09-27 19:45:15 +0000130
131 if (!ID)
132 continue;
133
134 // Store the IVar to property mapping.
135 IvarToPropMap[ID] = PD;
136 }
137
138 if (IvarToPropMap.empty())
139 return;
140
141 for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
142 E = D->instmeth_end(); I != E; ++I) {
143
144 ObjCMethodDecl *M = *I;
145 AnalysisDeclContext *DCtx = Mgr.getAnalysisDeclContext(M);
146
Anna Zaks39a62fc2012-12-05 01:14:37 +0000147 if ((*ShouldSkipMethod)(M))
Anna Zaks88a83e32012-09-27 19:45:15 +0000148 continue;
149
150 const Stmt *Body = M->getBody();
Anna Zaksbf247922012-09-27 21:57:17 +0000151 assert(Body);
Anna Zaks88a83e32012-09-27 19:45:15 +0000152
153 MethodCrawler MC(IvarToPropMap, M->getCanonicalDecl(), InterD, BR, DCtx);
154 MC.VisitStmt(Body);
155 }
156}
157
Anna Zaksd7b1d242013-01-16 01:36:00 +0000158static bool isAnnotatedToAllowDirectAssignment(const ObjCPropertyDecl *D) {
159 for (specific_attr_iterator<AnnotateAttr>
160 AI = D->specific_attr_begin<AnnotateAttr>(),
161 AE = D->specific_attr_end<AnnotateAttr>(); AI != AE; ++AI) {
162 const AnnotateAttr *Ann = *AI;
163 if (Ann->getAnnotation() ==
164 "objc_allow_direct_instance_variable_assignment")
165 return true;
166 }
167 return false;
168}
169
Anna Zaks88a83e32012-09-27 19:45:15 +0000170void DirectIvarAssignment::MethodCrawler::VisitBinaryOperator(
171 const BinaryOperator *BO) {
172 if (!BO->isAssignmentOp())
173 return;
174
Anna Zaksbf247922012-09-27 21:57:17 +0000175 const ObjCIvarRefExpr *IvarRef =
176 dyn_cast<ObjCIvarRefExpr>(BO->getLHS()->IgnoreParenCasts());
Anna Zaks88a83e32012-09-27 19:45:15 +0000177
178 if (!IvarRef)
179 return;
180
181 if (const ObjCIvarDecl *D = IvarRef->getDecl()) {
182 IvarToPropertyMapTy::const_iterator I = IvarToPropMap.find(D);
Anna Zaksd7b1d242013-01-16 01:36:00 +0000183
Anna Zaks88a83e32012-09-27 19:45:15 +0000184 if (I != IvarToPropMap.end()) {
185 const ObjCPropertyDecl *PD = I->second;
Anna Zaksd7b1d242013-01-16 01:36:00 +0000186 // Skip warnings on Ivars that correspond to properties, annotated with
187 // objc_allow_direct_instance_variable_assignment. This annotation serves
188 // as a false positive suppression mechanism for the checker.
189 if (isAnnotatedToAllowDirectAssignment(PD))
190 return;
Anna Zaks88a83e32012-09-27 19:45:15 +0000191
192 ObjCMethodDecl *GetterMethod =
193 InterfD->getInstanceMethod(PD->getGetterName());
194 ObjCMethodDecl *SetterMethod =
195 InterfD->getInstanceMethod(PD->getSetterName());
196
197 if (SetterMethod && SetterMethod->getCanonicalDecl() == MD)
198 return;
199
200 if (GetterMethod && GetterMethod->getCanonicalDecl() == MD)
201 return;
202
Anna Zaks88a83e32012-09-27 19:45:15 +0000203 BR.EmitBasicReport(MD,
204 "Property access",
205 categories::CoreFoundationObjectiveC,
206 "Direct assignment to an instance variable backing a property; "
Anna Zaksbf247922012-09-27 21:57:17 +0000207 "use the setter instead", PathDiagnosticLocation(IvarRef,
208 BR.getSourceManager(),
209 DCtx));
Anna Zaks88a83e32012-09-27 19:45:15 +0000210 }
211 }
212}
213}
214
Anna Zaks39a62fc2012-12-05 01:14:37 +0000215// Register the checker that checks for direct accesses in all functions,
216// except for the initialization and copy routines.
Anna Zaks88a83e32012-09-27 19:45:15 +0000217void ento::registerDirectIvarAssignment(CheckerManager &mgr) {
218 mgr.registerChecker<DirectIvarAssignment>();
219}
Anna Zaks39a62fc2012-12-05 01:14:37 +0000220
221// Register the checker that checks for direct accesses in functions annotated
Ted Kremeneka05d2742012-12-22 00:34:48 +0000222// with __attribute__((annotate("objc_no_direct_instance_variable_assignment"))).
Anna Zaks39a62fc2012-12-05 01:14:37 +0000223namespace {
224struct InvalidatorMethodFilter : MethodFilter {
Daniel Jasperaacadfe2012-12-05 09:23:48 +0000225 virtual ~InvalidatorMethodFilter() {}
Anna Zaks39a62fc2012-12-05 01:14:37 +0000226 virtual bool operator()(ObjCMethodDecl *M) {
227 for (specific_attr_iterator<AnnotateAttr>
228 AI = M->specific_attr_begin<AnnotateAttr>(),
229 AE = M->specific_attr_end<AnnotateAttr>(); AI != AE; ++AI) {
230 const AnnotateAttr *Ann = *AI;
Ted Kremeneka05d2742012-12-22 00:34:48 +0000231 if (Ann->getAnnotation() == "objc_no_direct_instance_variable_assignment")
Anna Zaks39a62fc2012-12-05 01:14:37 +0000232 return false;
233 }
234 return true;
235 }
236};
237
238InvalidatorMethodFilter AttrFilter;
239}
240
241void ento::registerDirectIvarAssignmentForAnnotatedFunctions(
242 CheckerManager &mgr) {
243 mgr.registerChecker<DirectIvarAssignment>()->ShouldSkipMethod = &AttrFilter;
244}