查找未使用的对象(非原始值)
跟进问题:g++ 不显示“未使用”警告 。
我完全理解为什么 g++ 不警告这些变量,但我希望它能以某种方式找到它们。我正在处理的代码没有任何特殊情况,因此单个 FloatArray x;
几乎肯定是剩余的。
即使我必须标记各个类(例如对未使用的 FloatArray 对象的警告),它也会非常有用。我能做些什么?
Follow up to question: g++ does not show a 'unused' warning.
I fully understand why g++ doesn't warn about these variables, but I would like it to somehow find them anyway. The code I'm working on doesn't have any of those special cases, so a single FloatArray x;
is almost definitely left-overs.
Even If i have to mark individual classes (Such as warning for unused FloatArray-objects) it would be very useful. What can I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,对于 GCC,下面的代码确实会根据您的需要发出警告:
但是如果您向 Foo 或 Bar 结构添加构造函数/析构函数,即使是一个微不足道的构造函数/析构函数,它也不会发出警告。
因此,重新获得警告的最简单方法是有条件地编译所有相关的构造函数和析构函数:
现在使用 g++ -DTEST_UNUSED 进行编译以检查额外未使用的变量。
这不是我最聪明的主意,但它确实有效。
Well, with GCC the following code does warn as you want:
But if you add a constructor/destructor to the Foo or Bar struct, even a trivial one, it will not warn.
So the easiest way to regain the warning is to compile all the relevant constructors AND destructors conditionally:
Now compile with
g++ -DTEST_UNUSED
to check for extra unused variables.Not my brightest idea, but it works.
好吧,基本上你想创建某种插入 GCC 的简单静态分析工具?如果是这样,您可以首先使用 MELT 快速实现未使用的可变打印机。
http://gcc.gnu.org/wiki/MELT%20tutorial
Well, basically you want to create some sort of simple static analysis tool plugged in GCC ? If that's so, you could start by using MELT to quickly implement an unused variable printer.
http://gcc.gnu.org/wiki/MELT%20tutorial
我不确定我是否遗漏了问题中的某些内容,但 gcc/g++ 有选项允许您指定想要的警告和不需要的警告。因此只需启用 -Wunused-变量即可。
请参阅此处了解更多详细信息: http://gcc.gnu.org/onlinedocs/gcc /Warning-Options.html
此外,-Wall 会打开此警告和许多更有用的警告。
I'm not sure if I'm missing something in the question but gcc/g++ has options which allow you to specify which warnings you want and which you don't. So simply just enabled the -Wunused-variable.
See here for more details: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
Also, -Wall will turn this and many more useful warning on.