有没有办法让 gcc 或 clang 对显式强制转换发出警告? [英] Is there any way to get gcc or clang to warn on explicit casts?

查看:50
本文介绍了有没有办法让 gcc 或 clang 对显式强制转换发出警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是在我拥有的一些源文件中找到从 double 类型或 float 类型到任何其他类型的所有显式转换.是否有内置的 gcc 方法可以做到这一点?语言是C.谢谢!

What I'm trying to do is find all explicit casts from type double or float to any other type in some source files I have. Is there a built-in gcc way to do this? Language is C. Thanks!

推荐答案

由于强制转换是明确合法的,并且是执行奇怪转换的正确方法,gcc 不太可能包含警告它们的选项

Since casts are explicitly legal, and the right way to perform weird conversions, it's highly unlikely that gcc would contain an option to warn about them

相反,根据您的来源有多大,您可能能够侥幸逃脱:

Instead, depending on how huge your source is, you might be able to get away with:

grep '(double|float) ' *

为您提供所有的双精度或浮点变量.由于 c 不是常规语言,因此将其解析为双精度或浮点变量列表并非易事(使用 shell 工具),但如果您的源代码足够小,则手动执行此操作很容易.

to give you all of the double or float variables. Since c isn't a regular language, it's not trivial (with shell tools) to parse this into a list of double or float variables, but if your source is small enough, doing this by hand is easy.

grep '([^()]*)[ ()]*(your list of variable names)' *

从那里将向您展示您的许多演员表.

From there would show you many of your casts.

这篇关于有没有办法让 gcc 或 clang 对显式强制转换发出警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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