Attributes in Clang¶
Introduction¶
This page lists the attributes currently supported by Clang.
AArch64 SME Attributes¶
Clang supports a number of AArch64-specific attributes to manage state
added by the Scalable Matrix Extension (SME). This state includes the
runtime mode that the processor is in (e.g. non-streaming or streaming)
as well as the state of the ZA
Matrix Storage.
The attributes come in the form of type- and declaration attributes:
The SME declaration attributes can appear anywhere that a standard
[[...]]
declaration attribute can appear.The SME type attributes apply only to prototyped functions and can appear anywhere that a standard
[[...]]
type attribute can appear. The SME type attributes do not apply to functions having a K&R-style unprototyped function type.
See Arm C Language Extensions for more details about the features related to the SME extension.
See Procedure Call Standard for the Arm® 64-bit Architecture (AArch64) for more details about streaming-interface functions and shared/private-ZA interface functions.
__arm_in¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __arm_in
keyword applies to prototyped function types and specifies
that the function shares a given state S with its caller. For __arm_in
, the
function takes the state S as input and returns with the state S unchanged.
The attribute takes string arguments to instruct the compiler which state is shared. The supported states for S are:
"za"
for Matrix Storage (requires SME)
The attributes __arm_in(S)
, __arm_out(S)
, __arm_inout(S)
and
__arm_preserves(S)
are all mutually exclusive for the same state S.
__arm_inout¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __arm_inout
keyword applies to prototyped function types and specifies
that the function shares a given state S with its caller. For __arm_inout
,
the function takes the state S as input and returns new state for S.
The attribute takes string arguments to instruct the compiler which state is shared. The supported states for S are:
"za"
for Matrix Storage (requires SME)
The attributes __arm_in(S)
, __arm_out(S)
, __arm_inout(S)
and
__arm_preserves(S)
are all mutually exclusive for the same state S.
__arm_locally_streaming¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __arm_locally_streaming
keyword applies to function declarations
and specifies that all the statements in the function are executed in
streaming mode. This means that:
the function requires that the target processor implements the Scalable Matrix Extension (SME).
the program automatically puts the machine into streaming mode before executing the statements and automatically restores the previous mode afterwards.
Clang manages PSTATE.SM automatically; it is not the source code’s responsibility to do this. For example, Clang will emit code to enable streaming mode at the start of the function, and disable streaming mode at the end of the function.
__arm_new¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __arm_new
keyword applies to function declarations and specifies
that the function will create a new scope for state S.
The attribute takes string arguments to instruct the compiler for which state to create new scope. The supported states for S are:
"za"
for Matrix Storage (requires SME)
For state "za"
, this means that:
the function requires that the target processor implements the Scalable Matrix Extension (SME).
the function will commit any lazily saved ZA data.
the function will create a new ZA context and enable PSTATE.ZA.
the function will disable PSTATE.ZA (by setting it to 0) before returning.
For __arm_new("za")
functions Clang will set up the ZA context automatically
on entry to the function and disable it before returning. For example, if ZA is
in a dormant state Clang will generate the code to commit a lazy-save and set up
a new ZA state before executing user code.
__arm_out¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __arm_out
keyword applies to prototyped function types and specifies
that the function shares a given state S with its caller. For __arm_out
,
the function ignores the incoming state for S and returns new state for S.
The attribute takes string arguments to instruct the compiler which state is shared. The supported states for S are:
"za"
for Matrix Storage (requires SME)
The attributes __arm_in(S)
, __arm_out(S)
, __arm_inout(S)
and
__arm_preserves(S)
are all mutually exclusive for the same state S.
__arm_preserves¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __arm_preserves
keyword applies to prototyped function types and
specifies that the function does not read a given state S and returns
with state S unchanged.
The attribute takes string arguments to instruct the compiler which state is shared. The supported states for S are:
"za"
for Matrix Storage (requires SME)
The attributes __arm_in(S)
, __arm_out(S)
, __arm_inout(S)
and
__arm_preserves(S)
are all mutually exclusive for the same state S.
__arm_streaming¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __arm_streaming
keyword applies to prototyped function types and specifies
that the function has a “streaming interface”. This means that:
the function requires that the processor implements the Scalable Matrix Extension (SME).
the function must be entered in streaming mode (that is, with PSTATE.SM set to 1)
the function must return in streaming mode
Clang manages PSTATE.SM automatically; it is not the source code’s
responsibility to do this. For example, if a non-streaming
function calls an __arm_streaming
function, Clang generates code
that switches into streaming mode before calling the function and
switches back to non-streaming mode on return.
__arm_streaming_compatible¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __arm_streaming_compatible
keyword applies to prototyped function types and
specifies that the function has a “streaming compatible interface”. This
means that:
the function may be entered in either non-streaming mode (PSTATE.SM=0) or in streaming mode (PSTATE.SM=1).
the function must return in the same mode as it was entered.
the code executed in the function is compatible with either mode.
Clang manages PSTATE.SM automatically; it is not the source code’s
responsibility to do this. Clang will ensure that the generated code in
streaming-compatible functions is valid in either mode (PSTATE.SM=0 or
PSTATE.SM=1). For example, if an __arm_streaming_compatible
function calls a
non-streaming function, Clang generates code to temporarily switch out of streaming
mode before calling the function and switch back to streaming-mode on return if
PSTATE.SM
is 1
on entry of the caller. If PSTATE.SM
is 0
on
entry to the __arm_streaming_compatible
function, the call will be executed
without changing modes.
AMD GPU Attributes¶
amdgpu_flat_work_group_size¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
The flat work-group size is the number of work-items in the work-group size specified when the kernel is dispatched. It is the product of the sizes of the x, y, and z dimension of the work-group.
Clang supports the
__attribute__((amdgpu_flat_work_group_size(<min>, <max>)))
attribute for the
AMDGPU target. This attribute may be attached to a kernel function definition
and is an optimization hint.
<min>
parameter specifies the minimum flat work-group size, and <max>
parameter specifies the maximum flat work-group size (must be greater than
<min>
) to which all dispatches of the kernel will conform. Passing 0, 0
as <min>, <max>
implies the default behavior (128, 256
).
If specified, the AMDGPU target backend might be able to produce better machine code for barriers and perform scratch promotion by estimating available group segment size.
- An error will be given if:
Specified values violate subtarget specifications;
Specified values are not compatible with values provided through other attributes.
amdgpu_max_num_work_groups¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
This attribute specifies the max number of work groups when the kernel is dispatched.
Clang supports the
__attribute__((amdgpu_max_num_work_groups(<x>, <y>, <z>)))
or
[[clang::amdgpu_max_num_work_groups(<x>, <y>, <z>)]]
attribute for the
AMDGPU target. This attribute may be attached to HIP or OpenCL kernel function
definitions and is an optimization hint.
The <x>
parameter specifies the maximum number of work groups in the x dimension.
Similarly <y>
and <z>
are for the y and z dimensions respectively.
Each of the three values must be greater than 0 when provided. The <x>
parameter
is required, while <y>
and <z>
are optional with default value of 1.
If specified, the AMDGPU target backend might be able to produce better machine code.
- An error will be given if:
Specified values violate subtarget specifications;
Specified values are not compatible with values provided through other attributes.
amdgpu_num_sgpr¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
Clang supports the __attribute__((amdgpu_num_sgpr(<num_sgpr>)))
and
__attribute__((amdgpu_num_vgpr(<num_vgpr>)))
attributes for the AMDGPU
target. These attributes may be attached to a kernel function definition and are
an optimization hint.
If these attributes are specified, then the AMDGPU target backend will attempt
to limit the number of SGPRs and/or VGPRs used to the specified value(s). The
number of used SGPRs and/or VGPRs may further be rounded up to satisfy the
allocation requirements or constraints of the subtarget. Passing 0
as
num_sgpr
and/or num_vgpr
implies the default behavior (no limits).
These attributes can be used to test the AMDGPU target backend. It is
recommended that the amdgpu_waves_per_eu
attribute be used to control
resources such as SGPRs and VGPRs since it is aware of the limits for different
subtargets.
- An error will be given if:
Specified values violate subtarget specifications;
Specified values are not compatible with values provided through other attributes;
The AMDGPU target backend is unable to create machine code that can meet the request.
amdgpu_num_vgpr¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
Clang supports the __attribute__((amdgpu_num_sgpr(<num_sgpr>)))
and
__attribute__((amdgpu_num_vgpr(<num_vgpr>)))
attributes for the AMDGPU
target. These attributes may be attached to a kernel function definition and are
an optimization hint.
If these attributes are specified, then the AMDGPU target backend will attempt
to limit the number of SGPRs and/or VGPRs used to the specified value(s). The
number of used SGPRs and/or VGPRs may further be rounded up to satisfy the
allocation requirements or constraints of the subtarget. Passing 0
as
num_sgpr
and/or num_vgpr
implies the default behavior (no limits).
These attributes can be used to test the AMDGPU target backend. It is
recommended that the amdgpu_waves_per_eu
attribute be used to control
resources such as SGPRs and VGPRs since it is aware of the limits for different
subtargets.
- An error will be given if:
Specified values violate subtarget specifications;
Specified values are not compatible with values provided through other attributes;
The AMDGPU target backend is unable to create machine code that can meet the request.
amdgpu_waves_per_eu¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
A compute unit (CU) is responsible for executing the wavefronts of a work-group. It is composed of one or more execution units (EU), which are responsible for executing the wavefronts. An EU can have enough resources to maintain the state of more than one executing wavefront. This allows an EU to hide latency by switching between wavefronts in a similar way to symmetric multithreading on a CPU. In order to allow the state for multiple wavefronts to fit on an EU, the resources used by a single wavefront have to be limited. For example, the number of SGPRs and VGPRs. Limiting such resources can allow greater latency hiding, but can result in having to spill some register state to memory.
Clang supports the __attribute__((amdgpu_waves_per_eu(<min>[, <max>])))
attribute for the AMDGPU target. This attribute may be attached to a kernel
function definition and is an optimization hint.
<min>
parameter specifies the requested minimum number of waves per EU, and
optional <max>
parameter specifies the requested maximum number of waves
per EU (must be greater than <min>
if specified). If <max>
is omitted,
then there is no restriction on the maximum number of waves per EU other than
the one dictated by the hardware for which the kernel is compiled. Passing
0, 0
as <min>, <max>
implies the default behavior (no limits).
If specified, this attribute allows an advanced developer to tune the number of
wavefronts that are capable of fitting within the resources of an EU. The AMDGPU
target backend can use this information to limit resources, such as number of
SGPRs, number of VGPRs, size of available group and private memory segments, in
such a way that guarantees that at least <min>
wavefronts and at most
<max>
wavefronts are able to fit within the resources of an EU. Requesting
more wavefronts can hide memory latency but limits available registers which
can result in spilling. Requesting fewer wavefronts can help reduce cache
thrashing, but can reduce memory latency hiding.
This attribute controls the machine code generated by the AMDGPU target backend to ensure it is capable of meeting the requested values. However, when the kernel is executed, there may be other reasons that prevent meeting the request, for example, there may be wavefronts from other kernels executing on the EU.
- An error will be given if:
Specified values violate subtarget specifications;
Specified values are not compatible with values provided through other attributes;
The AMDGPU target backend will emit a warning whenever it is unable to create machine code that meets the request.
Calling Conventions¶
Clang supports several different calling conventions, depending on the target platform and architecture. The calling convention used for a function determines how parameters are passed, how results are returned to the caller, and other low-level details of calling a function.
aarch64_sve_pcs¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
On AArch64 targets, this attribute changes the calling convention of a function to preserve additional Scalable Vector registers and Scalable Predicate registers relative to the default calling convention used for AArch64.
This means it is more efficient to call such functions from code that performs extensive scalable vector and scalable predicate calculations, because fewer live SVE registers need to be saved. This property makes it well-suited for SVE math library functions, which are typically leaf functions that require a small number of registers.
However, using this attribute also means that it is more expensive to call a function that adheres to the default calling convention from within such a function. Therefore, it is recommended that this attribute is only used for leaf functions.
For more information, see the documentation for aarch64_sve_pcs in the ARM C Language Extension (ACLE) documentation.
aarch64_vector_pcs¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
On AArch64 targets, this attribute changes the calling convention of a function to preserve additional floating-point and Advanced SIMD registers relative to the default calling convention used for AArch64.
This means it is more efficient to call such functions from code that performs extensive floating-point and vector calculations, because fewer live SIMD and FP registers need to be saved. This property makes it well-suited for e.g. floating-point or vector math library functions, which are typically leaf functions that require a small number of registers.
However, using this attribute also means that it is more expensive to call a function that adheres to the default calling convention from within such a function. Therefore, it is recommended that this attribute is only used for leaf functions.
For more information, see the documentation for aarch64_vector_pcs on the Arm Developer website.
fastcall¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
On 32-bit x86 targets, this attribute changes the calling convention of a
function to use ECX and EDX as register parameters and clear parameters off of
the stack on return. This convention does not support variadic calls or
unprototyped functions in C, and has no effect on x86_64 targets. This calling
convention is supported primarily for compatibility with existing code. Users
seeking register parameters should use the regparm
attribute, which does
not require callee-cleanup. See the documentation for __fastcall on MSDN.
m68k_rtd¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
On M68k targets, this attribute changes the calling convention of a function to clear parameters off the stack on return. In other words, callee is responsible for cleaning out the stack space allocated for incoming paramters. This convention does not support variadic calls or unprototyped functions in C. When targeting M68010 or newer CPUs, this calling convention is implemented using the rtd instruction.
ms_abi¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
On non-Windows x86_64 targets, this attribute changes the calling convention of a function to match the default convention used on Windows x86_64. This attribute has no effect on Windows targets or non-x86_64 targets.
pcs¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
On ARM targets, this attribute can be used to select calling conventions
similar to stdcall
on x86. Valid parameter values are “aapcs” and
“aapcs-vfp”.
preserve_all¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
On X86-64 and AArch64 targets, this attribute changes the calling convention of
a function. The preserve_all
calling convention attempts to make the code
in the caller even less intrusive than the preserve_most
calling convention.
This calling convention also behaves identical to the C
calling convention
on how arguments and return values are passed, but it uses a different set of
caller/callee-saved registers. This removes the burden of saving and
recovering a large register set before and after the call in the caller. If
the arguments are passed in callee-saved registers, then they will be
preserved by the callee across the call. This doesn’t apply for values
returned in callee-saved registers.
On X86-64 the callee preserves all general purpose registers, except for R11. R11 can be used as a scratch register. Furthermore it also preserves all floating-point registers (XMMs/YMMs).
On AArch64 the callee preserve all general purpose registers, except X0-X8 and X16-X18. Furthermore it also preserves lower 128 bits of V8-V31 SIMD - floating point registers.
The idea behind this convention is to support calls to runtime functions that don’t need to call out to any other functions.
This calling convention, like the preserve_most
calling convention, will be
used by a future version of the Objective-C runtime and should be considered
experimental at this time.
preserve_most¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
On X86-64 and AArch64 targets, this attribute changes the calling convention of
a function. The preserve_most
calling convention attempts to make the code
in the caller as unintrusive as possible. This convention behaves identically
to the C
calling convention on how arguments and return values are passed,
but it uses a different set of caller/callee-saved registers. This alleviates
the burden of saving and recovering a large register set before and after the
call in the caller. If the arguments are passed in callee-saved registers,
then they will be preserved by the callee across the call. This doesn’t
apply for values returned in callee-saved registers.
On X86-64 the callee preserves all general purpose registers, except for R11. R11 can be used as a scratch register. Floating-point registers (XMMs/YMMs) are not preserved and need to be saved by the caller.
On AArch64 the callee preserve all general purpose registers, except X0-X8 and X16-X18.
The idea behind this convention is to support calls to runtime functions
that have a hot path and a cold path. The hot path is usually a small piece
of code that doesn’t use many registers. The cold path might need to call out to
another function and therefore only needs to preserve the caller-saved
registers, which haven’t already been saved by the caller. The
preserve_most
calling convention is very similar to the cold
calling
convention in terms of caller/callee-saved registers, but they are used for
different types of function calls. coldcc
is for function calls that are
rarely executed, whereas preserve_most
function calls are intended to be
on the hot path and definitely executed a lot. Furthermore preserve_most
doesn’t prevent the inliner from inlining the function call.
This calling convention will be used by a future version of the Objective-C runtime and should therefore still be considered experimental at this time. Although this convention was created to optimize certain runtime calls to the Objective-C runtime, it is not limited to this runtime and might be used by other runtimes in the future too. The current implementation only supports X86-64 and AArch64, but the intention is to support more architectures in the future.
preserve_none¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
On X86-64 and AArch64 targets, this attribute changes the calling convention of a function.
The preserve_none
calling convention tries to preserve as few general
registers as possible. So all general registers are caller saved registers. It
also uses more general registers to pass arguments. This attribute doesn’t
impact floating-point registers. preserve_none
’s ABI is still unstable, and
may be changed in the future.
On X86-64, only RSP and RBP are preserved by the callee. Registers R12, R13, R14, R15, RDI, RSI, RDX, RCX, R8, R9, R11, and RAX now can be used to pass function arguments. Floating-point registers (XMMs/YMMs) still follow the C calling convention.
On AArch64, only LR and FP are preserved by the callee. Registers X20-X28, X0-X7, and X9-X14 are used to pass function arguments. X8, X16-X19, SIMD and floating-point registers follow the AAPCS calling convention. X15 is not available for argument passing on Windows, but is used to pass arguments on other platforms.
regcall¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
On x86 targets, this attribute changes the calling convention to __regcall convention. This convention aims to pass as many arguments as possible in registers. It also tries to utilize registers for the return value whenever it is possible.
regparm¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
On 32-bit x86 targets, the regparm attribute causes the compiler to pass the first three integer parameters in EAX, EDX, and ECX instead of on the stack. This attribute has no effect on variadic functions, and all parameters are passed via the stack as normal.
riscv::vector_cc, riscv_vector_cc, clang::riscv_vector_cc¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The riscv_vector_cc
attribute can be applied to a function. It preserves 15
registers namely, v1-v7 and v24-v31 as callee-saved. Callers thus don’t need
to save these registers before function calls, and callees only need to save
them if they use them.
stdcall¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
On 32-bit x86 targets, this attribute changes the calling convention of a function to clear parameters off of the stack on return. This convention does not support variadic calls or unprototyped functions in C, and has no effect on x86_64 targets. This calling convention is used widely by the Windows API and COM applications. See the documentation for __stdcall on MSDN.
sysv_abi¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
On Windows x86_64 targets, this attribute changes the calling convention of a function to match the default convention used on Sys V targets such as Linux, Mac, and BSD. This attribute has no effect on other targets.
thiscall¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
On 32-bit x86 targets, this attribute changes the calling convention of a
function to use ECX for the first parameter (typically the implicit this
parameter of C++ methods) and clear parameters off of the stack on return. This
convention does not support variadic calls or unprototyped functions in C, and
has no effect on x86_64 targets. See the documentation for __thiscall on
MSDN.
vectorcall¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
On 32-bit x86 and x86_64 targets, this attribute changes the calling convention of a function to pass vector parameters in SSE registers.
On 32-bit x86 targets, this calling convention is similar to __fastcall
.
The first two integer parameters are passed in ECX and EDX. Subsequent integer
parameters are passed in memory, and callee clears the stack. On x86_64
targets, the callee does not clear the stack, and integer parameters are
passed in RCX, RDX, R8, and R9 as is done for the default Windows x64 calling
convention.
On both 32-bit x86 and x86_64 targets, vector and floating point arguments are passed in XMM0-XMM5. Homogeneous vector aggregates of up to four elements are passed in sequential SSE registers if enough are available. If AVX is enabled, 256 bit vectors are passed in YMM0-YMM5. Any vector or aggregate type that cannot be passed in registers for any reason is passed by reference, which allows the caller to align the parameter memory.
See the documentation for __vectorcall on MSDN for more details.
Consumed Annotation Checking¶
Clang supports additional attributes for checking basic resource management properties, specifically for unique objects that have a single owning reference. The following attributes are currently supported, although the implementation for these annotations is currently in development and are subject to change.
callable_when¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
Use __attribute__((callable_when(...)))
to indicate what states a method
may be called in. Valid states are unconsumed, consumed, or unknown. Each
argument to this attribute must be a quoted string. E.g.:
__attribute__((callable_when("unconsumed", "unknown")))
consumable¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
Each class
that uses any of the typestate annotations must first be marked
using the consumable
attribute. Failure to do so will result in a warning.
This attribute accepts a single parameter that must be one of the following:
unknown
, consumed
, or unconsumed
.
param_typestate¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
This attribute specifies expectations about function parameters. Calls to an function with annotated parameters will issue a warning if the corresponding argument isn’t in the expected state. The attribute is also used to set the initial state of the parameter when analyzing the function’s body.
return_typestate¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
The return_typestate
attribute can be applied to functions or parameters.
When applied to a function the attribute specifies the state of the returned
value. The function’s body is checked to ensure that it always returns a value
in the specified state. On the caller side, values returned by the annotated
function are initialized to the given state.
When applied to a function parameter it modifies the state of an argument after a call to the function returns. The function’s body is checked to ensure that the parameter is in the expected state before returning.
set_typestate¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
Annotate methods that transition an object into a new state with
__attribute__((set_typestate(new_state)))
. The new state must be
unconsumed, consumed, or unknown.
test_typestate¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
Use __attribute__((test_typestate(tested_state)))
to indicate that a method
returns true if the object is in the specified state..
Customizing Swift Import¶
Clang supports additional attributes for customizing how APIs are imported into Swift.
swift_async¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The swift_async
attribute specifies if and how a particular function or
Objective-C method is imported into a swift async method. For instance:
@interface MyClass : NSObject
-(void)notActuallyAsync:(int)p1 withCompletionHandler:(void (^)())handler
__attribute__((swift_async(none)));
-(void)actuallyAsync:(int)p1 callThisAsync:(void (^)())fun
__attribute__((swift_async(swift_private, 1)));
@end
Here, notActuallyAsync:withCompletionHandler
would have been imported as
async
(because it’s last parameter’s selector piece is
withCompletionHandler
) if not for the swift_async(none)
attribute.
Conversely, actuallyAsync:callThisAsync
wouldn’t have been imported as
async
if not for the swift_async
attribute because it doesn’t match the
naming convention.
When using swift_async
to enable importing, the first argument to the
attribute is either swift_private
or not_swift_private
to indicate
whether the function/method is private to the current framework, and the second
argument is the index of the completion handler parameter.
swift_async_error¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The swift_async_error
attribute specifies how an error state will be
represented in a swift async method. It’s a bit analogous to the swift_error
attribute for the generated async method. The swift_async_error
attribute
can indicate a variety of different ways of representing an error.
__attribute__((swift_async_error(zero_argument, N)))
, specifies that the async method is considered to have failed if the Nth argument to the completion handler is zero.__attribute__((swift_async_error(nonzero_argument, N)))
, specifies that the async method is considered to have failed if the Nth argument to the completion handler is non-zero.__attribute__((swift_async_error(nonnull_error)))
, specifies that the async method is considered to have failed if theNSError *
argument to the completion handler is non-null.__attribute__((swift_async_error(none)))
, specifies that the async method cannot fail.
For instance:
@interface MyClass : NSObject
-(void)asyncMethod:(void (^)(char, int, float))handler
__attribute__((swift_async(swift_private, 1)))
__attribute__((swift_async_error(zero_argument, 2)));
@end
Here, the swift_async
attribute specifies that handler
is the completion
handler for this method, and the swift_async_error
attribute specifies that
the int
parameter is the one that represents the error.
swift_async_name¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
The swift_async_name
attribute provides the name of the async
overload for
the given declaration in Swift. If this attribute is absent, the name is
transformed according to the algorithm built into the Swift compiler.
The argument is a string literal that contains the Swift name of the function or
method. The name may be a compound Swift name. The function or method with such
an attribute must have more than zero parameters, as its last parameter is
assumed to be a callback that’s eliminated in the Swift async
name.
@interface URL + (void) loadContentsFrom:(URL *)url callback:(void (^)(NSData *))data __attribute__((__swift_async_name__("URL.loadContentsFrom(_:)"))) @end
swift_attr¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
The swift_attr
provides a Swift-specific annotation for the declaration
or type to which the attribute appertains to. It can be used on any declaration
or type in Clang. This kind of annotation is ignored by Clang as it doesn’t have any
semantic meaning in languages supported by Clang. The Swift compiler can
interpret these annotations according to its own rules when importing C or
Objective-C declarations.
swift_bridge¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The swift_bridge
attribute indicates that the declaration to which the
attribute appertains is bridged to the named Swift type.
__attribute__((__objc_root__)) @interface Base - (instancetype)init; @end __attribute__((__swift_bridge__("BridgedI"))) @interface I : Base @end
In this example, the Objective-C interface I
will be made available to Swift
with the name BridgedI
. It would be possible for the compiler to refer to
I
still in order to bridge the type back to Objective-C.
swift_bridged¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
The swift_bridged_typedef
attribute indicates that when the typedef to which
the attribute appertains is imported into Swift, it should refer to the bridged
Swift type (e.g. Swift’s String
) rather than the Objective-C type as written
(e.g. NSString
).
@interface NSString; typedef NSString *AliasedString __attribute__((__swift_bridged_typedef__)); extern void acceptsAliasedString(AliasedString _Nonnull parameter);
In this case, the function acceptsAliasedString
will be imported into Swift
as a function which accepts a String
type parameter.
swift_error¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
The swift_error
attribute controls whether a particular function (or
Objective-C method) is imported into Swift as a throwing function, and if so,
which dynamic convention it uses.
All of these conventions except none
require the function to have an error
parameter. Currently, the error parameter is always the last parameter of type
NSError**
or CFErrorRef*
. Swift will remove the error parameter from
the imported API. When calling the API, Swift will always pass a valid address
initialized to a null pointer.
swift_error(none)
means that the function should not be imported as throwing. The error parameter and result type will be imported normally.swift_error(null_result)
means that calls to the function should be considered to have thrown if they return a null value. The return type must be a pointer type, and it will be imported into Swift with a non-optional type. This is the default error convention for Objective-C methods that return pointers.swift_error(zero_result)
means that calls to the function should be considered to have thrown if they return a zero result. The return type must be an integral type. If the return type would have been imported asBool
, it is instead imported asVoid
. This is the default error convention for Objective-C methods that return a type that would be imported asBool
.swift_error(nonzero_result)
means that calls to the function should be considered to have thrown if they return a non-zero result. The return type must be an integral type. If the return type would have been imported asBool
, it is instead imported asVoid
.swift_error(nonnull_error)
means that calls to the function should be considered to have thrown if they leave a non-null error in the error parameter. The return type is left unmodified.
swift_name¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The swift_name
attribute provides the name of the declaration in Swift. If
this attribute is absent, the name is transformed according to the algorithm
built into the Swift compiler.
The argument is a string literal that contains the Swift name of the function, variable, or type. When renaming a function, the name may be a compound Swift name. For a type, enum constant, property, or variable declaration, the name must be a simple or qualified identifier.
@interface URL - (void) initWithString:(NSString *)s __attribute__((__swift_name__("URL.init(_:)"))) @end void __attribute__((__swift_name__("squareRoot()"))) sqrt(double v) { }
swift_newtype¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
The swift_newtype
attribute indicates that the typedef to which the
attribute appertains is imported as a new Swift type of the typedef’s name.
Previously, the attribute was spelt swift_wrapper
. While the behaviour of
the attribute is identical with either spelling, swift_wrapper
is
deprecated, only exists for compatibility purposes, and should not be used in
new code.
swift_newtype(struct)
means that a Swift struct will be created for this typedef.swift_newtype(enum)
means that a Swift enum will be created for this typedef.// Import UIFontTextStyle as an enum type, with enumerated values being // constants. typedef NSString * UIFontTextStyle __attribute__((__swift_newtype__(enum))); // Import UIFontDescriptorFeatureKey as a structure type, with enumerated // values being members of the type structure. typedef NSString * UIFontDescriptorFeatureKey __attribute__((__swift_newtype__(struct)));
swift_objc_members¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
This attribute indicates that Swift subclasses and members of Swift extensions
of this class will be implicitly marked with the @objcMembers
Swift
attribute, exposing them back to Objective-C.
swift_private¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Declarations marked with the swift_private
attribute are hidden from the
framework client but are still made available for use within the framework or
Swift SDK overlay.
The purpose of this attribute is to permit a more idomatic implementation of declarations in Swift while hiding the non-idiomatic one.
Declaration Attributes¶
Owner¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
Note
This attribute is experimental and its effect on analysis is subject to change in a future version of clang.
The attribute [[gsl::Owner(T)]]
applies to structs and classes that own an
object of type T
:
class [[gsl::Owner(int)]] IntOwner {
private:
int value;
public:
int *getInt() { return &value; }
};
The argument T
is optional and is ignored.
This attribute may be used by analysis tools and has no effect on code
generation. A void
argument means that the class can own any type.
See Pointer for an example.
Pointer¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
Note
This attribute is experimental and its effect on analysis is subject to change in a future version of clang.
The attribute [[gsl::Pointer(T)]]
applies to structs and classes that behave
like pointers to an object of type T
:
class [[gsl::Pointer(int)]] IntPointer {
private:
int *valuePointer;
public:
IntPointer(const IntOwner&);
int *getInt() { return valuePointer; }
};
The argument T
is optional and is ignored.
This attribute may be used by analysis tools and has no effect on code
generation. A void
argument means that the pointer can point to any type.
Example:
When constructing an instance of a class annotated like this (a Pointer) from
an instance of a class annotated with [[gsl::Owner]]
(an Owner),
then the analysis will consider the Pointer to point inside the Owner.
When the Owner’s lifetime ends, it will consider the Pointer to be dangling.
int f() {
IntPointer P(IntOwner{}); // P "points into" a temporary IntOwner object
P.getInt(); // P is dangling
}
If a template class is annotated with [[gsl::Owner]]
, and the first
instantiated template argument is a pointer type (raw pointer, or [[gsl::Pointer]]
),
the analysis will consider the instantiated class as a container of the pointer.
When constructing such an object from a GSL owner object, the analysis will
assume that the container holds a pointer to the owner object. Consequently,
when the owner object is destroyed, the pointer will be considered dangling.
int f() {
std::vector<std::string_view> v = {std::string()}; // v holds a dangling pointer.
std::optional<std::string_view> o = std::string(); // o holds a dangling pointer.
}
__single_inheritance, __multiple_inheritance, __virtual_inheritance¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
This collection of keywords is enabled under -fms-extensions
and controls
the pointer-to-member representation used on *-*-win32
targets.
The *-*-win32
targets utilize a pointer-to-member representation which
varies in size and alignment depending on the definition of the underlying
class.
However, this is problematic when a forward declaration is only available and no definition has been made yet. In such cases, Clang is forced to utilize the most general representation that is available to it.
These keywords make it possible to use a pointer-to-member representation other than the most general one regardless of whether or not the definition will ever be present in the current translation unit.
This family of keywords belong between the class-key
and class-name
:
struct __single_inheritance S;
int S::*i;
struct S {};
This keyword can be applied to class templates but only has an effect when used on full specializations:
template <typename T, typename U> struct __single_inheritance A; // warning: inheritance model ignored on primary template
template <typename T> struct __multiple_inheritance A<T, T>; // warning: inheritance model ignored on partial specialization
template <> struct __single_inheritance A<int, float>;
Note that choosing an inheritance model less general than strictly necessary is an error:
struct __multiple_inheritance S; // error: inheritance model does not match definition
int S::*i;
struct S {};
asm¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
This attribute can be used on a function or variable to specify its symbol name.
On some targets, all C symbols are prefixed by default with a single character,
typically _
. This was done historically to distinguish them from symbols
used by other languages. (This prefix is also added to the standard Itanium
C++ ABI prefix on “mangled” symbol names, so that e.g. on such targets the true
symbol name for a C++ variable declared as int cppvar;
would be
__Z6cppvar
; note the two underscores.) This prefix is not added to the
symbol names specified by the asm
attribute; programmers wishing to match a
C symbol name must compensate for this.
For example, consider the following C code:
int var1 asm("altvar") = 1; // "altvar" in symbol table.
int var2 = 1; // "_var2" in symbol table.
void func1(void) asm("altfunc");
void func1(void) {} // "altfunc" in symbol table.
void func2(void) {} // "_func2" in symbol table.
Clang’s implementation of this attribute is compatible with GCC’s, documented here.
While it is possible to use this attribute to name a special symbol used internally by the compiler, such as an LLVM intrinsic, this is neither recommended nor supported and may cause the compiler to crash or miscompile. Users who wish to gain access to intrinsic behavior are strongly encouraged to request new builtin functions.
coro_await_elidable¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The [[clang::coro_await_elidable]]
is a class attribute which can be
applied to a coroutine return type. It provides a hint to the compiler to apply
Heap Allocation Elision more aggressively.
When a coroutine function returns such a type, a direct call expression therein
that returns a prvalue of a type attributed [[clang::coro_await_elidable]]
is said to be under a safe elide context if one of the following is true:
- it is the immediate right-hand side operand to a co_await expression.
- it is an argument to a [[clang::coro_await_elidable_argument]]
parameter
or parameter pack of another direct call expression under a safe elide context.
Do note that the safe elide context applies only to the call expression itself,
and the context does not transitively include any of its subexpressions unless
exceptional rules of [[clang::coro_await_elidable_argument]]
apply.
The compiler performs heap allocation elision on call expressions under a safe elide context, if the callee is a coroutine.
Example:
class [[clang::coro_await_elidable]] Task { ... };
Task foo();
Task bar() {
co_await foo(); // foo()'s coroutine frame on this line is elidable
auto t = foo(); // foo()'s coroutine frame on this line is NOT elidable
co_await t;
}
Such elision replaces the heap allocated activation frame of the callee coroutine with a local variable within the enclosing braces in the caller’s stack frame. The local variable, like other variables in coroutines, may be collected into the coroutine frame, which may be allocated on the heap. The behavior is undefined if the caller coroutine is destroyed earlier than the callee coroutine.
coro_await_elidable_argument¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The [[clang::coro_await_elidable_argument]]
is a function parameter attribute.
It works in conjunction with [[clang::coro_await_elidable]]
to propagate a
safe elide context to a parameter or parameter pack if the function is called
under a safe elide context.
This is sometimes necessary on utility functions used to compose or modify the behavior of a callee coroutine.
Example:
template <typename T>
class [[clang::coro_await_elidable]] Task { ... };
template <typename... T>
class [[clang::coro_await_elidable]] WhenAll { ... };
// `when_all` is a utility function that composes coroutines. It does not
// need to be a coroutine to propagate.
template <typename... T>
WhenAll<T...> when_all([[clang::coro_await_elidable_argument]] Task<T> tasks...);
Task<int> foo();
Task<int> bar();
Task<void> example1() {
// `when_all``, `foo``, and `bar` are all elide safe because `when_all` is
// under a safe elide context and, thanks to the [[clang::coro_await_elidable_argument]]
// attribute, such context is propagated to foo and bar.
co_await when_all(foo(), bar());
}
Task<void> example2() {
// `when_all` and `bar` are elide safe. `foo` is not elide safe.
auto f = foo();
co_await when_all(f, bar());
}
Task<void> example3() {
// None of the calls are elide safe.
auto t = when_all(foo(), bar());
co_await t;
}
coro_disable_lifetimebound¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The [[clang::coro_lifetimebound]]
is a class attribute which can be applied
to a coroutine return type (CRT) (i.e.
it should also be annotated with [[clang::coro_return_type]]
).
All parameters of a function are considered to be lifetime bound if the function returns a
coroutine return type (CRT) annotated with [[clang::coro_lifetimebound]]
.
This lifetime bound analysis can be disabled for a coroutine wrapper or a coroutine by annotating the function
with [[clang::coro_disable_lifetimebound]]
function attribute .
See documentation of [[clang::lifetimebound]]
for details about lifetime bound analysis.
Reference parameters of a coroutine are susceptible to capturing references to temporaries or local variables.
For example,
task<int> coro(const int& a) { co_return a + 1; }
task<int> dangling_refs(int a) {
// `coro` captures reference to a temporary. `foo` would now contain a dangling reference to `a`.
auto foo = coro(1);
// `coro` captures reference to local variable `a` which is destroyed after the return.
return coro(a);
}
Lifetime bound static analysis can be used to detect such instances when coroutines capture references
which may die earlier than the coroutine frame itself. In the above example, if the CRT task is annotated with
[[clang::coro_lifetimebound]]
, then lifetime bound analysis would detect capturing reference to
temporaries or return address of a local variable.
Both coroutines and coroutine wrappers are part of this analysis.
template <typename T> struct [[clang::coro_return_type, clang::coro_lifetimebound]] Task {
using promise_type = some_promise_type;
};
Task<int> coro(const int& a) { co_return a + 1; }
[[clang::coro_wrapper]] Task<int> coro_wrapper(const int& a, const int& b) {
return a > b ? coro(a) : coro(b);
}
Task<int> temporary_reference() {
auto foo = coro(1); // warning: capturing reference to a temporary which would die after the expression.
int a = 1;
auto bar = coro_wrapper(a, 0); // warning: `b` captures reference to a temporary.
co_return co_await coro(1); // fine.
}
[[clang::coro_wrapper]] Task<int> stack_reference(int a) {
return coro(a); // warning: returning address of stack variable `a`.
}
This analysis can be disabled for all calls to a particular function by annotating the function
with function attribute [[clang::coro_disable_lifetimebound]]
.
For example, this could be useful for coroutine wrappers which accept reference parameters
but do not pass them to the underlying coroutine or pass them by value.
Task<int> coro(int a) { co_return a + 1; }
[[clang::coro_wrapper, clang::coro_disable_lifetimebound]] Task<int> coro_wrapper(const int& a) {
return coro(a + 1);
}
void use() {
auto task = coro_wrapper(1); // use of temporary is fine as the argument is not lifetime bound.
}
coro_lifetimebound¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The [[clang::coro_lifetimebound]]
is a class attribute which can be applied
to a coroutine return type (CRT) (i.e.
it should also be annotated with [[clang::coro_return_type]]
).
All parameters of a function are considered to be lifetime bound if the function returns a
coroutine return type (CRT) annotated with [[clang::coro_lifetimebound]]
.
This lifetime bound analysis can be disabled for a coroutine wrapper or a coroutine by annotating the function
with [[clang::coro_disable_lifetimebound]]
function attribute .
See documentation of [[clang::lifetimebound]]
for details about lifetime bound analysis.
Reference parameters of a coroutine are susceptible to capturing references to temporaries or local variables.
For example,
task<int> coro(const int& a) { co_return a + 1; }
task<int> dangling_refs(int a) {
// `coro` captures reference to a temporary. `foo` would now contain a dangling reference to `a`.
auto foo = coro(1);
// `coro` captures reference to local variable `a` which is destroyed after the return.
return coro(a);
}
Lifetime bound static analysis can be used to detect such instances when coroutines capture references
which may die earlier than the coroutine frame itself. In the above example, if the CRT task is annotated with
[[clang::coro_lifetimebound]]
, then lifetime bound analysis would detect capturing reference to
temporaries or return address of a local variable.
Both coroutines and coroutine wrappers are part of this analysis.
template <typename T> struct [[clang::coro_return_type, clang::coro_lifetimebound]] Task {
using promise_type = some_promise_type;
};
Task<int> coro(const int& a) { co_return a + 1; }
[[clang::coro_wrapper]] Task<int> coro_wrapper(const int& a, const int& b) {
return a > b ? coro(a) : coro(b);
}
Task<int> temporary_reference() {
auto foo = coro(1); // warning: capturing reference to a temporary which would die after the expression.
int a = 1;
auto bar = coro_wrapper(a, 0); // warning: `b` captures reference to a temporary.
co_return co_await coro(1); // fine.
}
[[clang::coro_wrapper]] Task<int> stack_reference(int a) {
return coro(a); // warning: returning address of stack variable `a`.
}
This analysis can be disabled for all calls to a particular function by annotating the function
with function attribute [[clang::coro_disable_lifetimebound]]
.
For example, this could be useful for coroutine wrappers which accept reference parameters
but do not pass them to the underlying coroutine or pass them by value.
Task<int> coro(int a) { co_return a + 1; }
[[clang::coro_wrapper, clang::coro_disable_lifetimebound]] Task<int> coro_wrapper(const int& a) {
return coro(a + 1);
}
void use() {
auto task = coro_wrapper(1); // use of temporary is fine as the argument is not lifetime bound.
}
coro_only_destroy_when_complete¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The coro_only_destroy_when_complete attribute should be marked on a C++ class. The coroutines whose return type is marked with the attribute are assumed to be destroyed only after the coroutine has reached the final suspend point.
This is helpful for the optimizers to reduce the size of the destroy function for the coroutines.
For example,
A foo() {
dtor d;
co_await something();
dtor d1;
co_await something();
dtor d2;
co_return 43;
}
The compiler may generate the following pseudocode:
void foo.destroy(foo.Frame *frame) {
switch(frame->suspend_index()) {
case 1:
frame->d.~dtor();
break;
case 2:
frame->d.~dtor();
frame->d1.~dtor();
break;
case 3:
frame->d.~dtor();
frame->d1.~dtor();
frame->d2.~dtor();
break;
default: // coroutine completed or haven't started
break;
}
frame->promise.~promise_type();
delete frame;
}
The foo.destroy() function’s purpose is to release all of the resources initialized for the coroutine when it is destroyed in a suspended state. However, if the coroutine is only ever destroyed at the final suspend state, the rest of the conditions are superfluous.
The user can use the coro_only_destroy_when_complete attributo suppress generation of the other destruction cases, optimizing the above foo.destroy to:
void foo.destroy(foo.Frame *frame) {
frame->promise.~promise_type();
delete frame;
}
coro_return_type¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The [[clang::coro_return_type]]
attribute is used to help static analyzers to recognize
coroutines from the function signatures.
The coro_return_type
attribute should be marked on a C++ class to mark it as
a coroutine return type (CRT).
A function R func(P1, .., PN)
has a coroutine return type (CRT) R
if R
is marked by [[clang::coro_return_type]]
and R
has a promise type associated to it
(i.e., std::coroutine_traits<R, P1, .., PN>::promise_type is a valid promise type).
If the return type of a function is a CRT
then the function must be a coroutine.
Otherwise the program is invalid. It is allowed for a non-coroutine to return a CRT
if the function is marked with [[clang::coro_wrapper]]
.
The [[clang::coro_wrapper]]
attribute should be marked on a C++ function to mark it as
a coroutine wrapper. A coroutine wrapper is a function which returns a CRT
,
is not a coroutine itself and is marked with [[clang::coro_wrapper]]
.
Clang will enforce that all functions that return a CRT
are either coroutines or marked
with [[clang::coro_wrapper]]
. Clang will enforce this with an error.
From a language perspective, it is not possible to differentiate between a coroutine and a function returning a CRT by merely looking at the function signature.
Coroutine wrappers, in particular, are susceptible to capturing references to temporaries and other lifetime issues. This allows to avoid such lifetime issues with coroutine wrappers.
For example,
// This is a CRT.
template <typename T> struct [[clang::coro_return_type]] Task {
using promise_type = some_promise_type;
};
Task<int> increment(int a) { co_return a + 1; } // Fine. This is a coroutine.
Task<int> foo() { return increment(1); } // Error. foo is not a coroutine.
// Fine for a coroutine wrapper to return a CRT.
[[clang::coro_wrapper]] Task<int> foo() { return increment(1); }
void bar() {
// Invalid. This intantiates a function which returns a CRT but is not marked as
// a coroutine wrapper.
std::function<Task<int>(int)> f = increment;
}
Note: a_promise_type::get_return_object
is exempted from this analysis as it is a necessary
implementation detail of any coroutine library.
coro_wrapper¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The [[clang::coro_return_type]]
attribute is used to help static analyzers to recognize
coroutines from the function signatures.
The coro_return_type
attribute should be marked on a C++ class to mark it as
a coroutine return type (CRT).
A function R func(P1, .., PN)
has a coroutine return type (CRT) R
if R
is marked by [[clang::coro_return_type]]
and R
has a promise type associated to it
(i.e., std::coroutine_traits<R, P1, .., PN>::promise_type is a valid promise type).
If the return type of a function is a CRT
then the function must be a coroutine.
Otherwise the program is invalid. It is allowed for a non-coroutine to return a CRT
if the function is marked with [[clang::coro_wrapper]]
.
The [[clang::coro_wrapper]]
attribute should be marked on a C++ function to mark it as
a coroutine wrapper. A coroutine wrapper is a function which returns a CRT
,
is not a coroutine itself and is marked with [[clang::coro_wrapper]]
.
Clang will enforce that all functions that return a CRT
are either coroutines or marked
with [[clang::coro_wrapper]]
. Clang will enforce this with an error.
From a language perspective, it is not possible to differentiate between a coroutine and a function returning a CRT by merely looking at the function signature.
Coroutine wrappers, in particular, are susceptible to capturing references to temporaries and other lifetime issues. This allows to avoid such lifetime issues with coroutine wrappers.
For example,
// This is a CRT.
template <typename T> struct [[clang::coro_return_type]] Task {
using promise_type = some_promise_type;
};
Task<int> increment(int a) { co_return a + 1; } // Fine. This is a coroutine.
Task<int> foo() { return increment(1); } // Error. foo is not a coroutine.
// Fine for a coroutine wrapper to return a CRT.
[[clang::coro_wrapper]] Task<int> foo() { return increment(1); }
void bar() {
// Invalid. This intantiates a function which returns a CRT but is not marked as
// a coroutine wrapper.
std::function<Task<int>(int)> f = increment;
}
Note: a_promise_type::get_return_object
is exempted from this analysis as it is a necessary
implementation detail of any coroutine library.
deprecated¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
The deprecated
attribute can be applied to a function, a variable, or a
type. This is useful when identifying functions, variables, or types that are
expected to be removed in a future version of a program.
Consider the function declaration for a hypothetical function f
:
void f(void) __attribute__((deprecated("message", "replacement")));
When spelled as __attribute__((deprecated))
, the deprecated attribute can have
two optional string arguments. The first one is the message to display when
emitting the warning; the second one enables the compiler to provide a Fix-It
to replace the deprecated name with a new name. Otherwise, when spelled as
[[gnu::deprecated]]
or [[deprecated]]
, the attribute can have one optional
string argument which is the message to display when emitting the warning.
empty_bases¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The empty_bases attribute permits the compiler to utilize the empty-base-optimization more frequently. This attribute only applies to struct, class, and union types. It is only supported when using the Microsoft C++ ABI.
enum_extensibility¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Attribute enum_extensibility
is used to distinguish between enum definitions
that are extensible and those that are not. The attribute can take either
closed
or open
as an argument. closed
indicates a variable of the
enum type takes a value that corresponds to one of the enumerators listed in the
enum definition or, when the enum is annotated with flag_enum
, a value that
can be constructed using values corresponding to the enumerators. open
indicates a variable of the enum type can take any values allowed by the
standard and instructs clang to be more lenient when issuing warnings.
enum __attribute__((enum_extensibility(closed))) ClosedEnum {
A0, A1
};
enum __attribute__((enum_extensibility(open))) OpenEnum {
B0, B1
};
enum __attribute__((enum_extensibility(closed),flag_enum)) ClosedFlagEnum {
C0 = 1 << 0, C1 = 1 << 1
};
enum __attribute__((enum_extensibility(open),flag_enum)) OpenFlagEnum {
D0 = 1 << 0, D1 = 1 << 1
};
void foo1() {
enum ClosedEnum ce;
enum OpenEnum oe;
enum ClosedFlagEnum cfe;
enum OpenFlagEnum ofe;
ce = A1; // no warnings
ce = 100; // warning issued
oe = B1; // no warnings
oe = 100; // no warnings
cfe = C0 | C1; // no warnings
cfe = C0 | C1 | 4; // warning issued
ofe = D0 | D1; // no warnings
ofe = D0 | D1 | 4; // no warnings
}
external_source_symbol¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The external_source_symbol
attribute specifies that a declaration originates
from an external source and describes the nature of that source.
The fact that Clang is capable of recognizing declarations that were defined externally can be used to provide better tooling support for mixed-language projects or projects that rely on auto-generated code. For instance, an IDE that uses Clang and that supports mixed-language projects can use this attribute to provide a correct ‘jump-to-definition’ feature. For a concrete example, consider a protocol that’s defined in a Swift file:
@objc public protocol SwiftProtocol {
func method()
}
This protocol can be used from Objective-C code by including a header file that
was generated by the Swift compiler. The declarations in that header can use
the external_source_symbol
attribute to make Clang aware of the fact
that SwiftProtocol
actually originates from a Swift module:
__attribute__((external_source_symbol(language="Swift",defined_in="module")))
@protocol SwiftProtocol
@required
- (void) method;
@end
Consequently, when ‘jump-to-definition’ is performed at a location that
references SwiftProtocol
, the IDE can jump to the original definition in
the Swift source file rather than jumping to the Objective-C declaration in the
auto-generated header file.
The external_source_symbol
attribute is a comma-separated list that includes
clauses that describe the origin and the nature of the particular declaration.
Those clauses can be:
- language=string-literal
The name of the source language in which this declaration was defined.
- defined_in=string-literal
The name of the source container in which the declaration was defined. The exact definition of source container is language-specific, e.g. Swift’s source containers are modules, so
defined_in
should specify the Swift module name.- USR=string-literal
String that specifies a unified symbol resolution (USR) value for this declaration. USR string uniquely identifies this particular declaration, and is typically used when constructing an index of a codebase. The USR value in this attribute is expected to be generated by an external compiler that compiled the native declaration using its original source language. The exact format of the USR string and its other attributes are determined by the specification of this declaration’s source language. When not specified, Clang’s indexer will use the Clang USR for this symbol. User can query to see if Clang supports the use of the
USR
clause in theexternal_source_symbol
attribute with__has_attribute(external_source_symbol) >= 20230206
.- generated_declaration
This declaration was automatically generated by some tool.
The clauses can be specified in any order. The clauses that are listed above are all optional, but the attribute has to have at least one clause.
flag_enum¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
This attribute can be added to an enumerator to signal to the compiler that it is intended to be used as a flag type. This will cause the compiler to assume that the range of the type includes all of the values that you can get by manipulating bits of the enumerator when issuing warnings.
grid_constant¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
The __grid_constant__
attribute can be applied to a const
-qualified kernel
function argument and allows compiler to take the address of that argument without
making a copy. The argument applies to sm_70 or newer GPUs, during compilation
with CUDA-11.7(PTX 7.7) or newer, and is ignored otherwise.
layout_version¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The layout_version attribute requests that the compiler utilize the class layout rules of a particular compiler version. This attribute only applies to struct, class, and union types. It is only supported when using the Microsoft C++ ABI.
lto_visibility_public¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
See LTO Visibility.
managed¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
The __managed__
attribute can be applied to a global variable declaration in HIP.
A managed variable is emitted as an undefined global symbol in the device binary and is
registered by __hipRegisterManagedVariable
in init functions. The HIP runtime allocates
managed memory and uses it to define the symbol when loading the device binary.
A managed variable can be accessed in both device and host code.
novtable¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
This attribute can be added to a class declaration or definition to signal to the compiler that constructors and destructors will not reference the virtual function table. It is only supported when using the Microsoft C++ ABI.
ns_error_domain¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
In Cocoa frameworks in Objective-C, one can group related error codes in enums and categorize these enums with error domains.
The ns_error_domain
attribute indicates a global NSString
or
CFString
constant representing the error domain that an error code belongs
to. For pointer uniqueness and code size this is a constant symbol, not a
literal.
The domain and error code need to be used together. The ns_error_domain
attribute links error codes to their domain at the source level.
This metadata is useful for documentation purposes, for static analysis, and for improving interoperability between Objective-C and Swift. It is not used for code generation in Objective-C.
For example:
#define NS_ERROR_ENUM(_type, _name, _domain) \ enum _name : _type _name; enum __attribute__((ns_error_domain(_domain))) _name : _type extern NSString *const MyErrorDomain; typedef NS_ERROR_ENUM(unsigned char, MyErrorEnum, MyErrorDomain) { MyErrFirst, MyErrSecond, };
objc_boxable¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Structs and unions marked with the objc_boxable
attribute can be used
with the Objective-C boxed expression syntax, @(...)
.
Usage: __attribute__((objc_boxable))
. This attribute
can only be placed on a declaration of a trivially-copyable struct or union:
struct __attribute__((objc_boxable)) some_struct {
int i;
};
union __attribute__((objc_boxable)) some_union {
int i;
float f;
};
typedef struct __attribute__((objc_boxable)) _some_struct some_struct;
// ...
some_struct ss;
NSValue *boxed = @(ss);
objc_direct¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The objc_direct
attribute can be used to mark an Objective-C method as
being direct. A direct method is treated statically like an ordinary method,
but dynamically it behaves more like a C function. This lowers some of the costs
associated with the method but also sacrifices some of the ordinary capabilities
of Objective-C methods.
A message send of a direct method calls the implementation directly, as if it were a C function, rather than using ordinary Objective-C method dispatch. This is substantially faster and potentially allows the implementation to be inlined, but it also means the method cannot be overridden in subclasses or replaced dynamically, as ordinary Objective-C methods can.
Furthermore, a direct method is not listed in the class’s method lists. This substantially reduces the code-size overhead of the method but also means it cannot be called dynamically using ordinary Objective-C method dispatch at all; in particular, this means that it cannot override a superclass method or satisfy a protocol requirement.
Because a direct method cannot be overridden, it is an error to perform
a super
message send of one.
Although a message send of a direct method causes the method to be called directly as if it were a C function, it still obeys Objective-C semantics in other ways:
If the receiver is
nil
, the message send does nothing and returns the zero value for the return type.A message send of a direct class method will cause the class to be initialized, including calling the
+initialize
method if present.The implicit
_cmd
parameter containing the method’s selector is still defined. In order to minimize code-size costs, the implementation will not emit a reference to the selector if the parameter is unused within the method.
Symbols for direct method implementations are implicitly given hidden visibility, meaning that they can only be called within the same linkage unit.
It is an error to do any of the following:
declare a direct method in a protocol,
declare an override of a direct method with a method in a subclass,
declare an override of a non-direct method with a direct method in a subclass,
declare a method with different directness in different class interfaces, or
implement a non-direct method (as declared in any class interface) with a direct method.
If any of these rules would be violated if every method defined in an
@implementation
within a single linkage unit were declared in an
appropriate class interface, the program is ill-formed with no diagnostic
required. If a violation of this rule is not diagnosed, behavior remains
well-defined; this paragraph is simply reserving the right to diagnose such
conflicts in the future, not to treat them as undefined behavior.
Additionally, Clang will warn about any @selector
expression that
names a selector that is only known to be used for direct methods.
For the purpose of these rules, a “class interface” includes a class’s primary
@interface
block, its class extensions, its categories, its declared protocols,
and all the class interfaces of its superclasses.
An Objective-C property can be declared with the direct
property
attribute. If a direct property declaration causes an implicit declaration of
a getter or setter method (that is, if the given method is not explicitly
declared elsewhere), the method is declared to be direct.
Some programmers may wish to make many methods direct at once. In order
to simplify this, the objc_direct_members
attribute is provided; see its
documentation for more information.
objc_direct_members¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The objc_direct_members
attribute can be placed on an Objective-C
@interface
or @implementation
to mark that methods declared
therein should be considered direct by default. See the documentation
for objc_direct
for more information about direct methods.
When objc_direct_members
is placed on an @interface
block, every
method in the block is considered to be declared as direct. This includes any
implicit method declarations introduced by property declarations. If the method
redeclares a non-direct method, the declaration is ill-formed, exactly as if the
method was annotated with the objc_direct
attribute.
When objc_direct_members
is placed on an @implementation
block,
methods defined in the block are considered to be declared as direct unless
they have been previously declared as non-direct in any interface of the class.
This includes the implicit method definitions introduced by synthesized
properties, including auto-synthesized properties.
objc_non_runtime_protocol¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The objc_non_runtime_protocol
attribute can be used to mark that an
Objective-C protocol is only used during static type-checking and doesn’t need
to be represented dynamically. This avoids several small code-size and run-time
overheads associated with handling the protocol’s metadata. A non-runtime
protocol cannot be used as the operand of a @protocol
expression, and
dynamic attempts to find it with objc_getProtocol
will fail.
If a non-runtime protocol inherits from any ordinary protocols, classes and derived protocols that declare conformance to the non-runtime protocol will dynamically list their conformance to those bare protocols.
objc_nonlazy_class¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
This attribute can be added to an Objective-C @interface
or
@implementation
declaration to add the class to the list of non-lazily
initialized classes. A non-lazy class will be initialized eagerly when the
Objective-C runtime is loaded. This is required for certain system classes which
have instances allocated in non-standard ways, such as the classes for blocks
and constant strings. Adding this attribute is essentially equivalent to
providing a trivial +load
method but avoids the (fairly small) load-time
overheads associated with defining and calling such a method.
objc_runtime_name¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
By default, the Objective-C interface or protocol identifier is used
in the metadata name for that object. The objc_runtime_name
attribute allows annotated interfaces or protocols to use the
specified string argument in the object’s metadata name instead of the
default name.
Usage: __attribute__((objc_runtime_name("MyLocalName")))
. This attribute
can only be placed before an @protocol or @interface declaration:
__attribute__((objc_runtime_name("MyLocalName")))
@interface Message
@end
objc_runtime_visible¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
This attribute specifies that the Objective-C class to which it applies is visible to the Objective-C runtime but not to the linker. Classes annotated with this attribute cannot be subclassed and cannot have categories defined for them.
objc_subclassing_restricted¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
This attribute can be added to an Objective-C @interface
declaration to
ensure that this class cannot be subclassed.
preferred_name¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
The preferred_name
attribute can be applied to a class template, and
specifies a preferred way of naming a specialization of the template. The
preferred name will be used whenever the corresponding template specialization
would otherwise be printed in a diagnostic or similar context.
The preferred name must be a typedef or type alias declaration that refers to a specialization of the class template (not including any type qualifiers). In general this requires the template to be declared at least twice. For example:
template<typename T> struct basic_string;
using string = basic_string<char>;
using wstring = basic_string<wchar_t>;
template<typename T> struct [[clang::preferred_name(string),
clang::preferred_name(wstring)]] basic_string {
// ...
};
Note that the preferred_name
attribute will be ignored when the compiler
writes a C++20 Module interface now. This is due to a compiler issue
(https://github.com/llvm/llvm-project/issues/56490) that blocks users to modularize
declarations with preferred_name. This is intended to be fixed in the future.
randomize_layout, no_randomize_layout¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The attribute randomize_layout
, when attached to a C structure, selects it
for structure layout field randomization; a compile-time hardening technique. A
“seed” value, is specified via the -frandomize-layout-seed=
command line flag.
For example:
SEED=`od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n'`
make ... CFLAGS="-frandomize-layout-seed=$SEED" ...
You can also supply the seed in a file with -frandomize-layout-seed-file=
.
For example:
od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n' > /tmp/seed_file.txt
make ... CFLAGS="-frandomize-layout-seed-file=/tmp/seed_file.txt" ...
The randomization is deterministic based for a given seed, so the entire program should be compiled with the same seed, but keep the seed safe otherwise.
The attribute no_randomize_layout
, when attached to a C structure,
instructs the compiler that this structure should not have its field layout
randomized.
randomize_layout, no_randomize_layout¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The attribute randomize_layout
, when attached to a C structure, selects it
for structure layout field randomization; a compile-time hardening technique. A
“seed” value, is specified via the -frandomize-layout-seed=
command line flag.
For example:
SEED=`od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n'`
make ... CFLAGS="-frandomize-layout-seed=$SEED" ...
You can also supply the seed in a file with -frandomize-layout-seed-file=
.
For example:
od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n' > /tmp/seed_file.txt
make ... CFLAGS="-frandomize-layout-seed-file=/tmp/seed_file.txt" ...
The randomization is deterministic based for a given seed, so the entire program should be compiled with the same seed, but keep the seed safe otherwise.
The attribute no_randomize_layout
, when attached to a C structure,
instructs the compiler that this structure should not have its field layout
randomized.
selectany¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
This attribute appertains to a global symbol, causing it to have a weak definition ( linkonce ), allowing the linker to select any definition.
For more information see gcc documentation or msvc documentation.
transparent_union¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
This attribute can be applied to a union to change the behavior of calls to functions that have an argument with a transparent union type. The compiler behavior is changed in the following manner:
A value whose type is any member of the transparent union can be passed as an argument without the need to cast that value.
The argument is passed to the function using the calling convention of the first member of the transparent union. Consequently, all the members of the transparent union should have the same calling convention as its first member.
Transparent unions are not supported in C++.
trivial_abi¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
The trivial_abi
attribute can be applied to a C++ class, struct, or union.
It instructs the compiler to pass and return the type using the C ABI for the
underlying type when the type would otherwise be considered non-trivial for the
purpose of calls.
A class annotated with trivial_abi
can have non-trivial destructors or
copy/move constructors without automatically becoming non-trivial for the
purposes of calls. For example:
// A is trivial for the purposes of calls because ``trivial_abi`` makes the // user-provided special functions trivial. struct __attribute__((trivial_abi)) A { ~A(); A(const A &); A(A &&); int x; }; // B's destructor and copy/move constructor are considered trivial for the // purpose of calls because A is trivial. struct B { A a; };
If a type is trivial for the purposes of calls, has a non-trivial destructor, and is passed as an argument by value, the convention is that the callee will destroy the object before returning.
If a type is trivial for the purpose of calls, it is assumed to be trivially
relocatable for the purpose of __is_trivially_relocatable
.
Attribute trivial_abi
has no effect in the following cases:
The class directly declares a virtual base or virtual methods.
Copy constructors and move constructors of the class are all deleted.
The class has a base class that is non-trivial for the purposes of calls.
The class has a non-static data member whose type is non-trivial for the purposes of calls, which includes:
classes that are non-trivial for the purposes of calls
__weak-qualified types in Objective-C++
arrays of any of the above
using_if_exists¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
The using_if_exists
attribute applies to a using-declaration. It allows
programmers to import a declaration that potentially does not exist, instead
deferring any errors to the point of use. For instance:
namespace empty_namespace {};
__attribute__((using_if_exists))
using empty_namespace::does_not_exist; // no error!
does_not_exist x; // error: use of unresolved 'using_if_exists'
The C++ spelling of the attribute ([[clang::using_if_exists]]) is also supported as a clang extension, since ISO C++ doesn’t support attributes in this position. If the entity referred to by the using-declaration is found by name lookup, the attribute has no effect. This attribute is useful for libraries (primarily, libc++) that wish to redeclare a set of declarations in another namespace, when the availability of those declarations is difficult or impossible to detect at compile time with the preprocessor.
weak¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
In supported output formats the weak
attribute can be used to
specify that a variable or function should be emitted as a symbol with
weak
(if a definition) or extern_weak
(if a declaration of an
external symbol) linkage.
If there is a non-weak definition of the symbol the linker will select that over the weak. They must have same type and alignment (variables must also have the same size), but may have a different value.
If there are multiple weak definitions of same symbol, but no non-weak definition, they should have same type, size, alignment and value, the linker will select one of them (see also selectany attribute).
If the weak
attribute is applied to a const
qualified variable
definition that variable is no longer consider a compiletime constant
as its value can change during linking (or dynamic linking). This
means that it can e.g no longer be part of an initializer expression.
const int ANSWER __attribute__ ((weak)) = 42;
/* This function may be replaced link-time */
__attribute__ ((weak)) void debug_log(const char *msg)
{
fprintf(stderr, "DEBUG: %s\n", msg);
}
int main(int argc, const char **argv)
{
debug_log ("Starting up...");
/* This may print something else than "6 * 7 = 42",
if there is a non-weak definition of "ANSWER" in
an object linked in */
printf("6 * 7 = %d\n", ANSWER);
return 0;
}
If an external declaration is marked weak and that symbol does not exist during linking (possibly dynamic) the address of the symbol will evaluate to NULL.
void may_not_exist(void) __attribute__ ((weak));
int main(int argc, const char **argv)
{
if (may_not_exist) {
may_not_exist();
} else {
printf("Function did not exist\n");
}
return 0;
}
Field Attributes¶
counted_by¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Clang supports the counted_by
attribute on the flexible array member of a
structure in C. The argument for the attribute is the name of a field member
holding the count of elements in the flexible array. This information can be
used to improve the results of the array bound sanitizer and the
__builtin_dynamic_object_size
builtin. The count
field member must be
within the same non-anonymous, enclosing struct as the flexible array member.
This example specifies that the flexible array member array
has the number
of elements allocated for it in count
:
struct bar;
struct foo {
size_t count;
char other;
struct bar *array[] __attribute__((counted_by(count)));
};
This establishes a relationship between array
and count
. Specifically,
array
must have at least count
number of elements available. It’s the
user’s responsibility to ensure that this relationship is maintained through
changes to the structure.
In the following example, the allocated array erroneously has fewer elements
than what’s specified by p->count
. This would result in an out-of-bounds
access not being detected.
#define SIZE_INCR 42
struct foo *p;
void foo_alloc(size_t count) {
p = malloc(MAX(sizeof(struct foo),
offsetof(struct foo, array[0]) + count * sizeof(struct bar *)));
p->count = count + SIZE_INCR;
}
The next example updates p->count
, but breaks the relationship requirement
that p->array
must have at least p->count
number of elements available:
#define SIZE_INCR 42
struct foo *p;
void foo_alloc(size_t count) {
p = malloc(MAX(sizeof(struct foo),
offsetof(struct foo, array[0]) + count * sizeof(struct bar *)));
p->count = count;
}
void use_foo(int index, int val) {
p->count += SIZE_INCR + 1; /* 'count' is now larger than the number of elements of 'array'. */
p->array[index] = val; /* The sanitizer can't properly check this access. */
}
In this example, an update to p->count
maintains the relationship
requirement:
void use_foo(int index, int val) {
if (p->count == 0)
return;
--p->count;
p->array[index] = val;
}
counted_by_or_null¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Clang supports the counted_by
attribute on the flexible array member of a
structure in C. The argument for the attribute is the name of a field member
holding the count of elements in the flexible array. This information can be
used to improve the results of the array bound sanitizer and the
__builtin_dynamic_object_size
builtin. The count
field member must be
within the same non-anonymous, enclosing struct as the flexible array member.
This example specifies that the flexible array member array
has the number
of elements allocated for it in count
:
struct bar;
struct foo {
size_t count;
char other;
struct bar *array[] __attribute__((counted_by(count)));
};
This establishes a relationship between array
and count
. Specifically,
array
must have at least count
number of elements available. It’s the
user’s responsibility to ensure that this relationship is maintained through
changes to the structure.
In the following example, the allocated array erroneously has fewer elements
than what’s specified by p->count
. This would result in an out-of-bounds
access not being detected.
#define SIZE_INCR 42
struct foo *p;
void foo_alloc(size_t count) {
p = malloc(MAX(sizeof(struct foo),
offsetof(struct foo, array[0]) + count * sizeof(struct bar *)));
p->count = count + SIZE_INCR;
}
The next example updates p->count
, but breaks the relationship requirement
that p->array
must have at least p->count
number of elements available:
#define SIZE_INCR 42
struct foo *p;
void foo_alloc(size_t count) {
p = malloc(MAX(sizeof(struct foo),
offsetof(struct foo, array[0]) + count * sizeof(struct bar *)));
p->count = count;
}
void use_foo(int index, int val) {
p->count += SIZE_INCR + 1; /* 'count' is now larger than the number of elements of 'array'. */
p->array[index] = val; /* The sanitizer can't properly check this access. */
}
In this example, an update to p->count
maintains the relationship
requirement:
void use_foo(int index, int val) {
if (p->count == 0)
return;
--p->count;
p->array[index] = val;
}
no_unique_address¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The no_unique_address
attribute allows tail padding in a non-static data
member to overlap other members of the enclosing class (and in the special
case when the type is empty, permits it to fully overlap other members).
The field is laid out as if a base class were encountered at the corresponding
point within the class (except that it does not share a vptr with the enclosing
object).
Example usage:
template<typename T, typename Alloc> struct my_vector {
T *p;
[[no_unique_address]] Alloc alloc;
// ...
};
static_assert(sizeof(my_vector<int, std::allocator<int>>) == sizeof(int*));
[[no_unique_address]]
is a standard C++20 attribute. Clang supports its use
in C++11 onwards.
On MSVC targets, [[no_unique_address]]
is ignored; use
[[msvc::no_unique_address]]
instead. Currently there is no guarantee of ABI
compatibility or stability with MSVC.
preferred_type¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
This attribute allows adjusting the type of a bit-field in debug information. This can be helpful when a bit-field is intended to store an enumeration value, but has to be specified as having the enumeration’s underlying type in order to facilitate compiler optimizations or bit-field packing behavior. Normally, the underlying type is what is emitted in debug information, which can make it hard for debuggers to know to map a bit-field’s value back to a particular enumeration.
enum Colors { Red, Green, Blue };
struct S {
[[clang::preferred_type(Colors)]] unsigned ColorVal : 2;
[[clang::preferred_type(bool)]] unsigned UseAlternateColorSpace : 1;
} s = { Green, false };
Without the attribute, a debugger is likely to display the value 1
for ColorVal
and 0
for UseAlternateColorSpace
. With the attribute, the debugger may now
display Green
and false
instead.
This can be used to map a bit-field to an arbitrary type that isn’t integral or an enumeration type. For example:
struct A {
short a1;
short a2;
};
struct B {
[[clang::preferred_type(A)]] unsigned b1 : 32 = 0x000F'000C;
};
will associate the type A
with the b1
bit-field and is intended to display
something like this in the debugger:
Process 2755547 stopped
* thread #1, name = 'test-preferred-', stop reason = step in
frame #0: 0x0000555555555148 test-preferred-type`main at test.cxx:13:14
10 int main()
11 {
12 B b;
-> 13 return b.b1;
14 }
(lldb) v -T
(B) b = {
(A:32) b1 = {
(short) a1 = 12
(short) a2 = 15
}
}
Note that debuggers may not be able to handle more complex mappings, and so this usage is debugger-dependent.
sized_by¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Clang supports the counted_by
attribute on the flexible array member of a
structure in C. The argument for the attribute is the name of a field member
holding the count of elements in the flexible array. This information can be
used to improve the results of the array bound sanitizer and the
__builtin_dynamic_object_size
builtin. The count
field member must be
within the same non-anonymous, enclosing struct as the flexible array member.
This example specifies that the flexible array member array
has the number
of elements allocated for it in count
:
struct bar;
struct foo {
size_t count;
char other;
struct bar *array[] __attribute__((counted_by(count)));
};
This establishes a relationship between array
and count
. Specifically,
array
must have at least count
number of elements available. It’s the
user’s responsibility to ensure that this relationship is maintained through
changes to the structure.
In the following example, the allocated array erroneously has fewer elements
than what’s specified by p->count
. This would result in an out-of-bounds
access not being detected.
#define SIZE_INCR 42
struct foo *p;
void foo_alloc(size_t count) {
p = malloc(MAX(sizeof(struct foo),
offsetof(struct foo, array[0]) + count * sizeof(struct bar *)));
p->count = count + SIZE_INCR;
}
The next example updates p->count
, but breaks the relationship requirement
that p->array
must have at least p->count
number of elements available:
#define SIZE_INCR 42
struct foo *p;
void foo_alloc(size_t count) {
p = malloc(MAX(sizeof(struct foo),
offsetof(struct foo, array[0]) + count * sizeof(struct bar *)));
p->count = count;
}
void use_foo(int index, int val) {
p->count += SIZE_INCR + 1; /* 'count' is now larger than the number of elements of 'array'. */
p->array[index] = val; /* The sanitizer can't properly check this access. */
}
In this example, an update to p->count
maintains the relationship
requirement:
void use_foo(int index, int val) {
if (p->count == 0)
return;
--p->count;
p->array[index] = val;
}
sized_by_or_null¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Clang supports the counted_by
attribute on the flexible array member of a
structure in C. The argument for the attribute is the name of a field member
holding the count of elements in the flexible array. This information can be
used to improve the results of the array bound sanitizer and the
__builtin_dynamic_object_size
builtin. The count
field member must be
within the same non-anonymous, enclosing struct as the flexible array member.
This example specifies that the flexible array member array
has the number
of elements allocated for it in count
:
struct bar;
struct foo {
size_t count;
char other;
struct bar *array[] __attribute__((counted_by(count)));
};
This establishes a relationship between array
and count
. Specifically,
array
must have at least count
number of elements available. It’s the
user’s responsibility to ensure that this relationship is maintained through
changes to the structure.
In the following example, the allocated array erroneously has fewer elements
than what’s specified by p->count
. This would result in an out-of-bounds
access not being detected.
#define SIZE_INCR 42
struct foo *p;
void foo_alloc(size_t count) {
p = malloc(MAX(sizeof(struct foo),
offsetof(struct foo, array[0]) + count * sizeof(struct bar *)));
p->count = count + SIZE_INCR;
}
The next example updates p->count
, but breaks the relationship requirement
that p->array
must have at least p->count
number of elements available:
#define SIZE_INCR 42
struct foo *p;
void foo_alloc(size_t count) {
p = malloc(MAX(sizeof(struct foo),
offsetof(struct foo, array[0]) + count * sizeof(struct bar *)));
p->count = count;
}
void use_foo(int index, int val) {
p->count += SIZE_INCR + 1; /* 'count' is now larger than the number of elements of 'array'. */
p->array[index] = val; /* The sanitizer can't properly check this access. */
}
In this example, an update to p->count
maintains the relationship
requirement:
void use_foo(int index, int val) {
if (p->count == 0)
return;
--p->count;
p->array[index] = val;
}
Function Attributes¶
#pragma omp declare simd¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
`` declare simd`` |
The declare simd
construct can be applied to a function to enable the creation
of one or more versions that can process multiple arguments using SIMD
instructions from a single invocation in a SIMD loop. The declare simd
directive is a declarative directive. There may be multiple declare simd
directives for a function. The use of a declare simd
construct on a function
enables the creation of SIMD versions of the associated function that can be
used to process multiple arguments from a single invocation from a SIMD loop
concurrently.
The syntax of the declare simd
construct is as follows:
#pragma omp declare simd [clause[[,] clause] ...] new-line [#pragma omp declare simd [clause[[,] clause] ...] new-line] [...] function definition or declaration
where clause is one of the following:
simdlen(length) linear(argument-list[:constant-linear-step]) aligned(argument-list[:alignment]) uniform(argument-list) inbranch notinbranch
#pragma omp declare target¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
`` declare target`` |
The declare target
directive specifies that variables and functions are mapped
to a device for OpenMP offload mechanism.
The syntax of the declare target directive is as follows:
#pragma omp declare target new-line declarations-definition-seq #pragma omp end declare target new-line
or
#pragma omp declare target (extended-list) new-line
or
#pragma omp declare target clause[ [,] clause ... ] new-line
where clause is one of the following:
to(extended-list) link(list) device_type(host | nohost | any)
#pragma omp declare variant¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
`` declare variant`` |
The declare variant
directive declares a specialized variant of a base
function and specifies the context in which that specialized variant is used.
The declare variant directive is a declarative directive.
The syntax of the declare variant
construct is as follows:
#pragma omp declare variant(variant-func-id) clause new-line [#pragma omp declare variant(variant-func-id) clause new-line] [...] function definition or declaration
where clause is one of the following:
match(context-selector-specification)
and where variant-func-id
is the name of a function variant that is either a
base language identifier or, for C++, a template-id.
Clang provides the following context selector extensions, used via
implementation={extension(EXTENSION)}
:
match_all match_any match_none disable_implicit_base allow_templates bind_to_declaration
The match extensions change when the entire context selector is considered a
match for an OpenMP context. The default is all
, with none
no trait in the
selector is allowed to be in the OpenMP context, with any
a single trait in
both the selector and OpenMP context is sufficient. Only a single match
extension trait is allowed per context selector.
The disable extensions remove default effects of the begin declare variant
applied to a definition. If disable_implicit_base
is given, we will not
introduce an implicit base function for a variant if no base function was
found. The variant is still generated but will never be called, due to the
absence of a base function and consequently calls to a base function.
The allow extensions change when the begin declare variant
effect is
applied to a definition. If allow_templates
is given, template function
definitions are considered as specializations of existing or assumed template
declarations with the same name. The template parameters for the base functions
are used to instantiate the specialization. If bind_to_declaration
is given,
apply the same variant rules to function declarations. This allows the user to
override declarations with only a function declaration.
SV_DispatchThreadID¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The SV_DispatchThreadID
semantic, when applied to an input parameter,
specifies a data binding to map the global thread offset within the Dispatch
call (per dimension of the group) to the specified parameter.
When applied to a field of a struct, the data binding is specified to the field
when the struct is used as a parameter type.
The semantic on the field is ignored when not used as a parameter.
This attribute is only supported in compute shaders.
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-dispatchthreadid
SV_GroupIndex¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The SV_GroupIndex
semantic, when applied to an input parameter, specifies a
data binding to map the group index to the specified parameter. This attribute
is only supported in compute shaders.
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sv-groupindex
WaveSize¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
The WaveSize
attribute specify a wave size on a shader entry point in order
to indicate either that a shader depends on or strongly prefers a specific wave
size.
There’re 2 versions of the attribute: WaveSize
and RangedWaveSize
.
The syntax for WaveSize
is:
``[WaveSize(<numLanes>)]``
The allowed wave sizes that an HLSL shader may specify are the powers of 2 between 4 and 128, inclusive. In other words, the set: [4, 8, 16, 32, 64, 128].
The syntax for RangedWaveSize
is:
``[WaveSize(<minWaveSize>, <maxWaveSize>, [prefWaveSize])]``
Where minWaveSize is the minimum wave size supported by the shader representing the beginning of the allowed range, maxWaveSize is the maximum wave size supported by the shader representing the end of the allowed range, and prefWaveSize is the optional preferred wave size representing the size expected to be the most optimal for this shader.
WaveSize
is available for HLSL shader model 6.6 and later.
RangedWaveSize
available for HLSL shader model 6.8 and later.
The full documentation is available here: https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_WaveSize.html and https://microsoft.github.io/hlsl-specs/proposals/0013-wave-size-range.html
_Noreturn¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
A function declared as _Noreturn
shall not return to its caller. The
compiler will generate a diagnostic for a function declared as _Noreturn
that appears to be capable of returning to its caller. Despite being a type
specifier, the _Noreturn
attribute cannot be specified on a function
pointer type.
__funcref¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Clang supports the __attribute__((export_name(<name>)))
attribute for the WebAssembly target. This attribute may be attached to a
function declaration, where it modifies how the symbol is to be exported
from the linked WebAssembly.
WebAssembly functions are exported via string name. By default when a symbol is exported, the export name for C/C++ symbols are the same as their C/C++ symbol names. This attribute can be used to override the default behavior, and request a specific string name be used instead.
abi_tag¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
The abi_tag
attribute can be applied to a function, variable, class or
inline namespace declaration to modify the mangled name of the entity. It gives
the ability to distinguish between different versions of the same entity but
with different ABI versions supported. For example, a newer version of a class
could have a different set of data members and thus have a different size. Using
the abi_tag
attribute, it is possible to have different mangled names for
a global variable of the class type. Therefore, the old code could keep using
the old mangled name and the new code will use the new mangled name with tags.
alloc_align¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Use __attribute__((alloc_align(<alignment>))
on a function
declaration to specify that the return value of the function (which must be a
pointer type) is at least as aligned as the value of the indicated parameter. The
parameter is given by its index in the list of formal parameters; the first
parameter has index 1 unless the function is a C++ non-static member function,
in which case the first parameter has index 2 to account for the implicit this
parameter.
// The returned pointer has the alignment specified by the first parameter.
void *a(size_t align) __attribute__((alloc_align(1)));
// The returned pointer has the alignment specified by the second parameter.
void *b(void *v, size_t align) __attribute__((alloc_align(2)));
// The returned pointer has the alignment specified by the second visible
// parameter, however it must be adjusted for the implicit 'this' parameter.
void *Foo::b(void *v, size_t align) __attribute__((alloc_align(3)));
Note that this attribute merely informs the compiler that a function always returns a sufficiently aligned pointer. It does not cause the compiler to emit code to enforce that alignment. The behavior is undefined if the returned pointer is not sufficiently aligned.
alloc_size¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The alloc_size
attribute can be placed on functions that return pointers in
order to hint to the compiler how many bytes of memory will be available at the
returned pointer. alloc_size
takes one or two arguments.
alloc_size(N)
implies that argument number N equals the number of available bytes at the returned pointer.alloc_size(N, M)
implies that the product of argument number N and argument number M equals the number of available bytes at the returned pointer.
Argument numbers are 1-based.
An example of how to use alloc_size
void *my_malloc(int a) __attribute__((alloc_size(1)));
void *my_calloc(int a, int b) __attribute__((alloc_size(1, 2)));
int main() {
void *const p = my_malloc(100);
assert(__builtin_object_size(p, 0) == 100);
void *const a = my_calloc(20, 5);
assert(__builtin_object_size(a, 0) == 100);
}
Note
This attribute works differently in clang than it does in GCC.
Specifically, clang will only trace const
pointers (as above); we give up
on pointers that are not marked as const
. In the vast majority of cases,
this is unimportant, because LLVM has support for the alloc_size
attribute. However, this may cause mildly unintuitive behavior when used with
other attributes, such as enable_if
.
allocator¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __declspec(allocator)
attribute is applied to functions that allocate
memory, such as operator new in C++. When CodeView debug information is emitted
(enabled by clang -gcodeview
or clang-cl /Z7
), Clang will attempt to
record the code offset of heap allocation call sites in the debug info. It will
also record the type being allocated using some local heuristics. The Visual
Studio debugger uses this information to profile memory usage.
This attribute does not affect optimizations in any way, unlike GCC’s
__attribute__((malloc))
.
always_inline, __force_inline¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
Yes |
Inlining heuristics are disabled and inlining is always attempted regardless of optimization level.
[[clang::always_inline]]
spelling can be used as a statement attribute; other
spellings of the attribute are not supported on statements. If a statement is
marked [[clang::always_inline]]
and contains calls, the compiler attempts
to inline those calls.
int example(void) {
int i;
[[clang::always_inline]] foo(); // attempts to inline foo
[[clang::always_inline]] i = bar(); // attempts to inline bar
[[clang::always_inline]] return f(42, baz(bar())); // attempts to inline everything
}
A declaration statement, which is a statement, is not a statement that can have an attribute associated with it (the attribute applies to the declaration, not the statement in that case). So this use case will not work:
int example(void) {
[[clang::always_inline]] int i = bar();
return i;
}
This attribute does not guarantee that inline substitution actually occurs.
<ins>Note: applying this attribute to a coroutine at the -O0 optimization level has no effect; other optimization levels may only partially inline and result in a diagnostic.</ins>
See also the Microsoft Docs on Inline Functions, the GCC Common Function Attribute docs, and the GCC Inline docs.
artificial¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The artificial
attribute can be applied to an inline function. If such a
function is inlined, the attribute indicates that debuggers should associate
the resulting instructions with the call site, rather than with the
corresponding line within the inlined callee.
assume¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
Clang supports the [[omp::assume("assumption")]]
attribute to
provide additional information to the optimizer. The string-literal, here
“assumption”, will be attached to the function declaration such that later
analysis and optimization passes can assume the “assumption” to hold.
This is similar to __builtin_assume but
instead of an expression that can be assumed to be non-zero, the assumption is
expressed as a string and it holds for the entire function.
A function can have multiple assume attributes and they propagate from prior
declarations to later definitions. Multiple assumptions are aggregated into a
single comma separated string. Thus, one can provide multiple assumptions via
a comma separated string, i.a.,
[[omp::assume("assumption1,assumption2")]]
.
While LLVM plugins might provide more assumption strings, the default LLVM optimization passes are aware of the following assumptions:
"omp_no_openmp" "omp_no_openmp_routines" "omp_no_parallelism"
The OpenMP standard defines the meaning of OpenMP assumptions (“omp_XYZ” is spelled “XYZ” in the OpenMP 5.1 Standard).
assume_aligned¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Use __attribute__((assume_aligned(<alignment>[,<offset>]))
on a function
declaration to specify that the return value of the function (which must be a
pointer type) has the specified offset, in bytes, from an address with the
specified alignment. The offset is taken to be zero if omitted.
// The returned pointer value has 32-byte alignment.
void *a() __attribute__((assume_aligned (32)));
// The returned pointer value is 4 bytes greater than an address having
// 32-byte alignment.
void *b() __attribute__((assume_aligned (32, 4)));
Note that this attribute provides information to the compiler regarding a condition that the code already ensures is true. It does not cause the compiler to enforce the provided alignment assumption.
availability¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The availability
attribute can be placed on declarations to describe the
lifecycle of that declaration relative to operating system versions. Consider
the function declaration for a hypothetical function f
:
void f(void) __attribute__((availability(macos,introduced=10.4,deprecated=10.6,obsoleted=10.7)));
The availability attribute states that f
was introduced in macOS 10.4,
deprecated in macOS 10.6, and obsoleted in macOS 10.7. This information
is used by Clang to determine when it is safe to use f
: for example, if
Clang is instructed to compile code for macOS 10.5, a call to f()
succeeds. If Clang is instructed to compile code for macOS 10.6, the call
succeeds but Clang emits a warning specifying that the function is deprecated.
Finally, if Clang is instructed to compile code for macOS 10.7, the call
fails because f()
is no longer available.
Clang is instructed to compile code for a minimum deployment version using
the -target
or -mtargetos
command line arguments. For example,
macOS 10.7 would be specified as -target x86_64-apple-macos10.7
or
-mtargetos=macos10.7
. Variants like Mac Catalyst are specified as
-target arm64-apple-ios15.0-macabi
or -mtargetos=ios15.0-macabi
The availability attribute is a comma-separated list starting with the platform name and then including clauses specifying important milestones in the declaration’s lifetime (in any order) along with additional information. Those clauses can be:
- introduced=version
The first version in which this declaration was introduced.
- deprecated=version
The first version in which this declaration was deprecated, meaning that users should migrate away from this API.
- obsoleted=version
The first version in which this declaration was obsoleted, meaning that it was removed completely and can no longer be used.
- unavailable
This declaration is never available on this platform.
- message=string-literal
Additional message text that Clang will provide when emitting a warning or error about use of a deprecated or obsoleted declaration. Useful to direct users to replacement APIs.
- replacement=string-literal
Additional message text that Clang will use to provide Fix-It when emitting a warning about use of a deprecated declaration. The Fix-It will replace the deprecated declaration with the new declaration specified.
- environment=identifier
Target environment in which this declaration is available. If present, the availability attribute applies only to targets with the same platform and environment. The parameter is currently supported only in HLSL.
Multiple availability attributes can be placed on a declaration, which may
correspond to different platforms. For most platforms, the availability
attribute with the platform corresponding to the target platform will be used;
any others will be ignored. However, the availability for watchOS
and
tvOS
can be implicitly inferred from an iOS
availability attribute.
Any explicit availability attributes for those platforms are still preferred over
the implicitly inferred availability attributes. If no availability attribute
specifies availability for the current target platform, the availability
attributes are ignored. Supported platforms are:
iOS
macOS
tvOS
watchOS
iOSApplicationExtension
macOSApplicationExtension
tvOSApplicationExtension
watchOSApplicationExtension
macCatalyst
macCatalystApplicationExtension
visionOS
visionOSApplicationExtension
driverkit
swift
android
fuchsia
ohos
zos
ShaderModel
Some platforms have alias names:
ios
macos
macosx (deprecated)
tvos
watchos
ios_app_extension
macos_app_extension
macosx_app_extension (deprecated)
tvos_app_extension
watchos_app_extension
maccatalyst
maccatalyst_app_extension
visionos
visionos_app_extension
shadermodel
Supported environment names for the ShaderModel platform:
pixel
vertex
geometry
hull
domain
compute
raygeneration
intersection
anyhit
closesthit
miss
callable
mesh
amplification
library
A declaration can typically be used even when deploying back to a platform
version prior to when the declaration was introduced. When this happens, the
declaration is weakly linked,
as if the weak_import
attribute were added to the declaration. A
weakly-linked declaration may or may not be present a run-time, and a program
can determine whether the declaration is present by checking whether the
address of that declaration is non-NULL.
The flag strict
disallows using API when deploying back to a
platform version prior to when the declaration was introduced. An
attempt to use such API before its introduction causes a hard error.
Weakly-linking is almost always a better API choice, since it allows
users to query availability at runtime.
If there are multiple declarations of the same entity, the availability attributes must either match on a per-platform basis or later declarations must not have availability attributes for that platform. For example:
void g(void) __attribute__((availability(macos,introduced=10.4)));
void g(void) __attribute__((availability(macos,introduced=10.4))); // okay, matches
void g(void) __attribute__((availability(ios,introduced=4.0))); // okay, adds a new platform
void g(void); // okay, inherits both macos and ios availability from above.
void g(void) __attribute__((availability(macos,introduced=10.5))); // error: mismatch
When one method overrides another, the overriding method can be more widely available than the overridden method, e.g.,:
@interface A
- (id)method __attribute__((availability(macos,introduced=10.4)));
- (id)method2 __attribute__((availability(macos,introduced=10.4)));
@end
@interface B : A
- (id)method __attribute__((availability(macos,introduced=10.3))); // okay: method moved into base class later
- (id)method __attribute__((availability(macos,introduced=10.5))); // error: this method was available via the base class in 10.4
@end
Starting with the macOS 10.12 SDK, the API_AVAILABLE
macro from
<os/availability.h>
can simplify the spelling:
@interface A
- (id)method API_AVAILABLE(macos(10.11)));
- (id)otherMethod API_AVAILABLE(macos(10.11), ios(11.0));
@end
Availability attributes can also be applied using a #pragma clang attribute
.
Any explicit availability attribute whose platform corresponds to the target
platform is applied to a declaration regardless of the availability attributes
specified in the pragma. For example, in the code below,
hasExplicitAvailabilityAttribute
will use the macOS
availability
attribute that is specified with the declaration, whereas
getsThePragmaAvailabilityAttribute
will use the macOS
availability
attribute that is applied by the pragma.
#pragma clang attribute push (__attribute__((availability(macOS, introduced=10.12))), apply_to=function)
void getsThePragmaAvailabilityAttribute(void);
void hasExplicitAvailabilityAttribute(void) __attribute__((availability(macos,introduced=10.4)));
#pragma clang attribute pop
For platforms like watchOS
and tvOS
, whose availability attributes can
be implicitly inferred from an iOS
availability attribute, the logic is
slightly more complex. The explicit and the pragma-applied availability
attributes whose platform corresponds to the target platform are applied as
described in the previous paragraph. However, the implicitly inferred attributes
are applied to a declaration only when there is no explicit or pragma-applied
availability attribute whose platform corresponds to the target platform. For
example, the function below will receive the tvOS
availability from the
pragma rather than using the inferred iOS
availability from the declaration:
#pragma clang attribute push (__attribute__((availability(tvOS, introduced=12.0))), apply_to=function)
void getsThePragmaTVOSAvailabilityAttribute(void) __attribute__((availability(iOS,introduced=11.0)));
#pragma clang attribute pop
The compiler is also able to apply implicitly inferred attributes from a pragma
as well. For example, when targeting tvOS
, the function below will receive
a tvOS
availability attribute that is implicitly inferred from the iOS
availability attribute applied by the pragma:
#pragma clang attribute push (__attribute__((availability(iOS, introduced=12.0))), apply_to=function)
void infersTVOSAvailabilityFromPragma(void);
#pragma clang attribute pop
The implicit attributes that are inferred from explicitly specified attributes
whose platform corresponds to the target platform are applied to the declaration
even if there is an availability attribute that can be inferred from a pragma.
For example, the function below will receive the tvOS, introduced=11.0
availability that is inferred from the attribute on the declaration rather than
inferring availability from the pragma:
#pragma clang attribute push (__attribute__((availability(iOS, unavailable))), apply_to=function)
void infersTVOSAvailabilityFromAttributeNextToDeclaration(void)
__attribute__((availability(iOS,introduced=11.0)));
#pragma clang attribute pop
Also see the documentation for @available
btf_decl_tag¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the __attribute__((btf_decl_tag("ARGUMENT")))
attribute for
all targets. This attribute may be attached to a struct/union, struct/union
field, function, function parameter, variable or typedef declaration. If -g is
specified, the ARGUMENT
info will be preserved in IR and be emitted to
dwarf. For BPF targets, the ARGUMENT
info will be emitted to .BTF ELF
section too.
callback¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The callback
attribute specifies that the annotated function may invoke the
specified callback zero or more times. The callback, as well as the passed
arguments, are identified by their parameter name or position (starting with
1!) in the annotated function. The first position in the attribute identifies
the callback callee, the following positions declare describe its arguments.
The callback callee is required to be callable with the number, and order, of
the specified arguments. The index 0
, or the identifier this
, is used to
represent an implicit “this” pointer in class methods. If there is no implicit
“this” pointer it shall not be referenced. The index ‘-1’, or the name “__”,
represents an unknown callback callee argument. This can be a value which is
not present in the declared parameter list, or one that is, but is potentially
inspected, captured, or modified. Parameter names and indices can be mixed in
the callback attribute.
The callback
attribute, which is directly translated to callback
metadata <http://llvm.org/docs/LangRef.html#callback-metadata>, make the
connection between the call to the annotated function and the callback callee.
This can enable interprocedural optimizations which were otherwise impossible.
If a function parameter is mentioned in the callback
attribute, through its
position, it is undefined if that parameter is used for anything other than the
actual callback. Inspected, captured, or modified parameters shall not be
listed in the callback
metadata.
Example encodings for the callback performed by pthread_create
are shown
below. The explicit attribute annotation indicates that the third parameter
(start_routine
) is called zero or more times by the pthread_create
function,
and that the fourth parameter (arg
) is passed along. Note that the callback
behavior of pthread_create
is automatically recognized by Clang. In addition,
the declarations of __kmpc_fork_teams
and __kmpc_fork_call
, generated for
#pragma omp target teams
and #pragma omp parallel
, respectively, are also
automatically recognized as broker functions. Further functions might be added
in the future.
__attribute__((callback (start_routine, arg))) int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); __attribute__((callback (3, 4))) int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
carries_dependency¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
The carries_dependency
attribute specifies dependency propagation into and
out of functions.
When specified on a function or Objective-C method, the carries_dependency
attribute means that the return value carries a dependency out of the function,
so that the implementation need not constrain ordering upon return from that
function. Implementations of the function and its caller may choose to preserve
dependencies instead of emitting memory ordering instructions such as fences.
Note, this attribute does not change the meaning of the program, but may result in generation of more efficient code.
cf_consumed¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
cf_returns_not_retained¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
cf_returns_retained¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
cfi_canonical_jump_table¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Use __attribute__((cfi_canonical_jump_table))
on a function declaration to
make the function’s CFI jump table canonical. See the CFI documentation for more details.
clang::builtin_alias, clang_builtin_alias¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
This attribute is used in the implementation of the C intrinsics.
It allows the C intrinsic functions to be declared using the names defined
in target builtins, and still be recognized as clang builtins equivalent to the
underlying name. For example, riscv_vector.h
declares the function vadd
with __attribute__((clang_builtin_alias(__builtin_rvv_vadd_vv_i8m1)))
.
This ensures that both functions are recognized as that clang builtin,
and in the latter case, the choice of which builtin to identify the
function as can be deferred until after overload resolution.
This attribute can only be used to set up the aliases for certain ARM/RISC-V
C intrinsic functions; it is intended for use only inside arm_*.h
and
riscv_*.h
and is not a general mechanism for declaring arbitrary aliases
for clang builtin functions.
clang_arm_builtin_alias¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
This attribute is used in the implementation of the ACLE intrinsics.
It allows the intrinsic functions to
be declared using the names defined in ACLE, and still be recognized
as clang builtins equivalent to the underlying name. For example,
arm_mve.h
declares the function vaddq_u32
with
__attribute__((__clang_arm_mve_alias(__builtin_arm_mve_vaddq_u32)))
,
and similarly, one of the type-overloaded declarations of vaddq
will have the same attribute. This ensures that both functions are
recognized as that clang builtin, and in the latter case, the choice
of which builtin to identify the function as can be deferred until
after overload resolution.
This attribute can only be used to set up the aliases for certain Arm
intrinsic functions; it is intended for use only inside arm_*.h
and is not a general mechanism for declaring arbitrary aliases for
clang builtin functions.
In order to avoid duplicating the attribute definitions for similar purpose for other architecture, there is a general form for the attribute clang_builtin_alias.
clspv_libclc_builtin¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Attribute used by clspv (OpenCL-C to Vulkan SPIR-V compiler) to identify functions coming from libclc (OpenCL-C builtin library).
void __attribute__((clspv_libclc_builtin)) libclc_builtin() {}
cmse_nonsecure_entry¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
This attribute declares a function that can be called from non-secure state, or from secure state. Entering from and returning to non-secure state would switch to and from secure state, respectively, and prevent flow of information to non-secure state, except via return values. See ARMv8-M Security Extensions: Requirements on Development Tools - Engineering Specification Documentation for more information.
code_seg¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The __declspec(code_seg)
attribute enables the placement of code into separate
named segments that can be paged or locked in memory individually. This attribute
is used to control the placement of instantiated templates and compiler-generated
code. See the documentation for __declspec(code_seg) on MSDN.
cold¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
__attribute__((cold))
marks a function as cold, as a manual alternative to PGO hotness data.
If PGO data is available, the profile count based hotness overrides the __attribute__((cold))
annotation (unlike __attribute__((hot))
).
constructor¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The constructor
attribute causes the function to be called before entering
main()
, and the destructor
attribute causes the function to be called
after returning from main()
or when the exit()
function has been
called. Note, quick_exit()
, _Exit()
, and abort()
prevent a function
marked destructor
from being called.
The constructor or destructor function should not accept any arguments and its
return type should be void
.
The attributes accept an optional argument used to specify the priority order in which to execute constructor and destructor functions. The priority is given as an integer constant expression between 101 and 65535 (inclusive). Priorities outside of that range are reserved for use by the implementation. A lower value indicates a higher priority of initialization. Note that only the relative ordering of values is important. For example:
__attribute__((constructor(200))) void foo(void);
__attribute__((constructor(101))) void bar(void);
bar()
will be called before foo()
, and both will be called before
main()
. If no argument is given to the constructor
or destructor
attribute, they default to the value 65535
.
convergent¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The convergent
attribute can be placed on a function declaration. It is
translated into the LLVM convergent
attribute, which indicates that the call
instructions of a function with this attribute cannot be made control-dependent
on any additional values.
In languages designed for SPMD/SIMT programming model, e.g. OpenCL or CUDA, the call instructions of a function with this attribute must be executed by all work items or threads in a work group or sub group.
This attribute is different from noduplicate
because it allows duplicating
function calls if it can be proved that the duplicated function calls are
not made control-dependent on any additional values, e.g., unrolling a loop
executed by all work items.
Sample usage:
void convfunc(void) __attribute__((convergent));
// Setting it as a C++11 attribute is also valid in a C++ program.
// void convfunc(void) [[clang::convergent]];
cpu_dispatch¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
Yes |
The cpu_specific
and cpu_dispatch
attributes are used to define and
resolve multiversioned functions. This form of multiversioning provides a
mechanism for declaring versions across translation units and manually
specifying the resolved function list. A specified CPU defines a set of minimum
features that are required for the function to be called. The result of this is
that future processors execute the most restrictive version of the function the
new processor can execute.
In addition, unlike the ICC implementation of this feature, the selection of the version does not consider the manufacturer or microarchitecture of the processor. It tests solely the list of features that are both supported by the specified processor and present in the compiler-rt library. This can be surprising at times, as the runtime processor may be from a completely different manufacturer, as long as it supports the same feature set.
This can additionally be surprising, as some processors are indistringuishable from others based on the list of testable features. When this happens, the variant is selected in an unspecified manner.
Function versions are defined with cpu_specific
, which takes one or more CPU
names as a parameter. For example:
// Declares and defines the ivybridge version of single_cpu.
__attribute__((cpu_specific(ivybridge)))
void single_cpu(void){}
// Declares and defines the atom version of single_cpu.
__attribute__((cpu_specific(atom)))
void single_cpu(void){}
// Declares and defines both the ivybridge and atom version of multi_cpu.
__attribute__((cpu_specific(ivybridge, atom)))
void multi_cpu(void){}
A dispatching (or resolving) function can be declared anywhere in a project’s
source code with cpu_dispatch
. This attribute takes one or more CPU names
as a parameter (like cpu_specific
). Functions marked with cpu_dispatch
are not expected to be defined, only declared. If such a marked function has a
definition, any side effects of the function are ignored; trivial function
bodies are permissible for ICC compatibility.
// Creates a resolver for single_cpu above.
__attribute__((cpu_dispatch(ivybridge, atom)))
void single_cpu(void){}
// Creates a resolver for multi_cpu, but adds a 3rd version defined in another
// translation unit.
__attribute__((cpu_dispatch(ivybridge, atom, sandybridge)))
void multi_cpu(void){}
Note that it is possible to have a resolving function that dispatches based on more or fewer options than are present in the program. Specifying fewer will result in the omitted options not being considered during resolution. Specifying a version for resolution that isn’t defined in the program will result in a linking failure.
It is also possible to specify a CPU name of generic
which will be resolved
if the executing processor doesn’t satisfy the features required in the CPU
name. The behavior of a program executing on a processor that doesn’t satisfy
any option of a multiversioned function is undefined.
cpu_specific¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
Yes |
The cpu_specific
and cpu_dispatch
attributes are used to define and
resolve multiversioned functions. This form of multiversioning provides a
mechanism for declaring versions across translation units and manually
specifying the resolved function list. A specified CPU defines a set of minimum
features that are required for the function to be called. The result of this is
that future processors execute the most restrictive version of the function the
new processor can execute.
In addition, unlike the ICC implementation of this feature, the selection of the version does not consider the manufacturer or microarchitecture of the processor. It tests solely the list of features that are both supported by the specified processor and present in the compiler-rt library. This can be surprising at times, as the runtime processor may be from a completely different manufacturer, as long as it supports the same feature set.
This can additionally be surprising, as some processors are indistringuishable from others based on the list of testable features. When this happens, the variant is selected in an unspecified manner.
Function versions are defined with cpu_specific
, which takes one or more CPU
names as a parameter. For example:
// Declares and defines the ivybridge version of single_cpu.
__attribute__((cpu_specific(ivybridge)))
void single_cpu(void){}
// Declares and defines the atom version of single_cpu.
__attribute__((cpu_specific(atom)))
void single_cpu(void){}
// Declares and defines both the ivybridge and atom version of multi_cpu.
__attribute__((cpu_specific(ivybridge, atom)))
void multi_cpu(void){}
A dispatching (or resolving) function can be declared anywhere in a project’s
source code with cpu_dispatch
. This attribute takes one or more CPU names
as a parameter (like cpu_specific
). Functions marked with cpu_dispatch
are not expected to be defined, only declared. If such a marked function has a
definition, any side effects of the function are ignored; trivial function
bodies are permissible for ICC compatibility.
// Creates a resolver for single_cpu above.
__attribute__((cpu_dispatch(ivybridge, atom)))
void single_cpu(void){}
// Creates a resolver for multi_cpu, but adds a 3rd version defined in another
// translation unit.
__attribute__((cpu_dispatch(ivybridge, atom, sandybridge)))
void multi_cpu(void){}
Note that it is possible to have a resolving function that dispatches based on more or fewer options than are present in the program. Specifying fewer will result in the omitted options not being considered during resolution. Specifying a version for resolution that isn’t defined in the program will result in a linking failure.
It is also possible to specify a CPU name of generic
which will be resolved
if the executing processor doesn’t satisfy the features required in the CPU
name. The behavior of a program executing on a processor that doesn’t satisfy
any option of a multiversioned function is undefined.
destructor¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The constructor
attribute causes the function to be called before entering
main()
, and the destructor
attribute causes the function to be called
after returning from main()
or when the exit()
function has been
called. Note, quick_exit()
, _Exit()
, and abort()
prevent a function
marked destructor
from being called.
The constructor or destructor function should not accept any arguments and its
return type should be void
.
The attributes accept an optional argument used to specify the priority order in which to execute constructor and destructor functions. The priority is given as an integer constant expression between 101 and 65535 (inclusive). Priorities outside of that range are reserved for use by the implementation. A lower value indicates a higher priority of initialization. Note that only the relative ordering of values is important. For example:
__attribute__((constructor(200))) void foo(void);
__attribute__((constructor(101))) void bar(void);
bar()
will be called before foo()
, and both will be called before
main()
. If no argument is given to the constructor
or destructor
attribute, they default to the value 65535
.
diagnose_as_builtin¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The diagnose_as_builtin
attribute indicates that Fortify diagnostics are to
be applied to the declared function as if it were the function specified by the
attribute. The builtin function whose diagnostics are to be mimicked should be
given. In addition, the order in which arguments should be applied must also
be given.
For example, the attribute can be used as follows.
__attribute__((diagnose_as_builtin(__builtin_memset, 3, 2, 1)))
void *mymemset(int n, int c, void *s) {
// ...
}
This indicates that calls to mymemset
should be diagnosed as if they were
calls to __builtin_memset
. The arguments 3, 2, 1
indicate by index the
order in which arguments of mymemset
should be applied to
__builtin_memset
. The third argument should be applied first, then the
second, and then the first. Thus (when Fortify warnings are enabled) the call
mymemset(n, c, s)
will diagnose overflows as if it were the call
__builtin_memset(s, c, n)
.
For variadic functions, the variadic arguments must come in the same order as they would to the builtin function, after all normal arguments. For instance, to diagnose a new function as if it were sscanf, we can use the attribute as follows.
__attribute__((diagnose_as_builtin(sscanf, 1, 2)))
int mysscanf(const char *str, const char *format, ...) {
// ...
}
Then the call mysscanf(“abc def”, “%4s %4s”, buf1, buf2) will be diagnosed as if it were the call sscanf(“abc def”, “%4s %4s”, buf1, buf2).
This attribute cannot be applied to non-static member functions.
diagnose_if¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The diagnose_if
attribute can be placed on function declarations to emit
warnings or errors at compile-time if calls to the attributed function meet
certain user-defined criteria. For example:
int abs(int a)
__attribute__((diagnose_if(a >= 0, "Redundant abs call", "warning")));
int must_abs(int a)
__attribute__((diagnose_if(a >= 0, "Redundant abs call", "error")));
int val = abs(1); // warning: Redundant abs call
int val2 = must_abs(1); // error: Redundant abs call
int val3 = abs(val);
int val4 = must_abs(val); // Because run-time checks are not emitted for
// diagnose_if attributes, this executes without
// issue.
diagnose_if
is closely related to enable_if
, with a few key differences:
Overload resolution is not aware of
diagnose_if
attributes: they’re considered only after we select the best candidate from a given candidate set.Function declarations that differ only in their
diagnose_if
attributes are considered to be redeclarations of the same function (not overloads).If the condition provided to
diagnose_if
cannot be evaluated, no diagnostic will be emitted.
Otherwise, diagnose_if
is essentially the logical negation of enable_if
.
As a result of bullet number two, diagnose_if
attributes will stack on the
same function. For example:
int foo() __attribute__((diagnose_if(1, "diag1", "warning")));
int foo() __attribute__((diagnose_if(1, "diag2", "warning")));
int bar = foo(); // warning: diag1
// warning: diag2
int (*fooptr)(void) = foo; // warning: diag1
// warning: diag2
constexpr int supportsAPILevel(int N) { return N < 5; }
int baz(int a)
__attribute__((diagnose_if(!supportsAPILevel(10),
"Upgrade to API level 10 to use baz", "error")));
int baz(int a)
__attribute__((diagnose_if(!a, "0 is not recommended.", "warning")));
int (*bazptr)(int) = baz; // error: Upgrade to API level 10 to use baz
int v = baz(0); // error: Upgrade to API level 10 to use baz
Query for this feature with __has_attribute(diagnose_if)
.
disable_sanitizer_instrumentation¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Use the disable_sanitizer_instrumentation
attribute on a function,
Objective-C method, or global variable, to specify that no sanitizer
instrumentation should be applied.
This is not the same as __attribute__((no_sanitize(...)))
, which depending
on the tool may still insert instrumentation to prevent false positive reports.
disable_tail_calls¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The disable_tail_calls
attribute instructs the backend to not perform tail
call optimization inside the marked function.
For example:
int callee(int); int foo(int a) __attribute__((disable_tail_calls)) { return callee(a); // This call is not tail-call optimized. }
Marking virtual functions as disable_tail_calls
is legal.
int callee(int); class Base { public: [[clang::disable_tail_calls]] virtual int foo1() { return callee(); // This call is not tail-call optimized. } }; class Derived1 : public Base { public: int foo1() override { return callee(); // This call is tail-call optimized. } };
enable_if¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Yes |
Note
Some features of this attribute are experimental. The meaning of multiple enable_if attributes on a single declaration is subject to change in a future version of clang. Also, the ABI is not standardized and the name mangling may change in future versions. To avoid that, use asm labels.
The enable_if
attribute can be placed on function declarations to control
which overload is selected based on the values of the function’s arguments.
When combined with the overloadable
attribute, this feature is also
available in C.
int isdigit(int c);
int isdigit(int c) __attribute__((enable_if(c <= -1 || c > 255, "chosen when 'c' is out of range"))) __attribute__((unavailable("'c' must have the value of an unsigned char or EOF")));
void foo(char c) {
isdigit(c);
isdigit(10);
isdigit(-10); // results in a compile-time error.
}
The enable_if attribute takes two arguments, the first is an expression written in terms of the function parameters, the second is a string explaining why this overload candidate could not be selected to be displayed in diagnostics. The expression is part of the function signature for the purposes of determining whether it is a redeclaration (following the rules used when determining whether a C++ template specialization is ODR-equivalent), but is not part of the type.
The enable_if expression is evaluated as if it were the body of a bool-returning constexpr function declared with the arguments of the function it is being applied to, then called with the parameters at the call site. If the result is false or could not be determined through constant expression evaluation, then this overload will not be chosen and the provided string may be used in a diagnostic if the compile fails as a result.
Because the enable_if expression is an unevaluated context, there are no global state changes, nor the ability to pass information from the enable_if expression to the function body. For example, suppose we want calls to strnlen(strbuf, maxlen) to resolve to strnlen_chk(strbuf, maxlen, size of strbuf) only if the size of strbuf can be determined:
__attribute__((always_inline))
static inline size_t strnlen(const char *s, size_t maxlen)
__attribute__((overloadable))
__attribute__((enable_if(__builtin_object_size(s, 0) != -1))),
"chosen when the buffer size is known but 'maxlen' is not")))
{
return strnlen_chk(s, maxlen, __builtin_object_size(s, 0));
}
Multiple enable_if attributes may be applied to a single declaration. In this case, the enable_if expressions are evaluated from left to right in the following manner. First, the candidates whose enable_if expressions evaluate to false or cannot be evaluated are discarded. If the remaining candidates do not share ODR-equivalent enable_if expressions, the overload resolution is ambiguous. Otherwise, enable_if overload resolution continues with the next enable_if attribute on the candidates that have not been discarded and have remaining enable_if attributes. In this way, we pick the most specific overload out of a number of viable overloads using enable_if.
void f() __attribute__((enable_if(true, ""))); // #1
void f() __attribute__((enable_if(true, ""))) __attribute__((enable_if(true, ""))); // #2
void g(int i, int j) __attribute__((enable_if(i, ""))); // #1
void g(int i, int j) __attribute__((enable_if(j, ""))) __attribute__((enable_if(true))); // #2
In this example, a call to f() is always resolved to #2, as the first enable_if expression is ODR-equivalent for both declarations, but #1 does not have another enable_if expression to continue evaluating, so the next round of evaluation has only a single candidate. In a call to g(1, 1), the call is ambiguous even though #2 has more enable_if attributes, because the first enable_if expressions are not ODR-equivalent.
Query for this feature with __has_attribute(enable_if)
.
Note that functions with one or more enable_if
attributes may not have
their address taken, unless all of the conditions specified by said
enable_if
are constants that evaluate to true
. For example:
const int TrueConstant = 1;
const int FalseConstant = 0;
int f(int a) __attribute__((enable_if(a > 0, "")));
int g(int a) __attribute__((enable_if(a == 0 || a != 0, "")));
int h(int a) __attribute__((enable_if(1, "")));
int i(int a) __attribute__((enable_if(TrueConstant, "")));
int j(int a) __attribute__((enable_if(FalseConstant, "")));
void fn() {
int (*ptr)(int);
ptr = &f; // error: 'a > 0' is not always true
ptr = &g; // error: 'a == 0 || a != 0' is not a truthy constant
ptr = &h; // OK: 1 is a truthy constant
ptr = &i; // OK: 'TrueConstant' is a truthy constant
ptr = &j; // error: 'FalseConstant' is a constant, but not truthy
}
Because enable_if
evaluation happens during overload resolution,
enable_if
may give unintuitive results when used with templates, depending
on when overloads are resolved. In the example below, clang will emit a
diagnostic about no viable overloads for foo
in bar
, but not in baz
:
double foo(int i) __attribute__((enable_if(i > 0, "")));
void *foo(int i) __attribute__((enable_if(i <= 0, "")));
template <int I>
auto bar() { return foo(I); }
template <typename T>
auto baz() { return foo(T::number); }
struct WithNumber { constexpr static int number = 1; };
void callThem() {
bar<sizeof(WithNumber)>();
baz<WithNumber>();
}
This is because, in bar
, foo
is resolved prior to template
instantiation, so the value for I
isn’t known (thus, both enable_if
conditions for foo
fail). However, in baz
, foo
is resolved during
template instantiation, so the value for T::number
is known.
enforce_tcb¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
- The
enforce_tcb
attribute can be placed on functions to enforce that a trusted compute base (TCB) does not call out of the TCB. This generates a warning every time a function not marked with an
enforce_tcb
attribute is called from a function with theenforce_tcb
attribute. A function may be a part of multiple TCBs. Invocations through function pointers are currently not checked. Builtins are considered to a part of every TCB.enforce_tcb(Name)
indicates that this function is a part of the TCB namedName
enforce_tcb_leaf¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
- The
enforce_tcb_leaf
attribute satisfies the requirement enforced by enforce_tcb
for the marked function to be in the named TCB but does not continue to check the functions called from within the leaf function.enforce_tcb_leaf(Name)
indicates that this function is a part of the TCB namedName
error, warning¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The error
and warning
function attributes can be used to specify a
custom diagnostic to be emitted when a call to such a function is not
eliminated via optimizations. This can be used to create compile time
assertions that depend on optimizations, while providing diagnostics
pointing to precise locations of the call site in the source.
__attribute__((warning("oh no"))) void dontcall();
void foo() {
if (someCompileTimeAssertionThatsTrue)
dontcall(); // Warning
dontcall(); // Warning
if (someCompileTimeAssertionThatsFalse)
dontcall(); // No Warning
sizeof(dontcall()); // No Warning
}
exclude_from_explicit_instantiation¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The exclude_from_explicit_instantiation
attribute opts-out a member of a
class template from being part of explicit template instantiations of that
class template. This means that an explicit instantiation will not instantiate
members of the class template marked with the attribute, but also that code
where an extern template declaration of the enclosing class template is visible
will not take for granted that an external instantiation of the class template
would provide those members (which would otherwise be a link error, since the
explicit instantiation won’t provide those members). For example, let’s say we
don’t want the data()
method to be part of libc++’s ABI. To make sure it
is not exported from the dylib, we give it hidden visibility:
// in <string> template <class CharT> class basic_string { public: __attribute__((__visibility__("hidden"))) const value_type* data() const noexcept { ... } }; template class basic_string<char>;
Since an explicit template instantiation declaration for basic_string<char>
is provided, the compiler is free to assume that basic_string<char>::data()
will be provided by another translation unit, and it is free to produce an
external call to this function. However, since data()
has hidden visibility
and the explicit template instantiation is provided in a shared library (as
opposed to simply another translation unit), basic_string<char>::data()
won’t be found and a link error will ensue. This happens because the compiler
assumes that basic_string<char>::data()
is part of the explicit template
instantiation declaration, when it really isn’t. To tell the compiler that
data()
is not part of the explicit template instantiation declaration, the
exclude_from_explicit_instantiation
attribute can be used:
// in <string> template <class CharT> class basic_string { public: __attribute__((__visibility__("hidden"))) __attribute__((exclude_from_explicit_instantiation)) const value_type* data() const noexcept { ... } }; template class basic_string<char>;
Now, the compiler won’t assume that basic_string<char>::data()
is provided
externally despite there being an explicit template instantiation declaration:
the compiler will implicitly instantiate basic_string<char>::data()
in the
TUs where it is used.
This attribute can be used on static and non-static member functions of class templates, static data members of class templates and member classes of class templates.
export_name¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the __attribute__((export_name(<name>)))
attribute for the WebAssembly target. This attribute may be attached to a
function declaration, where it modifies how the symbol is to be exported
from the linked WebAssembly.
WebAssembly functions are exported via string name. By default when a symbol is exported, the export name for C/C++ symbols are the same as their C/C++ symbol names. This attribute can be used to override the default behavior, and request a specific string name be used instead.
flatten¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The flatten
attribute causes calls within the attributed function to
be inlined unless it is impossible to do so, for example if the body of the
callee is unavailable or if the callee has the noinline
attribute.
force_align_arg_pointer¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Use this attribute to force stack alignment.
Legacy x86 code uses 4-byte stack alignment. Newer aligned SSE instructions (like ‘movaps’) that work with the stack require operands to be 16-byte aligned. This attribute realigns the stack in the function prologue to make sure the stack can be used with SSE instructions.
Note that the x86_64 ABI forces 16-byte stack alignment at the call site. Because of this, ‘force_align_arg_pointer’ is not needed on x86_64, except in rare cases where the caller does not align the stack properly (e.g. flow jumps from i386 arch code).
__attribute__ ((force_align_arg_pointer)) void f () { ... }
format¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Clang supports the format
attribute, which indicates that the function
accepts (among other possibilities) a printf
or scanf
-like format string
and corresponding arguments or a va_list
that contains these arguments.
Please see GCC documentation about format attribute to find details about attribute syntax.
Clang implements two kinds of checks with this attribute.
Clang checks that the function with the
format
attribute is called with a format string that uses format specifiers that are allowed, and that arguments match the format string. This is the-Wformat
warning, it is on by default.Clang checks that the format string argument is a literal string. This is the
-Wformat-nonliteral
warning, it is off by default.Clang implements this mostly the same way as GCC, but there is a difference for functions that accept a
va_list
argument (for example,vprintf
). GCC does not emit-Wformat-nonliteral
warning for calls to such functions. Clang does not warn if the format string comes from a function parameter, where the function is annotated with a compatible attribute, otherwise it warns. For example:__attribute__((__format__ (__scanf__, 1, 3))) void foo(const char* s, char *buf, ...) { va_list ap; va_start(ap, buf); vprintf(s, ap); // warning: format string is not a string literal }
In this case we warn because
s
contains a format string for ascanf
-like function, but it is passed to aprintf
-like function.If the attribute is removed, clang still warns, because the format string is not a string literal.
Another example:
__attribute__((__format__ (__printf__, 1, 3))) void foo(const char* s, char *buf, ...) { va_list ap; va_start(ap, buf); vprintf(s, ap); // warning }
In this case Clang does not warn because the format string
s
and the corresponding arguments are annotated. If the arguments are incorrect, the caller offoo
will receive a warning.
As an extension to GCC’s behavior, Clang accepts the format
attribute on
non-variadic functions. Clang checks non-variadic format functions for the same
classes of issues that can be found on variadic functions, as controlled by the
same warning flags, except that the types of formatted arguments is forced by
the function signature. For example:
__attribute__((__format__(__printf__, 1, 2)))
void fmt(const char *s, const char *a, int b);
void bar(void) {
fmt("%s %i", "hello", 123); // OK
fmt("%i %g", "hello", 123); // warning: arguments don't match format
extern const char *fmt;
fmt(fmt, "hello", 123); // warning: format string is not a string literal
}
When using the format attribute on a variadic function, the first data parameter _must_ be the index of the ellipsis in the parameter list. Clang will generate a diagnostic otherwise, as it wouldn’t be possible to forward that argument list to printf-family functions. For instance, this is an error:
__attribute__((__format__(__printf__, 1, 2)))
void fmt(const char *s, int b, ...);
// ^ error: format attribute parameter 3 is out of bounds
// (must be __printf__, 1, 3)
Using the format
attribute on a non-variadic function emits a GCC
compatibility diagnostic.
function_return¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The attribute function_return
can replace return instructions with jumps to
target-specific symbols. This attribute supports 2 possible values,
corresponding to the values supported by the -mfunction-return=
command
line flag:
__attribute__((function_return("keep")))
to disable related transforms. This is useful for undoing global setting from-mfunction-return=
locally for individual functions.__attribute__((function_return("thunk-extern")))
to replace returns with jumps, while NOT emitting the thunk.
The values thunk
and thunk-inline
from GCC are not supported.
The symbol used for thunk-extern
is target specific:
* X86: __x86_return_thunk
As such, this function attribute is currently only supported on X86 targets.
gnu_inline¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The gnu_inline
changes the meaning of extern inline
to use GNU inline
semantics, meaning:
If any declaration that is declared
inline
is not declaredextern
, then theinline
keyword is just a hint. In particular, an out-of-line definition is still emitted for a function with external linkage, even if all call sites are inlined, unlike in C99 and C++ inline semantics.If all declarations that are declared
inline
are also declaredextern
, then the function body is present only for inlining and no out-of-line version is emitted.
Some important consequences: static inline
emits an out-of-line
version if needed, a plain inline
definition emits an out-of-line version
always, and an extern inline
definition (in a header) followed by a
(non-extern
) inline
declaration in a source file emits an out-of-line
version of the function in that source file but provides the function body for
inlining to all includers of the header.
Either __GNUC_GNU_INLINE__
(GNU inline semantics) or
__GNUC_STDC_INLINE__
(C99 semantics) will be defined (they are mutually
exclusive). If __GNUC_STDC_INLINE__
is defined, then the gnu_inline
function attribute can be used to get GNU inline semantics on a per function
basis. If __GNUC_GNU_INLINE__
is defined, then the translation unit is
already being compiled with GNU inline semantics as the implied default. It is
unspecified which macro is defined in a C++ compilation.
GNU inline semantics are the default behavior with -std=gnu89
,
-std=c89
, -std=c94
, or -fgnu89-inline
.
guard¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
Yes |
Code can indicate CFG checks are not wanted with the __declspec(guard(nocf))
attribute. This directs the compiler to not insert any CFG checks for the entire
function. This approach is typically used only sparingly in specific situations
where the programmer has manually inserted “CFG-equivalent” protection. The
programmer knows that they are calling through some read-only function table
whose address is obtained through read-only memory references and for which the
index is masked to the function table limit. This approach may also be applied
to small wrapper functions that are not inlined and that do nothing more than
make a call through a function pointer. Since incorrect usage of this directive
can compromise the security of CFG, the programmer must be very careful using
the directive. Typically, this usage is limited to very small functions that
only call one function.
Control Flow Guard documentation <https://docs.microsoft.com/en-us/windows/win32/secbp/pe-metadata>
hot¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
__attribute__((hot))
marks a function as hot, as a manual alternative to PGO hotness data.
If PGO data is available, the annotation __attribute__((hot))
overrides the profile count based hotness (unlike __attribute__((cold))
).
hybrid_patchable¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
Yes |
The hybrid_patchable
attribute declares an ARM64EC function with an additional
x86-64 thunk, which may be patched at runtime.
For more information see ARM64EC ABI documentation.
ifunc¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
__attribute__((ifunc("resolver")))
is used to mark that the address of a
declaration should be resolved at runtime by calling a resolver function.
The symbol name of the resolver function is given in quotes. A function with
this name (after mangling) must be defined in the current translation unit; it
may be static
. The resolver function should return a pointer.
The ifunc
attribute may only be used on a function declaration. A function
declaration with an ifunc
attribute is considered to be a definition of the
declared entity. The entity must not have weak linkage; for example, in C++,
it cannot be applied to a declaration if a definition at that location would be
considered inline.
Not all targets support this attribute. ELF target support depends on both the linker and runtime linker, and is available in at least lld 4.0 and later, binutils 2.20.1 and later, glibc v2.11.1 and later, and FreeBSD 9.1 and later. Mach-O targets support it, but with slightly different semantics: the resolver is run at first call, instead of at load time by the runtime linker. Targets other than ELF and Mach-O currently do not support this attribute.
import_module¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the __attribute__((import_module(<module_name>)))
attribute for the WebAssembly target. This attribute may be attached to a
function declaration, where it modifies how the symbol is to be imported
within the WebAssembly linking environment.
WebAssembly imports use a two-level namespace scheme, consisting of a module name, which typically identifies a module from which to import, and a field name, which typically identifies a field from that module to import. By default, module names for C/C++ symbols are assigned automatically by the linker. This attribute can be used to override the default behavior, and request a specific module name be used instead.
import_name¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the __attribute__((import_name(<name>)))
attribute for the WebAssembly target. This attribute may be attached to a
function declaration, where it modifies how the symbol is to be imported
within the WebAssembly linking environment.
WebAssembly imports use a two-level namespace scheme, consisting of a module name, which typically identifies a module from which to import, and a field name, which typically identifies a field from that module to import. By default, field names for C/C++ symbols are the same as their C/C++ symbol names. This attribute can be used to override the default behavior, and request a specific field name be used instead.
internal_linkage¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The internal_linkage
attribute changes the linkage type of the declaration
to internal. This is similar to C-style static
, but can be used on classes
and class methods. When applied to a class definition, this attribute affects
all methods and static data members of that class. This can be used to contain
the ABI of a C++ library by excluding unwanted class methods from the export
tables.
interrupt (ARM)¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Clang supports the GNU style __attribute__((interrupt("TYPE")))
attribute on
ARM targets. This attribute may be attached to a function definition and
instructs the backend to generate appropriate function entry/exit code so that
it can be used directly as an interrupt service routine.
The parameter passed to the interrupt attribute is optional, but if provided it must be a string literal with one of the following values: “IRQ”, “FIQ”, “SWI”, “ABORT”, “UNDEF”.
The semantics are as follows:
If the function is AAPCS, Clang instructs the backend to realign the stack to 8 bytes on entry. This is a general requirement of the AAPCS at public interfaces, but may not hold when an exception is taken. Doing this allows other AAPCS functions to be called.
If the CPU is M-class this is all that needs to be done since the architecture itself is designed in such a way that functions obeying the normal AAPCS ABI constraints are valid exception handlers.
If the CPU is not M-class, the prologue and epilogue are modified to save all non-banked registers that are used, so that upon return the user-mode state will not be corrupted. Note that to avoid unnecessary overhead, only general-purpose (integer) registers are saved in this way. If VFP operations are needed, that state must be saved manually.
Specifically, interrupt kinds other than “FIQ” will save all core registers except “lr” and “sp”. “FIQ” interrupts will save r0-r7.
If the CPU is not M-class, the return instruction is changed to one of the canonical sequences permitted by the architecture for exception return. Where possible the function itself will make the necessary “lr” adjustments so that the “preferred return address” is selected.
Unfortunately the compiler is unable to make this guarantee for an “UNDEF” handler, where the offset from “lr” to the preferred return address depends on the execution state of the code which generated the exception. In this case a sequence equivalent to “movs pc, lr” will be used.
interrupt (AVR)¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the GNU style __attribute__((interrupt))
attribute on
AVR targets. This attribute may be attached to a function definition and instructs
the backend to generate appropriate function entry/exit code so that it can be used
directly as an interrupt service routine.
On the AVR, the hardware globally disables interrupts when an interrupt is executed. The first instruction of an interrupt handler declared with this attribute is a SEI instruction to re-enable interrupts. See also the signal attribute that does not insert a SEI instruction.
interrupt (MIPS)¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the GNU style __attribute__((interrupt("ARGUMENT")))
attribute on
MIPS targets. This attribute may be attached to a function definition and instructs
the backend to generate appropriate function entry/exit code so that it can be used
directly as an interrupt service routine.
By default, the compiler will produce a function prologue and epilogue suitable for an interrupt service routine that handles an External Interrupt Controller (eic) generated interrupt. This behavior can be explicitly requested with the “eic” argument.
Otherwise, for use with vectored interrupt mode, the argument passed should be of the form “vector=LEVEL” where LEVEL is one of the following values: “sw0”, “sw1”, “hw0”, “hw1”, “hw2”, “hw3”, “hw4”, “hw5”. The compiler will then set the interrupt mask to the corresponding level which will mask all interrupts up to and including the argument.
The semantics are as follows:
The prologue is modified so that the Exception Program Counter (EPC) and Status coprocessor registers are saved to the stack. The interrupt mask is set so that the function can only be interrupted by a higher priority interrupt. The epilogue will restore the previous values of EPC and Status.
The prologue and epilogue are modified to save and restore all non-kernel registers as necessary.
The FPU is disabled in the prologue, as the floating pointer registers are not spilled to the stack.
The function return sequence is changed to use an exception return instruction.
The parameter sets the interrupt mask for the function corresponding to the interrupt level specified. If no mask is specified the interrupt mask defaults to “eic”.
interrupt (RISC-V)¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the GNU style __attribute__((interrupt))
attribute on RISCV
targets. This attribute may be attached to a function definition and instructs
the backend to generate appropriate function entry/exit code so that it can be
used directly as an interrupt service routine.
Permissible values for this parameter are user
, supervisor
,
and machine
. If there is no parameter, then it defaults to machine.
Repeated interrupt attribute on the same declaration will cause a warning to be emitted. In case of repeated declarations, the last one prevails.
Refer to: https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Function-Attributes.html https://riscv.org/specifications/privileged-isa/ The RISC-V Instruction Set Manual Volume II: Privileged Architecture Version 1.10.
interrupt (X86)¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Clang supports the GNU style __attribute__((interrupt))
attribute on X86
targets. This attribute may be attached to a function definition and instructs
the backend to generate appropriate function entry/exit code so that it can be
used directly as an interrupt service routine.
Interrupt handlers have access to the stack frame pushed onto the stack by the processor,
and return using the IRET
instruction. All registers in an interrupt handler are callee-saved.
Exception handlers also have access to the error code pushed onto the stack by the processor,
when applicable.
An interrupt handler must take the following arguments:
__attribute__ ((interrupt)) void f (struct stack_frame *frame) { ... }Where
struct stack_frame
is a suitable struct matching the stack frame pushed by the processor.
An exception handler must take the following arguments:
__attribute__ ((interrupt)) void g (struct stack_frame *frame, unsigned long code) { ... }On 32-bit targets, the
code
argument should be of typeunsigned int
.
Exception handlers should only be used when an error code is pushed by the processor. Using the incorrect handler type will crash the system.
Interrupt and exception handlers cannot be called by other functions and must have return type void
.
Interrupt and exception handlers should only call functions with the ‘no_caller_saved_registers’ attribute, or should be compiled with the ‘-mgeneral-regs-only’ flag to avoid saving unused non-GPR registers.
lifetimebound¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
The lifetimebound
attribute on a function parameter or implicit object
parameter indicates that objects that are referred to by that parameter may
also be referred to by the return value of the annotated function (or, for a
parameter of a constructor, by the value of the constructed object).
By default, a reference is considered to refer to its referenced object, a
pointer is considered to refer to its pointee, a std::initializer_list<T>
is considered to refer to its underlying array, and aggregates (arrays and
simple struct
s) are considered to refer to all objects that their
transitive subobjects refer to.
Clang warns if it is able to detect that an object or reference refers to
another object with a shorter lifetime. For example, Clang will warn if a
function returns a reference to a local variable, or if a reference is bound to
a temporary object whose lifetime is not extended. By using the
lifetimebound
attribute, this determination can be extended to look through
user-declared functions. For example:
#include <map>
#include <string>
using namespace std::literals;
// Returns m[key] if key is present, or default_value if not.
template<typename T, typename U>
const U &get_or_default(const std::map<T, U> &m [[clang::lifetimebound]],
const T &key, /* note, not lifetimebound */
const U &default_value [[clang::lifetimebound]]) {
if (auto iter = m.find(key); iter != m.end()) return iter->second;
else return default_value;
}
int main() {
std::map<std::string, std::string> m;
// warning: temporary bound to local reference 'val1' will be destroyed
// at the end of the full-expression
const std::string &val1 = get_or_default(m, "foo"s, "bar"s);
// No warning in this case.
std::string def_val = "bar"s;
const std::string &val2 = get_or_default(m, "foo"s, def_val);
return 0;
}
The attribute can be applied to the implicit this
parameter of a member
function by writing the attribute after the function type:
struct string {
// The returned pointer should not outlive ``*this``.
const char *data() const [[clang::lifetimebound]];
};
This attribute is inspired by the C++ committee paper P0936R0, but does not affect whether temporary objects have their lifetimes extended.
long_call, far¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the __attribute__((long_call))
, __attribute__((far))
,
and __attribute__((near))
attributes on MIPS targets. These attributes may
only be added to function declarations and change the code generated
by the compiler when directly calling the function. The near
attribute
allows calls to the function to be made using the jal
instruction, which
requires the function to be located in the same naturally aligned 256MB
segment as the caller. The long_call
and far
attributes are synonyms
and require the use of a different call sequence that works regardless
of the distance between the functions.
These attributes have no effect for position-independent code.
These attributes take priority over command line switches such
as -mlong-calls
and -mno-long-calls
.
malloc¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
Yes |
The malloc
attribute indicates that the function acts like a system memory
allocation function, returning a pointer to allocated storage disjoint from the
storage for any other object accessible to the caller.
micromips¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the GNU style __attribute__((micromips))
and
__attribute__((nomicromips))
attributes on MIPS targets. These attributes
may be attached to a function definition and instructs the backend to generate
or not to generate microMIPS code for that function.
These attributes override the -mmicromips
and -mno-micromips
options
on the command line.
mig_server_routine¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The Mach Interface Generator release-on-success convention dictates
functions that follow it to only release arguments passed to them when they
return “success” (a kern_return_t
error code that indicates that
no errors have occurred). Otherwise the release is performed by the MIG client
that called the function. The annotation __attribute__((mig_server_routine))
is applied in order to specify which functions are expected to follow the
convention. This allows the Static Analyzer to find bugs caused by violations of
that convention. The attribute would normally appear on the forward declaration
of the actual server routine in the MIG server header, but it may also be
added to arbitrary functions that need to follow the same convention - for
example, a user can add them to auxiliary functions called by the server routine
that have their return value of type kern_return_t
unconditionally returned
from the routine. The attribute can be applied to C++ methods, and in this case
it will be automatically applied to overrides if the method is virtual. The
attribute can also be written using C++11 syntax: [[mig::server_routine]]
.
min_vector_width¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the __attribute__((min_vector_width(width)))
attribute. This
attribute may be attached to a function and informs the backend that this
function desires vectors of at least this width to be generated. Target-specific
maximum vector widths still apply. This means even if you ask for something
larger than the target supports, you will only get what the target supports.
This attribute is meant to be a hint to control target heuristics that may
generate narrower vectors than what the target hardware supports.
This is currently used by the X86 target to allow some CPUs that support 512-bit
vectors to be limited to using 256-bit vectors to avoid frequency penalties.
This is currently enabled with the -prefer-vector-width=256
command line
option. The min_vector_width
attribute can be used to prevent the backend
from trying to split vector operations to match the prefer-vector-width
. All
X86 vector intrinsics from x86intrin.h already set this attribute. Additionally,
use of any of the X86-specific vector builtins will implicitly set this
attribute on the calling function. The intent is that explicitly writing vector
code using the X86 intrinsics will prevent prefer-vector-width
from
affecting the code.
minsize¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
This function attribute indicates that optimization passes and code generator passes make choices that keep the function code size as small as possible. Optimizations may also sacrifice runtime performance in order to minimize the size of the generated code.
no_builtin¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The __attribute__((no_builtin))
is similar to the -fno-builtin
flag
except it is specific to the body of a function. The attribute may also be
applied to a virtual function but has no effect on the behavior of overriding
functions in a derived class.
It accepts one or more strings corresponding to the specific names of the builtins to disable (e.g. “memcpy”, “memset”). If the attribute is used without parameters it will disable all buitins at once.
// The compiler is not allowed to add any builtin to foo's body.
void foo(char* data, size_t count) __attribute__((no_builtin)) {
// The compiler is not allowed to convert the loop into
// `__builtin_memset(data, 0xFE, count);`.
for (size_t i = 0; i < count; ++i)
data[i] = 0xFE;
}
// The compiler is not allowed to add the `memcpy` builtin to bar's body.
void bar(char* data, size_t count) __attribute__((no_builtin("memcpy"))) {
// The compiler is allowed to convert the loop into
// `__builtin_memset(data, 0xFE, count);` but cannot generate any
// `__builtin_memcpy`
for (size_t i = 0; i < count; ++i)
data[i] = 0xFE;
}
no_caller_saved_registers¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Use this attribute to indicate that the specified function has no caller-saved registers. That is, all registers are callee-saved except for registers used for passing parameters to the function or returning parameters from the function. The compiler saves and restores any modified registers that were not used for passing or returning arguments to the function.
The user can call functions specified with the ‘no_caller_saved_registers’ attribute from an interrupt handler without saving and restoring all call-clobbered registers.
Functions specified with the ‘no_caller_saved_registers’ attribute should only call other functions with the ‘no_caller_saved_registers’ attribute, or should be compiled with the ‘-mgeneral-regs-only’ flag to avoid saving unused non-GPR registers.
Note that ‘no_caller_saved_registers’ attribute is not a calling convention. In fact, it only overrides the decision of which registers should be saved by the caller, but not how the parameters are passed from the caller to the callee.
For example:
__attribute__ ((no_caller_saved_registers, fastcall)) void f (int arg1, int arg2) { ... }In this case parameters ‘arg1’ and ‘arg2’ will be passed in registers. In this case, on 32-bit x86 targets, the function ‘f’ will use ECX and EDX as register parameters. However, it will not assume any scratch registers and should save and restore any modified registers except for ECX and EDX.
no_profile_instrument_function¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Use the no_profile_instrument_function
attribute on a function declaration
to denote that the compiler should not instrument the function with
profile-related instrumentation, such as via the
-fprofile-generate
/ -fprofile-instr-generate
/
-fcs-profile-generate
/ -fprofile-arcs
flags.
no_sanitize¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Use the no_sanitize
attribute on a function or a global variable
declaration to specify that a particular instrumentation or set of
instrumentations should not be applied.
The attribute takes a list of string literals with the following accepted
values:
* all values accepted by -fno-sanitize=
;
* coverage
, to disable SanitizerCoverage instrumentation.
For example, __attribute__((no_sanitize("address", "thread")))
specifies
that AddressSanitizer and ThreadSanitizer should not be applied to the function
or variable. Using __attribute__((no_sanitize("coverage")))
specifies that
SanitizerCoverage should not be applied to the function.
See Controlling Code Generation for a full list of supported sanitizer flags.
no_sanitize_address, no_address_safety_analysis¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Use __attribute__((no_sanitize_address))
on a function or a global
variable declaration to specify that address safety instrumentation
(e.g. AddressSanitizer) should not be applied.
no_sanitize_memory¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Use __attribute__((no_sanitize_memory))
on a function declaration to
specify that checks for uninitialized memory should not be inserted
(e.g. by MemorySanitizer). The function may still be instrumented by the tool
to avoid false positives in other places.
no_sanitize_thread¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Use __attribute__((no_sanitize_thread))
on a function declaration to
specify that checks for data races on plain (non-atomic) memory accesses should
not be inserted by ThreadSanitizer. The function is still instrumented by the
tool to avoid false positives and provide meaningful stack traces.
no_speculative_load_hardening¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
- This attribute can be applied to a function declaration in order to indicate
that Speculative Load Hardening is not needed for the function body. This can also be applied to a method in Objective C. This attribute will take precedence over the command line flag in the case where -mspeculative-load-hardening is specified.
Warning: This attribute may not prevent Speculative Load Hardening from being enabled for a function which inlines a function that has the ‘speculative_load_hardening’ attribute. This is intended to provide a maximally conservative model where the code that is marked with the ‘speculative_load_hardening’ attribute will always (even when inlined) be hardened. A user of this attribute may want to mark functions called by a function they do not want to be hardened with the ‘noinline’ attribute.
For example:
__attribute__((speculative_load_hardening)) int foo(int i) { return i; } // Note: bar() may still have speculative load hardening enabled if // foo() is inlined into bar(). Mark foo() with __attribute__((noinline)) // to avoid this situation. __attribute__((no_speculative_load_hardening)) int bar(int i) { return foo(i); }
no_split_stack¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The no_split_stack
attribute disables the emission of the split stack
preamble for a particular function. It has no effect if -fsplit-stack
is not specified.
no_stack_protector, safebuffers¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
Yes |
Clang supports the GNU style __attribute__((no_stack_protector))
and Microsoft
style __declspec(safebuffers)
attribute which disables
the stack protector on the specified function. This attribute is useful for
selectively disabling the stack protector on some functions when building with
-fstack-protector
compiler option.
For example, it disables the stack protector for the function foo
but function
bar
will still be built with the stack protector with the -fstack-protector
option.
int __attribute__((no_stack_protector))
foo (int x); // stack protection will be disabled for foo.
int bar(int y); // bar can be built with the stack protector.
noalias¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The noalias
attribute indicates that the only memory accesses inside
function are loads and stores from objects pointed to by its pointer-typed
arguments, with arbitrary offsets.
nocf_check¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Jump Oriented Programming attacks rely on tampering with addresses used by
indirect call / jmp, e.g. redirect control-flow to non-programmer
intended bytes in the binary.
X86 Supports Indirect Branch Tracking (IBT) as part of Control-Flow
Enforcement Technology (CET). IBT instruments ENDBR instructions used to
specify valid targets of indirect call / jmp.
The nocf_check
attribute has two roles:
1. Appertains to a function - do not add ENDBR instruction at the beginning of
the function.
2. Appertains to a function pointer - do not track the target function of this
pointer (by adding nocf_check prefix to the indirect-call instruction).
noconvergent¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
Yes |
This attribute prevents a function from being treated as convergent, which
means that optimizations can only move calls to that function to
control-equivalent blocks. If a statement is marked as noconvergent
and
contains calls, it also prevents those calls from being treated as convergent.
In other words, those calls are not restricted to only being moved to
control-equivalent blocks.
In languages following SPMD/SIMT programming model, e.g., CUDA/HIP, function
declarations and calls are treated as convergent by default for correctness.
This noconvergent
attribute is helpful for developers to prevent them from
being treated as convergent when it’s safe.
__device__ float bar(float);
__device__ float foo(float) __attribute__((noconvergent)) {}
__device__ int example(void) {
float x;
[[clang::noconvergent]] x = bar(x);
}
nodiscard, warn_unused_result¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the ability to diagnose when the results of a function call
expression are discarded under suspicious circumstances. A diagnostic is
generated when a function or its return type is marked with [[nodiscard]]
(or __attribute__((warn_unused_result))
) and the function call appears as a
potentially-evaluated discarded-value expression that is not explicitly cast to
void
.
A string literal may optionally be provided to the attribute, which will be reproduced in any resulting diagnostics. Redeclarations using different forms of the attribute (with or without the string literal or with different string literal contents) are allowed. If there are redeclarations of the entity with differing string literals, it is unspecified which one will be used by Clang in any resulting diagnostics.
struct [[nodiscard]] error_info { /*...*/ };
error_info enable_missile_safety_mode();
void launch_missiles();
void test_missiles() {
enable_missile_safety_mode(); // diagnoses
launch_missiles();
}
error_info &foo();
void f() { foo(); } // Does not diagnose, error_info is a reference.
Additionally, discarded temporaries resulting from a call to a constructor
marked with [[nodiscard]]
or a constructor of a type marked
[[nodiscard]]
will also diagnose. This also applies to type conversions that
use the annotated [[nodiscard]]
constructor or result in an annotated type.
struct [[nodiscard]] marked_type {/*..*/ };
struct marked_ctor {
[[nodiscard]] marked_ctor();
marked_ctor(int);
};
struct S {
operator marked_type() const;
[[nodiscard]] operator int() const;
};
void usages() {
marked_type(); // diagnoses.
marked_ctor(); // diagnoses.
marked_ctor(3); // Does not diagnose, int constructor isn't marked nodiscard.
S s;
static_cast<marked_type>(s); // diagnoses
(int)s; // diagnoses
}
noduplicate¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The noduplicate
attribute can be placed on function declarations to control
whether function calls to this function can be duplicated or not as a result of
optimizations. This is required for the implementation of functions with
certain special requirements, like the OpenCL “barrier” function, that might
need to be run concurrently by all the threads that are executing in lockstep
on the hardware. For example this attribute applied on the function
“nodupfunc” in the code below avoids that:
void nodupfunc() __attribute__((noduplicate));
// Setting it as a C++11 attribute is also valid
// void nodupfunc() [[clang::noduplicate]];
void foo();
void bar();
nodupfunc();
if (a > n) {
foo();
} else {
bar();
}
gets possibly modified by some optimizations into code similar to this:
if (a > n) {
nodupfunc();
foo();
} else {
nodupfunc();
bar();
}
where the call to “nodupfunc” is duplicated and sunk into the two branches of the condition.
noinline¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
|
Yes |
This function attribute suppresses the inlining of a function at the call sites of the function.
[[clang::noinline]]
spelling can be used as a statement attribute; other
spellings of the attribute are not supported on statements. If a statement is
marked [[clang::noinline]]
and contains calls, those calls inside the
statement will not be inlined by the compiler.
__noinline__
can be used as a keyword in CUDA/HIP languages. This is to
avoid diagnostics due to usage of __attribute__((__noinline__))
with __noinline__
defined as a macro as __attribute__((noinline))
.
int example(void) {
int r;
[[clang::noinline]] foo();
[[clang::noinline]] r = bar();
return r;
}
nomicromips¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the GNU style __attribute__((micromips))
and
__attribute__((nomicromips))
attributes on MIPS targets. These attributes
may be attached to a function definition and instructs the backend to generate
or not to generate microMIPS code for that function.
These attributes override the -mmicromips
and -mno-micromips
options
on the command line.
noreturn, _Noreturn¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
Yes |
A function declared as [[noreturn]]
shall not return to its caller. The
compiler will generate a diagnostic for a function declared as [[noreturn]]
that appears to be capable of returning to its caller.
The [[_Noreturn]]
spelling is deprecated and only exists to ease code
migration for code using [[noreturn]]
after including <stdnoreturn.h>
.
not_tail_called¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The not_tail_called
attribute prevents tail-call optimization on statically
bound calls. Objective-c methods, and functions marked as always_inline
cannot be marked as not_tail_called
.
For example, it prevents tail-call optimization in the following case:
int __attribute__((not_tail_called)) foo1(int); int foo2(int a) { return foo1(a); // No tail-call optimization on direct calls. }
However, it doesn’t prevent tail-call optimization in this case:
int __attribute__((not_tail_called)) foo1(int); int foo2(int a) { int (*fn)(int) = &foo1; // not_tail_called has no effect on an indirect call even if the call can // be resolved at compile time. return (*fn)(a); }
Generally, marking an overriding virtual function as not_tail_called
is
not useful, because this attribute is a property of the static type. Calls
made through a pointer or reference to the base class type will respect
the not_tail_called
attribute of the base class’s member function,
regardless of the runtime destination of the call:
struct Foo { virtual void f(); }; struct Bar : Foo { [[clang::not_tail_called]] void f() override; }; void callera(Bar& bar) { Foo& foo = bar; // not_tail_called has no effect on here, even though the // underlying method is f from Bar. foo.f(); bar.f(); // No tail-call optimization on here. }
nothrow¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
Yes |
Clang supports the GNU style __attribute__((nothrow))
and Microsoft style
__declspec(nothrow)
attribute as an equivalent of noexcept
on function
declarations. This attribute informs the compiler that the annotated function
does not throw an exception. This prevents exception-unwinding. This attribute
is particularly useful on functions in the C Standard Library that are
guaranteed to not throw an exception.
nouwtable¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the nouwtable
attribute which skips emitting
the unwind table entry for the specified function. This attribute is useful for
selectively emitting the unwind table entry on some functions when building with
-funwind-tables
compiler option.
ns_consumed¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
ns_consumes_self¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
ns_returns_autoreleased¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
ns_returns_not_retained¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
ns_returns_retained¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
numthreads¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
The numthreads
attribute applies to HLSL shaders where explcit thread counts
are required. The X
, Y
, and Z
values provided to the attribute
dictate the thread id. Total number of threads executed is X * Y * Z
.
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-attributes-numthreads
objc_method_family¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Many methods in Objective-C have conventional meanings determined by their selectors. It is sometimes useful to be able to mark a method as having a particular conventional meaning despite not having the right selector, or as not having the conventional meaning that its selector would suggest. For these use cases, we provide an attribute to specifically describe the “method family” that a method belongs to.
Usage: __attribute__((objc_method_family(X)))
, where X
is one of
none
, alloc
, copy
, init
, mutableCopy
, or new
. This
attribute can only be placed at the end of a method declaration:
- (NSString *)initMyStringValue __attribute__((objc_method_family(none)));
Users who do not wish to change the conventional meaning of a method, and who
merely want to document its non-standard retain and release semantics, should
use the retaining behavior attributes (ns_returns_retained
,
ns_returns_not_retained
, etc).
Query for this feature with __has_attribute(objc_method_family)
.
objc_requires_super¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Some Objective-C classes allow a subclass to override a particular method in a
parent class but expect that the overriding method also calls the overridden
method in the parent class. For these cases, we provide an attribute to
designate that a method requires a “call to super
” in the overriding
method in the subclass.
Usage: __attribute__((objc_requires_super))
. This attribute can only
be placed at the end of a method declaration:
- (void)foo __attribute__((objc_requires_super));
This attribute can only be applied the method declarations within a class, and
not a protocol. Currently this attribute does not enforce any placement of
where the call occurs in the overriding method (such as in the case of
-dealloc
where the call must appear at the end). It checks only that it
exists.
Note that on both OS X and iOS that the Foundation framework provides a
convenience macro NS_REQUIRES_SUPER
that provides syntactic sugar for this
attribute:
- (void)foo NS_REQUIRES_SUPER;
This macro is conditionally defined depending on the compiler’s support for this attribute. If the compiler does not support the attribute the macro expands to nothing.
Operationally, when a method has this annotation the compiler will warn if the implementation of an override in a subclass does not call super. For example:
warning: method possibly missing a [super AnnotMeth] call
- (void) AnnotMeth{};
^
optnone¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The optnone
attribute suppresses essentially all optimizations
on a function or method, regardless of the optimization level applied to
the compilation unit as a whole. This is particularly useful when you
need to debug a particular function, but it is infeasible to build the
entire application without optimization. Avoiding optimization on the
specified function can improve the quality of the debugging information
for that function.
This attribute is incompatible with the always_inline
and minsize
attributes.
Note that this attribute does not apply recursively to nested functions such as
lambdas or blocks when using declaration-specific attribute syntaxes such as double
square brackets ([[]]
) or __attribute__
. The #pragma
syntax can be
used to apply the attribute to all functions, including nested functions, in a
range of source code.
os_consumed¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
os_consumes_this¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
os_returns_not_retained¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
os_returns_retained¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
os_returns_retained_on_non_zero¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
os_returns_retained_on_zero¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The behavior of a function with respect to reference counting for Foundation
(Objective-C), CoreFoundation (C) and OSObject (C++) is determined by a naming
convention (e.g. functions starting with “get” are assumed to return at
+0
).
It can be overridden using a family of the following attributes. In
Objective-C, the annotation __attribute__((ns_returns_retained))
applied to
a function communicates that the object is returned at +1
, and the caller
is responsible for freeing it.
Similarly, the annotation __attribute__((ns_returns_not_retained))
specifies that the object is returned at +0
and the ownership remains with
the callee.
The annotation __attribute__((ns_consumes_self))
specifies that
the Objective-C method call consumes the reference to self
, e.g. by
attaching it to a supplied parameter.
Additionally, parameters can have an annotation
__attribute__((ns_consumed))
, which specifies that passing an owned object
as that parameter effectively transfers the ownership, and the caller is no
longer responsible for it.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
In C programs using CoreFoundation, a similar set of attributes:
__attribute__((cf_returns_not_retained))
,
__attribute__((cf_returns_retained))
and __attribute__((cf_consumed))
have the same respective semantics when applied to CoreFoundation objects.
These attributes affect code generation when interacting with ARC code, and
they are used by the Clang Static Analyzer.
Finally, in C++ interacting with XNU kernel (objects inheriting from OSObject),
the same attribute family is present:
__attribute__((os_returns_not_retained))
,
__attribute__((os_returns_retained))
and __attribute__((os_consumed))
,
with the same respective semantics.
Similar to __attribute__((ns_consumes_self))
,
__attribute__((os_consumes_this))
specifies that the method call consumes
the reference to “this” (e.g., when attaching it to a different object supplied
as a parameter).
Out parameters (parameters the function is meant to write into,
either via pointers-to-pointers or references-to-pointers)
may be annotated with __attribute__((os_returns_retained))
or __attribute__((os_returns_not_retained))
which specifies that the object
written into the out parameter should (or respectively should not) be released
after use.
Since often out parameters may or may not be written depending on the exit
code of the function,
annotations __attribute__((os_returns_retained_on_zero))
and __attribute__((os_returns_retained_on_non_zero))
specify that
an out parameter at +1
is written if and only if the function returns a zero
(respectively non-zero) error code.
Observe that return-code-dependent out parameter annotations are only
available for retained out parameters, as non-retained object do not have to be
released by the callee.
These attributes are only used by the Clang Static Analyzer.
The family of attributes X_returns_X_retained
can be added to functions,
C++ methods, and Objective-C methods and properties.
Attributes X_consumed
can be added to parameters of methods, functions,
and Objective-C methods.
overloadable¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang provides support for C++ function overloading in C. Function overloading
in C is introduced using the overloadable
attribute. For example, one
might provide several overloaded versions of a tgsin
function that invokes
the appropriate standard function computing the sine of a value with float
,
double
, or long double
precision:
#include <math.h>
float __attribute__((overloadable)) tgsin(float x) { return sinf(x); }
double __attribute__((overloadable)) tgsin(double x) { return sin(x); }
long double __attribute__((overloadable)) tgsin(long double x) { return sinl(x); }
Given these declarations, one can call tgsin
with a float
value to
receive a float
result, with a double
to receive a double
result,
etc. Function overloading in C follows the rules of C++ function overloading
to pick the best overload given the call arguments, with a few C-specific
semantics:
Conversion from
float
ordouble
tolong double
is ranked as a floating-point promotion (per C99) rather than as a floating-point conversion (as in C++).A conversion from a pointer of type
T*
to a pointer of typeU*
is considered a pointer conversion (with conversion rank) ifT
andU
are compatible types.A conversion from type
T
to a value of typeU
is permitted ifT
andU
are compatible types. This conversion is given “conversion” rank.If no viable candidates are otherwise available, we allow a conversion from a pointer of type
T*
to a pointer of typeU*
, whereT
andU
are incompatible. This conversion is ranked below all other types of conversions. Please note:U
lacking qualifiers that are present onT
is sufficient forT
andU
to be incompatible.
The declaration of overloadable
functions is restricted to function
declarations and definitions. If a function is marked with the overloadable
attribute, then all declarations and definitions of functions with that name,
except for at most one (see the note below about unmarked overloads), must have
the overloadable
attribute. In addition, redeclarations of a function with
the overloadable
attribute must have the overloadable
attribute, and
redeclarations of a function without the overloadable
attribute must not
have the overloadable
attribute. e.g.,
int f(int) __attribute__((overloadable));
float f(float); // error: declaration of "f" must have the "overloadable" attribute
int f(int); // error: redeclaration of "f" must have the "overloadable" attribute
int g(int) __attribute__((overloadable));
int g(int) { } // error: redeclaration of "g" must also have the "overloadable" attribute
int h(int);
int h(int) __attribute__((overloadable)); // error: declaration of "h" must not
// have the "overloadable" attribute
Functions marked overloadable
must have prototypes. Therefore, the
following code is ill-formed:
int h() __attribute__((overloadable)); // error: h does not have a prototype
However, overloadable
functions are allowed to use a ellipsis even if there
are no named parameters (as is permitted in C++). This feature is particularly
useful when combined with the unavailable
attribute:
void honeypot(...) __attribute__((overloadable, unavailable)); // calling me is an error
Functions declared with the overloadable
attribute have their names mangled
according to the same rules as C++ function names. For example, the three
tgsin
functions in our motivating example get the mangled names
_Z5tgsinf
, _Z5tgsind
, and _Z5tgsine
, respectively. There are two
caveats to this use of name mangling:
Future versions of Clang may change the name mangling of functions overloaded in C, so you should not depend on an specific mangling. To be completely safe, we strongly urge the use of
static inline
withoverloadable
functions.The
overloadable
attribute has almost no meaning when used in C++, because names will already be mangled and functions are already overloadable. However, when anoverloadable
function occurs within anextern "C"
linkage specification, its name will be mangled in the same way as it would in C.
For the purpose of backwards compatibility, at most one function with the same
name as other overloadable
functions may omit the overloadable
attribute. In this case, the function without the overloadable
attribute
will not have its name mangled.
For example:
// Notes with mangled names assume Itanium mangling.
int f(int);
int f(double) __attribute__((overloadable));
void foo() {
f(5); // Emits a call to f (not _Z1fi, as it would with an overload that
// was marked with overloadable).
f(1.0); // Emits a call to _Z1fd.
}
Support for unmarked overloads is not present in some versions of clang. You may
query for it using __has_extension(overloadable_unmarked)
.
Query for this attribute with __has_attribute(overloadable)
.
packoffset¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The packoffset attribute is used to change the layout of a cbuffer.
Attribute spelling in HLSL is: packoffset( c[Subcomponent][.component] )
.
A subcomponent is a register number, which is an integer. A component is in the form of [.xyzw].
Examples:
cbuffer A {
float3 a : packoffset(c0.y);
float4 b : packoffset(c4);
}
The full documentation is available here: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-variable-packoffset
patchable_function_entry¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
__attribute__((patchable_function_entry(N,M)))
is used to generate M NOPs
before the function entry and N-M NOPs after the function entry. This attribute
takes precedence over the command line option -fpatchable-function-entry=N,M
.
M
defaults to 0 if omitted.
This attribute is only supported on aarch64/aarch64-be/loongarch32/loongarch64/riscv32/riscv64/i386/x86-64/ppc/ppc64 targets. For ppc/ppc64 targets, AIX is still not supported.
preserve_access_index¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the __attribute__((preserve_access_index))
attribute for the BPF target. This attribute may be attached to a
struct or union declaration, where if -g is specified, it enables
preserving struct or union member access debuginfo indices of this
struct or union, similar to clang __builtin_preserve_access_index()
.
preserve_static_offset¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the __attribute__((preserve_static_offset))
attribute for the BPF target. This attribute may be attached to a
struct or union declaration. Reading or writing fields of types having
such annotation is guaranteed to generate LDX/ST/STX instruction with
offset corresponding to the field.
For example:
struct foo {
int a;
int b;
};
struct bar {
int a;
struct foo b;
} __attribute__((preserve_static_offset));
void buz(struct bar *g) {
g->b.a = 42;
}
The assignment to g
’s field would produce an ST instruction with
offset 8: *(u32)(r1 + 8) = 42;
.
Without this attribute generated instructions might be different,
depending on optimizations behavior. E.g. the example above could be
rewritten as r1 += 8; *(u32)(r1 + 0) = 42;
.
register¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The resource binding attribute sets the virtual register and logical register space for a resource.
Attribute spelling in HLSL is: register(slot [, space])
.
slot
takes the format [type][number]
,
where type
is a single character specifying the resource type and number
is the virtual register number.
Register types are: t for shader resource views (SRV), s for samplers, u for unordered access views (UAV), b for constant buffer views (CBV).
Register space is specified in the format space[number]
and defaults to space0
if omitted.
Here’re resource binding examples with and without space:
RWBuffer<float> Uav : register(u3, space1);
Buffer<float> Buf : register(t1);
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3d12/resource-binding-in-hlsl
reinitializes¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
The reinitializes
attribute can be applied to a non-static, non-const C++
member function to indicate that this member function reinitializes the entire
object to a known state, independent of the previous state of the object.
This attribute can be interpreted by static analyzers that warn about uses of an
object that has been left in an indeterminate state by a move operation. If a
member function marked with the reinitializes
attribute is called on a
moved-from object, the analyzer can conclude that the object is no longer in an
indeterminate state.
A typical example where this attribute would be used is on functions that clear a container class:
template <class T>
class Container {
public:
...
[[clang::reinitializes]] void Clear();
...
};
retain¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
This attribute, when attached to a function or variable definition, prevents section garbage collection in the linker. It does not prevent other discard mechanisms, such as archive member selection, and COMDAT group resolution.
If the compiler does not emit the definition, e.g. because it was not used in
the translation unit or the compiler was able to eliminate all of the uses,
this attribute has no effect. This attribute is typically combined with the
used
attribute to force the definition to be emitted and preserved into the
final linked image.
This attribute is only necessary on ELF targets; other targets prevent section
garbage collection by the linker when using the used
attribute alone.
Using the attributes together should result in consistent behavior across
targets.
This attribute requires the linker to support the SHF_GNU_RETAIN
extension.
This support is available in GNU ld
and gold
as of binutils 2.36, as
well as in ld.lld
13.
shader¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
The shader
type attribute applies to HLSL shader entry functions to
identify the shader type for the entry function.
The syntax is:
``[shader(string-literal)]``
where the string literal is one of: “pixel”, “vertex”, “geometry”, “hull”,
“domain”, “compute”, “raygeneration”, “intersection”, “anyhit”, “closesthit”,
“miss”, “callable”, “mesh”, “amplification”. Normally the shader type is set
by shader target with the -T
option like -Tps_6_1
. When compiling to a
library target like lib_6_3
, the shader type attribute can help the
compiler to identify the shader type. It is mostly used by Raytracing shaders
where shaders must be compiled into a library and linked at runtime.
short_call, near¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the __attribute__((long_call))
, __attribute__((far))
,
__attribute__((short__call))
, and __attribute__((near))
attributes
on MIPS targets. These attributes may only be added to function declarations
and change the code generated by the compiler when directly calling
the function. The short_call
and near
attributes are synonyms and
allow calls to the function to be made using the jal
instruction, which
requires the function to be located in the same naturally aligned 256MB segment
as the caller. The long_call
and far
attributes are synonyms and
require the use of a different call sequence that works regardless
of the distance between the functions.
These attributes have no effect for position-independent code.
These attributes take priority over command line switches such
as -mlong-calls
and -mno-long-calls
.
signal¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the GNU style __attribute__((signal))
attribute on
AVR targets. This attribute may be attached to a function definition and instructs
the backend to generate appropriate function entry/exit code so that it can be used
directly as an interrupt service routine.
Interrupt handler functions defined with the signal attribute do not re-enable interrupts.
speculative_load_hardening¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
- This attribute can be applied to a function declaration in order to indicate
that Speculative Load Hardening should be enabled for the function body. This can also be applied to a method in Objective C. This attribute will take precedence over the command line flag in the case where -mno-speculative-load-hardening is specified.
Speculative Load Hardening is a best-effort mitigation against information leak attacks that make use of control flow miss-speculation - specifically miss-speculation of whether a branch is taken or not. Typically vulnerabilities enabling such attacks are classified as “Spectre variant #1”. Notably, this does not attempt to mitigate against miss-speculation of branch target, classified as “Spectre variant #2” vulnerabilities.
When inlining, the attribute is sticky. Inlining a function that carries this attribute will cause the caller to gain the attribute. This is intended to provide a maximally conservative model where the code in a function annotated with this attribute will always (even after inlining) end up hardened.
strict_gs_check¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
Clang supports the Microsoft style __declspec((strict_gs_check))
attribute
which upgrades the stack protector check from -fstack-protector
to
-fstack-protector-strong
.
For example, it upgrades the stack protector for the function foo
to
-fstack-protector-strong
but function bar
will still be built with the
stack protector with the -fstack-protector
option.
__declspec((strict_gs_check))
int foo(int x); // stack protection will be upgraded for foo.
int bar(int y); // bar can be built with the standard stack protector checks.
sycl_kernel¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The sycl_kernel
attribute specifies that a function template will be used
to outline device code and to generate an OpenCL kernel.
Here is a code example of the SYCL program, which demonstrates the compiler’s
outlining job:
int foo(int x) { return ++x; }
using namespace cl::sycl;
queue Q;
buffer<int, 1> a(range<1>{1024});
Q.submit([&](handler& cgh) {
auto A = a.get_access<access::mode::write>(cgh);
cgh.parallel_for<init_a>(range<1>{1024}, [=](id<1> index) {
A[index] = index[0] + foo(42);
});
}
A C++ function object passed to the parallel_for
is called a “SYCL kernel”.
A SYCL kernel defines the entry point to the “device part” of the code. The
compiler will emit all symbols accessible from a “kernel”. In this code
example, the compiler will emit “foo” function. More details about the
compilation of functions for the device part can be found in the SYCL 1.2.1
specification Section 6.4.
To show to the compiler entry point to the “device part” of the code, the SYCL
runtime can use the sycl_kernel
attribute in the following way:
namespace cl {
namespace sycl {
class handler {
template <typename KernelName, typename KernelType/*, ...*/>
__attribute__((sycl_kernel)) void sycl_kernel_function(KernelType KernelFuncObj) {
// ...
KernelFuncObj();
}
template <typename KernelName, typename KernelType, int Dims>
void parallel_for(range<Dims> NumWorkItems, KernelType KernelFunc) {
#ifdef __SYCL_DEVICE_ONLY__
sycl_kernel_function<KernelName, KernelType, Dims>(KernelFunc);
#else
// Host implementation
#endif
}
};
} // namespace sycl
} // namespace cl
The compiler will also generate an OpenCL kernel using the function marked with
the sycl_kernel
attribute.
Here is the list of SYCL device compiler expectations with regard to the
function marked with the sycl_kernel
attribute:
The function must be a template with at least two type template parameters. The compiler generates an OpenCL kernel and uses the first template parameter as a unique name for the generated OpenCL kernel. The host application uses this unique name to invoke the OpenCL kernel generated for the SYCL kernel specialized by this name and second template parameter
KernelType
(which might be an unnamed function object type).The function must have at least one parameter. The first parameter is required to be a function object type (named or unnamed i.e. lambda). The compiler uses function object type fields to generate OpenCL kernel parameters.
The function must return void. The compiler reuses the body of marked functions to generate the OpenCL kernel body, and the OpenCL kernel must return
void
.
The SYCL kernel in the previous code sample meets these expectations.
sycl_kernel_entry_point¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The sycl_kernel_entry_point
attribute facilitates the generation of an
offload kernel entry point, sometimes called a SYCL kernel caller function,
suitable for invoking a SYCL kernel on an offload device. The attribute is
intended for use in the implementation of SYCL kernel invocation functions
like the single_task
and parallel_for
member functions of the
sycl::handler
class specified in section 4.9.4, “Command group handler
class”, of the SYCL 2020 specification.
The attribute requires a single type argument that specifies a class type that meets the requirements for a SYCL kernel name as described in section 5.2, “Naming of kernels”, of the SYCL 2020 specification. A unique kernel name type is required for each function declared with the attribute. The attribute may not first appear on a declaration that follows a definition of the function.
The attribute only appertains to functions and only those that meet the following requirements.
Has a
void
return type.Is not a non-static member function, constructor, or destructor.
Is not a C variadic function.
Is not a coroutine.
Is not defined as deleted or as defaulted.
Is not declared with the
constexpr
orconsteval
specifiers.Is not declared with the
[[noreturn]]
attribute.
Use in the implementation of a SYCL kernel invocation function might look as follows.
namespace sycl {
class handler {
template<typename KernelNameType, typename KernelType>
[[ clang::sycl_kernel_entry_point(KernelNameType) ]]
static void kernel_entry_point(KernelType kernel) {
kernel();
}
public:
template<typename KernelNameType, typename KernelType>
void single_task(KernelType kernel) {
// Call kernel_entry_point() to trigger generation of an offload
// kernel entry point.
kernel_entry_point<KernelNameType>(kernel);
// Call functions appropriate for the desired offload backend
// (OpenCL, CUDA, HIP, Level Zero, etc...).
}
};
} // namespace sycl
A SYCL kernel is a callable object of class type that is constructed on a host, often via a lambda expression, and then passed to a SYCL kernel invocation function to be executed on an offload device. A SYCL kernel invocation function is responsible for copying the provided SYCL kernel object to an offload device and initiating a call to it. The SYCL kernel object and its data members constitute the parameters of an offload kernel.
A SYCL kernel type is required to satisfy the device copyability requirements
specified in section 3.13.1, “Device copyable”, of the SYCL 2020 specification.
Additionally, any data members of the kernel object type are required to satisfy
section 4.12.4, “Rules for parameter passing to kernels”. For most types, these
rules require that the type is trivially copyable. However, the SYCL
specification mandates that certain special SYCL types, such as
sycl::accessor
and sycl::stream
be device copyable even if they are not
trivially copyable. These types require special handling because they cannot
be copied to device memory as if by memcpy()
. Additionally, some offload
backends, OpenCL for example, require objects of some of these types to be
passed as individual arguments to the offload kernel.
An offload kernel consists of an entry point function that declares the parameters of the offload kernel and the set of all functions and variables that are directly or indirectly used by the entry point function.
A SYCL kernel invocation function invokes a SYCL kernel on a device by performing the following tasks (likely with the help of an offload backend like OpenCL):
Identifying the offload kernel entry point to be used for the SYCL kernel.
Deconstructing the SYCL kernel object, if necessary, to produce the set of offload kernel arguments required by the offload kernel entry point.
Copying the offload kernel arguments to device memory.
Initiating execution of the offload kernel entry point.
The offload kernel entry point for a SYCL kernel performs the following tasks:
Reconstituting the SYCL kernel object, if necessary, using the offload kernel parameters.
Calling the
operator()
member function of the (reconstituted) SYCL kernel object.
The sycl_kernel_entry_point
attribute automates generation of an offload
kernel entry point that performs those latter tasks. The parameters and body of
a function declared with the sycl_kernel_entry_point
attribute specify a
pattern from which the parameters and body of the entry point function are
derived. Consider the following call to a SYCL kernel invocation function.
struct S { int i; };
void f(sycl::handler &handler, sycl::stream &sout, S s) {
handler.single_task<struct KN>([=] {
sout << "The value of s.i is " << s.i << "\n";
});
}
The SYCL kernel object is the result of the lambda expression. It has two
data members corresponding to the captures of sout
and s
. Since one
of these data members corresponds to a special SYCL type that must be passed
individually as an offload kernel parameter, it is necessary to decompose the
SYCL kernel object into its constituent parts; the offload kernel will have
two kernel parameters. Given a SYCL implementation that uses a
sycl_kernel_entry_point
attributed function like the one shown above, an
offload kernel entry point function will be generated that looks approximately
as follows.
void sycl-kernel-caller-for-KN(sycl::stream sout, S s) {
kernel-type kernel = { sout, s );
kernel();
}
There are a few items worthy of note:
The name of the generated function incorporates the SYCL kernel name,
KN
, that was passed as theKernelNameType
template parameter tokernel_entry_point()
and provided as the argument to thesycl_kernel_entry_point
attribute. There is a one-to-one correspondence between SYCL kernel names and offload kernel entry points.The SYCL kernel is a lambda closure type and therefore has no name;
kernel-type
is substituted above and corresponds to theKernelType
template parameter deduced in the call tokernel_entry_point()
. Lambda types cannot be declared and initialized using the aggregate initialization syntax used above, but the intended behavior should be clear.S
is a device copyable type that does not directly or indirectly contain a data member of a SYCL special type. It therefore does not need to be decomposed into its constituent members to be passed as a kernel argument.The depiction of the
sycl::stream
parameter as a single self contained kernel parameter is an oversimplification. SYCL special types may require additional decomposition such that the generated function might have three or more parameters depending on how the SYCL library implementation defines these types.The call to
kernel_entry_point()
has no effect other than to trigger emission of the entry point function. The statments that make up the body of the function are not executed when the function is called; they are only used in the generation of the entry point function.
It is not necessary for a function declared with the sycl_kernel_entry_point
attribute to be called for the offload kernel entry point to be emitted. For
inline functions and function templates, any ODR-use will suffice. For other
functions, an ODR-use is not required; the offload kernel entry point will be
emitted if the function is defined.
Functions declared with the sycl_kernel_entry_point
attribute are not
limited to the simple example shown above. They may have additional template
parameters, declare additional function parameters, and have complex control
flow in the function body. Function parameter decomposition and reconstitution
is performed for all function parameters. The function must abide by the
language feature restrictions described in section 5.4, “Language restrictions
for device functions” in the SYCL 2020 specification.
target¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the GNU style __attribute__((target("OPTIONS")))
attribute.
This attribute may be attached to a function definition and instructs
the backend to use different code generation options than were passed on the
command line.
The current set of options correspond to the existing “subtarget features” for
the target with or without a “-mno-” in front corresponding to the absence
of the feature, as well as arch="CPU"
which will change the default “CPU”
for the function.
For X86, the attribute also allows tune="CPU"
to optimize the generated
code for the given CPU without changing the available instructions.
For AArch64, arch="Arch"
will set the architecture, similar to the -march
command line options. cpu="CPU"
can be used to select a specific cpu,
as per the -mcpu
option, similarly for tune=
. The attribute also allows the
“branch-protection=<args>” option, where the permissible arguments and their
effect on code generation are the same as for the command-line option
-mbranch-protection
.
Example “subtarget features” from the x86 backend include: “mmx”, “sse”, “sse4.2”, “avx”, “xop” and largely correspond to the machine specific options handled by the front end.
Note that this attribute does not apply transitively to nested functions such as blocks or C++ lambdas.
Additionally, this attribute supports function multiversioning for ELF based
x86/x86-64 targets, which can be used to create multiple implementations of the
same function that will be resolved at runtime based on the priority of their
target
attribute strings. A function is considered a multiversioned function
if either two declarations of the function have different target
attribute
strings, or if it has a target
attribute string of default
. For
example:
__attribute__((target("arch=atom"))) void foo() {} // will be called on 'atom' processors. __attribute__((target("default"))) void foo() {} // will be called on any other processors.
All multiversioned functions must contain a default
(fallback)
implementation, otherwise usages of the function are considered invalid.
Additionally, a function may not become multiversioned after its first use.
target_clones¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
Clang supports the target_clones("OPTIONS")
attribute. This attribute may be
attached to a function declaration and causes function multiversioning, where
multiple versions of the function will be emitted with different code
generation options. Additionally, these versions will be resolved at runtime
based on the priority of their attribute options. All target_clone
functions
are considered multiversioned functions.
For AArch64 target: The attribute contains comma-separated strings of target features joined by “+” sign. For example:
__attribute__((target_clones("sha2+memtag", "fcma+sve2-pmull128"))) void foo() {}
For every multiversioned function a default
(fallback) implementation
always generated if not specified directly.
For x86/x86-64 targets:
All multiversioned functions must contain a default
(fallback)
implementation, otherwise usages of the function are considered invalid.
Additionally, a function may not become multiversioned after its first use.
The options to target_clones
can either be a target-specific architecture
(specified as arch=CPU
), or one of a list of subtarget features.
Example “subtarget features” from the x86 backend include: “mmx”, “sse”, “sse4.2”, “avx”, “xop” and largely correspond to the machine specific options handled by the front end.
The versions can either be listed as a comma-separated sequence of string literals or as a single string literal containing a comma-separated list of versions. For compatibility with GCC, the two formats can be mixed. For example, the following will emit 4 versions of the function:
__attribute__((target_clones("arch=atom,avx2","arch=ivybridge","default"))) void foo() {}
For targets that support the GNU indirect function (IFUNC) feature, dispatch
is performed by emitting an indirect function that is resolved to the appropriate
target clone at load time. The indirect function is given the name the
multiversioned function would have if it had been declared without the attribute.
For backward compatibility with earlier Clang releases, a function alias with an
.ifunc
suffix is also emitted. The .ifunc
suffixed symbol is a deprecated
feature and support for it may be removed in the future.
target_version¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
For AArch64 target clang supports function multiversioning by
__attribute__((target_version("OPTIONS")))
attribute. When applied to a
function it instructs compiler to emit multiple function versions based on
target_version
attribute strings, which resolved at runtime depend on their
priority and target features availability. One of the versions is always
( implicitly or explicitly ) the default
(fallback). Attribute strings can
contain dependent features names joined by the “+” sign.
For targets that support the GNU indirect function (IFUNC) feature, dispatch
is performed by emitting an indirect function that is resolved to the appropriate
target clone at load time. The indirect function is given the name the
multiversioned function would have if it had been declared without the attribute.
For backward compatibility with earlier Clang releases, a function alias with an
.ifunc
suffix is also emitted. The .ifunc
suffixed symbol is a deprecated
feature and support for it may be removed in the future.
unsafe_buffer_usage¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The attribute [[clang::unsafe_buffer_usage]]
should be placed on functions
that need to be avoided as they are prone to buffer overflows or unsafe buffer
struct fields. It is designed to work together with the off-by-default compiler
warning -Wunsafe-buffer-usage
to help codebases transition away from raw pointer
based buffer management, in favor of safer abstractions such as C++20 std::span
.
The attribute causes -Wunsafe-buffer-usage
to warn on every use of the function or
the field it is attached to, and it may also lead to emission of automatic fix-it
hints which would help the user replace the use of unsafe functions(/fields) with safe
alternatives, though the attribute can be used even when the fix can’t be automated.
Attribute attached to functions: The attribute does not suppress
-Wunsafe-buffer-usage
inside the function to which it is attached. These warnings still need to be addressed.The attribute is warranted even if the only way a function can overflow the buffer is by violating the function’s preconditions. For example, it would make sense to put the attribute on function
foo()
below because passing an incorrect size parameter would cause a buffer overflow:[[clang::unsafe_buffer_usage]] void foo(int *buf, size_t size) { for (size_t i = 0; i < size; ++i) { buf[i] = i; } }
The attribute is NOT warranted when the function uses safe abstractions, assuming that these abstractions weren’t misused outside the function. For example, function
bar()
below doesn’t need the attribute, because assuming that the containerbuf
is well-formed (has size that fits the original buffer it refers to), overflow cannot occur:void bar(std::span<int> buf) { for (size_t i = 0; i < buf.size(); ++i) { buf[i] = i; } }
In this case function
bar()
enables the user to keep the buffer “containerized” in a span for as long as possible. On the other hand, Functionfoo()
in the previous example may have internal consistency, but by accepting a raw buffer it requires the user to unwrap their span, which is undesirable according to the programming model behind-Wunsafe-buffer-usage
.The attribute is warranted when a function accepts a raw buffer only to immediately put it into a span:
[[clang::unsafe_buffer_usage]] void baz(int *buf, size_t size) { std::span<int> sp{ buf, size }; for (size_t i = 0; i < sp.size(); ++i) { sp[i] = i; } }
In this case
baz()
does not contain any unsafe operations, but the awkward parameter type causes the caller to unwrap the span unnecessarily. Note that regardless of the attribute, code insidebaz()
isn’t flagged by-Wunsafe-buffer-usage
as unsafe. It is definitely undesirable, but ifbaz()
is on an API surface, there is no way to improve it to make it as safe asbar()
without breaking the source and binary compatibility with existing users of the function. In such cases the proper solution would be to create a different function (possibly an overload ofbaz()
) that accepts a safe container likebar()
, and then use the attribute on the originalbaz()
to help the users update their code to use the new function.Attribute attached to fields: The attribute should only be attached to struct fields, if the fields can not be updated to a safe type with bounds check, such as std::span. In other words, the buffers prone to unsafe accesses should always be updated to use safe containers/views and attaching the attribute must be last resort when such an update is infeasible.
The attribute can be placed on individual fields or a set of them as shown below.
struct A { [[clang::unsafe_buffer_usage]] int *ptr1; [[clang::unsafe_buffer_usage]] int *ptr2, buf[10]; [[clang::unsafe_buffer_usage]] size_t sz; };
Here, every read/write to the fields ptr1, ptr2, buf and sz will trigger a warning that the field has been explcitly marked as unsafe due to unsafe-buffer operations.
used¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
This attribute, when attached to a function or variable definition, indicates
that there may be references to the entity which are not apparent in the source
code. For example, it may be referenced from inline asm
, or it may be
found through a dynamic symbol or section lookup.
The compiler must emit the definition even if it appears to be unused, and it must not apply optimizations which depend on fully understanding how the entity is used.
Whether this attribute has any effect on the linker depends on the target and
the linker. Most linkers support the feature of section garbage collection
(--gc-sections
), also known as “dead stripping” (ld64 -dead_strip
) or
discarding unreferenced sections (link.exe /OPT:REF
). On COFF and Mach-O
targets (Windows and Apple platforms), the used attribute prevents symbols
from being removed by linker section GC. On ELF targets, it has no effect on its
own, and the linker may remove the definition if it is not otherwise referenced.
This linker GC can be avoided by also adding the retain
attribute. Note
that retain
requires special support from the linker; see that attribute’s
documentation for further information.
xray_always_instrument, xray_never_instrument, xray_log_args¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
__attribute__((xray_always_instrument))
or
[[clang::xray_always_instrument]]
is used to mark member functions (in C++),
methods (in Objective C), and free functions (in C, C++, and Objective C) to be
instrumented with XRay. This will cause the function to always have space at
the beginning and exit points to allow for runtime patching.
Conversely, __attribute__((xray_never_instrument))
or
[[clang::xray_never_instrument]]
will inhibit the insertion of these
instrumentation points.
If a function has neither of these attributes, they become subject to the XRay heuristics used to determine whether a function should be instrumented or otherwise.
__attribute__((xray_log_args(N)))
or [[clang::xray_log_args(N)]]
is
used to preserve N function arguments for the logging function. Currently,
only N==1 is supported.
xray_always_instrument, xray_never_instrument, xray_log_args¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
__attribute__((xray_always_instrument))
or
[[clang::xray_always_instrument]]
is used to mark member functions (in C++),
methods (in Objective C), and free functions (in C, C++, and Objective C) to be
instrumented with XRay. This will cause the function to always have space at
the beginning and exit points to allow for runtime patching.
Conversely, __attribute__((xray_never_instrument))
or
[[clang::xray_never_instrument]]
will inhibit the insertion of these
instrumentation points.
If a function has neither of these attributes, they become subject to the XRay heuristics used to determine whether a function should be instrumented or otherwise.
__attribute__((xray_log_args(N)))
or [[clang::xray_log_args(N)]]
is
used to preserve N function arguments for the logging function. Currently,
only N==1 is supported.
zero_call_used_regs¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
This attribute, when attached to a function, causes the compiler to zero a subset of all call-used registers before the function returns. It’s used to increase program security by either mitigating Return-Oriented Programming (ROP) attacks or preventing information leakage through registers.
The term “call-used” means registers which are not guaranteed to be preserved unchanged for the caller by the current calling convention. This could also be described as “caller-saved” or “not callee-saved”.
The choice parameters gives the programmer flexibility to choose the subset of the call-used registers to be zeroed:
skip
doesn’t zero any call-used registers. This choice overrides any command-line arguments.used
only zeros call-used registers used in the function. Byused
, we mean a register whose contents have been set or referenced in the function.used-gpr
only zeros call-used GPR registers used in the function.used-arg
only zeros call-used registers used to pass arguments to the function.used-gpr-arg
only zeros call-used GPR registers used to pass arguments to the function.all
zeros all call-used registers.all-gpr
zeros all call-used GPR registers.all-arg
zeros all call-used registers used to pass arguments to the function.all-gpr-arg
zeros all call-used GPR registers used to pass arguments to the function.
The default for the attribute is controlled by the -fzero-call-used-regs
flag.
Handle Attributes¶
Handles are a way to identify resources like files, sockets, and processes. They are more opaque than pointers and widely used in system programming. They have similar risks such as never releasing a resource associated with a handle, attempting to use a handle that was already released, or trying to release a handle twice. Using the annotations below it is possible to make the ownership of the handles clear: whose responsibility is to release them. They can also aid static analysis tools to find bugs.
acquire_handle¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
If this annotation is on a function or a function type it is assumed to return
a new handle. In case this annotation is on an output parameter,
the function is assumed to fill the corresponding argument with a new
handle. The attribute requires a string literal argument which used to
identify the handle with later uses of use_handle
or
release_handle
.
// Output arguments from Zircon.
zx_status_t zx_socket_create(uint32_t options,
zx_handle_t __attribute__((acquire_handle("zircon"))) * out0,
zx_handle_t* out1 [[clang::acquire_handle("zircon")]]);
// Returned handle.
[[clang::acquire_handle("tag")]] int open(const char *path, int oflag, ... );
int open(const char *path, int oflag, ... ) __attribute__((acquire_handle("tag")));
release_handle¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
If a function parameter is annotated with release_handle(tag)
it is assumed to
close the handle. It is also assumed to require an open handle to work with. The
attribute requires a string literal argument to identify the handle being released.
zx_status_t zx_handle_close(zx_handle_t handle [[clang::release_handle("tag")]]);
use_handle¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
A function taking a handle by value might close the handle. If a function
parameter is annotated with use_handle(tag)
it is assumed to not to change
the state of the handle. It is also assumed to require an open handle to work with.
The attribute requires a string literal argument to identify the handle being used.
zx_status_t zx_port_wait(zx_handle_t handle [[clang::use_handle("zircon")]],
zx_time_t deadline,
zx_port_packet_t* packet);
Nullability Attributes¶
Whether a particular pointer may be “null” is an important concern when working
with pointers in the C family of languages. The various nullability attributes
indicate whether a particular pointer can be null or not, which makes APIs more
expressive and can help static analysis tools identify bugs involving null
pointers. Clang supports several kinds of nullability attributes: the
nonnull
and returns_nonnull
attributes indicate which function or
method parameters and result types can never be null, while nullability type
qualifiers indicate which pointer types can be null (_Nullable
) or cannot
be null (_Nonnull
).
The nullability (type) qualifiers express whether a value of a given pointer
type can be null (the _Nullable
qualifier), doesn’t have a defined meaning
for null (the _Nonnull
qualifier), or for which the purpose of null is
unclear (the _Null_unspecified
qualifier). Because nullability qualifiers
are expressed within the type system, they are more general than the
nonnull
and returns_nonnull
attributes, allowing one to express (for
example) a nullable pointer to an array of nonnull pointers. Nullability
qualifiers are written to the right of the pointer to which they apply. For
example:
// No meaningful result when 'ptr' is null (here, it happens to be undefined behavior). int fetch(int * _Nonnull ptr) { return *ptr; } // 'ptr' may be null. int fetch_or_zero(int * _Nullable ptr) { return ptr ? *ptr : 0; } // A nullable pointer to non-null pointers to const characters. const char *join_strings(const char * _Nonnull * _Nullable strings, unsigned n);
In Objective-C, there is an alternate spelling for the nullability qualifiers that can be used in Objective-C methods and properties using context-sensitive, non-underscored keywords. For example:
@interface NSView : NSResponder - (nullable NSView *)ancestorSharedWithView:(nonnull NSView *)aView; @property (assign, nullable) NSView *superview; @property (readonly, nonnull) NSArray *subviews; @end
As well as built-in pointer types, the nullability attributes can be attached
to C++ classes marked with the _Nullable
attribute.
The following C++ standard library types are considered nullable:
unique_ptr
, shared_ptr
, auto_ptr
, exception_ptr
, function
,
move_only_function
and coroutine_handle
.
Types should be marked nullable only where the type itself leaves nullability
ambiguous. For example, std::optional
is not marked _Nullable
, because
optional<int> _Nullable
is redundant and optional<int> _Nonnull
is
not a useful type. std::weak_ptr
is not nullable, because its nullability
can change with no visible modification, so static annotation is unlikely to be
unhelpful.
_Nonnull¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The _Nonnull
nullability qualifier indicates that null is not a meaningful
value for a value of the _Nonnull
pointer type. For example, given a
declaration such as:
int fetch(int * _Nonnull ptr);
a caller of fetch
should not provide a null value, and the compiler will
produce a warning if it sees a literal null value passed to fetch
. Note
that, unlike the declaration attribute nonnull
, the presence of
_Nonnull
does not imply that passing null is undefined behavior: fetch
is free to consider null undefined behavior or (perhaps for
backward-compatibility reasons) defensively handle null.
_Null_unspecified¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The _Null_unspecified
nullability qualifier indicates that neither the
_Nonnull
nor _Nullable
qualifiers make sense for a particular pointer
type. It is used primarily to indicate that the role of null with specific
pointers in a nullability-annotated header is unclear, e.g., due to
overly-complex implementations or historical factors with a long-lived API.
_Nullable¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The _Nullable
nullability qualifier indicates that a value of the
_Nullable
pointer type can be null. For example, given:
int fetch_or_zero(int * _Nullable ptr);
a caller of fetch_or_zero
can provide null.
The _Nullable
attribute on classes indicates that the given class can
represent null values, and so the _Nullable
, _Nonnull
etc qualifiers
make sense for this type. For example:
class _Nullable ArenaPointer { ... }; ArenaPointer _Nonnull x = ...; ArenaPointer _Nullable y = nullptr;
_Nullable_result¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
The _Nullable_result
nullability qualifier means that a value of the
_Nullable_result
pointer can be nil
, just like _Nullable
. Where this
attribute differs from _Nullable
is when it’s used on a parameter to a
completion handler in a Swift async method. For instance, here:
-(void)fetchSomeDataWithID:(int)identifier completionHandler:(void (^)(Data *_Nullable_result result, NSError *error))completionHandler;
This method asynchronously calls completionHandler
when the data is
available, or calls it with an error. _Nullable_result
indicates to the
Swift importer that this is the uncommon case where result
can get nil
even if no error has occurred, and will therefore import it as a Swift optional
type. Otherwise, if result
was annotated with _Nullable
, the Swift
importer will assume that result
will always be non-nil unless an error
occurred.
nonnull¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The nonnull
attribute indicates that some function parameters must not be
null, and can be used in several different ways. It’s original usage
(from GCC)
is as a function (or Objective-C method) attribute that specifies which
parameters of the function are nonnull in a comma-separated list. For example:
extern void * my_memcpy (void *dest, const void *src, size_t len) __attribute__((nonnull (1, 2)));
Here, the nonnull
attribute indicates that parameters 1 and 2
cannot have a null value. Omitting the parenthesized list of parameter indices
means that all parameters of pointer type cannot be null:
extern void * my_memcpy (void *dest, const void *src, size_t len) __attribute__((nonnull));
Clang also allows the nonnull
attribute to be placed directly on a function
(or Objective-C method) parameter, eliminating the need to specify the
parameter index ahead of type. For example:
extern void * my_memcpy (void *dest __attribute__((nonnull)), const void *src __attribute__((nonnull)), size_t len);
Note that the nonnull
attribute indicates that passing null to a non-null
parameter is undefined behavior, which the optimizer may take advantage of to,
e.g., remove null checks. The _Nonnull
type qualifier indicates that a
pointer cannot be null in a more general manner (because it is part of the type
system) and does not imply undefined behavior, making it more widely applicable.
returns_nonnull¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Yes |
The returns_nonnull
attribute indicates that a particular function (or
Objective-C method) always returns a non-null pointer. For example, a
particular system malloc
might be defined to terminate a process when
memory is not available rather than returning a null pointer:
extern void * malloc (size_t size) __attribute__((returns_nonnull));
The returns_nonnull
attribute implies that returning a null pointer is
undefined behavior, which the optimizer may take advantage of. The _Nonnull
type qualifier indicates that a pointer cannot be null in a more general manner
(because it is part of the type system) and does not imply undefined behavior,
making it more widely applicable
OpenCL Address Spaces¶
The address space qualifier may be used to specify the region of memory that is used to allocate the object. OpenCL supports the following address spaces: __generic(generic), __global(global), __local(local), __private(private), __constant(constant).
__constant int c = ...; __generic int* foo(global int* g) { __local int* l; private int p; ... return l; }
More details can be found in the OpenCL C language Spec v2.0, Section 6.5.
[[clang::opencl_global_device]], [[clang::opencl_global_host]]¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The global_device
and global_host
address space attributes specify that
an object is allocated in global memory on the device/host. It helps to
distinguish USM (Unified Shared Memory) pointers that access global device
memory from those that access global host memory. These new address spaces are
a subset of the __global/opencl_global
address space, the full address space
set model for OpenCL 2.0 with the extension looks as follows:
generic->global->host->device->private->localconstant
As global_device
and global_host
are a subset of
__global/opencl_global
address spaces it is allowed to convert
global_device
and global_host
address spaces to
__global/opencl_global
address spaces (following ISO/IEC TR 18037 5.1.3
“Address space nesting and rules for pointers”).
[[clang::opencl_global_device]], [[clang::opencl_global_host]]¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
The global_device
and global_host
address space attributes specify that
an object is allocated in global memory on the device/host. It helps to
distinguish USM (Unified Shared Memory) pointers that access global device
memory from those that access global host memory. These new address spaces are
a subset of the __global/opencl_global
address space, the full address space
set model for OpenCL 2.0 with the extension looks as follows:
generic->global->host->device->private->localconstant
As global_device
and global_host
are a subset of
__global/opencl_global
address spaces it is allowed to convert
global_device
and global_host
address spaces to
__global/opencl_global
address spaces (following ISO/IEC TR 18037 5.1.3
“Address space nesting and rules for pointers”).
__constant, constant, [[clang::opencl_constant]]¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
The constant address space attribute signals that an object is located in a constant (non-modifiable) memory region. It is available to all work items. Any type can be annotated with the constant address space attribute. Objects with the constant address space qualifier can be declared in any scope and must have an initializer.
__generic, generic, [[clang::opencl_generic]]¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
The generic address space attribute is only available with OpenCL v2.0 and later. It can be used with pointer types. Variables in global and local scope and function parameters in non-kernel functions can have the generic address space type attribute. It is intended to be a placeholder for any other address space except for ‘__constant’ in OpenCL code which can be used with multiple address spaces.
__global, global, [[clang::opencl_global]]¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
The global address space attribute specifies that an object is allocated in global memory, which is accessible by all work items. The content stored in this memory area persists between kernel executions. Pointer types to the global address space are allowed as function parameters or local variables. Starting with OpenCL v2.0, the global address space can be used with global (program scope) variables and static local variable as well.
__local, local, [[clang::opencl_local]]¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
The local address space specifies that an object is allocated in the local (work group) memory area, which is accessible to all work items in the same work group. The content stored in this memory region is not accessible after the kernel execution ends. In a kernel function scope, any variable can be in the local address space. In other scopes, only pointer types to the local address space are allowed. Local address space variables cannot have an initializer.
__private, private, [[clang::opencl_private]]¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
|
The private address space specifies that an object is allocated in the private (work item) memory. Other work items cannot access the same memory area and its content is destroyed after work item execution ends. Local variables can be declared in the private address space. Function arguments are always in the private address space. Kernel function arguments of a pointer or an array type cannot point to the private address space.
Performance Constraint Attributes¶
The nonblocking
, blocking
, nonallocating
and allocating
attributes can be attached
to function types, including blocks, C++ lambdas, and member functions. The attributes declare
constraints about a function’s behavior pertaining to blocking and heap memory allocation.
There are several rules for function types with these attributes, enforced with compiler warnings:
When assigning or otherwise converting to a function pointer of
nonblocking
ornonallocating
type, the source must also be a function or function pointer of that type, unless it is a null pointer, i.e. the attributes should not be “spoofed”. Conversions that remove the attributes are transparent and valid.An override of a
nonblocking
ornonallocating
virtual method must also be declared with that same attribute (or a stronger one.) An overriding method may add an attribute.A redeclaration of a
nonblocking
ornonallocating
function must also be declared with the same attribute (or a stronger one). A redeclaration may add an attribute.
The warnings are controlled by -Wfunction-effects
, which is disabled by default.
The compiler also diagnoses function calls from nonblocking
and nonallocating
functions to other functions which lack the appropriate attribute.
allocating¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Declares that a function potentially allocates heap memory, and prevents any potential inference
of nonallocating
by the compiler.
blocking¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Declares that a function potentially blocks, and prevents any potential inference of nonblocking
by the compiler.
nonallocating¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Declares that a function or function type either does or does not allocate heap memory, according
to the optional, compile-time constant boolean argument, which defaults to true. When the argument
is false, the attribute is equivalent to allocating
.
nonblocking¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
|
|
|
Declares that a function or function type either does or does not block in any way, according
to the optional, compile-time constant boolean argument, which defaults to true. When the argument
is false, the attribute is equivalent to blocking
.
For the purposes of diagnostics, nonblocking
is considered to include the
nonallocating
guarantee and is therefore a “stronger” constraint or attribute.
Statement Attributes¶
#pragma clang loop¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
`` loop`` |
The #pragma clang loop
directive allows loop optimization hints to be
specified for the subsequent loop. The directive allows pipelining to be
disabled, or vectorization, vector predication, interleaving, and unrolling to
be enabled or disabled. Vector width, vector predication, interleave count,
unrolling count, and the initiation interval for pipelining can be explicitly
specified. See language extensions
for details.
#pragma unroll, #pragma nounroll¶
GNU |
C++11 |
C23 |
|
Keyword |
|
HLSL Annotation |
|
---|---|---|---|---|---|---|---|
`` loop`` |
Loop unrolling optimization hints can be specified with #pragma unroll
and
#pragma nounroll
. The pragma is placed immediately before a for, while,
do-while, or c++11 range-based for loop. GCC’s loop unrolling hints
#pragma GCC unroll
and #pragma GCC nounroll
are also supported and have
identical semantics to #pragma unroll
and #pragma nounroll
.
Specifying #pragma unroll
without a parameter directs the loop unroller to
attempt to fully unroll the loop if the trip count is known at compile time and
attempt to partially unroll the loo