Traditionally, C compilers have provided a high level of optimized performance while offering programmers a great deal of flexibility. With a minimum of compiler-enforced rules, C programs are easy to port from one computing system to another. The LINT program can be used to detect certain language constructs that may cause portability problems. In addition, the LINT program can be used to check C programs for syntax and data type errors. LINT checks these areas of a program much more carefully than the C compiler does, displaying many messages that point out possible problems. Directives can be added to your code to control how LINT processes your program.
LINT checks language semantics and syntax errors, considering the following areas:
Passing all of the above tests improves the likelihood that your program will compile without these kinds of errors. However, LINT cannot ensure that your code will execute correctly. LINT provides information on potential problems due to coding constructs. With this information, you can choose to recode specific sections of code, if you feel this action is necessary.
LINT detects parts of a program that cannot be reached during execution. It writes messages concerning statements that change the program flow, such as
LINT also detects certain kinds of infinite loops as well as loops that cannot be exited or entered. Note that some working programs may include such loops.
While processing programs, LINT enforces strict type checking rules. It writes messages concerning the mixing of data types within expressions and assignment statements that are accepted by the compiler. LINT also detects type casts, which are legal C language constructs that allow the program to treat data of one type as if it were data of another type.
LINT checks the usage of both functions and variables. It detects functions that
LINT also ensures that variables are assigned values before they are ever used and that variables defined are actually used.
LINT can help ensure your code will compile and run on other systems that have a C compiler conforming to the UNIX System V standard. Use the -p option to check your code for specific portability requirements. Passing portability tests improves the likelihood but does not guarantee that your code will run on any other system.
LINT processes programs individually, displaying messages about possible source code errors to standard output. Messages concerning source files are written first, followed by messages concerning any included files. Finally, LINT checks for consistency across all the input files. Optional LINT arguments can be specified to suppress certain kinds of warning messages generated.
The -c option allows LINT to be used incrementally on a collection of C source files. First use LINT with the -c flag once for each source file. This first pass of LINT will produce a .ln file corresponding to the original .c file. After running all the source files separately through LINT, use LINT again without the -c option, listing all the .ln files together. This second pass of LINT will detect all inter-file inconsistencies.
The above procedure works quite well with the make utility. By creating .ln files, LINT need only be run on those source files modified since the last time the set of source files was checked. Use LINT on these updated source files with the -c option. A second pass of LINT can be used to check inter-file problems.
Directives are comments inserted within a C program that change the way that the LINT command operates when checking the source code. The following directives are recognized by LINT:
| /*NOSTRICT*/ | suppresses strict type checking for the following line in the source code. |
| /*NOTREACHED*/ | suppresses reporting of unreachable code. |
| /*ARGSUSED*/ | suppresses the reporting of messages concerning unused function arguments for the following function. |
| /*VARARGS*/ | suppresses the reporting of messages concerning varying number of arguments to the following function. |
| /*VARARGn*/ | suppresses the reporting of messages concerning varying number of arguments to the following function, but will perform data type checking on the first n arguments passed to the function. In particular when n=0, no arguments will be checked. |
The following options can be used to prevent LINT from reporting information on certain potential problems:
| -a | suppresses messages about assignments of long values to variables that are not declared as long. |
| -b | suppresses messages about unreachable break statements. |
| -h | suppresses messages concerning style and inefficient constructions. |
| -n | suppresses compatibility checks with either the standard or portable lint libraries. |
| -u | suppresses messages concerning functions and external variables. |
| -v | suppresses messages concerning unused function parameters. |
| -x | suppresses messages concerning unused external variables. |
| -z | suppresses messages concerning undeclared structures. |
The following examples show you how to use LINT as well as fix the errors LINT discovers. Comments in the file lint2.c will help you to correct the problems LINT finds with this program.
Before starting the example: If you can open more than one window on your screen, then leave these instructions in one window and type the commands in another. If your screen does not have windows, you may wish to print out these instructions so that you can refer to the text while you work through the example.
Tutorial directory: /afs/pdc.kth.se/public/www/training/Tutor/Basics/lint/
Sample files: lint1.c and lint2.c
lint lint1.c use LINT lint -v -x lint1.c use LINT, suppressing some messages
lint lint2.c use LINT vi lint2.c edit this file, fixing the errors spotted by LINT. Look at the comments in this program for further assistance. lint lint2.c re-run LINT cc lint2.c -o lint2 compile your program lint2 1 10 execute the program