Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc warns about unused static functions, but not static inline: is there a practical distinction?

My version (5.4) of gcc warns about unused static functions, even in a header file when -Wall is used. It doesn't complain if the same functions are defined static inline or simply inline.

For example, the following function in a file unused.h:

static void foo() {}

... when included in a test.cpp file as follows:

#include "unused.h"

Generates the following compiler diagnostic when compiler with -Wall:

In file included from test.cpp:11:0:
unused.h: At global scope:
unused.h:9:13: warning: ‘void foo()’ defined but not used [-Wunused-function]
 static void foo() {}
             ^

It is common practice, as far as I know, to include headers with many utility functions, only a handful of which might be used in any given source file. This behavior means that I get warnings for any functions I don't use which are declared only static.

As a practical matter I can simply change these to static inline to get rid of the warning (or turn off the specific warning entirely, but I do find it useful from time to time), but it seems that large utility functions which won't benefit from inlining1 are more logically declared static2.

As far as I know unused static functions (just like static inline) are simply dropped by gcc when the translation unit is compiled, so they pose no binary size or link-time overhead at all.

Am I missing something here? Is there a good reason that unused static functions are more problematic than static inline?


1 Yes, I'm aware it's a hint only but gcc actually takes the hint in many cases.

2 Or perhaps better, only declared in the header file and defined somewhere else in a .cpp file - but that inhibits header-only use which is sometimes convenient.

like image 258
BeeOnRope Avatar asked Nov 28 '17 23:11

BeeOnRope


People also ask

Do inline functions need to be static?

In C99, an inline or extern inline function must not access static global variables or define non- const static local variables. const static local variables may or may not be different objects in different translation units, depending on whether the function was inlined or whether a call was made.

What does static inline mean in C?

In C, static means the function or variable you define can be only used in this file(i.e. the compile unit) So, static inline means the inline function which can be used in this file only.

What is __ Static_inline?

#define __STATIC_INLINE. Define a static function that may be inlined by the compiler. Defines a static function that may be inlined by the compiler. If the compiler generates inline code for all calls to this functions, no additional function implementation is generated which may further optimize space.


1 Answers

The warning is because an unused static function might indicate a logic error: why would you have written such a function if it was never called?

However, it is a common idiom to have static inline functions in a header file. Those functions might only be used by some translation units that include the header. It would be annoying if the compiler gave a warning for a translation unit that didn't happen to use one of the functions.

If you deliberately have an unused static non-inline function, you probably will want to either disable the warning entirely, or use a compiler-specific feature to suppress the warning for that function.


Someone asked, "why would you use static inline anyway?". Well, in new C++ you mostly wouldn't use it. However, in C it is a reasonable thing to do. (This is because static inline means the same thing in ISO C and GNU C; however inline without static behaves differently in ISO C than GNU C, so defaulting to static inline just avoids all those issues with no down-side).

People might use static inline in headers that are to be included from both .c and .cpp files; or perhaps they just carry over that habit from C to C++. In the latter case, IMHO it would be annoying for the compiler to warn about something that, although unnecessary, is not a mistake or a problem either.

like image 80
M.M Avatar answered Sep 17 '22 19:09

M.M