使用 Sparse 检查 C 代码
有人有 Sparse 的经验吗?我似乎找不到任何文档,因此我不清楚它产生的警告和错误。我尝试检查邮件列表和手册页,但实际上两者都没有太多内容。
例如,我在我的一个文件中使用 INT_MAX。即使我#include limits.h,这也会生成错误(未定义的标识符)。
是否有任何地方对错误和警告进行了解释?
Does anyone have experience with Sparse? I seem unable to find any documentation, so the warnings, and errors it produces are unclear to me. I tried checking the mailing list and man page but there really isn't much in either.
For instance, I use INT_MAX in one of my files. This generates an error (undefined identifier) even though I #include limits.h.
Is there any place where the errors and warnings have been explained?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以说,稀疏并不是为了成为 lint。 Sparse 旨在生成任意代码的解析树,以便可以对其进行进一步分析。
在您的示例中,您要么想要定义 GNU_SOURCE (我相信它会打开 __GNUC__ ),它会公开您在 limit.h 中需要的位,
我会避免单独定义 __GNUC__ ,因为它激活的一些东西可能会以未定义的方式运行,而不需要GNU_SOURCE 打开的所有其他开关都已定义。
我的观点不是帮助您逐个消除错误,而是重申稀疏主要用作库,而不是作为独立的静态分析工具。
从我的自述文件副本(不确定我是否有当前版本):
包含的客户端比任何东西都更多是“功能测试套件和示例”。它是一个非常有用的工具,但如果您想使用它,您可能会考虑另一个使用角度。我喜欢它,因为它不使用 *lex / bison ,这使得它更容易被破解。
Sparse isn't intended to be a lint, per say. Sparse is intended to produce a parse tree of arbitrary code so that it can be further analyzed.
In your example, you either want to define GNU_SOURCE (which I believe turns on __GNUC__), which exposes the bits you need in limits.h
I would avoid defining __GNUC__ on its own, as several things it activates might behave in an undefined way without all of the other switches that GNU_SOURCE turns on being defined.
My point isn't to help you squash error by error, its to reiterate that sparse is mostly used as a library, not as a stand alone static analysis tool.
From my copy of the README (not sure if I have the current version) :
The included clients are more 'functional test suites and examples' than anything. Its a very useful tool, but you might consider another usage angle if you want to employ it. I like it because it doesn't use *lex / bison , which makes it remarkably easier to hack.
如果您查看 limit.h,您会发现 INT_MAX 是在 #if 内定义的,
因此要使其正常工作,您应该在包含 limit.h 之前取消定义
__GNUC__
If you look at limits.h you'll see that INT_MAX is defined inside this #if
so to get it to work you should undefine
__GNUC__
before including limits.h