static inline functions in headers and -Wunused-function

Hi all,

I try to use -Werror -Wall for my project, which includes -Wunused-function.

Unfortunately, I receive a couple of warnings from library header files which include

static inline … func() { … }

These functions are indeed often unused, but as of my understanding this should be perfectly fine with static inline functions (they just replace macro definitions in a better way).

Am I wrong here? What’s the best path to fix this?

version: Ubuntu clang version 3.6.2-1 (tags/RELEASE_362/final) (based on LLVM 3.6.2)

Any help is deeply appreciated.

Rainer

Hi all,

I try to use -Werror -Wall for my project, which includes
-Wunused-function.

Unfortunately, I receive a couple of warnings from library header files
which include

static inline ... func() { ... }

These functions are indeed often unused, but as of my understanding this
should be perfectly fine with static inline functions (they just replace
macro definitions in a better way).

Am I wrong here? What's the best path to fix this?

Ish. They shuold probably just be "inline" without the static.

static functions are distinct functions per translation unit - so having
static functions in headers has a number of problems:

* The function definitions won't be deduplicated by the linker across
object files -> code bloat (larger binaries) & address inequality
* This can lead to technical (& undiagnosed) Undefined Behavior due to ODR
violations

Consider the following header:

static inline void f() { }
struct foo {
  void g() { f();
};

Include that header in two .cpp files, compile and link the two objects
together and the program now violates the ODR

The violation is in 'g' which is a (non-static, in the sense we're talking
about here - though it could be class-static and still reproduce the
problem) inline function. Therefore its definition must be the same in
every translation unit - by "the same", the C++ standard says the same
sequence of tokens (which it is) and that every name lookup finds the same
entities (which it doesn't - because 'f' in one translation unit is a
distinct 'f' from the other translation unit because of the 'static'
keyword)).

So the assumption is that, generally, file/namespace-scope-static (as
opposed to class scoped static, which is a different thing) functions do
not go in headers. Or, put another way - static functions are there to be
used in the translation unit that contains their definition. So that's why
the warning fires on your code & why removing 'static' is a good/reasonable
fix to the warning and what it is, tangentially, trying to warn you about.

Hope that helps,
- Dave

[Please reply *only* to the list and do not include my email directly
in the To: or Cc: of your reply; otherwise I will not see your reply.
Thanks.]

In article <CAENS6Evwx8k=eq7hicg5bj0jbysOOFmVQV3fy4B6p3xAaEqMkw@mail.gmail.com>,
    David Blaikie via cfe-users <cfe-users@lists.llvm.org> writes:

[Please reply *only* to the list and do not include my email directly
in the To: or Cc: of your reply; otherwise I will not see your reply.
Thanks.]

In article <CAENS6Evwx8k=eq7hicg5bj0jbysOOFmVQV3fy4B6p3xAaEqMkw@mail.gmail.com>,
    David Blaikie via cfe-users <cfe-users@lists.llvm.org> writes:

Thanks David and Richard for the explanations. This really helps. Looks like I need to re-read my language manual :wink:

Rainer