Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is __attribute__((unused)) static?

Tags:

c

gcc

gnu

libuv

In libuv file heap-inl.h, I see the following macro

#if defined(__GNUC__)
# define HEAP_EXPORT(declaration) __attribute__((unused)) static declaration
...
HEAP_EXPORT(void heap_init(struct heap* heap));
...

heap-inl.h is included in a source file loop.c that then uses the declared function heap_init.

From what I interpret...

  • heap-inl.h stands for heap "inline"?
  • HEAP_EXPORT is exporting a function to be used by other source files.

What I don't understand is why an exported function is marked __attribute((unused))__. Also, why is it also a static declaration? I thought static functions can only be used in the file it is defined in. Also, what does in-lining have to do with any of this?

like image 428
richizy Avatar asked Dec 14 '22 14:12

richizy


2 Answers

The static keyword indicates that the function is local to the compiled file. When it's in a header, it means that it is included in all compiled files. Then the issue is that if this function is not used, some compilers (clang, gcc, VS with the proper files) will generate a warning. Tagging the function as unused will remove this warning (and potential error if the warning is considered as an error).

So HEAP_EXPORT is not really exporting anything, just making the function available (if the body is also in the header, which is the case if the file is named -inl, which is indeed to indicate that the content will be inlined in a compiled file).

like image 174
Matthieu Brucher Avatar answered Dec 28 '22 00:12

Matthieu Brucher


As described here:

unused

This attribute, attached to a function, means that the function is meant to be possibly unused. GCC does not produce a warning for this function.

This attribute also has the added benefit that, depending on circumstances, the function might not be emitted at all (it won't use space in the compiled file) if it's never called.

This is often used with static functions in header libraries, so only the functions that are actually used are emitted as machine code and warnings are avoided.

like image 22
Myst Avatar answered Dec 28 '22 01:12

Myst