[PATCH] D92886: [Sema] Warn about unused functions even when they're inline

Aaron Puchert via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 9 14:04:08 PST 2020


aaronpuchert added a comment.

In D92886#2443176 <https://reviews.llvm.org/D92886#2443176>, @aaronpuchert wrote:

>> Non-static inline functions in C have confusing semantics.
>
> I'm not terribly familiar with C, but judging from C11 6.7.4 it seems that C is a bit more permissive, but then says that certain things are implementation-defined. Whereas in C++ it's forbidden by the one-definition rule that an inline function has another external definition, it's allowed in C but implementation-defined which variant is called.
>
> So if you do non-static inline functions like you'd do them in C++, I think you should be fine. But perhaps I'm missing something, in that case feel free to give me a pointer.

Ah, I missed 6.9p5:

> If an identifier declared with external linkage is used in an expression [...], somewhere in the entire program there shall be exactly one external definition for the identifier; [...]

So you also have to provide an external definition if you actually intend to use the function. A lot like variables actually, if you declare one in a header you also have to provide a definition in some TU.

Thus C is different from C++ in that C has no notion of weak linkage, is that right? Which means you can't really have header-only libraries in C. Not if that's a concern, I don't think anybody was doing that anyway.

Still I think that internal linkage in headers is inherently nonsensical, why would you put something in a header if it's supposedly internal to a translation unit?

In D92886#2443176 <https://reviews.llvm.org/D92886#2443176>, @aaronpuchert wrote:

> Perhaps this is a different issue though, and the proper fix is rather to suppress `unused-*` warnings in headers entirely and have a different warning about using internal linkage in header files.

I am growing fond of this idea. I've seen lots of programmers mightily confused by internal declarations in headers being unused and they would show me where it's used and complain about false positives. The compiler was right, but in the case of `-Wunused-function` the warning doesn't talk about linkage at all.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D92886/new/

https://reviews.llvm.org/D92886



More information about the cfe-commits mailing list