PDC's Tutorial on LINT
[PDC - Center for Parallel Computers, KTH]

Tutorial on LINT


Table of Contents


What is LINT?

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 : A C Programming Tool

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.


Operation of LINT

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.


Using LINT with the Make Utility

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.


LINT Directives

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:


Using LINT

examples:

Useful LINT Options

The following options can be used to prevent LINT from reporting information on certain potential problems:


Step by Step Examples

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.

  1. Copy the sample files to your working directory. You may copy the file from within your browser, or use the cp command. Some browsers add or replace characters when files are copied.

    Tutorial directory: /afs/pdc.kth.se/public/www/training/Tutor/Basics/lint/

    Sample files: lint1.c and lint2.c

  2. First example:
     
    	lint lint1.c			use LINT
     
    	lint -v -x lint1.c		use LINT, suppressing some
    					messages
  3. Second example:
     
    	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
    
  4. Cleanup: This tutorial will leave the following files in your working directory, which you may wish to erase to conserve space:

References

AIX Operating System, Version 1.2, Programming Tools and Interface
AIX Operating System, Version 1.2, Commands Reference, Volume 1


[Copyright Statement] [Feedback]

Changed by:$Author: kinsella $,$Date: 1999/07/13 08:07:54 $