Skip to content

Commit 8dc1395

Browse files
[clang][docs] document __attribute__((cleanup())) GNU C extension
Provide an example of how to use this extension and more importantly, document that cleanup functions are run in reverse nested order. Reviewed By: erichkeane Differential Revision: https://reviews.llvm.org/D151732
1 parent 5022fc2 commit 8dc1395

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ def Cleanup : InheritableAttr {
10971097
let Spellings = [GCC<"cleanup">];
10981098
let Args = [DeclArgument<Function, "FunctionDecl">];
10991099
let Subjects = SubjectList<[LocalVar]>;
1100-
let Documentation = [Undocumented];
1100+
let Documentation = [CleanupDocs];
11011101
}
11021102

11031103
def CmseNSEntry : InheritableAttr, TargetSpecificAttr<TargetARM> {

clang/include/clang/Basic/AttrDocs.td

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7023,3 +7023,30 @@ This attribute may be attached to a function pointer type, where it modifies
70237023
its underlying representation to be a WebAssembly ``funcref``.
70247024
}];
70257025
}
7026+
7027+
def CleanupDocs : Documentation {
7028+
let Category = DocCatType;
7029+
let Content = [{
7030+
This attribute allows a function to be run when a local variable goes out of
7031+
scope. The attribute takes the identifier of a function with a parameter type
7032+
that is a pointer to the type with the attribute.
7033+
7034+
.. code-block:: c
7035+
7036+
static void foo (int *) { ... }
7037+
static void bar (int *) { ... }
7038+
void baz (void) {
7039+
int x __attribute__((cleanup(foo)));
7040+
{
7041+
int y __attribute__((cleanup(bar)));
7042+
}
7043+
}
7044+
7045+
The above example will result in a call to ``bar`` being passed the address of
7046+
`y`` when ``y`` goes out of scope, then a call to ``foo`` being passed the
7047+
address of ``x`` when ``x`` goes out of scope. If two or more variables share
7048+
the same scope, their ``cleanup`` callbacks are invoked in the reverse order
7049+
the variables were declared in. It is not possible to check the return value
7050+
(if any) of these ``cleanup`` callback functions.
7051+
}];
7052+
}

0 commit comments

Comments
 (0)