GCC allows attributes to be set on statements. See Attribute Syntax, for details of the exact syntax for using attributes. Other attributes are available for functions (see Declaring Attributes of Functions), variables (see Specifying Attributes of Variables), labels (see Label Attributes), enumerators (see Enumerator Attributes), and for types (see Specifying Attributes of Types).
fallthrough
¶The fallthrough
attribute with a null statement serves as a
fallthrough statement. It hints to the compiler that a statement
that falls through to another case label, or user-defined label
in a switch statement is intentional and thus the
-Wimplicit-fallthrough warning must not trigger. The
fallthrough attribute may appear at most once in each attribute
list, and may not be mixed with other attributes. It can only
be used in a switch statement (the compiler will issue an error
otherwise), after a preceding statement and before a logically
succeeding case label, or user-defined label.
This example uses the fallthrough
statement attribute to indicate that
the -Wimplicit-fallthrough warning should not be emitted:
switch (cond) { case 1: bar (1); __attribute__((fallthrough)); case 2: … }
assume
¶The assume
attribute with a null statement serves as portable
assumption. It should have a single argument, a conditional expression,
which is not evaluated. If the argument would evaluate to true
at the point where it appears, it has no effect, otherwise there
is undefined behavior. This is a GNU variant of the ISO C++23
standard assume
attribute, but it can be used in any version of
both C and C++.
int foo (int x, int y) { __attribute__((assume(x == 42))); __attribute__((assume(++y == 43))); return x + y; }
y
is not actually incremented and the compiler can but does not
have to optimize it to just return 42 + 42;
.
musttail
¶The gnu::musttail
or clang::musttail
attribute
can be applied to a return
statement with a return-value expression
that is a function call. It asserts that the call must be a tail call that
does not allocate extra stack space, so it is safe to use tail recursion
to implement long running loops.
[[gnu::musttail]] return foo();
If the compiler cannot generate a musttail
tail call it will report
an error. On some targets tail calls may never be supported.
Tail calls cannot reference locals in memory, which may affect
builds without optimization when passing small structures, or passing
or returning large structures. Enabling -O1 or -O2 can
improve the success of tail calls.