有什么方法可以为功能指针比较生成警告? [英] Any way to generate warnings for function-pointer comparisons?

查看:60
本文介绍了有什么方法可以为功能指针比较生成警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很长时间才追踪到我的代码中有一个错误是由 / OPT:ICF 触发的:

It took me forever to track down that there was a bug in my code being triggered by /OPT:ICF:


因为 / OPT:ICF可以将同一地址分配给不同的函数或只读数据成员(使用/ Gy编译的const变量),它可能会破坏依赖于函数或只读数据成员的唯一地址的程序。

Because /OPT:ICF can cause the same address to be assigned to different functions or read-only data members (const variables compiled by using /Gy), it can break a program that depends on unique addresses for functions or read-only data members.

(我一直在存储和比较函数相等的指针,当链接器丢弃相同的函数时,该指针将中断。)

(I had been storing and comparing function pointers for equality, which breaks when the linker throws away identical functions.)

现在,我需要找到可能做过这种事情的每个地方。

Now I need to find every place where I might have done such a thing.

测试用例当然很简单:

//MSVC: /Gy /link /OPT:ICF
int test1(void) { return 0; }
int test2(void) { return 0; }
int main(void) { return test1 == test2; }

我已经尝试过 -Wall -Wextra -所有内容 -pedantic 等。

I've tried -Wall, -Wextra, -Weverything, -pedantic, etc. but none of them generate warnings.

是否有任何编译器选项或工具(是否为Visual C ++,GCC,Clang或其他组件)可以分析我的代码并告诉我在哪里将函数指针进行比较,就像上面的代码一样?

Is there any compiler option or tool (whether part of Visual C++, GCC, Clang, or other) that can analyze my code and tell me where I'm comparing function pointers with each other, like in the code above?

推荐答案


是否有任何编译器选项或工具(无论是Visual C ++,GCC,Clang还是其他组件)可以分析我的代码,并告诉我在哪里相互比较函数指针,例如在上面的代码?

Is there any compiler option or tool (whether part of Visual C++, GCC, Clang, or other) that can analyze my code and tell me where I'm comparing function pointers with each other, like in the code above?

我不确定是否存在这样的编译器选项。

I'm not sure if there exists such a compiler option.

但是,有这样的工具。整洁。您可以编写自己的clang-tidy支票,如果您遵循此博客。具体来说,AST已经附带了很多匹配器,它们应该可以处理您想要的用例。

However, there is such a tool. clang-tidy. You can write your own checks for clang-tidy, it's actually remarkably easy if you follow this blog. Specifically, the AST comes with a bunch of matchers already, which should handle the use-case you want.

类似的东西似乎可行:

binaryOperator(
    anyOf(hasOperatorName("=="), hasOperatorName("!=")),
    hasLHS(ignoringImpCasts(declRefExpr(hasType(functionType())))),
    hasRHS(ignoringImpCasts(declRefExpr(hasType(functionType())))))

在OP中标记示例:

fp.cxx:3:25: note: "root" binds here
int main(void) { return test1 == test2; }
                        ^~~~~~~~~~~~~~






这特别适用于OP情况,但实际上您必须更加明确地匹配所有其他可能的情况:


That works specifically for the OP case, but you actually have to be more explicit to match all the other likely cases:

const auto AnyFunc = ignoringImpCasts(declRefExpr(hasType(anyOf(
    functionType(),
    pointsTo(functionType()),
    references(functionType())))));

Finder->AddMatcher(binaryOperator(
    anyOf(hasOperatorName("=="), hasOperatorName("!=")),
    hasLHS(AnyFunc),
    hasRHS(AnyFunc)).bind("op"), this);

或者类似的效果。

这篇关于有什么方法可以为功能指针比较生成警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