Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: function 'F' is not needed and will not be emitted

Tags:

c++

clang

Here is a little program (live on godbolt):

static void UnusedDeclaration();
static void UnusedDefinition() {}
static void Declaration();

decltype(Declaration())*  global;

Ideally I would expect the following warnings, if I compile it with clang, -Wunused:

  1. UnusedDeclaration(): It is an unused function declaration with internal linkage. So I should get a warning.
  2. UnusedDefinition(): It is an unused function definition with internal linkage. So I should get a warning.
  3. Declaration(): This declaration is used. So I should not get a warning.

Actually all three cases get a warning:

warning: unused function 'UnusedDeclaration'
warning: unused function 'UnusedDefinition'
warning: function 'Declaration' is not needed and will not be emitted

I have a problem with case 3. I think, the compiler should not warn me about anything, but it does.

  • Why do I get a warning for case 3?
    • function 'Declaration' is not needed – I honestly need that declaration. I use it in an unevaluated context, the compiler does not need to complain about it.
    • and will not be emitted – I am not sure what emitting means. I have written a separate question about it.
  • Is there a combination of warning flags to pass to the compiler on the command line, to achieve what I expect (warning for case 1 and 2, no warning for case 3).
  • I could turn off warning for case 3 with -Wunused -Wno-unneeded-internal-declaration, but I don't know if I lose some important information with it. Maybe in some other cases this warning is useful. In what exact situation does this 3rd warning come?
like image 554
Dr. Gut Avatar asked Oct 05 '19 03:10

Dr. Gut


1 Answers

There's no reference to the first function, so it can be basically ignored.

The second function is "called", but as you noted, only in an unevaluated context. That means the call will never be evaluated. The compiler determines the type the function would return if it was called, but that's all. Since it's never actually called, the function itself is never needed or used as a function, so the compiler doesn't generate any code for it.

The messages are different because even though the final effect is the same, each reaches that result slightly differently, and the compiler is trying to give you the information that might be useful.

like image 100
Jerry Coffin Avatar answered Sep 28 '22 17:09

Jerry Coffin