简体   繁体   English

查找未使用的对象(非原始值)

[英]Finding unused objects (non-primitive values)

Follow up to question: g++ does not show a 'unused' warning . 跟进问题: g ++没有显示“未使用”警告

I fully understand why g++ doesn't warn about these variables, but I would like it to somehow find them anyway. 我完全理解为什么g ++没有警告这些变量,但我还是想以某种方式找到它们。 The code I'm working on doesn't have any of those special cases, so a single FloatArray x; 我正在处理的代码没有任何特殊情况,所以单个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. 即使我必须标记单个类(例如警告未使用的FloatArray对象),它也会非常有用。 What can I do? 我能做什么?

Well, with GCC the following code does warn as you want: 好吧,使用GCC,以下代码会根据需要发出警告:

struct Foo
{
};
struct Bar
{
    Foo f;
};
int main()
{
    Bar b; //warning: unused variable 'b' 
}

But if you add a constructor/destructor to the Foo or Bar struct, even a trivial one, it will not warn. 但是如果你将一个构造函数/析构函数添加到Foo或Bar结构中,即使是一个微不足道的结构,它也不会发出警告。

struct Foo
{
    Foo() {}
};
struct Bar
{
    Foo f;
};
int main()
{
    Bar b; //no warning! It calls Foo::Foo() into b.f
}

So the easiest way to regain the warning is to compile all the relevant constructors AND destructors conditionally: 因此,重新获得警告的最简单方法是有条件地编译所有相关的构造函数和析构函数:

struct Foo
{
#ifndef TEST_UNUSED
    Foo() {}
#endif
};
struct Bar
{
    Foo f;
};
int main()
{
    Bar b; //warning!
}

Now compile with g++ -DTEST_UNUSED to check for extra unused variables. 现在使用g++ -DTEST_UNUSED进行编译以检查额外的未使用变量。

Not my brightest idea, but it works. 不是我最聪明的想法,但它确实有效。

Well, basically you want to create some sort of simple static analysis tool plugged in GCC ? 那么,基本上你想创建一些插入GCC的简单静态分析工具? If that's so, you could start by using MELT to quickly implement an unused variable printer. 如果是这样,您可以先使用MELT快速实现未使用的可变打印机。

http://gcc.gnu.org/wiki/MELT%20tutorial http://gcc.gnu.org/wiki/MELT%20tutorial

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. 我不确定我是否遗漏了问题中的某些内容,但是gcc / g ++有一些选项可以让你指定你想要的警告和你不想要的警告。 So simply just enabled the -Wunused-variable. 所以只需启用-Wunused-variable即可。

See here for more details: http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html 有关详细信息,请参阅此处: http//gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

Also, -Wall will turn this and many more useful warning on. 此外,-Wall将转向此以及更多有用的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM