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)
^