I also think that in your case there is no naming pattern matching ns_consumed . Name templates are mainly based on NeXTSTEP / Apple, and I cannot come up with a method within apples with the same semantics that you want.
Note that you can tell Xcode to use the newer version of Clang Static Analyzer , which supports the ns_consumed attribute, which was released with checker-254 .
I am using checker-256 (released today, but any version> = 254 should work), and Ive just tried the following:
// MyClass.h #ifndef __has_feature // Optional. #define __has_feature(x) 0 // Compatibility with non-clang compilers. #endif #ifndef NS_CONSUMED #if __has_feature(attribute_ns_consumed) #define NS_CONSUMED __attribute__((ns_consumed)) #else #define NS_CONSUMED #endif #endif @interface MyClass : NSObject { @private NSString *_string; } + (MyClass *)myClassWithNewStringConsumed:(NSString *) NS_CONSUMED string NS_RETURNS_RETAINED; + (MyClass *)myClassWithNewString:(NSString *)string NS_RETURNS_RETAINED; @end
and
// MyClass.m #import "MyClass.h" @implementation MyClass + (MyClass *)myClassWithNewStringConsumed:(NSString *)string { MyClass *o = [MyClass new]; if (o) o->_string = string; return o; } + (MyClass *)myClassWithNewString:(NSString *)string { MyClass *o = [MyClass new]; if (o) o->_string = string; return o; } @end
This code gives the static analyzer a warning about a potential leak of a string stored in s :
// SomewhereElse.m NSString *s = [[NSString alloc] initWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]]; MyClass *o = [MyClass myClassWithNewString:s]; [o release];
whereas this code, which uses a method parameter with the ns_consumed attribute, does not give a static analyzer warning:
// SomewhereElse.m NSString *s = [[NSString alloc] initWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]]; MyClass *o = [MyClass myClassWithNewStringConsumed:s]; [o release];
user557219
source share