使用 Sparse 检查 C 代码

发布于 2024-08-13 03:22:29 字数 266 浏览 5 评论 0原文

有人有 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 技术交流群。

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

发布评论

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

评论(2

若水微香 2024-08-20 03:22:29

可以说,稀疏并不是为了成为 lint。 Sparse 旨在生成任意代码的解析树,以便可以对其进行进一步分析。

在您的示例中,您要么想要定义 GNU_SOURCE (我相信它会打开 __GNUC__ ),它会公开您在 limit.h 中需要的位,

我会避免单独定义 __GNUC__ ,因为它激活的一些东西可能会以未定义的方式运行,而不需要GNU_SOURCE 打开的所有其他开关都已定义。

我的观点不是帮助您逐个消除错误,而是重申稀疏主要用作库,而不是作为独立的静态分析工具。

从我的自述文件副本(不确定我是否有当前版本):

This means that a user of the library will literally just need to do

  struct string_list *filelist = NULL;
  char *file;

  action(sparse_initialize(argc, argv, filelist));

  FOR_EACH_PTR_NOTAG(filelist, file) {
    action(sparse(file));
  } END_FOR_EACH_PTR_NOTAG(file);

and he is now done - having a full C parse of the file he opened.  The
library doesn't need any more setup, and once done does not impose any
more requirements.  The user is free to do whatever he wants with the
parse tree that got built up, and needs not worry about the library ever
again.  There is no extra state, there are no parser callbacks, there is
only the parse tree that is described by the header files. The action
function takes a pointer to a symbol_list and does whatever it likes with it.

The library also contains (as an example user) a few clients that do the
preprocessing, parsing and type evaluation and just print out the
results.  These clients were done to verify and debug the library, and
also as trivial examples of what you can do with the parse tree once it
is formed, so that users can see how the tree is organized.

包含的客户端比任何东西都更多是“功能测试套件和示例”。它是一个非常有用的工具,但如果您想使用它,您可能会考虑另一个使用角度。我喜欢它,因为它不使用 *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) :

This means that a user of the library will literally just need to do

  struct string_list *filelist = NULL;
  char *file;

  action(sparse_initialize(argc, argv, filelist));

  FOR_EACH_PTR_NOTAG(filelist, file) {
    action(sparse(file));
  } END_FOR_EACH_PTR_NOTAG(file);

and he is now done - having a full C parse of the file he opened.  The
library doesn't need any more setup, and once done does not impose any
more requirements.  The user is free to do whatever he wants with the
parse tree that got built up, and needs not worry about the library ever
again.  There is no extra state, there are no parser callbacks, there is
only the parse tree that is described by the header files. The action
function takes a pointer to a symbol_list and does whatever it likes with it.

The library also contains (as an example user) a few clients that do the
preprocessing, parsing and type evaluation and just print out the
results.  These clients were done to verify and debug the library, and
also as trivial examples of what you can do with the parse tree once it
is formed, so that users can see how the tree is organized.

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.

东风软 2024-08-20 03:22:29

如果您查看 limit.h,您会发现 INT_MAX 是在 #if 内定义的,

/* If we are not using GNU CC we have to define all the symbols ourself.
 Otherwise use gcc's definitions (see below).  */
#if !defined __GNUC__ || __GNUC__ < 2

因此要使其正常工作,您应该在包含 limit.h 之前取消定义 __GNUC__

If you look at limits.h you'll see that INT_MAX is defined inside this #if

/* If we are not using GNU CC we have to define all the symbols ourself.
 Otherwise use gcc's definitions (see below).  */
#if !defined __GNUC__ || __GNUC__ < 2

so to get it to work you should undefine __GNUC__ before including limits.h

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