Skip to content
Snippets Groups Projects
Commit d7b1d246 authored by Anna Zaks's avatar Anna Zaks
Browse files

[analyzer] Add an annotation to allow suppression of direct ivar

assignment

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172597 91177308-0d34-0410-b5e6-96231b3b80d8
parent 79ccd563
No related branches found
No related tags found
No related merge requests found
...@@ -155,6 +155,18 @@ void DirectIvarAssignment::checkASTDecl(const ObjCImplementationDecl *D, ...@@ -155,6 +155,18 @@ void DirectIvarAssignment::checkASTDecl(const ObjCImplementationDecl *D,
} }
} }
static bool isAnnotatedToAllowDirectAssignment(const ObjCPropertyDecl *D) {
for (specific_attr_iterator<AnnotateAttr>
AI = D->specific_attr_begin<AnnotateAttr>(),
AE = D->specific_attr_end<AnnotateAttr>(); AI != AE; ++AI) {
const AnnotateAttr *Ann = *AI;
if (Ann->getAnnotation() ==
"objc_allow_direct_instance_variable_assignment")
return true;
}
return false;
}
void DirectIvarAssignment::MethodCrawler::VisitBinaryOperator( void DirectIvarAssignment::MethodCrawler::VisitBinaryOperator(
const BinaryOperator *BO) { const BinaryOperator *BO) {
if (!BO->isAssignmentOp()) if (!BO->isAssignmentOp())
...@@ -168,8 +180,14 @@ void DirectIvarAssignment::MethodCrawler::VisitBinaryOperator( ...@@ -168,8 +180,14 @@ void DirectIvarAssignment::MethodCrawler::VisitBinaryOperator(
if (const ObjCIvarDecl *D = IvarRef->getDecl()) { if (const ObjCIvarDecl *D = IvarRef->getDecl()) {
IvarToPropertyMapTy::const_iterator I = IvarToPropMap.find(D); IvarToPropertyMapTy::const_iterator I = IvarToPropMap.find(D);
if (I != IvarToPropMap.end()) { if (I != IvarToPropMap.end()) {
const ObjCPropertyDecl *PD = I->second; const ObjCPropertyDecl *PD = I->second;
// Skip warnings on Ivars that correspond to properties, annotated with
// objc_allow_direct_instance_variable_assignment. This annotation serves
// as a false positive suppression mechanism for the checker.
if (isAnnotatedToAllowDirectAssignment(PD))
return;
ObjCMethodDecl *GetterMethod = ObjCMethodDecl *GetterMethod =
InterfD->getInstanceMethod(PD->getGetterName()); InterfD->getInstanceMethod(PD->getGetterName());
......
...@@ -13,14 +13,14 @@ typedef signed char BOOL; ...@@ -13,14 +13,14 @@ typedef signed char BOOL;
@interface MyClass; @interface MyClass;
@end @end
@interface AnnotatedClass :NSObject { @interface AnnotatedClass : NSObject {
} }
- (void) someMethod: (MyClass*)In __attribute__((annotate("objc_no_direct_instance_variable_assignment"))); - (void) someMethod: (MyClass*)In __attribute__((annotate("objc_no_direct_instance_variable_assignment")));
- (void) someMethodNotAnnaotated: (MyClass*)In; - (void) someMethodNotAnnaotated: (MyClass*)In;
@end @end
@interface TestProperty :AnnotatedClass { @interface TestProperty : AnnotatedClass {
MyClass *_Z; MyClass *_Z;
id _nonSynth; id _nonSynth;
} }
...@@ -33,6 +33,9 @@ typedef signed char BOOL; ...@@ -33,6 +33,9 @@ typedef signed char BOOL;
@property (assign, nonatomic) MyClass* Z; // non synthesized ivar, implemented setter @property (assign, nonatomic) MyClass* Z; // non synthesized ivar, implemented setter
@property (readonly) id nonSynth; // non synthesized, explicitly implemented to return ivar with expected name @property (readonly) id nonSynth; // non synthesized, explicitly implemented to return ivar with expected name
@property (assign) MyClass* NotX __attribute__((annotate("objc_allow_direct_instance_variable_assignment"))); // warnings should be suppressed
@end @end
@implementation TestProperty @implementation TestProperty
...@@ -44,6 +47,7 @@ typedef signed char BOOL; ...@@ -44,6 +47,7 @@ typedef signed char BOOL;
_Y = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}} _Y = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
_Z = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}} _Z = In; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
_nonSynth = 0; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}} _nonSynth = 0; // expected-warning {{Direct assignment to an instance variable backing a property; use the setter instead}}
_NotX = 0; // no-warning
} }
- (void) someMethodNotAnnaotated: (MyClass*)In { - (void) someMethodNotAnnaotated: (MyClass*)In {
(__A) = In; (__A) = In;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment