Bug 87407 - Enhance -Wunused-function to handle also inline functions
Summary: Enhance -Wunused-function to handle also inline functions
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 9.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning Wunused
  Show dependency treegraph
 
Reported: 2018-09-24 10:44 UTC by Martin Liška
Modified: 2021-04-10 18:15 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-09-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Liška 2018-09-24 10:44:00 UTC
We properly warn about:

$ cat /tmp/unused.c 
static void foo()
{
}

$ gcc /tmp/unused.c -c -Wall
/tmp/unused.c:1:13: warning: ‘foo’ defined but not used [-Wunused-function]
 static void foo()
             ^~~

but not about:

$ cat /tmp/unused.c 
static inline void foo()
{
}

Clang does that:
$ clang /tmp/unused.c  -c -Wall
/tmp/unused.c:1:20: warning: unused function 'foo' [-Wunused-function]
static inline void foo()
                   ^
1 warning generated.
Comment 1 Andrew Pinski 2018-09-24 10:47:09 UTC
This is done on purpose as static inline functions are located in many headers files including gcc's.
Comment 2 Andrew Pinski 2018-09-24 10:49:01 UTC
Note I ran into this bug with clang with the vpp project sources too.
Comment 3 Martin Liška 2018-09-24 10:50:42 UTC
Andrew I don't get it. Can you please explain why it does not make sense to warn e.g. about function that are declared in a .c file (not header file)?
Comment 4 Martin Liška 2018-09-24 10:54:48 UTC
Like these addressed here:
https://gcc.gnu.org/ml/gcc-patches/2018-09/msg01317.html
Comment 5 Andrew Pinski 2018-09-24 11:00:26 UTC
Test the warning out on clang from a header file and you will see you get the warning in the header too.  As I said I actually ran into this while working on the vpp project and cursed clang for having this warning turned on.
Comment 6 Jonathan Wakely 2018-09-24 11:10:30 UTC
I think for C++ (which is the component this bug was created for, even if comment 0 only has C examples) it makes sense to warn even for static inline functions in headers ... why would you want to define them in headers?

In C++ they should probably just be inline, not static inline.

Even if we don't want the warning for headers, it does make sense for functions defined in .C files.
Comment 7 Andrew Pinski 2018-09-24 11:14:07 UTC
Again I ran into this stupid bug in clang and had to use preprocessor macros to work around it.

I dont think we should change the behavior of gcc of this warning at all. In fact as I said I ran into a bug due to it suggestion in clang.
Comment 8 Jonathan Wakely 2018-09-24 11:20:34 UTC
Why do you want to have static inline functions in headers in C++?

Why do you want to have static inline functions NOT in headers in C++?
Comment 9 Andrew Pinski 2018-09-24 11:22:20 UTC
It was in a header file.  Clang still earned about it.
Comment 10 Jonathan Wakely 2018-09-24 11:35:45 UTC
That's not what I asked.

Anyway, see comment 3. We're not talking about what Clang does, we're asking why it would be invalid for GCC to implement this warning. Maybe it would make sense only for code that is not in headers (IMHO it makes sense in headers too, which is why I asked why you want such code in headers, but I could accept having the warning only for code outside headers).

Insisting it's stupid to warn about code in headers doesn't make it invalid to warn about code outside headers. Please address the questions instead of just closing it again.
Comment 11 Richard Biener 2018-09-24 15:00:32 UTC
It's probably inconvenient for C indeed (pre-dating C99 inline).  Even GCC has too many static inlines in headers.

I agree that we can warn for static inline declarations with locations that
are not in any included file (not sure how exactly to verify that).
Comment 12 Manuel López-Ibáñez 2018-09-24 19:32:33 UTC
(In reply to Richard Biener from comment #11)

> I agree that we can warn for static inline declarations with locations that
> are not in any included file (not sure how exactly to verify that).

MAIN_FILE_P() ?
Comment 13 Eric Gallager 2018-09-30 01:14:12 UTC
(In reply to Andrew Pinski from comment #5)
> Test the warning out on clang from a header file and you will see you get
> the warning in the header too.  As I said I actually ran into this while
> working on the vpp project and cursed clang for having this warning turned
> on.

I think something similar happened with the gdb project; I'll try to find it later...
Comment 14 Eric Gallager 2018-09-30 17:53:29 UTC
(In reply to Eric Gallager from comment #13)
> (In reply to Andrew Pinski from comment #5)
> > Test the warning out on clang from a header file and you will see you get
> > the warning in the header too.  As I said I actually ran into this while
> > working on the vpp project and cursed clang for having this warning turned
> > on.
> 
> I think something similar happened with the gdb project; I'll try to find it
> later...

Right, found it, thread starting here: 
https://sourceware.org/ml/gdb/2015-02/msg00045.html

cc-ing people from it.

Also, corresponding clang bug is here: 
https://bugs.llvm.org/show_bug.cgi?id=22712