2024年2月1日发(作者:)

1.安装PC-Lint

安装和PC-Lint在Keil中配置见《使用教程》

2.配置.lnt文件

Keil安装路径添加:

-w4 //生成所有信息

-os(D:) //输出结果到文件

-Lint代码检查功能

PC-Lint能够检查出很多语法错误和语法上正确的逻辑错误,PC-Lint为大部分错误消息都分配了一个错误号,编号小于1000的错误号是分配给C 语言的,编号大于1000的错误号则用来说明C++的错误消息,见表1

错误类型

语法错误

内部错误

致命错误

告警

消息

可选信息

C

1-199

200-299

300-399

400-699

700-899

900-999

C++

1001-1199

1400-1699

1700-1899

1900-1999

表1

告警级别

1

0

0

2

3

4

以C语言为例:

编号1-199指的是一般编译器也会产生的语法错误;

编号200-299是PC-Lint程序内部的错误,这类错误不会出现在代码中的;

编号300-399指的是由于内存限制等导致的系统致命错误;

编号400-999中出现的提示信息,是根据隐藏代码问题的可能性进行分类的,指的是被检查代码中很可能存在问题而产生的告警信息;

编号700-899中出现的信息,产生错误的可能性相比告警信息来说级别要低,但仍然可能是因为代码问题导致的问题;

编号900-999是可选信息,他们不会被默认检查,除非你在选项中指定检查他们。

PC-Lint告警级别分为以下几个级别,缺省告警级别为3级:

-w0 不产生信息(除了遇到致命的错误)

-w1 只生成错误信息 -- 没有告警信息和其它提示信息

-w2 只有错误和告警信息

-w3 生成错误、告警和其它提示信息(这是默认设置)

-w4 生成所有信息

温度校准程序

4.1FCA温度校准程序PC-Lint配置文件《》

4.2FCA温度校准程序Lint输出结果《FCA_TempCalibration_》

FCA_TempCalibration_

4.3输出结果统计见表2、3、4,有6种语法、36种告警和41种消息错误

常见错误如,40:变量未声明;91:行字符数超过600;506:固定的布尔值;525:缩排格式错误;527:无法执行到的语句;534:忽略函数返回值;616:在switch语句中未使用break;715:变量未引用;722:语句结束符“;”用在for()或者while()后面;734:赋值过程中精度丢失;

错误类型 PC-Lint错误编码 计数 PC-Lint告警信息

10

14

语法错误

40

46

91

123

128

2

128

192

56

24

Expecting a function

Symbol 'Symbol' previously defined (Location)

Undeclared identifier '__promise'

field type should be _Bool, unsigned int or signed int

Line exceeds 600 characters (use +linebuf)

Macro 'min' defined with arguments at line

表2

错误类型 PC-Lint错误编码 计数

401

427

438

442

451

453

502

506

508

514

522

524

525

526

527

529

告警

534

537

539

545

550

552

569

573

578

611

613

616

628

641

644

655

1

1

14

1

9

1

2

15

68

1

5

13

23

7

1

1

268

36

7

1

2

7

41

2

3

1

83

1

7

110

2

4

PC-Lint告警信息

symbol 'Symbol' not previously declared static at Location

// comment terminates in

Last value assigned to variable 'Symbol' not used

for clause irregularity: testing direction inconsistent with

increment direction

repeatedly included but does not have a standard include

guard

Function 'Symbol', previously designated pure, String

'Name'

Expected unsigned type

Constant value Boolean

extern used with definition

Unusual use of a Boolean

Highest operator or function lacks side-effects

Loss of precision (Context) (Type to Type)

Negative indentation from Location

'Symbol' (Location) not defined

Unreachable code at token Symbol

Symbol 'Symbol' (Location) not subsequently referenced

Ignoring return value of function 'Symbol' (compare with

Location)

Repeated include file 'FileName'

Did not expect positive indentation from Location

Suspicious use of & -- An attempt was made to take the

address of an array name.

Symbol 'Symbol' (Location) not accessed

Symbol 'Symbol' (Location) not accessed

Loss of information (Context) (Integer bits to Integer bits)

Signed-unsigned mix with divide

Declaration of symbol 'Symbol' hides symbol 'Symbol'

(Location)

Suspicious cast

Possible use of null pointer 'Symbol' in [left/right] argument

to operator 'String' Reference

control flows into case/default

no argument information provided for function 'Symbol'

(Location)

Converting enum to int

Variable 'Symbol' (Location) may not have been initialized

bit-wise operation uses (compatible) enum's

661

662

668

669

25

1

1

1

possible access of out-of-bounds pointer ('Integer' beyond

end of data) by operator 'String'

possible creation of out-of-bounds pointer ('Integer' beyond

end of data) by operator 'String'

Possibly passing a null pointer to function 'Symbol', Context

Reference

Possible data overrun for function 'Symbol', argument

Integer exceeds argument Integer Reference

表3

错误类型 PC-Lint错误编码 计数

701

702

705

708

713

714

715

716

717

718

722

消息

725

730

732

734

736

737

740

744

746

747

750

752

754

33

1

1

1

21

24

13

12

12

12

56

3

3

8

20

7

4

14

4

7

15

14

2

6

PC-Lint告警信息

Shift left of signed quantity (int)

Shift right of signed quantity (int)

Argument no. Integer nominally inconsistent with format

union initialization

Loss of precision (Context) (Type to Type)

Symbol 'Symbol' (Location) not referenced

Symbol 'Symbol' (Location) not referenced

while(1) ...

do ... while(0)

Symbol 'Symbol' undeclared, assumed to return int

Suspicious use of ;

Expected positive indentation from Location

