[英]GCC error: function might be candidate for attribute ‘pure’ if it is known to return normally
鉴于此代码:
#include <cstdlib>
void func(int x)
{
if (x)
abort();
};
g++ -Werror=suggest-attribute=pure
抱怨:
错误:如果已知函数正常返回,则该函数可能是属性“纯”的候选者
这对我来说似乎很奇怪——该函数无法正常返回,这难道不是很明显吗? 有什么方法可以告诉 GCC 它并不总是正常返回,或者我不希望此特定函数出现此警告?
演示: https : //godbolt.org/g/720VOT
这似乎是 gcc 中的一个错误(或者至少是文档和实际实现的差异)。 关于-Wsuggest-attribute=pure
的文档如下:
-Wsuggest-attribute=pure
-Wsuggest-attribute=const
-Wsuggest-attribute=noreturn
警告可能是属性
pure
、const
或noreturn
候选的noreturn
。 编译器只警告在其他编译单元中可见的函数,或者(在pure
和const
的情况下)如果它不能证明函数正常返回。 如果函数不包含无限循环或通过 throwing、调用abort
或 trapping 异常返回,则该函数正常返回。 此分析需要选项-fipa-pure-const
,默认情况下在-O
及更高级别启用。 更高的优化级别可提高分析的准确性。
然而,实际分析似乎忽略了不返回调用的可能性,尽管它尊重可能的异常:
$ 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)
^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.