Search code examples
c++cgccarmcc

Can an __attribute__ specifier be used with both the function prototype and the function definition?


My question directly pertains to the __attribute__((noreturn)) but more generally could pertain to others as well - such as __attribute__(noinline). I have looked at both the gcc manual and the Keil compiler reference guide to determine what the proper syntax is for using __attribute__ with a function. What I have generally seen is the following:

void function (void) __attribute__((noreturn));  //Prototype has __attribute__

void function (void)                             //Definition does not.
{
    while (1);
}

I have also seen the __attribute__ used before the function definition as follows:

__attribute__((noreturn)) void function (void)
{
    while (1);
}

However, I have not seen an example of it used with both the function prototype and the function definition. I think having the __attribute__ in both locations would result in better code readability; I would know by looking at either the function prototype or the definition that an attribute has been applied. The result would be as follows:

__attribute__((noreturn)) void function (void) ;  //Prototype has __attribute__

__attribute__((noreturn)) void function (void)    //Definition has __attribute__
{                                               //as well.
    while (1);
}

I have successfully compiled code with the Keil armcc compiler using my aforementioned method. Is there any reason why I should not use this method with either armcc or gcc?


Solution

  • Here is a snippet from the GCC 4.0 docs available here.

    The keyword __attribute__ allows you to specify special attributes when making a
    declaration.
    

    Note it says 'declaration' not 'definition'. This older Unix Wiz article also has lots of good advice. It also says to use attributes in declarations.