c-为什么PC Lint抱怨重新声明(错误18)



请注意,下面的代码非常没有意义,我只是想在更复杂的代码库中重现一个错误。显然,我不会创建一个具有全局作用域的变量来将其传递给只在变量所在的一个文件中使用的函数。

我正在运行PC Lint 9.00L.

在以下示例中,PC Lint抱怨重新声明:

example2.c 18错误18:符号'testFunction(const struct AnotherExample_t*('重新声明(Arg.no.1:qualification(与第21行,文件example.h,模块example1.c 冲突

这是代码:

示例.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

示例1.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;
}

示例2.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);

这并没有改变任何事情。

在这两种情况下,我都会收到一条信息818消息:

example2.c 20信息818:指针参数"p_example"(第17行(可以声明为指向常量

最小可复制代码

这也会导致同样的错误。

示例.h

#ifndef EXAMPLE_H
#define EXAMPLE_H
extern void testFunction (const char * const p_example);

#endif

示例1.c

#include "example.h"
#include <stdio.h>
int main(void)
{
char testValue = 'c';
char * p_testValue = &testValue;

testFunction(p_testValue);

return 0;
}

示例2.c

#include "example.h"
#include <stdio.h>
void testFunction (const char * const p_example)
{
(void)printf("%c", p_example);
}

这既不是PC Lint的错误,也不是我代码中的错误。编译器库中对关键字进行了一些重新定义,导致PC Lint在预处理步骤中剥离了一些关键字(如constvolatile(。

最终的结果是,当我添加#include <stdio.h>时,testFunction的定义将被剥夺其const限定符,并且它将不再与我的头中的声明匹配。

我在co-XXXX.lnt文件中添加了以下选项,错误就消失了。

+dconst=const
+dvolatile=volatile

特别感谢Gimpel的工作人员,他们愿意和我一起解决这个问题,尽管这个版本的PC Lint不再受支持。

更新

不幸的是,我还没有时间追踪constvolatile在库中被重新定义的位置,但我发现了一种比在lint编译器选项文件中重新定义这些关键字更好的方法。利用PC Lint中的-scavenge选项,然后运行该选项通过预处理器创建的.c文件,创建一个定义编译器内置宏的标头,供PC Lint使用。这是我使用的流程。

lint-nt -i"/path/to/my/libraries" -scavenge(*.h) > interim.c

然后,我从code Composer Studio运行了一个标准的代码构建,以验证哪些选项是用cl430编译器运行的。之后,我使用相同的选项在编译器中运行了temporary.c,唯一的例外是我使用-ppo选项仅在预处理器中运行它,并将生成的文件保存为interim.lnt.

在这一点上,PC Lint手册说要运行:lint -scavenge(clean,interim.lnt)。在我的情况下,似乎只清除了文件中的所有数据,所以我跳过了这一步。然而,检查在此步骤之前创建的interim.lnt文件显示,我的所有宏都是在该文件中创建的。我将其重命名为lint_header.h,并在我的同事MSP430.lnt文件的开头添加了以下行:

-header(lint_header.h)
+libh(lint_header.h)

我不再使用constvolatile的+d选项,现在可以更准确地表示我的库文件在使用PC Lint时应该做什么。我再也没有重新声明的错误了。

最新更新