When using gcc
or clang
, it's generally a good idea to enable a number of warnings, and a first batch of warnings is generally provided by -Wall
.
This batch is pretty large, and includes the specific warning -Wunused-function
.
Now, -Wunused-function
is useful to detect static
functions which are no longer invoked, meaning they are useless, and should therefore preferably be removed from source code.
When applying a "zero-warning" policy, it's no longer "preferable", but downright compulsory.
For performance reasons, some functions may be defined directly into header files *.h
, so that they can be inlined at compile time (disregarding any kind of LTO magic). Such functions are generally declared and defined as static inline
.
In the past, such functions would probably have been defined as macros instead, but it's considered better to make them static inline
functions instead, whenever applicable (no funny type issue).
OK, so now we have a bunch of functions defined directly into header files, for performance reasons. A unit including such a header file is under no obligation to use all its declared symbols. Therefore, a static inline
function defined in a header file may reasonably not be invoked.
For gcc
, that's fine. gcc
would flag an unused static
function, but not an inline static
one.
For clang
though, the outcome is different : static inline
functions declared in headers trigger a -Wunused-function
warning if a single unit does not invoke them. And it doesn't take a lot of flags to get there : -Wall
is enough.
A work-around is to introduce a compiler-specific extension, such as __attribute__((unused))
, which explicitly states to the compiler that the function defined in the header may not necessarily be invoked by all its units.
OK, but now, the code which used to be clean C99
is including some form of specific compiler extension, adding to the weight of portability and maintenance.
The question therefore is more about the logic of such a choice : why does clang
selects to trigger a warning when a static inline
function defined in a header is not invoked ? In which case is that a good idea ?
And what does clang
proposes to cover the relatively common case of inlined functions defined in header file, without requesting the usage of compiler extension ?
edit :
After further investigation, it appears the question is incorrect.
The warning is triggered in the editor (VSCode) using clang
linter applying a selected list compilation flags (-Wall
, etc.).
But when the source code is actually compiled with clang
and with exactly the same list of flags, the "unused function" warning is not present.
So far, the results visible in the editor used to be exactly the ones found at compilation time. It's the first time I witness a difference.
So the problem seems related to the way the linter uses clang
to produce its list of warnings. That's a much more complex and specific question.
Note the comment:
OK, sorry, this is actually different from expectation. It appears the warning is triggered in the editor using
clang
linter with selected compilation flags (-Wall
, etc.). But when the source code is compiled with exactly the same flags, the "unused function" warning is actually not present. So far, the results visible in the editor used to be exactly the ones found at compilation time; it's the first time I witness a difference. So the problem seems related to the way the linter usesclang
to produce its list of warnings. It seems to be a more complex question [than I realized].