Debian Codegraph @

clang/docs/analyzer/user-docs/Annotations.rst

raw
1 ==================
2 Source Annotations
3 ==================
4
5 The Clang frontend supports several source-level annotations in the form of
6 `GCC-style attributes <https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html>`_
7 and pragmas that can help make using the Clang Static Analyzer more useful.
8 These annotations can both help suppress false positives as well as enhance the
9 analyzer's ability to find bugs.
10
11 This page gives a practical overview of such annotations. For more technical
12 specifics regarding Clang-specific annotations please see the Clang's list of
13 `language extensions <https://clang.llvm.org/docs/LanguageExtensions.html>`_.
14 Details of "standard" GCC attributes (that Clang also supports) can
15 be found in the `GCC manual <https://gcc.gnu.org/onlinedocs/gcc/>`_, with the
16 majority of the relevant attributes being in the section on
17 `function attributes <https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_.
18
19 Note that attributes that are labeled **Clang-specific** are not
20 recognized by GCC. Their use can be conditioned using preprocessor macros
21 (examples included on this page).
22
23 .. contents::
24 :local:
25
26 General Purpose Annotations
27 ___________________________
28
29 Null Pointer Checking
30 #####################
31
32 Attribute 'nonnull'
33 -------------------
34
35 The analyzer recognizes the GCC attribute 'nonnull', which indicates that a
36 function expects that a given function parameter is not a null pointer.
37 Specific details of the syntax of using the 'nonnull' attribute can be found in
38 `GCC's documentation <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-nonnull-function-attribute>`_.
39
40 Both the Clang compiler and GCC will flag warnings for simple cases where a
41 null pointer is directly being passed to a function with a 'nonnull' parameter
42 (e.g., as a constant). The analyzer extends this checking by using its deeper
43 symbolic analysis to track what pointer values are potentially null and then
44 flag warnings when they are passed in a function call via a 'nonnull'
45 parameter.
46
47 **Example**
48
49 .. code-block:: c
50
51 int bar(int*p, int q, int *r) __attribute__((nonnull(1,3)));
52
53 int foo(int *p, int *q) {
54 return !p ? bar(q, 2, p)
55 : bar(p, 2, q);
56 }
57
58 Running ``scan-build`` over this source produces the following output:
59
60 .. image:: ../images/example_attribute_nonnull.png
61
62 .. _custom_assertion_handlers:
63
64 Custom Assertion Handlers
65 #########################
66
67 The analyzer exploits code assertions by pruning off paths where the
68 assertion condition is false. The idea is capture any program invariants
69 specified in the assertion that the developer may know but is not immediately
70 apparent in the code itself. In this way assertions make implicit assumptions
71 explicit in the code, which not only makes the analyzer more accurate when
72 finding bugs, but can help others better able to understand your code as well.
73 It can also help remove certain kinds of analyzer false positives by pruning off
74 false paths.
75
76 In order to exploit assertions, however, the analyzer must understand when it
77 encounters an "assertion handler". Typically assertions are
78 implemented with a macro, with the macro performing a check for the assertion
79 condition and, when the check fails, calling an assertion handler. For
80 example, consider the following code fragment:
81
82 .. code-block:: c
83
84 void foo(int *p) {
85 assert(p != NULL);
86 }
87
88 When this code is preprocessed on Mac OS X it expands to the following:
89
90 .. code-block:: c
91
92 void foo(int *p) {
93 (__builtin_expect(!(p != NULL), 0) ? __assert_rtn(__func__, "t.c", 4, "p != NULL") : (void)0);
94 }
95
96 In this example, the assertion handler is ``__assert_rtn``. When called,
97 most assertion handlers typically print an error and terminate the program. The
98 analyzer can exploit such semantics by ending the analysis of a path once it
99 hits a call to an assertion handler.
100
101 The trick, however, is that the analyzer needs to know that a called function
102 is an assertion handler; otherwise the analyzer might assume the function call
103 returns and it will continue analyzing the path where the assertion condition
104 failed. This can lead to false positives, as the assertion condition usually
105 implies a safety condition (e.g., a pointer is not null) prior to performing
106 some action that depends on that condition (e.g., dereferencing a pointer).
107
108 The analyzer knows about several well-known assertion handlers, but can
109 automatically infer if a function should be treated as an assertion handler if
110 it is annotated with the 'noreturn' attribute or the (Clang-specific)
111 'analyzer_noreturn' attribute. Note that, currently, clang does not support
112 these attributes on Objective-C methods and C++ methods.
113
114 Attribute 'noreturn'
115 --------------------
116
117 The 'noreturn' attribute is a GCC attribute that can be placed on the
118 declarations of functions. It means exactly what its name implies: a function
119 with a 'noreturn' attribute should never return.
120
121 Specific details of the syntax of using the 'noreturn' attribute can be found
122 in `GCC's documentation <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute>`__.
123
124 Not only does the analyzer exploit this information when pruning false paths,
125 but the compiler also takes it seriously and will generate different code (and
126 possibly better optimized) under the assumption that the function does not
127 return.
128
129 **Example**
130
131 On Mac OS X, the function prototype for ``__assert_rtn`` (declared in
132 ``assert.h``) is specifically annotated with the 'noreturn' attribute:
133
134 .. code-block:: c
135
136 void __assert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__));
137
138 Attribute 'analyzer_noreturn' (Clang-specific)
139 ----------------------------------------------
140
141 The Clang-specific 'analyzer_noreturn' attribute is almost identical to
142 'noreturn' except that it is ignored by the compiler for the purposes of code
143 generation.
144
145 This attribute is useful for annotating assertion handlers that actually
146 *can* return, but for the purpose of using the analyzer we want to
147 pretend that such functions do not return.
148
149 Because this attribute is Clang-specific, its use should be conditioned with
150 the use of preprocessor macros.
151
152 **Example**
153
154 .. code-block:: c
155
156 #ifndef CLANG_ANALYZER_NORETURN
157 #if __has_feature(attribute_analyzer_noreturn)
158 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
159 #else
160 #define CLANG_ANALYZER_NORETURN
161 #endif
162 #endif
163
164 void my_assert_rtn(const char *, const char *, int, const char *) CLANG_ANALYZER_NORETURN;
165
166 Dynamic Memory Modeling Annotations
167 ###################################
168
169 If a project uses custom functions for dynamic memory management (that e.g. act as wrappers around ``malloc``/``free`` or ``new``/``delete`` in C++) and the analyzer cannot "see" the _definitions_ of these functions, it's possible to annotate their declarations to let the analyzer model their behavior. (Otherwise the analyzer cannot know that the opaque ``my_free()`` is basically equivalent to a standard ``free()`` call.)
170
171 .. note::
172 **This page only provides a brief list of these annotations.** For a full documentation, see the main `Attributes in Clang <../../AttributeReference.html#ownership-holds-ownership-returns-ownership-takes-clang-static-analyzer>`_ page.
173
174 Attribute 'ownership_returns' (Clang-specific)
175 ----------------------------------------------
176
177 Use this attribute to mark functions that return dynamically allocated memory. Takes a single argument, the type of the allocation (e.g. ``malloc`` or ``new``).
178
179 .. code-block:: c
180
181 void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
182
183 Attribute 'ownership_takes' (Clang-specific)
184 --------------------------------------------
185
186 Use this attribute to mark functions that deallocate memory. Takes two arguments: the type of the allocation (e.g. ``malloc`` or ``new``) and the index of the parameter that is being deallocated (counting from 1).
187
188 .. code-block:: c
189
190 void __attribute((ownership_takes(malloc, 1))) my_free(void *);
191
192 Attribute 'ownership_holds' (Clang-specific)
193 --------------------------------------------
194
195 Use this attribute to mark functions that take ownership of memory and will deallocate it at some unspecified point in the future. Takes two arguments: the type of the allocation (e.g. ``malloc`` or ``new``) and the index of the parameter that is being held (counting from 1).
196
197 .. code-block:: c
198
199 void __attribute((ownership_holds(malloc, 2))) store_in_table(int key, record_t *val);
200
201 The annotations ``ownership_takes`` and ``ownership_holds`` both prevent memory leak reports (concerning the specified argument); the difference between them is that using taken memory is a use-after-free error, while using held memory is assumed to be legitimate.
202
203 Mac OS X API Annotations
204 ________________________
205
206 .. _cocoa_mem:
207
208 Cocoa & Core Foundation Memory Management Annotations
209 #####################################################
210
211 The analyzer supports the proper management of retain counts for
212 both Cocoa and Core Foundation objects. This checking is largely based on
213 enforcing Cocoa and Core Foundation naming conventions for Objective-C methods
214 (Cocoa) and C functions (Core Foundation). Not strictly following these
215 conventions can cause the analyzer to miss bugs or flag false positives.
216
217 One can educate the analyzer (and others who read your code) about methods or
218 functions that deviate from the Cocoa and Core Foundation conventions using the
219 attributes described here. However, you should consider using proper naming
220 conventions or the `objc_method_family <https://clang.llvm.org/docs/LanguageExtensions.html#the-objc-method-family-attribute>`_
221 attribute, if applicable.
222
223 .. _ns_returns_retained:
224
225 Attribute 'ns_returns_retained' (Clang-specific)
226 ------------------------------------------------
227
228 The GCC-style (Clang-specific) attribute 'ns_returns_retained' allows one to
229 annotate an Objective-C method or C function as returning a retained Cocoa
230 object that the caller is responsible for releasing (via sending a
231 ``release`` message to the object). The Foundation framework defines a
232 macro ``NS_RETURNS_RETAINED`` that is functionally equivalent to the
233 one shown below.
234
235 **Placing on Objective-C methods**: For Objective-C methods, this
236 annotation essentially tells the analyzer to treat the method as if its name
237 begins with "alloc" or "new" or contains the word
238 "copy".
239
240 **Placing on C functions**: For C functions returning Cocoa objects, the
241 analyzer typically does not make any assumptions about whether or not the object
242 is returned retained. Explicitly adding the 'ns_returns_retained' attribute to C
243 functions allows the analyzer to perform extra checking.
244
245 **Example**
246
247 .. code-block:: objc
248
249 #import <Foundation/Foundation.h>;
250
251 #ifndef __has_feature // Optional.
252 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
253 #endif
254
255 #ifndef NS_RETURNS_RETAINED
256 #if __has_feature(attribute_ns_returns_retained)
257 #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained))
258 #else
259 #define NS_RETURNS_RETAINED
260 #endif
261 #endif
262
263 @interface MyClass : NSObject {}
264 - (NSString*) returnsRetained NS_RETURNS_RETAINED;
265 - (NSString*) alsoReturnsRetained;
266 @end
267
268 @implementation MyClass
269 - (NSString*) returnsRetained {
270 return [[NSString alloc] initWithCString:"no leak here"];
271 }
272 - (NSString*) alsoReturnsRetained {
273 return [[NSString alloc] initWithCString:"flag a leak"];
274 }
275 @end
276
277 Running ``scan-build`` on this source file produces the following output:
278
279 .. image:: ../images/example_ns_returns_retained.png
280
281 .. _ns_returns_not_retained:
282
283 Attribute 'ns_returns_not_retained' (Clang-specific)
284 ----------------------------------------------------
285
286 The 'ns_returns_not_retained' attribute is the complement of
287 '`ns_returns_retained`_'. Where a function or method may appear to obey the
288 Cocoa conventions and return a retained Cocoa object, this attribute can be
289 used to indicate that the object reference returned should not be considered as
290 an "owning" reference being returned to the caller. The Foundation
291 framework defines a macro ``NS_RETURNS_NOT_RETAINED`` that is functionally
292 equivalent to the one shown below.
293
294 Usage is identical to `ns_returns_retained`_. When using the
295 attribute, be sure to declare it within the proper macro that checks for
296 its availability, as it is not available in earlier versions of the analyzer:
297
298 .. code-block:objc
299
300 #ifndef __has_feature // Optional.
301 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
302 #endif
303
304 #ifndef NS_RETURNS_NOT_RETAINED
305 #if __has_feature(attribute_ns_returns_not_retained)
306 #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))
307 #else
308 #define NS_RETURNS_NOT_RETAINED
309 #endif
310 #endif
311
312 .. _cf_returns_retained:
313
314 Attribute 'cf_returns_retained' (Clang-specific)
315 ------------------------------------------------
316
317 The GCC-style (Clang-specific) attribute 'cf_returns_retained' allows one to
318 annotate an Objective-C method or C function as returning a retained Core
319 Foundation object that the caller is responsible for releasing. The
320 CoreFoundation framework defines a macro ``CF_RETURNS_RETAINED`` that is
321 functionally equivalent to the one shown below.
322
323 **Placing on Objective-C methods**: With respect to Objective-C methods.,
324 this attribute is identical in its behavior and usage to 'ns_returns_retained'
325 except for the distinction of returning a Core Foundation object instead of a
326 Cocoa object.
327
328 This distinction is important for the following reason: as Core Foundation is a
329 C API, the analyzer cannot always tell that a pointer return value refers to a
330 Core Foundation object. In contrast, it is trivial for the analyzer to
331 recognize if a pointer refers to a Cocoa object (given the Objective-C type
332 system).
333
334 **Placing on C functions**: When placing the attribute
335 'cf_returns_retained' on the declarations of C functions, the analyzer
336 interprets the function as:
337
338 1. Returning a Core Foundation Object
339 2. Treating the function as if it its name contained the keywords
340 "create" or "copy". This means the returned object as a
341 +1 retain count that must be released by the caller, either by sending a
342 ``release`` message (via toll-free bridging to an Objective-C object
343 pointer), or calling ``CFRelease`` or a similar function.
344
345 **Example**
346
347 .. code-block:objc
348
349 #import <Cocoa/Cocoa.h>
350
351 #ifndef __has_feature // Optional.
352 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
353 #endif
354
355 #ifndef CF_RETURNS_RETAINED
356 #if __has_feature(attribute_cf_returns_retained)
357 #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
358 #else
359 #define CF_RETURNS_RETAINED
360 #endif
361 #endif
362
363 @interface MyClass : NSObject {}
364 - (NSDate*) returnsCFRetained CF_RETURNS_RETAINED;
365 - (NSDate*) alsoReturnsRetained;
366 - (NSDate*) returnsNSRetained NS_RETURNS_RETAINED;
367 @end
368
369 CF_RETURNS_RETAINED
370 CFDateRef returnsRetainedCFDate() {
371 return CFDateCreate(0, CFAbsoluteTimeGetCurrent());
372 }
373
374 @implementation MyClass
375 - (NSDate*) returnsCFRetained {
376 return (NSDate*) returnsRetainedCFDate(); // No leak.
377 }
378
379 - (NSDate*) alsoReturnsRetained {
380 return (NSDate*) returnsRetainedCFDate(); // Always report a leak.
381 }
382
383 - (NSDate*) returnsNSRetained {
384 return (NSDate*) returnsRetainedCFDate(); // Report a leak when using GC.
385 }
386 @end
387
388 Running ``scan-build`` on this example produces the following output:
389
390 .. image:: ../images/example_cf_returns_retained.png
391
392 Attribute 'cf_returns_not_retained' (Clang-specific)
393 ----------------------------------------------------
394
395 The 'cf_returns_not_retained' attribute is the complement of
396 '`cf_returns_retained`_'. Where a function or method may appear to obey the
397 Core Foundation or Cocoa conventions and return a retained Core Foundation
398 object, this attribute can be used to indicate that the object reference
399 returned should not be considered as an "owning" reference being
400 returned to the caller. The CoreFoundation framework defines a macro
401 **``CF_RETURNS_NOT_RETAINED``** that is functionally equivalent to the one
402 shown below.
403
404 Usage is identical to cf_returns_retained_. When using the attribute, be sure
405 to declare it within the proper macro that checks for its availability, as it
406 is not available in earlier versions of the analyzer:
407
408 .. code-block:objc
409
410 #ifndef __has_feature // Optional.
411 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
412 #endif
413
414 #ifndef CF_RETURNS_NOT_RETAINED
415 #if __has_feature(attribute_cf_returns_not_retained)
416 #define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained))
417 #else
418 #define CF_RETURNS_NOT_RETAINED
419 #endif
420 #endif
421
422 .. _ns_consumed:
423
424 Attribute 'ns_consumed' (Clang-specific)
425 ----------------------------------------
426
427 The 'ns_consumed' attribute can be placed on a specific parameter in either
428 the declaration of a function or an Objective-C method. It indicates to the
429 static analyzer that a ``release`` message is implicitly sent to the
430 parameter upon completion of the call to the given function or method. The
431 Foundation framework defines a macro ``NS_RELEASES_ARGUMENT`` that
432 is functionally equivalent to the ``NS_CONSUMED`` macro shown below.
433
434 **Example**
435
436 .. code-block:objc
437
438 #ifndef __has_feature // Optional.
439 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
440 #endif
441
442 #ifndef NS_CONSUMED
443 #if __has_feature(attribute_ns_consumed)
444 #define NS_CONSUMED __attribute__((ns_consumed))
445 #else
446 #define NS_CONSUMED
447 #endif
448 #endif
449
450 void consume_ns(id NS_CONSUMED x);
451
452 void test() {
453 id x = [[NSObject alloc] init];
454 consume_ns(x); // No leak!
455 }
456
457 @interface Foo : NSObject
458 + (void) releaseArg:(id) NS_CONSUMED x;
459 + (void) releaseSecondArg:(id)x second:(id) NS_CONSUMED y;
460 @end
461
462 void test_method() {
463 id x = [[NSObject alloc] init];
464 [Foo releaseArg:x]; // No leak!
465 }
466
467 void test_method2() {
468 id a = [[NSObject alloc] init];
469 id b = [[NSObject alloc] init];
470 [Foo releaseSecondArg:a second:b]; // 'a' is leaked, but 'b' is released.
471 }
472
473 Attribute 'cf_consumed' (Clang-specific)
474 ----------------------------------------
475
476 The 'cf_consumed' attribute is practically identical to ns_consumed_. The
477 attribute can be placed on a specific parameter in either the declaration of a
478 function or an Objective-C method. It indicates to the static analyzer that the
479 object reference is implicitly passed to a call to ``CFRelease`` upon
480 completion of the call to the given function or method. The CoreFoundation
481 framework defines a macro ``CF_RELEASES_ARGUMENT`` that is functionally
482 equivalent to the ``CF_CONSUMED`` macro shown below.
483
484 Operationally this attribute is nearly identical to 'ns_consumed'.
485
486 **Example**
487
488 .. code-block:objc
489
490 #ifndef __has_feature // Optional.
491 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
492 #endif
493
494 #ifndef CF_CONSUMED
495 #if __has_feature(attribute_cf_consumed)
496 #define CF_CONSUMED __attribute__((cf_consumed))
497 #else
498 #define CF_CONSUMED
499 #endif
500 #endif
501
502 void consume_cf(id CF_CONSUMED x);
503 void consume_CFDate(CFDateRef CF_CONSUMED x);
504
505 void test() {
506 id x = [[NSObject alloc] init];
507 consume_cf(x); // No leak!
508 }
509
510 void test2() {
511 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
512 consume_CFDate(date); // No leak, including under GC!
513
514 }
515
516 @interface Foo : NSObject
517 + (void) releaseArg:(CFDateRef) CF_CONSUMED x;
518 @end
519
520 void test_method() {
521 CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent());
522 [Foo releaseArg:date]; // No leak!
523 }
524
525 .. _ns_consumes_self:
526
527 Attribute 'ns_consumes_self' (Clang-specific)
528 ---------------------------------------------
529
530 The 'ns_consumes_self' attribute can be placed only on an Objective-C method
531 declaration. It indicates that the receiver of the message is
532 "consumed" (a single reference count decremented) after the message
533 is sent. This matches the semantics of all "init" methods.
534
535 One use of this attribute is declare your own init-like methods that do not
536 follow the standard Cocoa naming conventions.
537
538 **Example**
539
540 .. code-block:objc
541 #ifndef __has_feature
542 #define __has_feature(x) 0 // Compatibility with non-clang compilers.
543 #endif
544
545 #ifndef NS_CONSUMES_SELF
546 #if __has_feature((attribute_ns_consumes_self))
547 #define NS_CONSUMES_SELF __attribute__((ns_consumes_self))
548 #else
549 #define NS_CONSUMES_SELF
550 #endif
551 #endif
552
553 @interface MyClass : NSObject
554 - initWith:(MyClass *)x;
555 - nonstandardInitWith:(MyClass *)x NS_CONSUMES_SELF NS_RETURNS_RETAINED;
556 @end
557
558 In this example, ``-nonstandardInitWith:`` has the same ownership
559 semantics as the init method ``-initWith:``. The static analyzer will
560 observe that the method consumes the receiver, and then returns an object with
561 a +1 retain count.
562
563 The Foundation framework defines a macro ``NS_REPLACES_RECEIVER`` which is
564 functionally equivalent to the combination of ``NS_CONSUMES_SELF`` and
565 ``NS_RETURNS_RETAINED`` shown above.
566
567 Libkern Memory Management Annotations
568 #####################################
569
570 `Libkern <https://developer.apple.com/documentation/kernel/osobject?language=objc>`_
571 requires developers to inherit all heap allocated objects from ``OSObject`` and
572 to perform manual reference counting. The reference counting model is very
573 similar to MRR (manual retain-release) mode in
574 `Objective-C <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html>`_
575 or to CoreFoundation reference counting.
576 Freshly-allocated objects start with a reference count of 1, and calls to
577 ``retain`` increment it, while calls to ``release`` decrement it. The object is
578 deallocated whenever its reference count reaches zero.
579
580 Manually incrementing and decrementing reference counts is error-prone:
581 over-retains lead to leaks, and over-releases lead to uses-after-free.
582 The analyzer can help the programmer to check for unbalanced
583 retain/release calls.
584
585 The reference count checking is based on the principle of *locality*: it should
586 be possible to establish correctness (lack of leaks/uses after free) by looking
587 at each function body, and the declarations (not the definitions) of all the
588 functions it interacts with.
589
590 In order to support such reasoning, it should be possible to *summarize* the
591 behavior of each function, with respect to reference count of its returned
592 values and attributes.
593
594 By default, the following summaries are assumed:
595
596 - All functions starting with ``get`` or ``Get``, unless they are returning
597 subclasses of ``OSIterator``, are assumed to be returning at +0. That is, the
598 caller has no reference count *obligations* with respect to the reference
599 count of the returned object and should leave it untouched.
600
601 - All other functions are assumed to return at +1. That is, the caller has an
602 *obligation* to release such objects.
603
604 - Functions are assumed not to change the reference count of their parameters,
605 including the implicit ``this`` parameter.
606
607 These summaries can be overriden with the following
608 `attributes <https://clang.llvm.org/docs/AttributeReference.html#os-returns-not-retained>`_:
609
610 Attribute 'os_returns_retained'
611 -------------------------------
612
613 The ``os_returns_retained`` attribute (accessed through the macro
614 ``LIBKERN_RETURNS_RETAINED``) plays a role identical to `ns_returns_retained`_
615 for functions returning ``OSObject`` subclasses. The attribute indicates that
616 it is a callers responsibility to release the returned object.
617
618 Attribute 'os_returns_not_retained'
619 -----------------------------------
620
621 The ``os_returns_not_retained`` attribute (accessed through the macro
622 ``LIBKERN_RETURNS_NOT_RETAINED``) plays a role identical to
623 `ns_returns_not_retained`_ for functions returning ``OSObject`` subclasses. The
624 attribute indicates that the caller should not change the retain count of the
625 returned object.
626
627
628 **Example**
629
630 .. code-block:objc
631
632 class MyClass {
633 OSObject *f;
634 LIBKERN_RETURNS_NOT_RETAINED OSObject *myFieldGetter();
635 }
636
637 // Note that the annotation only has to be applied to the function declaration.
638 OSObject * MyClass::myFieldGetter() {
639 return f;
640 }
641
642 Attribute 'os_consumed'
643 -----------------------
644
645 Similarly to `ns_consumed`_ attribute, ``os_consumed`` (accessed through
646 ``LIBKERN_CONSUMED``) attribute, applied to a parameter, indicates that the
647 call to the function *consumes* the parameter: the callee should either release
648 it or store it and release it in the destructor, while the caller should assume
649 one is subtracted from the reference count after the call.
650
651 .. code-block:objc
652 IOReturn addToList(LIBKERN_CONSUMED IOPMinformee *newInformee);
653
654 Attribute 'os_consumes_this'
655 ----------------------------
656
657 Similarly to `ns_consumes_self`_, the ``os_consumes_self`` attribute indicates
658 that the method call *consumes* the implicit ``this`` argument: the caller
659 should assume one was subtracted from the reference count of the object after
660 the call, and the callee has on obligation to either release the argument, or
661 store it and eventually release it in the destructor.
662
663
664 .. code-block:objc
665 void addThisToList(OSArray *givenList) LIBKERN_CONSUMES_THIS;
666
667 Out Parameters
668 --------------
669
670 A function can also return an object to a caller by a means of an out parameter
671 (a pointer-to-OSObject-pointer is passed, and a callee writes a pointer to an
672 object into an argument). Currently the analyzer does not track unannotated out
673 parameters by default, but with annotations we distinguish four separate cases:
674
675 **1. Non-retained out parameters**, identified using
676 ``LIBKERN_RETURNS_NOT_RETAINED`` applied to parameters, e.g.:
677
678 .. code-block:objc
679 void getterViaOutParam(LIBKERN_RETURNS_NOT_RETAINED OSObject **obj)
680
681 Such functions write a non-retained object into an out parameter, and the
682 caller has no further obligations.
683
684 **2. Retained out parameters**, identified using ``LIBKERN_RETURNS_RETAINED``:
685
686 .. code-block:objc
687 void getterViaOutParam(LIBKERN_RETURNS_NOT_RETAINED OSObject **obj)
688
689 In such cases a retained object is written into an out parameter, which the caller has then to release in order to avoid a leak.
690
691 These two cases are simple - but in practice a functions returning an
692 out-parameter usually also return a return code, and then an out parameter may
693 or may not be written, which conditionally depends on the exit code, e.g.:
694
695 .. code-block:objc
696 bool maybeCreateObject(LIBKERN_RETURNS_RETAINED OSObject **obj);
697
698 For such functions, the usual semantics is that an object is written into on "success", and not written into on "failure".
699
700 For ``LIBKERN_RETURNS_RETAINED`` we assume the following definition of
701 success:
702
703 - For functions returning ``OSReturn`` or ``IOReturn`` (any typedef to
704 ``kern_return_t``) success is defined as having an output of zero
705 (``kIOReturnSuccess`` is zero).
706
707 - For all others, success is non-zero (e.g. non-nullptr for pointers)
708
709 **3. Retained out parameters on zero return** The annotation
710 ``LIBKERN_RETURNS_RETAINED_ON_ZERO`` states that a retained object is written
711 into if and only if the function returns a zero value:
712
713 .. code-block:objc
714 bool OSUnserializeXML(void *data, LIBKERN_RETURNS_RETAINED_ON_ZERO OSString **errString);
715
716 Then the caller has to release an object if the function has returned zero.
717
718 **4. Retained out parameters on non-zero return** Similarly,
719 ``LIBKERN_RETURNS_RETAINED_ON_NONZERO`` specifies that a retained object is
720 written into the parameter if and only if the function has returned a non-zero
721 value.
722
723 Note that for non-retained out parameters conditionals do not matter, as the
724 caller has no obligations regardless of whether an object is written into or
725 not.