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

发布于 2024-12-27 20:20:48 字数 284 浏览 2 评论 0原文

跟进问题: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇俏 2025-01-03 20:20:48

好吧,对于 GCC,下面的代码确实会根据您的需要发出警告:

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

但是如果您向 Foo 或 Bar 结构添加构造函数/析构函数,即使是一个微不足道的构造函数/析构函数,它也不会发出警告。

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

因此,重新获得警告的最简单方法是有条件地编译所有相关的构造函数和析构函数:

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

现在使用 g++ -DTEST_UNUSED 进行编译以检查额外未使用的变量。

这不是我最聪明的主意,但它确实有效。

Well, with GCC the following code does warn as you want:

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.

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.

Not my brightest idea, but it works.

东京女 2025-01-03 20:20:48

好吧,基本上你想创建某种插入 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

贱人配狗天长地久 2025-01-03 20:20:48

我不确定我是否遗漏了问题中的某些内容,但 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文