What is __attribute__((unused)) static?
Asked Answered
B

2

5

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?

Bugger answered 6/10, 2018 at 20:44 Comment(0)
O
8

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).

Optimistic answered 6/10, 2018 at 20:50 Comment(0)
C
6

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.

Carmacarmack answered 6/10, 2018 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.