📄 lint
字号:
.RP.ND "July 26, 1978".OKProgram PortabilityStrong Type Checking.TLLint, a C Program Checker.AU "MH 2C-559" 3968S. C. Johnson.AI.MH.AB.PP.I Lintis a command which examines C source programs,detectinga number of bugs and obscurities.It enforces the type rules of C more strictly thanthe C compilers.It may also be used to enforce a number of portabilityrestrictions involved in movingprograms between different machines and/or operating systems.Another option detects a number of wasteful, or error prone, constructionswhich nevertheless are, strictly speaking, legal..PP.I Lintaccepts multiple input files and library specifications, and checks them for consistency..PPThe separation of function between.I lintand the C compilers has both historical and practicalrationale.The compilers turn C programs into executable files rapidlyand efficiently.This is possible in part because thecompilers do not do sophisticatedtype checking, especially betweenseparately compiled programs..I Linttakes a more global, leisurely view of the program,looking much more carefully at the compatibilities..PPThis document discusses the use of.I lint ,gives an overview of the implementation, and gives some hints on thewriting of machine independent C code..AE.CS 10 2 12 0 0 5.SHIntroduction and Usage.PPSuppose there are two C.[Kernighan Ritchie Programming Prentice 1978.]source files,.I file1. cand.I file2.c ,which are ordinarily compiled and loaded together.Then the command.DSlint file1.c file2.c.DEproduces messages describing inconsistencies and inefficienciesin the programs.The program enforces the typing rules of Cmore strictly than the C compilers(for both historical and practical reasons)enforce them.The command.DSlint \-p file1.c file2.c.DEwill produce, in addition to the above messages, additional messageswhich relate to the portability of the programs to other operatingsystems and machines.Replacing the.B \-pby.B \-hwill produce messages about various error-prone or wasteful constructionswhich, strictly speaking, are not bugs.Saying.B \-hpgets the whole works..PPThe next several sections describe the major messages;the document closes with sectionsdiscussing the implementation and giving suggestionsfor writing portable C.An appendix gives a summary of the.I lintoptions..SHA Word About Philosophy.PPMany of the facts which.I lintneeds may be impossible todiscover.For example, whether a given function in a program ever gets calledmay depend on the input data.Deciding whether.I exitis ever called is equivalent to solving the famous ``halting problem,'' known to berecursively undecidable..PPThus, most of the.I lintalgorithms are a compromise.If a function is never mentioned, it can never be called.If a function is mentioned,.I lintassumes it can be called; this is not necessarily so, but in practice is quite reasonable..PP.I Linttries to give information with a high degree of relevance.Messages of the form ``\fIxxx\fR might be a bug''are easy to generate, but are acceptable only in proportionto the fraction of real bugs they uncover.If this fraction of real bugs is too small, the messages lose their credibilityand serve merely to clutter up the output,obscuring the more important messages..PPKeeping these issues in mind, we now consider in more detailthe classes of messages which.I lintproduces..SHUnused Variables and Functions.PPAs sets of programs evolve and develop,previously used variables and arguments tofunctions may become unused;it is not uncommon for external variables, or even entirefunctions, to become unnecessary, and yetnot be removed from the source.These ``errors of commission'' rarely cause working programs to fail, but they are a sourceof inefficiency, and make programs harder to understandand change.Moreover, information about such unused variables and functions can occasionallyserve to discover bugs; if a function does a necessary job, andis never called, something is wrong!.PP.I Lintcomplains about variables and functions which are defined but not otherwisementioned.An exception is variables which are declared through explicit.B externstatements but are never referenced; thus the statement.DSextern float sin(\|);.DEwill evoke no comment if.I sinis never used.Note that this agrees with the semantics of the C compiler.In some cases, these unused external declarations might be of some interest; theycan be discovered by adding the.B \-xflag to the.I lintinvocation..PPCertain styles of programmingrequire many functions to be written with similar interfaces;frequently, some of the arguments may be unusedin many of the calls.The.B \-voption is available to suppress the printing ofcomplaints about unused arguments.When.B \-vis in effect, no messages are produced about unusedarguments except for thosearguments which are unused and also declared asregister arguments; this can be consideredan active (and preventable) waste of the registerresources of the machine..PPThere is one case where information about unused, orundefined, variables is more distractingthan helpful.This is when.I lintis applied to some, but not all, files out of a collectionwhich are to be loaded together.In this case, many of the functions and variables definedmay not be used, and, conversely,many functions and variables defined elsewhere may be used.The.B \-uflag may be used to suppress the spurious messages which might otherwise appear..SHSet/Used Information.PP.I Lintattempts to detect cases where a variable is used before it is set.This is very difficult to do well;many algorithms take a good deal of time and space,and still produce messages about perfectly valid programs..I Lintdetects local variables (automatic and register storage classes)whose first use appears physically earlier in the input file than the first assignment to the variable.It assumes that taking the address of a variable constitutes a ``use,'' since the actual usemay occur at any later time, in a data dependent fashion..PPThe restriction to the physical appearance of variables in the file makes thealgorithm very simple and quick to implement,since the true flow of control need not be discovered.It does mean that.I lintcan complain about some programs which are legal,but these programs would probably be considered bad on stylistic grounds (e.g. mightcontain at least two \fBgoto\fR's).Because static and external variables are initialized to 0,no meaningful information can be discovered about their uses.The algorithm deals correctly, however, with initialized automatic variables, and variableswhich are used in the expression which first sets them..PPThe set/used information also permits recognition of those local variables which are setand never used; these form a frequent source of inefficiencies, and may also be symptomatic of bugs..SHFlow of Control.PP.I Lintattempts to detect unreachable portions of the programs which it processes.It will complain about unlabeled statements immediately following\fBgoto\fR, \fBbreak\fR, \fBcontinue\fR, or \fBreturn\fR statements.An attempt is made to detect loops which can never be left at the bottom, detecting thespecial cases\fBwhile\fR( 1 ) and \fBfor\fR(;;) as infinite loops..I Lintalso complains about loops which cannot be entered at the top;some valid programs may have such loops, but at best they are bad style,at worst bugs..PP.I Linthas an important area of blindness in the flow of control algorithm:it has no way of detecting functions which are called and never return.Thus, a call to.I exitmay cause unreachable code which.I lintdoes not detect; the most serious effects of this are in thedetermination of returned function values (see the next section)..PPOne form of unreachable statement is not usually complained about by.I lint;a.B breakstatement that cannot be reached causes no message.Programs generated by.I yacc ,.[Johnson Yacc 1975.]and especially.I lex ,.[Lesk Lex.]may have literally hundreds of unreachable.B breakstatements.The.B \-Oflag in the C compiler will often eliminate the resulting object code inefficiency.Thus, these unreached statements are of little importance,there is typically nothing the user can do about them, and theresulting messages would clutter up the.I lintoutput.If these messages are desired,.I lintcan be invoked with the.B \-boption..SHFunction Values.PPSometimes functions return values which are never used;sometimes programs incorrectly use function ``values''which have never been returned..I Lintaddresses this problem in a number of ways..PPLocally, within a function definition,the appearance of both.DSreturn( \fIexpr\fR );.DEand.DSreturn ;.DEstatements is cause for alarm;.I lintwill give the message.DSfunction \fIname\fR contains return(e) and return.DEThe most serious difficulty with this is detecting when a function return is impliedby flow of control reaching the end of the function.This can be seen with a simple example:.DS.ta .5i 1i 1.5i\fRf ( a ) { if ( a ) return ( 3 ); g (\|); }.DENotice that, if \fIa\fR tests false, \fIf\fR will call \fIg\fR and then returnwith no defined return value; this will trigger a complaint from.I lint .If \fIg\fR, like \fIexit\fR, never returns,the message will still be produced when in fact nothing is wrong..PPIn practice, some potentially serious bugs have been discovered by this feature;it also accounts for a substantial fraction of the ``noise'' messages producedby.I lint ..PPOn a global scale,.I lintdetects cases where a function returns a value, but this value is sometimes,or always, unused.When the value is always unused, it may constitute an inefficiency in the function definition.When the value is sometimes unused, it may represent bad style (e.g., not testing forerror conditions)..PPThe dual problem, using a function value when the function does not return one,is also detected.This is a serious problem.Amazingly, this bug has been observed on a couple of occasionsin ``working'' programs; the desired function value just happened to have been computedin the function return register!.SHType Checking.PP.I Lintenforces the type checking rules of C more strictly than the compilers do.The additional checking is in four major areas:across certain binary operators and implied assignments,at the structure selection operators,between the definition and uses of functions,and in the use of enumerations..PPThere are a number of operators which have an implied balancing between types of the operands.The assignment, conditional ( ?\|: ), and relational operatorshave this property; the argumentof a \fBreturn\fR statement,and expressions used in initialization also suffer similar conversions.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -