如何提高如果返回值被忽略警告 - 海湾合作委员会或静态code检? [英] how to raise warning if return value is disregarded - gcc or static code check?
本文介绍了如何提高如果返回值被忽略警告 - 海湾合作委员会或静态code检?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想看到我的code(C ++)函数的漠视返回值的所有地方。我该怎么办呢 - 用gcc或静态code分析工具
I'd like to see all the places in my code (C++) which disregard return value of a function. How can I do it - with gcc or static code analysis tool?
坏code例如:
int f(int z) {
return z + (z*2) + z/3 + z*z + 23;
}
int main()
{
int i = 7;
f(i); ///// <<----- here I disregard the return value
return 1;
}
更新:
- 它应该工作,即使函数及其使用在不同的文件
- 免费静态检查工具
- it should work even if the function and its use are in different files
- free static check tool
推荐答案
您想GCC的 warn_unused_result
属性:
#define WARN_UNUSED __attribute__((warn_unused_result))
int WARN_UNUSED f(int z) {
return z + (z*2) + z/3 + z*z + 23;
}
int main()
{
int i = 7;
f(i); ///// <<----- here i disregard the return value
return 1;
}
试图编译这个code生产:
Trying to compile this code produces:
$ gcc test.c
test.c: In function `main':
test.c:16: warning: ignoring return value of `f', declared with
attribute warn_unused_result
您可以在 Linux内核一见这>;他们有一个 __ must_check
宏,做同样的事情;看起来你需要为这个GCC 3.4或更高版本才能正常工作。这时你会发现在内核头文件使用的宏:
You can see this in use in the Linux kernel; they have a __must_check
macro that does the same thing; looks like you need GCC 3.4 or greater for this to work. Then you will find that macro used in kernel header files:
unsigned long __must_check copy_to_user(void __user *to,
const void *from, unsigned long n);
这篇关于如何提高如果返回值被忽略警告 - 海湾合作委员会或静态code检?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文