Objective-C 之Static Analysis 2
来源:互联网 发布:淘宝网评价管理 编辑:程序博客网 时间:2024/06/07 06:14
Source Annotations
主要支持几种检查
Specific Topics
Annotations to Enhance Generic Checks
Null Pointer Checking
Attribute 'nonnull'
Mac OS X API Annotations
Cocoa & Core Foundation Memory Management Annotations
Attribute 'ns_returns_retained'
Attribute 'ns_returns_not_retained'
Attribute 'cf_returns_retained'
Attribute 'cf_returns_not_retained'
Attribute 'ns_consumed'
Attribute 'cf_consumed'
Attribute 'ns_consumes_self'
Custom Assertion Handlers
Attribute 'noreturn'
Attribute 'analyzer_noreturn'
Attribute 'nonnull'
int bar(int*p, int q, int *r) __attribute__((nonnull(1,3)));
如果这样调用bar(nil,2,nil);在analyze中会提示
Null pointer passed as an argument to a 'nonnull' parameter
Attribute 'ns_returns_retained' (Clang-specific)
The GCC-style (Clang-specific) attribute 'ns_returns_retained' allows one to annotate an Objective-C method or C function as returning a retained Cocoa object that the caller is responsible for releasing (via sending a release message to the object).
Placing on Objective-C methods: For Objective-C methods, this annotation essentially tells the analyzer to treat the method as if its name begins with "alloc" or "new" or contais the word "copy".
Placing on C functions: For C functions returning Cocoa objects, the analyzer typically does not make any assumptions about whether or not the object is returned retained. Explicitly adding the 'ns_returns_retained' attribute to C functions allows the analyzer to perform extra checking. [小兵 注]这个是在混合编程的时候最重要的,因为如果没有标注,analyze是不会进行检查的。
Important note when using Garbage Collection: Note that the analyzer interprets this attribute slightly differently when using Objective-C garbage collection (available on Mac OS 10.5+). When analyzing Cocoa code that uses garbage collection, "alloc" methods are assumed to return an object that is managed by the garbage collector (and thus doesn't have a retain count the caller must balance). These same assumptions are applied to methods or functions annotated with 'ns_returns_retained'. If you are returning a Core Foundation object (which may not be managed by the garbage collector) you should use 'cf_returns_retained'.
和上一个blog写的一样
#define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
只不过上一次用的宏罢了,这次直接用它的本质。哈哈。
别的属性类推,咱们直接看定义
Attribute 'ns_returns_not_retained' (Clang-specific)
#define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
Attribute 'cf_returns_retained' (Clang-specific)
#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
Attribute 'cf_returns_not_retained' (Clang-specific)
#define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
Attribute 'ns_consumed' (Clang-specific)
#define NS_CONSUMED __attribute__((ns_consumed))
The 'ns_consumed' attribute can be placed on a specific parameter in either the declaration of a function or an Objective-C method. It indicates to the static analyzer that a release message is implicitly sent to the parameter upon completion of the call to the given function or method
Important note when using Garbage Collection: Note that the analyzer essentially ignores this attribute when code is compiled to use Objective-C garbage collection. This is because the release message does nothing when using GC. If the underlying function/method uses something like CFRelease to decrement the reference count, consider using the cf_consumed attribute instead.我还是多关心内存吧。不用什么垃圾回收,不会太考谱的。自力更生,更安全,不用的内存直接释放。
@interface Foo : NSObject
+ (void) releaseArg:(id) NS_CONSUMED x;
+ (void) releaseSecondArg:(id)x second:(id) NS_CONSUMED y;
@end
void test_method() {
id x = [[NSObject alloc] init];
[Foo releaseArg:x]; // No leak!
}
Attribute 'cf_consumed' (Clang-specific)
#define CF_CONSUMED __attribute__((cf_consumed))
Attribute 'ns_consumes_self' (Clang-specific)
The 'ns_consumes_self' attribute can be placed only on an Objective-C method declaration. It indicates that the receiver of the message is "consumed" (a single reference count decremented) after the message is sent. This matches the semantics of all "init" methods.
One use of this attribute is declare your own init-like methods that do not follow the standard Cocoa naming conventions.
Example
#ifndef __has_feature
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
#endif
#ifndef NS_CONSUMES_SELF
#if __has_feature((attribute_ns_consumes_self))
#define NS_CONSUMES_SELF __attribute__((ns_consumes_self))
#else
#define NS_CONSUMES_SELF
#endif
#endif
@interface MyClass : NSObject
- initWith:(MyClass *)x;
- nonstandardInitWith:(MyClass *)x NS_CONSUMES_SELF NS_RETURNS_RETAINED;
@end
In this example, nonstandardInitWith: has the same ownership semantics as the init method initWith:. The static analyzer will observe that the method consumes the receiver, and then returns an object with a +1 retain count
Attribute 'noreturn'
The 'noreturn' attribute is a GCC-attribute that can be placed on the declarations of functions. It means exactly what its name implies: a function with a 'noreturn' attribute should never return.
Example
On Mac OS X, the function prototype for __assert_rtn (declared in assert.h) is specifically annotated with the 'noreturn' attribute:
A few standard library functions, such as abort and exit, cannot return. GCC knows this automatically. Some programs define their own functions that never return. You can declare them noreturn to tell the compiler this fact. For example,
void __assert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__));
void fatal () __attribute__ ((noreturn));
void
fatal (/* ... */)
{
/* ... */ /* Print error message. */ /* ... */
exit (1);
}
Attribute 'analyzer_noreturn' (Clang-specific)
#define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
- Objective-C 之Static Analysis 2
- Objective-C 之Static Analysis
- objective-c 之 static
- Objective-C语法之static关键字
- Objective-C 语法之 static 关键字
- Objective-C 语法之 static 关键字
- C/C++ static analysis: CodeViz
- Objective-C研究院之static关键字(六)
- Objective-C语法之static关键字的那些事儿
- Objective-C研究之static关键字学习(六)
- Static variable in Objective C
- Objective-C中的static用法
- Objective-C static 类方法
- Objective-C类-Objective-C 局部静态变量 static
- Objective-C categories in static library
- Objective-C categories in static library
- Objective-C static关键字(六)
- Building Objective-C static libraries with categories
- 云计算与网格计算关系有别
- JAVA高级01_09 Math与Random类 学习API的方法 2011-4-21
- JSON-lib框架,转换JSON、XML不再困难
- VisualSVN Server的配置和使用方法--图文教程
- GDB用法详解
- Objective-C 之Static Analysis 2
- 什么是技术交底书? 如何撰写技术交底书
- django模板中使用静态文件的方法
- client and server direct
- kapit开源框架
- android启动后根文件系统分析
- mysql数据插入 编码问题
- Nt函数原型
- amazon simpledb分析