Skip to content

Commit 24b1511

Browse files
committed
[analyzer][docs] Document MallocChecker's ownership attributes
Exactly what it says on the tin! These were written ages ago (2010s), but are still functional, only the docs are missing.
1 parent 57c161a commit 24b1511

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2776,7 +2776,7 @@ def Ownership : InheritableAttr {
27762776
let Args = [IdentifierArgument<"Module">,
27772777
VariadicParamIdxArgument<"Args">];
27782778
let Subjects = SubjectList<[HasFunctionProto]>;
2779-
let Documentation = [Undocumented];
2779+
let Documentation = [OwnershipDocs];
27802780
}
27812781

27822782
def Packed : InheritableAttr {

clang/include/clang/Basic/AttrDocs.td

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,68 @@ Query for this attribute with ``__has_attribute(overloadable)``.
13891389
}];
13901390
}
13911391

1392+
def OwnershipDocs : Documentation {
1393+
let Heading = "ownership_holds, ownership_returns, ownership_takes (Clang "
1394+
"Static Analyzer)";
1395+
let Category = DocCatFunction;
1396+
let Content = [{
1397+
1398+
.. note::
1399+
1400+
In order for the Clang Static Analyzer to acknowledge these attributes, the
1401+
``Optimistic`` config needs to be set to true for the checker
1402+
``unix.DynamicMemoryModeling``:
1403+
1404+
``-Xclang -analyzer-config -Xclang unix.DynamicMemoryModeling:Optimistic=true``
1405+
1406+
These attributes are used by the Clang Static Analyzer's dynamic memory modeling
1407+
facilities to mark custom allocating/deallocating functions.
1408+
1409+
All 3 attributes' first parameter of type string is the type of the allocation:
1410+
``malloc``, ``new``, etc. to allow for catching mismatched deallocation bugs to
1411+
be found.
1412+
1413+
* Use ``ownership_returns`` to mark a function as an allocating function. Takes
1414+
1 parameter to denote the allocation type.
1415+
* Use ``ownership_takes`` to mark a function as a deallocating function. Takes 2
1416+
parameters: the allocation type, and the index of the parameter that is being
1417+
deallocated (counting from 1).
1418+
* Use ``ownership_holds`` to mark that a function takes over the ownership of a
1419+
piece of memory. The analyzer will assume that the memory is not leaked even
1420+
if it finds that the last pointer referencing it went out of scope (almost as
1421+
if it was freed). Takes 2 parameters: the allocation type, and the index of
1422+
the parameter whose ownership will be taken over (counting from 1).
1423+
1424+
Example:
1425+
1426+
.. code-block:: c
1427+
1428+
// Denotes that my_malloc will return with adynamically allocated piece of
1429+
// memory using malloc().
1430+
void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
1431+
1432+
// Denotes that my_free will deallocate its parameter using free().
1433+
void __attribute((ownership_takes(malloc, 1))) my_free(void *);
1434+
1435+
// Denotes that my_hold will take over the ownership of its parameter that was
1436+
// allocated via malloc().
1437+
void __attribute((ownership_holds(malloc, 1))) my_hold(void *);
1438+
1439+
Further reading about dynamic memory modeling in the Clang Static Analyzer is
1440+
found in these checker docs:
1441+
:ref:`unix.Malloc <unix-Malloc>`, :ref:`unix.MallocSizeof <unix-MallocSizeof>`,
1442+
:ref:`unix.MismatchedDeallocator <unix-MismatchedDeallocator>`,
1443+
:ref:`cplusplus.NewDelete <cplusplus-NewDelete>`,
1444+
:ref:`cplusplus.NewDeleteLeaks <cplusplus-NewDeleteLeaks>`,
1445+
:ref:`optin.taint.TaintedAlloc <optin-taint-TaintedAlloc>`.
1446+
Mind that many more checkers are affected by dynamic memory modeling changes to
1447+
some extent.
1448+
1449+
Further reading for other annotations:
1450+
`Source Annotations in the Clang Static Analyzer <https://clang-analyzer.llvm.org/annotations.html>`_.
1451+
}];
1452+
}
1453+
13921454
def ObjCMethodFamilyDocs : Documentation {
13931455
let Category = DocCatFunction;
13941456
let Content = [{

0 commit comments

Comments
 (0)