Objective-C Static Analysis 2

Source: Internet
Author: User

Source annotations

Mainly supports several checks

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

If bar (nil, 2, nil) is called in this way, a prompt is displayed in 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 essential tially 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. [Soldier Note] This is the most important part in mixed programming, because analyze will not check if there is no annotation.

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 '.

Same as the one written in the previous blog

# Define ns_returns_retained _ attribute _ (ns_returns_retained ))

It's just the macro used last time, but this time it's actually used. Haha.

 

Other attributes and so on. Let's look at the definition directly.

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 essential tially 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. I still care about the memory. No garbage collection is required. Self-reliance, more secure, and direct release of unused memory.

 

@ 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 caller 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 aggreger, 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 shocould 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 ))

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.