Boolean argument to function

Loss of sign (Context) (Type to Type)

Loss of precision (Context) (Integer bits to Integer bits)

Loss of precision (Context) (Integer bits to Integer bits)

Loss of sign in promotion from Type to Type

Unusual pointer cast (incompatible indirect types)

switch statement has no default

call to function 'Name' not made in the presence of a prototype

Significant prototype coercion (Context) Type to Type

local macro 'Symbol' (Location) not referenced

local declarator 'Symbol' (Location) not referenced

local structure member 'Symbol' (Location) not referenced

760

762

765

766

774

785

818

825

826

830

831

834

835

838

843

844

845

3

4

126

11

1

13

60

1

7

1227

221

8

87

17

117

2

11

Redundant macro 'Symbol' defined identically at Location

Redundantly declared symbol 'Symbol' previously declared at

Location

external 'Symbol' (Location) could be made static

Header file FileName not used in module String

Boolean within 'String' always evaluates to [True/False]

Too few initializers for aggregate 'Symbol'

Pointer parameter 'Symbol' (Location) could be declared ptr to

const

control flows into case/default without -fallthrough comment

Suspicious pointer-to-pointer conversion (area too small)

Location cited in prior message

Reference cited in prior message

Operator 'Name' followed by operator 'Name' is confusing. Use

parentheses.

A zero has been given as [left/right] argument to operator 'Name'

Previously assigned value to variable 'Symbol' has not been used

Variable 'Symbol' (Location) could be declared as const

Pointer variable 'Symbol' (Location) could be declared as

pointing to const

The [left/right] argument to operator 'Name' is certain to be 0

表4

5.舵机程序

5.1舵机程序PC-Lint配置文件《》

5.2舵机程序Lint输出结果《STM8_SERVO_》

STM8_SERVO_

5.3输出结果见表5、6、7,有16种语法、10种告警和23种消息错误

错误类型 PC-Lint错误编码 计数

7

10

14

18

19

31

36

语法错误

40

49

63

101

102

129

132

161

309

7

126

16

14

23

15

1

55

44

13

21

100

154

6

5

14

PC-Lint告警信息

Unable to open include file

Expecting 'String'

Symbol 'Symbol' previously defined (Location)

Symbol 'Symbol' redeclared (TypeDiff) conflicts with

Location

Useless Declaration

Redefinition of symbol 'Symbol' conflicts with Location

Redefining the storage class of symbol 'Symbol' conflicts

with Location

Undeclared identifier 'Name'

Expected a type

Expected an lvalue

Expected an identifier

Illegal parameter specification

declaration expected, identifier 'Symbol' ignored

Expected function definition

Repeated use of parameter 'Symbol' in parameter list

#error ...

表5

错误类型 PC-Lint错误编码 计数

506

526

告警

533

552

578

1

4

15

2

16

PC-Lint告警信息

Constant value Boolean

'Symbol' (Location) not defined

function 'Symbol' should (not) return a value (see Location)

Symbol 'Symbol' (Location) not accessed

Declaration of symbol 'Symbol' hides symbol 'Symbol'

(Location)

601

628

641

647

679

1

4

18

1

8

Expected a type for symbol Symbol, int assumed

no argument information provided for function 'Symbol'

(Location)

Converting enum to int

Suspicious truncation

Suspicious Truncation in arithmetic expression combining

with pointer

表6

错误类型 PC-Lint错误编码 计数

704

714

715

716

718

725

732

734

745

746

747

消息

750

765

766

772

774

776

783

818

830

831

832

843

3

3

18

3

4

1

2

2

15

5

1

2

5

1

2

1

1

3

1

118

1

16

1

PC-Lint告警信息

Shift right of signed quantity (long)

Symbol 'Symbol' (Location) not referenced

Symbol 'Symbol' (Location) not referenced

while(1) ...

Symbol 'Symbol' undeclared, assumed to return int

Expected positive indentation from Location

Loss of sign (Context) (Type to Type)

Loss of precision (Context) (Integer bits to Integer bits)

function 'Name' has no explicit type or class, int assumed

call to function 'Name' not made in the presence of a

prototype

Significant prototype coercion (Context) Type to Type

local macro 'Symbol' (Location) not referenced

external 'Symbol' (Location) could be made static

Header file FileName not used in module String

Symbol 'Symbol' (Location) conceivably not initialized

Boolean within 'String' always evaluates to [True/False]

Possible truncation of addition

Line does not end with new-line

Pointer parameter 'Symbol' (Location) could be declared ptr

to const

Location cited in prior message

Reference cited in prior message

Parameter 'Symbol' not explicitly declared, int assumed

Variable 'Symbol' (Location) could be declared as const

表7

6.对比分析

6.1语法错误对比

对比表2和表5,错误类型10、14和40在两个程序都有出现。由于测试机上缺少必要的头文件导致舵机程序出现更多的语法错误。

6.2告警错误对比

对比表3和表6,错误类型506、526、552、578、628和641在两个程序都有出现。其中错误类型641:枚举类型转为整型在两个程序均出现多次。

6.3消息错误对比

对比表4和表7,错误类型714、715、716、718、725、732、734、746、747、750、765、766、774、818、830、831和843在两个程序都有出现。其中错误类型830:地址引用在之前的信息中,在两个程序中出现次数均是最多的。通过提高告警级别至-w3或者-w2都不能禁止此错误信息,必须在配置文件中明确设置-e830来禁止。

7.常见问题

7.1不能打开头文件,一种解决方案是添加头文件包含路径到图1所示位置

图1

7.2错误信息太多,通过设置告警级别和禁止输出特定类型错误信息(-e#)解决