Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / xlint / xlint / lint.1
1 .\"     $NetBSD: lint.1,v 1.3 1995/10/23 13:45:31 jpo Exp $
2 .\"
3 .\" Copyright (c) 1994, 1995 Jochen Pohl
4 .\" All Rights Reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\"    notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\"    must display the following acknowledgement:
16 .\"      This product includes software developed by Jochen Pohl for
17 .\"      The NetBSD Project.
18 .\" 4. The name of the author may not be used to endorse or promote products
19 .\"    derived from this software without specific prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 .\"
32 .\" $FreeBSD: src/usr.bin/xlint/xlint/lint.1,v 1.8.2.7 2002/06/21 15:30:23 charnier Exp $
33 .\"
34 .Dd August 28, 1994
35 .Dt LINT 1
36 .Os
37 .Sh NAME
38 .Nm lint
39 .Nd a C program verifier
40 .Sh SYNOPSIS
41 .Bk -words
42 .Nm
43 .Op Fl abceghprvxzHFV
44 .Op Fl s | t
45 .Op Fl i | nu
46 .Op Fl D Ar name Ns Op = Ns Ar def
47 .Op Fl U Ar name
48 .Op Fl I Ar directory
49 .Op Fl L Ar directory
50 .Op Fl l Ar library
51 .Op Fl o Ar outputfile
52 .Ar
53 .Nm
54 .Op Fl abceghprvzHFV
55 .Op Fl s | t
56 .Fl C Ar library
57 .Op Fl D Ar name Ns Op = Ns Ar def
58 .Op Fl I Ar directory
59 .Op Fl U Ar name
60 .Ar
61 .Ek
62 .Sh DESCRIPTION
63 The
64 .Nm
65 utility attempts to detect features of the named C program files
66 that are likely to be bugs, to be non-portable, or to be
67 wasteful.
68 It also performs stricter type checking than
69 the C compiler.
70 The
71 .Nm
72 utility runs the C preprocessor as its first phase, with the
73 preprocessor symbol
74 .Dq Dv lint
75 defined to allow certain questionable code to be altered
76 or skipped by
77 .Nm .
78 Therefore, this symbol should be thought of as a reserved
79 word for all code that is to be checked by
80 .Nm .
81 .Pp
82 Among the possible problems that are currently noted are
83 unreachable statements, loops not entered at the top,
84 variables declared and not used, and logical expressions
85 with constant values.
86 Function calls are checked for
87 inconsistencies, such as calls to functions that return
88 values in some places and not in others, functions called
89 with varying numbers of arguments, function calls that
90 pass arguments of a type other than the type the function
91 expects to receive, functions whose values are not used,
92 and calls to functions not returning values that use
93 the non-existent return value of the function.
94 .Pp
95 Filename arguments ending with
96 .Pa .c
97 are taken to be C source files.
98 Filename arguments with
99 names ending with
100 .Pa .ln
101 are taken to be the result of an earlier invocation of
102 .Nm ,
103 with either the
104 .Fl i , o ,
105 or
106 .Fl C
107 option in effect.
108 The
109 .Pa .ln
110 files are analogous to the
111 .Pa .o
112 (object) files produced by
113 .Xr cc 1
114 from
115 .Pa .c
116 files.
117 The
118 .Nm
119 utility also accepts special libraries specified with the
120 .Fl l
121 option, which contain definitions of library routines and
122 variables.
123 .Pp
124 The
125 .Nm
126 utility takes all the
127 .Pa .c , .ln ,
128 and
129 .Pa llib-l Ns Ar library Ns Pa .ln
130 (lint library) files and processes them in command-line order.
131 By default,
132 .Nm
133 appends the standard C lint library
134 .Pq Pa llib-lc.ln
135 to the end of the list of files.
136 When the
137 .Fl i
138 option is used, the
139 .Pa .ln
140 files are ignored.
141 Also, when the
142 .Fl o
143 or
144 .Fl i
145 options are used, the
146 .Pa llib-l Ns Ar library Ns Pa .ln
147 files are ignored.
148 When the
149 .Fl i
150 option is
151 .Em omitted
152 the second pass of
153 .Nm
154 checks this list of files for mutual compatibility.
155 At this point,
156 if a complaint stems not from a given source file, but from one of
157 its included files, the source filename will be printed followed by
158 a question mark.
159 .Pp
160 The options are as follows:
161 .Bl -tag -width indent
162 .It Fl a
163 Report assignments of
164 .Vt long
165 values to variables that are not
166 .Vt long .
167 .It Fl aa
168 Additional to
169 .Fl a ,
170 report
171 .Em all
172 assignments of integer values to other integer values which
173 cause implicit narrowing conversion.
174 .It Fl b
175 Report
176 .Ic break
177 statements that cannot be reached.
178 This is not the default
179 because, unfortunately, most
180 .Xr lex 1
181 and many
182 .Xr yacc 1
183 outputs produce many such complaints.
184 .It Fl c
185 Complain about casts which have questionable portability.
186 .It Fl e
187 Complain about unusual operations on
188 .Vt enum Ns -Types
189 and combinations of
190 .Vt enum Ns -
191 and
192 .Vt int Ns eger-Types .
193 .It Fl g
194 Don't print warnings for some extensions of
195 .Xr gcc 1
196 to the C language.
197 Currently these are nonconstant initializers in
198 automatic aggregate initializations, arithmetic on pointer to void,
199 zero sized structures, subscripting of non-lvalue arrays, prototypes
200 overriding old style function declarations and long long
201 integer types.
202 The
203 .Fl g
204 flag also turns on the keywords
205 .Ic asm
206 and
207 .Ic inline
208 (alternate keywords with leading underscores for both
209 .Ic asm
210 and
211 .Ic inline
212 are always available).
213 .It Fl h
214 Apply a number of heuristic tests to attempt to intuit
215 bugs, improve style, and reduce waste.
216 .It Fl i
217 Produce a
218 .Pa .ln
219 file for every
220 .Pa .c
221 file on the command line.
222 These
223 .Pa .ln
224 files are the product of
225 .Nm Ns 's
226 first pass only, and are not checked for compatibility
227 between functions.
228 .It Fl n
229 Do not check compatibility against the standard library.
230 .It Fl p
231 Attempt to check portability of code to other dialects of C.
232 .It Fl r
233 In case of redeclarations report the position of the
234 previous declaration.
235 .It Fl s
236 Strict ANSI C mode.
237 Issue warnings and errors required by ANSI C.
238 Also do not produce warnings for constructs which behave
239 differently in traditional C and ANSI C.
240 With the
241 .Fl s
242 flag,
243 .Dv __STRICT_ANSI__
244 is a predefined preprocessor macro.
245 .It Fl t
246 Traditional C mode.
247 .Dv __STDC__
248 is not predefined in this mode.
249 Warnings are printed for constructs
250 not allowed in traditional C.
251 Warnings for constructs which behave
252 differently in traditional C and ANSI C are suppressed.
253 Preprocessor
254 macros describing the machine type (e.g.,
255 .Dv sun3 )
256 and machine architecture (e.g.,
257 .Dv m68k )
258 are defined without leading and trailing underscores.
259 The keywords
260 .Ic const , volatile
261 and
262 .Ic signed
263 are not available in traditional C mode (although the alternate
264 keywords with leading underscores still are).
265 .It Fl u
266 Do not complain about functions and external variables used
267 and not defined, or defined and not used (this is suitable
268 for running
269 .Nm
270 on a subset of files comprising part of a larger program).
271 .It Fl v
272 Suppress complaints about unused arguments in functions.
273 .It Fl x
274 Report variables referred to by
275 .Ic extern
276 declarations, but never used.
277 .It Fl z
278 Do not complain about structures that are never defined
279 (for example, using a structure pointer without knowing
280 its contents).
281 .It Fl C Ar library
282 Create a
283 .Nm
284 library with the name
285 .Pa llib-l Ns Ar library Ns Pa .ln .
286 This library is built from all
287 .Pa .c
288 and
289 .Pa .ln
290 input files.
291 After all global definitions of functions and
292 variables in these files are written to the newly created library,
293 .Nm
294 checks all input files, including libraries specified with the
295 .Fl l
296 option, for mutual compatibility.
297 .It Fl D Ar name Ns Op = Ns Ar def
298 Define
299 .Ar name
300 for
301 .Xr cpp 1 ,
302 as if by a
303 .Ic #define
304 directive.
305 If no definition is given,
306 .Ar name
307 is defined as 1.
308 .It Fl I Ar directory
309 Add
310 .Ar directory
311 to the list of directories in which to search for include files.
312 .It Fl l Ar library
313 Include the lint library
314 .Pa llib-l Ns Ar library Ns Pa .ln .
315 .It Fl L Ar directory
316 Search for lint libraries in
317 .Ar directory
318 and
319 .Ar directory Ns Pa /lint
320 before searching the standard place.
321 .It Fl F
322 Print pathnames of files.
323 The
324 .Nm
325 utility normally prints the filename without the path.
326 .It Fl H
327 If a complaint stems from an included file
328 .Nm
329 prints the name of the included file instead of the source file name
330 followed by a question mark.
331 .It Fl o Ar outputfile
332 Name the output file
333 .Ar outputfile .
334 The output file produced is the input that is given to
335 .Nm Ns 's
336 second pass.
337 The
338 .Fl o
339 option simply saves this file in the named output file.
340 If the
341 .Fl i
342 option is also used the files are not checked for compatibility.
343 To produce a
344 .Pa llib-l Ns Ar library Ns Pa .ln
345 without extraneous messages, use of the
346 .Fl u
347 option is suggested.
348 The
349 .Fl v
350 option is useful if the source file(s) for the lint library
351 are just external interfaces.
352 .It Fl U Ar name
353 Remove any initial definition of
354 .Ar name
355 for the preprocessor.
356 .It Fl V
357 Print the command lines constructed by the controller program to
358 run the C preprocessor and
359 .Nm Ns 's
360 first and second pass.
361 .El
362 .Ss Input Grammar
363 .Nm Ns 's
364 first pass reads standard C source files.
365 The
366 .Nm
367 utility recognizes the following C comments as commands.
368 .Bl -tag -width indent
369 .It Li /* ARGSUSED Ns Ar n Li */
370 makes
371 .Nm
372 check only the first
373 .Ar n
374 arguments for usage; a missing
375 .Ar n
376 is taken to be 0 (this option acts like the
377 .Fl v
378 option for the next function).
379 .It Xo
380 .Li /* CONSTCOND */
381 or
382 .Li /* CONSTANTCOND */
383 or
384 .Li /* CONSTANTCONDITION */
385 .Xc
386 suppress complaints about constant operands for the next expression.
387 .It Xo
388 .Li /* FALLTHRU */
389 or
390 .Li /* FALLTHROUGH */
391 .Xc
392 suppress complaints about fall through to a
393 .Ic case
394 or
395 .Ic default
396 labelled statement.
397 This directive should be placed immediately
398 preceding the label.
399 .It Li /* LINTLIBRARY */
400 At the beginning of a file, mark all functions and variables defined
401 in this file as
402 .Em used .
403 Also shut off complaints about unused function arguments.
404 .It Xo
405 .Li /* LINTED Oo Ar comment Oc Li */
406 or
407 .Li /* NOSTRICT Oo Ar comment Oc Li */
408 .Xc
409 Suppresses any intra-file warning except those dealing with
410 unused variables or functions.
411 This directive should be placed
412 on the line immediately preceding where the
413 .Nm
414 warning occurred.
415 .It Li /* LONGLONG */
416 Suppress complaints about use of long long integer types.
417 .It Li /* NOTREACHED */
418 At appropriate points, inhibit complaints about unreachable code.
419 (This comment is typically placed just after calls to functions
420 like
421 .Xr exit 3 ) .
422 .It Li /* PRINTFLIKE Ns Ar n Li */
423 makes
424 .Nm
425 check the first
426 .Pq Ar n Ns -1
427 arguments as usual.
428 The
429 .Ar n Ns -th
430 argument is interpreted as a
431 .Xr printf 3
432 format string that is used to check the remaining arguments.
433 .It Li /* PROTOLIB Ns Ar n Li */
434 causes
435 .Nm
436 to treat function declaration prototypes as function definitions
437 if
438 .Ar n
439 is non-zero.
440 This directive can only be used in conjunction with
441 the
442 .Li /* LINTLIBRARY */
443 directive.
444 If
445 .Ar n
446 is zero, function prototypes will be treated normally.
447 .It Li /* SCANFLIKE Ns Ar n Li */
448 makes
449 .Nm
450 check the first
451 .Pq Ar n Ns -1
452 arguments as usual.
453 The
454 .Ar n Ns -th
455 argument is interpreted as a
456 .Xr scanf 3
457 format string that is used to check the remaining arguments.
458 .It Li /* VARARGS Ns Ar n Li */
459 Suppress the usual checking for variable numbers of arguments in
460 the following function declaration.
461 The data types of the first
462 .Ar n
463 arguments are checked; a missing
464 .Ar n
465 is taken to be 0.
466 .El
467 .Pp
468 The behavior of the
469 .Fl i
470 and the
471 .Fl o
472 options allows for incremental use of
473 .Nm
474 on a set of C source files.
475 Generally, one invokes
476 .Nm
477 once for each source file with the
478 .Fl i
479 option.
480 Each of these invocations produces a
481 .Pa .ln
482 file that corresponds to the
483 .Pa .c
484 file, and prints all messages that are about just that
485 source file.
486 After all the source files have been separately
487 run through
488 .Nm ,
489 it is invoked once more (without the
490 .Fl i
491 option), listing all the
492 .Pa .ln
493 files with the needed
494 .Fl l Ns Ar library
495 options.
496 This will print all the inter-file inconsistencies.
497 This
498 scheme works well with
499 .Xr make 1 ;
500 it allows
501 .Xr make 1
502 to be used to
503 .Nm
504 only the source files that have been modified since the last
505 time the set of source files were
506 .Nm Ns ed .
507 .Sh ENVIRONMENT
508 .Bl -tag -width LIBDIR
509 .It Ev LIBDIR
510 the directory where the lint libraries specified by the
511 .Bk -words
512 .Fl l Ar library
513 .Ek
514 option must exist.
515 If this environment variable is undefined,
516 then the default path
517 .Pa /usr/libdata/lint
518 will be used to search for the libraries.
519 .It Ev TMPDIR
520 usually the path for temporary files can be redefined by setting
521 this environment variable.
522 .El
523 .Sh FILES
524 .Bl -tag -width /usr/libdata/lint/llib-lc.ln -compact
525 .It Pa /usr/libexec/lint Ns Bq Pa 12
526 programs
527 .It Pa /usr/libdata/lint/llib-l*.ln
528 various prebuilt lint libraries
529 .It Pa /tmp/lint*
530 temporaries
531 .El
532 .Sh SEE ALSO
533 .Xr cc 1 ,
534 .Xr cpp 1 ,
535 .Xr make 1
536 .Sh AUTHORS
537 .An Jochen Pohl
538 .Sh BUGS
539 .Bl -item
540 .It
541 The routines
542 .Xr exit 3 ,
543 .Xr longjmp 3
544 and other functions that do not return are not understood; this
545 causes various incorrect diagnostics.
546 .It
547 Static functions which are used only before their first
548 extern declaration are reported as unused.
549 .It
550 Libraries created by the
551 .Fl o
552 option will, when used in later
553 .Nm
554 runs, cause certain errors that were reported when the libraries
555 were created to be reported again, and cause line numbers and file
556 names from the original source used to create those libraries
557 to be reported in error messages.
558 For these reasons, it is recommended
559 to use the
560 .Fl C
561 option to create lint libraries.
562 .El