【发布时间】:2021-07-20 20:46:25
【问题描述】:
请注意,以下代码毫无意义,我只是想重现我在更复杂的代码库中看到的错误。显然,我不会创建具有全局范围的变量来将其传递给仅在该变量所在的一个文件中使用的函数。
我正在运行 PC-Lint 9.00L。
在以下示例中,PC-Lint 抱怨重新声明:
example2.c 18 错误 18: Symbol'testFunction(const struct AnotherExample_t *)' redeclared (Arg. no. 1:qualification) 与第 21 行冲突,文件 example.h,模块 example1.c
代码如下:
example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <stdint.h>
typedef struct
{
volatile uint8_t item1;
volatile uint8_t item2;
} Example_t;
typedef struct
{
Example_t * p_items;
uint8_t something;
uint16_t somethingElse;
} AnotherExample_t;
extern AnotherExample_t g_externalVariable;
extern void testFunction (AnotherExample_t const * const p_example); //line 21
#endif
example1.c
#include "example.h"
#include <stdio.h>
int main(void)
{
g_externalVariable.something = 5;
(void)printf("%d", g_externalVariable.something);
testFunction(&g_externalVariable);
return 0;
}
example2.c
#include "example.h"
#include <stdio.h>
static Example_t p =
{
.item1 = 0,
.item2 = 1,
};
AnotherExample_t g_externalVariable =
{
.p_items = &p,
.something = 2,
.somethingElse = 3,
};
void testFunction (AnotherExample_t const * const p_example)
{ // Line referenced in lint (line 18)
(void)printf("%d", (int)p_example->somethingElse);
}
为什么 lint 会抛出这个错误?
我尝试过的事情
我注意到,当我删除 const AnotherExample_t 的声明时,投诉就消失了。即 -
extern void testFunction (AnotherExample_t * const p_example); //example.h
void testFunction (AnotherExample_t * const p_example) //example2.c
{
...
}
我还尝试从 example1.c 进行调用,看看是否有任何改变:
testFunction((AnotherExample_t const * const)&g_externalVariable);
这并没有改变任何东西。
在这两种情况下,我都会收到一条 Info 818 消息:
example2.c 20 Info 818:指针参数“p_example”(第 17 行)可以声明为指向 const
最少的可重现代码
这也会导致同样的错误。
example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
extern void testFunction (const char * const p_example);
#endif
example1.c
#include "example.h"
#include <stdio.h>
int main(void)
{
char testValue = 'c';
char * p_testValue = &testValue;
testFunction(p_testValue);
return 0;
}
example2.c
#include "example.h"
#include <stdio.h>
void testFunction (const char * const p_example)
{
(void)printf("%c", p_example);
}
【问题讨论】:
-
AnotherExample_t const * const p_example->const AnotherExample_t* p_example。我也打赌这个例子不是minimal reproducible example(如 - 非 minimal) -
@SergeyA 我也试过了,但仍然有同样的错误。
-
您需要真正处理您的示例并将其最小化。我很确定那里有很多噪音。请发布完整但最少的代码,其中函数签名与我建议的匹配。
-
@SergeyA 添加了一个更好的例子。
-
嗯,这意味着你的 Lint 有错误。如果您确定这是代码,并且这是 Lint 给您的唯一警告,则意味着 Lint 未能就 printf 格式字符串不兼容的真正问题发出警告,而是报告了绝对正确的函数定义。在这一点上,我唯一能建议的就是向 Lint 维护者提交错误报告。
标签: c static-analysis lint pc-lint