Objective-C 之Static Analysis 2

來源:互聯網
上載者:User

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))

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.