Declaring Attributes of Functions
=================================
In GNU C, you declare certain things about functions called in your
program which help the compiler optimize function calls and check your
code more carefully.
The keyword `__attribute__' allows you to specify special attributes
when making a declaration. This keyword is followed by an attribute
specification inside double parentheses. Nine attributes, `noreturn',
`const', `format', `no_instrument_function', `section', `constructor',
`destructor', `unused' and `weak' are currently defined for functions.
Other attributes, including `section' are supported for variables
declarations (Note:Variable Attributes.) and for types (Note:Type
Attributes.).
You may also specify attributes with `__' preceding and following
each keyword. This allows you to use them in header files without
being concerned about a possible macro of the same name. For example,
you may use `__noreturn__' instead of `noreturn'.
`noreturn'
A few standard library functions, such as `abort' and `exit',
cannot return. GNU CC knows this automatically. Some programs
define their own functions that never return. You can declare them
`noreturn' to tell the compiler this fact. For example,
void fatal () __attribute__ ((noreturn));
void
fatal (...)
{
... /* Print error message. */ ...
exit (1);
}
The `noreturn' keyword tells the compiler to assume that `fatal'
cannot return. It can then optimize without regard to what would
happen if `fatal' ever did return. This makes slightly better
code. More importantly, it helps avoid spurious warnings of
uninitialized variables.
Do not assume that registers saved by the calling function are
restored before calling the `noreturn' function.
It does not make sense for a `noreturn' function to have a return
type other than `void'.
The attribute `noreturn' is not implemented in GNU C versions
earlier than 2.5. An alternative way to declare that a function
does not return, which works in the current version and in some
older versions, is as follows:
typedef void voidfn ();
volatile voidfn fatal;
`const'
Many functions do not examine any values except their arguments,
and have no effects except the return value. Such a function can
be subject to common subexpression elimination and loop
optimization just as an arithmetic operator would be. These
functions should be declared with the attribute `const'. For
example,
int square (int) __attribute__ ((const));
says that the hypothetical function `square' is safe to call fewer
times than the program says.
The attribute `const' is not implemented in GNU C versions earlier
than 2.5. An alternative way to declare that a function has no
side effects, which works in the current version and in some older
versions, is as follows:
typedef int intfn ();
extern const intfn square;
This approach does not work in GNU C++ from 2.6.0 on, since the
language specifies that the `const' must be attached to the return
value.
Note that a function that has pointer arguments and examines the
data pointed to must *not* be declared `const'. Likewise, a
function that calls a non-`const' function usually must not be
`const'. It does not make sense for a `const' function to return
`void'.
`format (ARCHETYPE, STRING-INDEX, FIRST-TO-CHECK)'
The `format' attribute specifies that a function takes `printf',
`scanf', or `strftime' style arguments which should be type-checked
against a format string. For example, the declaration:
extern int
my_printf (void *my_object, const char *my_format, ...)
__attribute__ ((format (printf, 2, 3)));
causes the compiler to check the arguments in calls to `my_printf'
for consistency with the `printf' style format string argument
`my_format'.
The parameter ARCHETYPE determines how the format string is
interpreted, and should be either `printf', `scanf', or
`strftime'. The parameter STRING-INDEX specifies which argument
is the format string argument (starting from 1), while
FIRST-TO-CHECK is the number of the first argument to check
against the format string. For functions where the arguments are
not available to be checked (such as `vprintf'), specify the third
parameter as zero. In this case the compiler only checks the
format string for consistency.
In the example above, the format string (`my_format') is the second
argument of the function `my_print', and the arguments to check
start with the third argument, so the correct parameters for the
format attribute are 2 and 3.
The `format' attribute allows you to identify your own functions
which take format strings as arguments, so that GNU CC can check
the calls to these functions for errors. The compiler always
checks formats for the ANSI library functions `printf', `fprintf',
`sprintf', `scanf', `fscanf', `sscanf', `strftime', `vprintf',
`vfprintf' and `vsprintf' whenever such warnings are requested
(using `-Wformat'), so there is no need to modify the header file
`stdio.h'.
`format_arg (STRING-INDEX)'
The `format_arg' attribute specifies that a function takes
`printf' or `scanf' style arguments, modifies it (for example, to
translate it into another language), and passes it to a `printf'
or `scanf' style function. For example, the declaration:
extern char *
my_dgettext (char *my_domain, const char *my_format)
__attribute__ ((format_arg (2)));
causes the compiler to check the arguments in calls to
`my_dgettext' whose result is passed to a `printf', `scanf', or
`strftime' type function for consistency with the `printf' style
format string argument `my_format'.
The parameter STRING-INDEX specifies which argument is the format
string argument (starting from 1).
The `format-arg' attribute allows you to identify your own
functions which modify format strings, so that GNU CC can check the
calls to `printf', `scanf', or `strftime' function whose operands
are a call to one of your own function. The compiler always
treats `gettext', `dgettext', and `dcgettext' in this manner.
`no_instrument_function'
If `-finstrument-functions' is given, profiling function calls will
be generated at entry and exit of most user-compiled functions.
Functions with this attribute will not be so instrumented.
`section ("section-name")'
Normally, the compiler places the code it generates in the `text'
section. Sometimes, however, you need additional sections, or you
need certain particular functions to appear in special sections.
The `section' attribute specifies that a function lives in a
particular section. For example, the declaration:
extern void foobar (void) __attribute__ ((section ("bar")));
puts the function `foobar' in the `bar' section.
Some file formats do not support arbitrary sections so the
`section' attribute is not available on all platforms. If you
need to map the entire contents of a module to a particular
section, consider using the facilities of the linker instead.
`constructor'
`destructor'
The `constructor' attribute causes the function to be called
automatically before execution enters `main ()'. Similarly, the
`destructor' attribute causes the function to be called
automatically after `main ()' has completed or `exit ()' has been
called. Functions with these attributes are useful for
initializing data that will be used implicitly during the
execution of the program.
These attributes are not currently implemented for Objective C.
`unused'
This attribute, attached to a function, means that the function is
meant to be possibly unused. GNU CC will not produce a warning
for this function. GNU C++ does not currently support this
attribute as definitions without parameters are valid in C++.
`weak'
The `weak' attribute causes the declaration to be emitted as a weak
symbol rather than a global. This is primarily useful in defining
library functions which can be overridden in user code, though it
can also be used with non-function declarations. Weak symbols are
supported for ELF targets, and also for a.out targets when using
the GNU assembler and linker.
`alias ("target")'
The `alias' attribute causes the declaration to be emitted as an
alias for another symbol, which must be specified. For instance,
void __f () { /* do something */; }
void f () __attribute__ ((weak, alias ("__f")));
declares `f' to be a weak alias for `__f'. In C++, the mangled
name for the target must be used.
Not all target machines support this attribute.
`no_check_memory_usage'
If `-fcheck-memory-usage' is given, calls to support routines will
be generated before most memory accesses, to permit support code to
record usage and detect uses of uninitialized or unallocated
storage. Since the compiler cannot handle them properly, `asm'
statements are not allowed. Declaring a function with this
attribute disables the memory checking code for that function,
permitting the use of `asm' statements without requiring separate
compilation with different options, and allowing you to write
support routines of your own if you wish, without getting infinite
recursion if they get compiled with this option.
`regparm (NUMBER)'
On the Intel 386, the `regparm' attribute causes the compiler to
pass up to NUMBER integer arguments in registers EAX, EDX, and ECX
instead of on the stack. Functions that take a variable number of
arguments will continue to be passed all of their arguments on the
stack.
`stdcall'
On the Intel 386, the `stdcall' attribute causes the compiler to
assume that the called function will pop off the stack space used
to pass arguments, unless it takes a variable number of arguments.
The PowerPC compiler for Windows NT currently ignores the `stdcall'
attribute.
`cdecl'
On the Intel 386, the `cdecl' attribute causes the compiler to
assume that the calling function will pop off the stack space used
to pass arguments. This is useful to override the effects of the
`-mrtd' switch.
The PowerPC compiler for Windows NT currently ignores the `cdecl'
attribute.
`longcall'
On the RS/6000 and PowerPC, the `longcall' attribute causes the
compiler to always call the function via a pointer, so that
functions which reside further than 64 megabytes (67,108,864
bytes) from the current location can be called.
`dllimport'
On the PowerPC running Windows NT, the `dllimport' attribute causes
the compiler to call the function via a global pointer to the
function pointer that is set up by the Windows NT dll library.
The pointer name is formed by combining `__imp_' and the function
name.
`dllexport'
On the PowerPC running Windows NT, the `dllexport' attribute causes
the compiler to provide a global pointer to the function pointer,
so that it can be called with the `dllimport' attribute. The
pointer name is formed by combining `__imp_' and the function name.
`exception (EXCEPT-FUNC [, EXCEPT-ARG])'
On the PowerPC running Windows NT, the `exception' attribute causes
the compiler to modify the structured exception table entry it
emits for the declared function. The string or identifier
EXCEPT-FUNC is placed in the third entry of the structured
exception table. It represents a function, which is called by the
exception handling mechanism if an exception occurs. If it was
specified, the string or identifier EXCEPT-ARG is placed in the
fourth entry of the structured exception table.
`function_vector'
Use this option on the H8/300 and H8/300H to indicate that the
specified function should be called through the function vector.
Calling a function through the function vector will reduce code
size, however; the function vector has a limited size (maximum 128
entries on the H8/300 and 64 entries on the H8/300H) and shares
space with the interrupt vector.
You must use GAS and GLD from GNU binutils version 2.7 or later for
this option to work correctly.
`interrupt_handler'
Use this option on the H8/300 and H8/300H to indicate that the
specified function is an interrupt handler. The compiler will
generate function entry and exit sequences suitable for use in an
interrupt handler when this attribute is present.
`eightbit_data'
Use this option on the H8/300 and H8/300H to indicate that the
specified variable should be placed into the eight bit data
section. The compiler will generate more efficient code for
certain operations on data in the eight bit data area. Note the
eight bit data area is limited to 256 bytes of data.
You must use GAS and GLD from GNU binutils version 2.7 or later for
this option to work correctly.
`tiny_data'
Use this option on the H8/300H to indicate that the specified
variable should be placed into the tiny data section. The
compiler will generate more efficient code for loads and stores on
data in the tiny data section. Note the tiny data area is limited
to slightly under 32kbytes of data.
`interrupt'
Use this option on the M32R/D to indicate that the specified
function is an interrupt handler. The compiler will generate
function entry and exit sequences suitable for use in an interrupt
handler when this attribute is present.
`model (MODEL-NAME)'
Use this attribute on the M32R/D to set the addressability of an
object, and the code generated for a function. The identifier
MODEL-NAME is one of `small', `medium', or `large', representing
each of the code models.
Small model objects live in the lower 16MB of memory (so that their
addresses can be loaded with the `ld24' instruction), and are
callable with the `bl' instruction.
Medium model objects may live anywhere in the 32 bit address space
(the compiler will generate `seth/add3' instructions to load their
addresses), and are callable with the `bl' instruction.
Large model objects may live anywhere in the 32 bit address space
(the compiler will generate `seth/add3' instructions to load their
addresses), and may not be reachable with the `bl' instruction
(the compiler will generate the much slower `seth/add3/jl'
instruction sequence).
You can specify multiple attributes in a declaration by separating
them by commas within the double parentheses or by immediately
following an attribute declaration with another attribute declaration.
Some people object to the `__attribute__' feature, suggesting that
ANSI C's `#pragma' should be used instead. There are two reasons for
not doing this.
1. It is impossible to generate `#pragma' commands from a macro.
2. There is no telling what the same `#pragma' might mean in another
compiler.
These two reasons apply to almost any application that might be
proposed for `#pragma'. It is basically a mistake to use `#pragma' for
*anything*.