2

Given this code:

#include <cstdlib>

void func(int x)
{
  if (x)
    abort();
};

g++ -Werror=suggest-attribute=pure complains:

error: function might be candidate for attribute ‘pure’ if it is known to return normally

This seems strange to me--isn't it plainly obvious that the function is not known to return normally? Is there any way to tell GCC that it doesn't always return normally, or that I don't want this warning to appear for this particular function?

Demo: https://godbolt.org/g/720VOT

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    The function has no effect but the returned value (that is a _no effect_ for itself) and that value depends (only) on parameters. It seems a good candidate. Why not? – skypjack Jan 18 '17 at 06:58
  • @skypjack: The function does have a side effect--it can call `abort()` thereby ending the program. I argue this means it is not a candidate for "pure" optimization. Do you think otherwise? – John Zwinck Jan 18 '17 at 07:34
  • 1
    Oh, you expect the compiler to _know_ that's `abort`. Well, imagine you use a pointer to function to which you assigned `abort`, would your expectation be the same? I don't think it inspect the function, it simply accepts it as a `void(void)` function type to be called that doesn't affect the returned value. The latter thus depends only on the parameters and the function is `pure`. – skypjack Jan 18 '17 at 07:41
  • 1
    @skypjack: *I don't think it inspect the function, it simply accepts it as a `void(void)` function type to be called that doesn't affect the returned value.* not really, `abort` is marked as `noreturn`, whose point is exactly to affect analyses of this kind - for example, [this](https://godbolt.org/g/RrDGd3) triggers a "control reaches end of non-void function", de-commenting the final `abort` call [makes the warning go away](https://godbolt.org/g/ZBCP8c), and having a function that always calls `abort` [makes it suggested for `noreturn` as well](https://godbolt.org/g/DWRGQV). – Matteo Italia Jan 18 '17 at 08:01
  • @MatteoItalia It makes sense, just a few thoughts probably wrong. – skypjack Jan 18 '17 at 08:05

1 Answers1

1

This seems like a bug in gcc (or at least discrepancy of the documentation and actual implementation). The documentation on -Wsuggest-attribute=pure reads:

-Wsuggest-attribute=pure
-Wsuggest-attribute=const
-Wsuggest-attribute=noreturn

Warn about functions that might be candidates for attributes pure, const or noreturn. The compiler only warns for functions visible in other compilation units or (in the case of pure and const) if it cannot prove that the function returns normally. A function returns normally if it doesn't contain an infinite loop or return abnormally by throwing, calling abort or trapping. This analysis requires option -fipa-pure-const, which is enabled by default at -O and higher. Higher optimization levels improve the accuracy of the analysis.

However, the actual analysis seems to ignore the possibility of non-returning calls, though it respects possible exceptions:

$ cat test-noreturn.cpp 
[[noreturn]] void foo();

void func(int x)
{
    if (x)
        foo();
}

$ g++ -std=c++11 -c -O -Wsuggest-attribute=pure test-noreturn.cpp 
$ cat test-noreturn-nothrow.cpp 
[[noreturn]] void foo() throw();
//                      ^^^^^^^

void func(int x)
{
    if (x)
        foo();
}
$ g++ -std=c++11 -c -O -Wsuggest-attribute=pure test-noreturn-nothrow.cpp 
test-noreturn-nothrow.cpp: In function ‘void func(int)’:
test-noreturn-nothrow.cpp:4:6: warning: function might be candidate for attribute ‘pure’ if it is known to return normally [-Wsuggest-attribute=pure]
 void func(int x)
      ^
Leon
  • 31,443
  • 4
  • 72
  • 97
  • I wouldn't call it a _bug_, rather lack of precision during analysis. This is signified by "if it is known to return normally" which is supposed to tell us that compiler failed to prove certain properties about function but still emits the warning, hoping for the best. Noreturn functions are quite frequent so I suggest to file PR in [GCC BZ](https://gcc.gnu.org/bugzilla/) about this. – yugr Jan 18 '17 at 08:11