学习-代码审查pclint使用手册_第1页
学习-代码审查pclint使用手册_第2页
学习-代码审查pclint使用手册_第3页
学习-代码审查pclint使用手册_第4页
学习-代码审查pclint使用手册_第5页
已阅读5页,还剩131页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PC-Lint简: 第工具安 PC-lint执行程序 PC-lint配置程序 打印工具 PC-lint 保存PC-lint检查代码后输出的错误信息,可用”记事本”Lnt\... 非ANSI 非ANSI 各种编辑环境如'sVisualStudio的配置文 作者检查项的配置文件,指ScottMeyers的EffectiveC++,MoreEffectiveC++,Misra,DanSaks的C++Gotchas 配 file2lin.bat的内容: -iConfDirstd.lnt&1&2Abatchfilewhich sobecopiedintoyourPATHandwhichcontainsareferenceSTD.LNTAnindirectlintfilethatwill"pointto"acompileroptionsfileandanoptionsfileandoptionallyotherfilesandoptions.AsequenceofdifferentversionsofSTD.LNTthatthewizardcanchoosefromAconvenientcentrallylocatedoptionsfilethatyouwilltendtouseformostofyourAbatchfileoptionallygeneratedtobeusedtoplacethePC-lintexecutableinyour Compileroptionsfile(s) Environmentoptionsfiles Libraryoptionsfiles Headersreferencedbylib-....lntau- Optionsfilesfor mended Apairoftestprogramsdescribedinthenext运试程 fileafilea和fileb//lint-w2reducethewarninglevelto2classX{staticintclass{intPC-lintforC/C++(NT)Vers.8.00,Copyright---Module:---Module:_fileb.cpp4Warning631:tagXdefineddifferentlyatline3,file---GlobalWrap-Warning1527:staticmemberX::a(line5,filefilea.cpp)notlint使用PC-Lint检查程序代 alpha.cppbeta.ccheckout 用+fcpC++ a1.ca2.c - a4.c用+cppC++ + a1.cca2.cca3.c用-u(Error 2 - - 创建文件alphabet.lnt:linIDE整env- Borland's5.0env- Borland's4.0IDEunderenv- Premiaenv- 'sProgrammer'sWorkenv- SourceInsightenv- VisualSlickenv- Turbo/BorlandIDEunderenv- 'sVisualC/C++,Versionenv- 'sVisualC/C++,Versionenv- 'sVisualC/C++,Versionenv- 'sVisualC/C++,Version…从.dsplint project.dsp -v1project.dspCFG+d"CFG=project-Win32建议放到单独的.lnt文件,例如+d"CFG=project-Win32从.dsplint project.dsp>lin--uproject.lntThiswilldoaunitcheckusingalltheoptionswithinproject.lntbutnoneofthemodules.Onlyalpha.cppwillbeprocessed.Lint命令lintoptionsfile1[file2file3...文件名支持.*,*.c表示检查指定 下的所有.c文件。也可以使用.lnt文件来包括C和C++文件。.lntCC++风格的注释,如/*…*/和C:\lintpp\lint-iC:\lintppstd.lnt%1%2%3.lntstd.lnt:退出代码(ExitCodeExitCodelint...usualargumentsiferrorlevel1gotolint选项设置lint/*lintoption1option2...optionalcommentary//lintoption1option2...optional在宏中指定lint选项,例如下面指定lint查除零错误#defineDIVZERO(x)/*lint-save-e54*/((x)/0)\/*lint-restore#defineDIVZERO(x)((x)//#defineDIVZERO(x)/*lint-save-e(54,414,522)*/((x)/0)/*lint-restore#include"stdafx.h"#includevoid{intres=}void{inta=100;intb=0;intres=}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: intres=DIVZERO(1);D:\PC-Lint\MyTest\Test1\Test1.cpp(9):error414:(Warning--Possibledivisionby0[Reference:fileD:\PC-Lint\MyTest\Test1\Test1.cpp:line9])}D:\PC-Lint\MyTest\Test1\Test1.cpp(10):error529:(Warning--Symbol'res'(line9)notsubsequentlyreferenced)intres=D:\PC-Lint\MyTest\Test1\Test1.cpp(16):error414:(Warning--Possibledivisionby0[Reference:fileD:\PC-Lint\MyTest\Test1\Test1.cpp:line15])}D:\PC-Lint\MyTest\Test1\Test1.cpp(17):error529:(Warning--Symbol'res'(line16)notsubsequentlyreferenced)---GlobalWrap-error900:(Note--Successfulcompletion,5messagesToolreturnedcode:#defineforif(0){}else//#definefor/*lint-e(506,774)*/if(0){}else//lint+e774#include"stdafx.h"#includecharCheryl[]=int{charintnaughty=intprevious=for(p=Cheryl;*p;p++{//intprevious= /*previousbehaviorwas'g'if(*p=='b'&&previous=='b')naughty++;elseif(*p=='b')previous='b';elseprevious=}if(naughty)printf("Cherylhasbeennaughty%dtimes\n",naughty);return0;}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: {}elseforfor(p=Cheryl;*p;p++D:\PC-Lint\MyTest\Test1\Test1.cpp(15):error506:(Warning--ConstantvalueBoolean) {}elseforfor(p=Cheryl;*p;p++D:\PC-Lint\MyTest\Test1\Test1.cpp(15):error774:(Info--Booleanwithin'if'alwaysevaluatestoFalse[Reference:fileD:\PC-Lint\MyTest\Test1\Test1.cpp:line15])--- ---GlobalWrap-error900:(Note--Successfulcompletion,2messagesToolreturnedcode:在宏定义中添加Options检查#definefor/*lint-e(506,774)*/if(0){}elseIfyouare#defineTRUEyouwillreceivethe506--"ConstantValuewhenTRUEisusedinanarithmeticexpression.(ForC,TRUEshouldbedefinedtobe1.However,otherlanguagesusetiesotherthan1sosomeprogrammersfeelthat!0isplayingitsafe.)Tosuppressthismessageforjustthiscontextyoucanuse:#defineTRUE/*lint-save-e506*/(!0)\/*lint-restoreorthe-emacro(506,#include"stdafx.h"#defineTRUE!0//#defineTRUE/*lint-e506*/(!0)/*lint-restore*/intmain(){boola=if(a==return}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: if(a==TRUE)---GlobalWrap-error900:(Note--Successfulcompletion,1messagesToolreturnedcode:-emacro(506,lintsavelintrestore#include"stdafx.h"#defineTRUE!0intmain(){boola=//lint-save-e506if(a==TRUE)//lint-restoreboolb=1;return}错误选PC-lint5.2小节1、规则lint-e7202、除1526外的所有规则lint-e???? lint-epp-eau- willinhibiterrorsarisingfrompointer-pointerclashesandunsignedargumentsandwillsuppresscomplaintsaboutalphanotbeingdefined./*lint-esym(530,a)*/#include"stdafx.h"void{char//char*a="oworld!\n";}Theseparatesettingoftheseparametersisnotnormallynecessaryasthedefaultsettingsareconsistentwithmostcompilersinyourenvironment.Usethesizeoptionsforspecifyingarchitecturesotherthanthenativearchitectures.Forexample,ifyouarelintingforanembeddedsystemwhereintandpointersarenormally16bitsyoushouldspecifylint-si2-sp2PC-lint5.3- //integersare4- //pointersare4bytes1KB=10241字节=8位PC-lint5.4 +voffile1file2PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: Line1:Option:+vofIncludingfileD:\PC-Lint\MyTest\Test1\stdafx.h(hdr)Line1:Option:+vofIncludingfileC:\ProgramFiles\ VisualStudio\VC98\Include\stdio.h(library)IncludingfileC:\Program --- ---Module: Line0:Option:+rw(inline)IncludingfileD:\PC-Lint\MyTest\Test1\stdafx.h(hdr)Line0:Option:+rw(inline)IncludingfileC:\ProgramFiles\ VisualStudio\VC98\Include\stdio.h(library)IncludingfileC:\Program ---GlobalWrap-error900:(Note--Successfulcompletion,0messagesToolreturnedcode:标记选项PC-lint5.5例如,对于下面代码,如果fpn标记设置为ON,所有指针参数都被假设可能为//lint+fpn#include"stdafx.h"voidf(char*p,char*q{*p=3;//warningonlyif+fpnq++;//warningonlyif+fpn}voidg(char*p,char*q{if(p&&q{*p=3;//nowarningq++;//nowarning}} Char-is-Unsignedflag(defaultOptions:+fcu-fcu++fcu--IfthisflagisON,plainchardeclarationsareassumedtobeunsigned.Thisisusefulforcompilers,which,bydefault,treatcharasunsigned.NotethatthistreatmentisspecificallyallowedbytheANSIstandard.Thatis,whethercharisunsignedorsignedisuptothe#includevoid{chara=}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: chara=128;D:\PC-Lint\MyTest\Test1\Test1.cpp(5):error569:(Warning--Lossofinformation(initialization)(8bitsto7bits))}D:\PC-Lint\MyTest\Test1\Test1.cpp(6):error529:(Warning--Symbol'a'(line5)notsubsequently---GlobalWrap-error900:(Note--Successfulcompletion,2messagesToolreturnedcode://lint#includevoid{chara=}charsignedcharunsignedcharsignedchar,8位的话取值范围是-128..127unsignedcharHeightThedefaultheightoptionis-TheoptionalmnrequestsNoMacrodisplay.Normally,thevalueofeachopenmacroatthetimeoftheerrorisshownonasepara ine,providedthemessageheight(i.e.thevalueofN)isatleast3.Thelineisbydefaultprefixedwith"#...".#include#definefoo(a,b)intn=PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: intn=intn=D:\PC-Lint\MyTest\Test1\Test1.cpp(5):error414:(Warning--Possibledivisionby0[Reference:fileD:\PC-Lint\MyTest\Test1\Test1.cpp:line5])---GlobalWrap-error900:(Note--Successfulcompletion,2messagesproduced)Toolreturnedcode:2//lint-hmn#include"stdafx.h"#definefoo(a,b)intn=PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: intn=foo(4,0);D:\PC-Lint\MyTest\Test1\Test1.cpp(6):error414:(Warning--Possibledivisionby0[Reference:fileD:\PC-Lint\MyTest\Test1\Test1.cpp:line6])---GlobalWrap-error900:(Note--Successfulcompletion,2messagesToolreturnedcode:- MakesureweALWAYSprovidefileinformation('F')anduse lines(lineinerrorplusWidth-width(79,4)isthedefault-width(0) don'tbreakmessagesatanyparticularFormatForexamplethedefaultmessageformat-"format=%(%f%l%)%t%n:-"format=%(%F(%l):%)error%n:(%t-- Messageswill fileinformation(%F),thelinenumber(%l), messagenumber(%n),messagetype(%t)andmessagetextappend-append(errno,string-append(936,-XCorp.SoftwarePolicy371Thepurposeofthisoption,astheexamplesuggests,istoaddadditionalinformation,toamessage,thatcouldbeusedtosupportacompanyorstandardsbodysoftwarepolicy.whenmessage936isissued,theprogrammercanseethatthishassomethingtodowithSoftwarePolicy371.TheprogrammercanthenlookupPolicy371andobtainsupplementaryinformationaboutthepracticethatistobeavoided.indicatesthattheprogrammerisawareofthefactthatflowofcontrolisfallingthroughfromonecase(ordefault)ofaswitchtoanother.WithoutsuchanoptionMessage825willbeissued.Forexample:casecase2:n=0;//settingnto0case3:n++;willresultinInfo825oncase3becausecontrolisfallingthroughfromthestatementabove,whichisneitheracasenoradefault.Thecureistousethe-fallthroughoption:case1:case2:n=0;//settingnto//lint-fallthroughcase3:n++;Warning616willbeissuedifnocommentatallappearsbetweenthetwocases.Ifthisisadequateprotection,thenjustinhibitmessage825.- Presumethattabsareevery4Allcompilersareslightlydifferentowinglargelytodifferencesinlibrariesandpreprocessorvariables,ifnottodifferencesinthelanguageprocessed.ForPC-lint,thekeytocowiththesedifferencesistheselectionand/ormodificationofoneortwocompiler-specificfilesprovidedonthedistributiondiskette.lintco- module1module2...编译器适配文件在InstallDIR\co 例如co-msc60.lnt:#include"stdafx.h"#includevoid{ i,k,fp=fopen("data.dat","w+");{for(i=1;i<6;{ }fscanf(fp,"%d%d",&k,printf("%d%d\n",k,n);}}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: D:\PC-Lint\MyTest\Test1\Test1.cpp(13):error534:(Warning--Ignoringreturnvalueoffunction'fprintf(struct_iobuf*,constchar*,...)'(comparewithline310,fileC:\ProgramFiles\D:\PC-Lint\MyTest\Test1\Test1.cpp(15):error534:(Warning--Ignoringreturnvalueoffunction'fprintf(struct_iobuf*,constchar*,...)'(comparewithline310,fileC:\ProgramFiles\VisualStudio\VC98\Include\stdio.h))D:\PC-Lint\MyTest\Test1\Test1.cpp(20):error534:(Warning--Ignoringreturnvalueoffunction'fclose(struct_iobuf*)'(comparewithline285,fileC:\ProgramFiles\Visual---GlobalWrap-error900:(Note--Successfulcompletion,3messagesToolreturnedcode:ANSIC一共有32个关键字,它们分别是autobreakcasecharconstcontiunedefau odoubleelseenumexternfloatfotoifintlongregisterreturnshortsignedsizeofstaticstructswitchtypedefunionunsignedvoidvolatilewhileInadditiontothelanguage-definedkeywords,thecompilershaveaddedovertheyearsanumberofnon-standardkeywords,whichhave eknownastheKeywords.ThesearelistedinSection5.8.2CompilerCodesundertheoption"-cmsc".Thekeywordshave esoprevalentinC/C++compilerssupportingoperatingsystemsfollowingstandardsthatitissomewhatsafertoincludethemthantoexcludethem.+rw( //activatetheinline //activate-+rw(_asm,AnothersequenceForthis+ppw(asm,endasm)is+pragma(asm,+pragma(endasm,Yetanotherconvention#pragmaasm#pragmaendasmForthisyouneedtodefinethetwopragmas+pragma(asm,+pragma(endasm,库- //setsthewarninglevelwithinlibraryheadersto//(nowarnings,justsyntax Commentoutif//areactuallylintinglibrary //optionmakesobsoleteoptionsoftheform- ) >=400 ayberetainedforhistoricalPC-Lint将头文件作为库文件处理?6.1//lint-//lint-//lint-#include<stdafx.h>#include"stdio.h"voidmain(){printf( oworld\n"}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: Includingfilestdafx.h(library)IncludingfileC:\Program ---GlobalWrap-error900:(Note--Successfulcompletion,0messagesToolreturnedcode:stdafx.h//lint-//lint-//lint-//lint-libclass(foreign)#include<stdafx.h>#include"stdio.h"voidmain(){printf( oworld\n"}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: Includingfilestdafx.h(hdr)IncludingfileC:\ProgramFiles\VisualStudio\VC98\Include\stdio.h---GlobalWrap-stdafx.h(13):error755:(Info--globalmacro'WIN32_LEAN_AND_MEAN'(line13,filestdafx.h)notreferenced)error900:(Note--Successfulcompletion,1messagesToolreturnedcode://lint-//lint-//lint+libh(stdafx.h)#include"stdafx.h"#include<stdio.h>void{printf("oworld\n"}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985---- IncludingfileC:\Program ---GlobalWrap-error900:(Note--Successfulcompletion,0messagesToolreturnedcode:+libh(stdafx.h-//lint-//lintlibh(stdafx.h)//stdafx.h//lint-wlib(0)//stdafx.h中虽然有错误,但是作为库文件处理,而对于库文件又不报错,所#include"stdafx.h"#includevoid{printf( oworld\n"}LINT对象模PleaseNote.LintObjectModulesareusedsolelytoimprovethespeedofprocessingpreviouslylintedmodulesandthespeedimprovementsareonlynoticeablewithverylargefiles.Theydonotofferanyadditionalerrorchecking.InfactsomeerrorcheckingislostastheLintObjectModulesdonotretainalltheinformationpresentintheoriginalsourcecode.强类#includetypedefintCount;typedefintBool;void{Countn;Boolstop;n=stop;//mistakebutno}ThisprogrammerbotchgoesundetectedbythecompilerbecausethecompilerisempoweredbytheANSIstandardtocheckonlyunderlyingtypes,which,inthiscase,areboththesame//lint-strong(A,Count,Bool)#include"stdafx.h"typedefintCount;typedefintBool;void{CountBooln=stop}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985---- n=stop;//mistakebutnoD:\PC-Lint\MyTest\Test1\Test1.cpp(12):error632:(Warning--Assignmenttostrongtype'Count'incontext:assignment)D:\PC-Lint\MyTest\Test1\Test1.cpp(12):error530:(Warning--Symbol'stop'(line10)not}---GlobalWrap-error900:(Note--Successfulcompletion,3messagesToolreturnedcode:-strong(A)--strong(A)-b–对每个布尔操作符(== ||)都会进行强类型检查,看//lint-strong(Ab,Bool)#include"stdafx.h"typedefintBool;Boolgt(inta,intb){if(a)returna>b;//OKelsereturn0;//}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: elsereturn0;//WarningD:\PC-Lint\MyTest\Test1\Test1.cpp(8):error632:(Warning--Assignmenttostrongtype'Bool'incontext:return)---GlobalWrap-error900:(Note--Successfulcompletion,1messagesToolreturnedcode://lint-strong(Ab,Bool)#include"stdafx.h"typedefintBoolgt(inta,int{}

returna>b;//OKreturn(Bool)0;//lint-strong(B,Bool)#include"stdafx.h"typedefintBool;Boolgt(inta,intb){}

returna>b;//OKreturn0;//PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module: D:\PC-Lint\MyTest\Test1\Test1.cpp(7):error640:(Warning--Expectedstrongtype'Bool'inBooleancontext)---GlobalWrap-error900:(Note--Successfulcompletion,1messagesToolreturnedcode://lint-strong(AJXb,Bool//lint-strong(AJX,BitField)#include"stdafx.h"typedefinttypedefunsignedBitField;structfoo{}

unsigneda:1,b:2;BitFieldc:1,d:2,e:3;void{x.a=(Bool)1;//x.b=(Bool)0;//strongtypex.a=0;//strongtypex.b=2;//x.c=x.a;//x.e=1;//strongtypex.e=x.d;//}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985---- x.b=(Bool)0;//strongtypeD:\PC-Lint\MyTest\Test1\Test1.cpp(16):error633:(Warning--Assignmentfromastrongtype'Bool'incontext:assignment)x.a=0;//strongtypeD:\PC-Lint\MyTest\Test1\Test1.cpp(17):error632:(Warning--Assignmenttostrongtype'Bool'incontext:assignment)x.e=1;//strongtypeD:\PC-Lint\MyTest\Test1\Test1.cpp(20):error632:(Warning--Assignmenttostrongtype'BitField'incontext:assignment)---GlobalWrap-error900:(Note--Successfulcompletion,3messagesproduced)Toolreturnedcode:3Intheabove,membersaandcarestronglytypedBool,membersdandearetypedBitFieldandmemberbisnotstronglytyped.TosuppresstheBooleanassumptionforone-bitbitfieldsusetheflag'f'inthe-strongoptionfortheBoolean.Intheexampleabove,thiswouldbe-strong(AJXbf,Bool).当强类型的值被赋予一个不同类型的变量时((assignmentoperatorreturnvaluef:1被当成是布尔类8.4-index(flags,ixtype,sitype[,sitype]...callowConstantsaswellasixtype,tobeusedasdallowarrayDimensionstobespecifiedwithoutusingan//lint-strong(AzJX,Count,Temperature//lint-index(d,Count,Temperature//OnlyCountcanindexaTemperature#include"stdafx.h"typedeffloatTemperature;typedefintCount;Temperaturet[100];//OKbecauseofdflagTemperature*pt=t;//pointersarealsocheckedvoid{Countt[0]=t[1];//Warnings,nocflagfor(i=0;i<100;i++)t[i]=0.0;//OK,iisaCountpt[1]=2.0;//Warningi=pt-t;//OK,pt-tisa}PC-lintforC/C++(NT)Ver.8.00e,CopyrightGimpelSoftware1985----Module:D:\PC-Lint\MyTest\Test1\Test1.cppt[0]=t[1];//Warnings,nocflagD:\PC-Lint\MyTest\Test1\Test1.cpp(14):error637:(Warning--Expectedindextype'Count'forstrongtype'Temperature')D:\PC-Lint\MyTest\Test1\Test1.cpp(14):error637:(Warning--Expectedindextype'Count'forstrongtype'Temperature')for(i=0;i<100;i++D:\PC-Lint\MyTest\Test1\Test1.cpp(15):error638:(Warning--Strongtypemismatchfortype'Count'inrelational)t[i]=0.0;//OK,iisaD:\PC-Lint\MyTest\Test1\Test1.cpp(16):error632:(Warning--Assignmenttostrongtype'Temper

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论