[cfe-users] static inline functions in headers and -Wunused-function

2016-01-27 Thread Rainer Gerhards via cfe-users
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
___
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users


Re: [cfe-users] static inline functions in headers and -Wunused-function

2016-01-27 Thread David Blaikie via cfe-users
On Wed, Jan 27, 2016 at 3:01 AM, Rainer Gerhards via cfe-users <
cfe-users@lists.llvm.org> wrote:

> 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


>
> version: Ubuntu clang version 3.6.2-1 (tags/RELEASE_362/final) (based on
> LLVM 3.6.2)
>
> Any help is deeply appreciated.
>
> Rainer
>
> ___
> cfe-users mailing list
> cfe-users@lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>
>
___
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users


Re: [cfe-users] static inline functions in headers and -Wunused-function

2016-01-27 Thread Richard via cfe-users

[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 ,
David Blaikie via cfe-users  writes:

> On Wed, Jan 27, 2016 at 3:01 AM, Rainer Gerhards via cfe-users <
> cfe-users@lists.llvm.org> wrote:

> > Am I wrong here? What's the best path to fix this?
> >
> 
> Ish. They shuold probably just be "inline" without the static.

+1 for all the reasons David explains :-).

(He beat me to it! LOL.)

-- 
"The Direct3D Graphics Pipeline" free book 
 The Computer Graphics Museum 
 The Terminals Wiki 
  Legalize Adulthood! (my blog) 
___
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users


Re: [cfe-users] static inline functions in headers and -Wunused-function

2016-01-27 Thread Richard via cfe-users

[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 ,
David Blaikie via cfe-users  writes:

> On Wed, Jan 27, 2016 at 3:01 AM, Rainer Gerhards via cfe-users <
> cfe-users@lists.llvm.org> wrote:

> > Am I wrong here? What's the best path to fix this?
> >
> 
> Ish. They shuold probably just be "inline" without the static.

+1 for all the reasons David explains :-).

(He beat me to it! LOL.)

-- 
"The Direct3D Graphics Pipeline" free book 
 The Computer Graphics Museum 
 The Terminals Wiki 
  Legalize Adulthood! (my blog) 
___
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users


Re: [cfe-users] static inline functions in headers and -Wunused-function

2016-01-27 Thread Rainer Gerhards via cfe-users
Thanks David and Richard for the explanations. This really helps. Looks
like I need to re-read my language manual ;)

Rainer

2016-01-27 18:09 GMT+01:00 Richard via cfe-users :

>
> [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  eq7hicg5bj0jbysoofmvqv3fy4b6p3xaaeq...@mail.gmail.com>,
> David Blaikie via cfe-users  writes:
>
> > On Wed, Jan 27, 2016 at 3:01 AM, Rainer Gerhards via cfe-users <
> > cfe-users@lists.llvm.org> wrote:
>
> > > Am I wrong here? What's the best path to fix this?
> > >
> >
> > Ish. They shuold probably just be "inline" without the static.
>
> +1 for all the reasons David explains :-).
>
> (He beat me to it! LOL.)
>
> --
> "The Direct3D Graphics Pipeline" free book <
> http://tinyurl.com/d3d-pipeline>
>  The Computer Graphics Museum 
>  The Terminals Wiki 
>   Legalize Adulthood! (my blog) 
> ___
> cfe-users mailing list
> cfe-users@lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>
___
cfe-users mailing list
cfe-users@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users