我正在尝试使用 Splint(在严格模式下)检查 C 程序。我用语义注释对源代码进行了注释,以帮助 Splint理解我的程序。一切都很好,但我就是无法摆脱警告:
语句无效(可能通过调用不受约束的函数 my_function_pointer 进行未检测的修改)。
语句没有可见的效果 --- 没有值被修改。它可以通过调用不受约束的函数来修改某些内容。(使用 -noeffectuncon 禁止警告)
这是由通过函数指针的函数调用引起的。我不喜欢使用no-effect-uncon
标志,而是写更多的注释来修复它。所以我typedef
用适当的@modifies
条款装饰了我的,但 Splint 似乎完全忽略了它。问题可以简化为:
#include <stdio.h>
static void foo(int foobar)
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
{
printf("foo: %d\n", foobar);
}
typedef void (*my_function_pointer_type)(int)
/*@globals fileSystem@*/
/*@modifies fileSystem@*/;
int main(/*@unused@*/ int argc, /*@unused@*/ char * argv[])
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
{
my_function_pointer_type my_function_pointer = foo;
int foobar = 123;
printf("main: %d\n", foobar);
/* No warning */
/* foo(foobar); */
/* Warning: Statement has no effect */
my_function_pointer(foobar);
return(EXIT_SUCCESS);
}
我已经阅读了手册,但是关于函数指针及其语义注释的信息并不多,所以我不知道是我做错了什么还是这是某种错误(顺便说一下,这里还没有列出:http ://www.splint.org/bugs.html )。
有没有人成功地在严格模式下使用 Splint 检查了这样的程序?请帮我找到让夹板开心的方法:)
提前致谢。
更新 #1: splint-3.1.2(windows 版本)产生警告,而 splint-3.1.1(Linux x86 版本)没有抱怨。
更新#2: Splint 不在乎分配和调用是短的还是长的:
/* assignment (short way) */
my_function_pointer_type my_function_pointer = foo;
/* assignment (long way) */
my_function_pointer_type my_function_pointer = &foo;
...
/* call (short way) */
my_function_pointer(foobar);
/* call (long way) */
(*my_function_pointer)(foobar);
更新#3:我对禁止警告不感兴趣。这很容易:
/*@-noeffectuncon@*/
my_function_pointer(foobar);
/*@=noeffectuncon@*/
我正在寻找的是正确的表达方式:
“这个函数指针指向一个函数
@modifies
,所以它确实有副作用”