9

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?

embedded_guy
  • 1,939
  • 3
  • 24
  • 39

2 Answers2

9

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.

Sean Perry
  • 3,776
  • 1
  • 19
  • 31
  • 1
    For completeness the current docs are here: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html – alk May 28 '14 at 17:05
  • On debian's gcc 10.2.1, attributes can be used in function declarations, but the attribute must come before the declarator, ie. precede the function signature. I don't know if both can be used as asked. – crass Dec 24 '22 at 02:40
1

As Sean Perry says, it would appear that GCC only specifies that special attributes can be used with declarations.

I was digging some more into the ARMCC docs and finally found what I was looking for here:

You can set these function attributes in the declaration, the definition, or both.

So, for ARMCC my use of __attribute__ as shown in the OP is safe, but that is not true for GCC.

embedded_guy
  • 1,939
  • 3
  • 24
  • 39
  • Thanks for the ARMCC bit. I was not familiar enough with it or its docs. Since you asked for either, I figured mentioning it was not proper use with GCC was sufficient. – Sean Perry May 28 '14 at 18:53
  • @Sean Good point. You did answer my question regarding GCC so I have selected yours as the correct answer. Thanks for finding the info for me. I read that section of the GCC docs before I even asked the question, but I had 'prototype' on my brain and wasn't thinking 'declaration'. Good find. – embedded_guy May 28 '14 at 20:15