Using Unix tools for C Code Preprocessing
CS371 spring 2000
Date Issued 02/22/'00
Date Due 02/29/'00
Introduction
This Laboratory will cover the Unix C Code preprocessing tools indent, cxref and lint.
indent A C program formatter which reads C programs from standard input or from specified files and writes them to standard output in a form that shows the structure of the code through indentations and spacing.
cxref This tool analyzes a collection of C files and attempts to build a cross-reference table. The cxref manual page is available at http://www.gedanken.demon.co.uk/
lclint A C program verifier. It attempts to detect features of the specified C programs on command line that are likely to be bugs, to be non-portable, or to be wasteful.
Note: for more information, please see man pages for these commands
and COG home page at http://intranet/COG/CogDocs/Linux_prog.html
The documentation for lclint is available only as a Postscript file . You will need to install Postscript browser Ghostview to read the document. Add
source /local/config/cshrc.ghostview
to the LINUX section of your .cshrc file and then type
ghostview file.ps
in the command line to read the Postscript file file.ps
Examples
We'll use maxline.c from the Lab3 as a sample for running the preprocessing tools.
In case you already don't have this command, add to your .cshrc file
source
/local/config/cshrc.c-tools
indent
Key in the following in the command line
indent -bad -bc -i4 -l60 maxline.c -o output_file
The above command formats the maxline.c and puts the output in a file called the output_file.
options:
-bc this puts a NEWLINE after each comma in a declaration.
-bad this puts a blank line after block of declaration.
-i4 this sets the number of spaces for one indentation level to 4.
-l60 this sets maximum length of an output line with a trailing comment.
You can also refer to the man page of the indent by using the command
oso[2]% man indent
cxref
Key in the following in the command line
cxref maxline.c -html -xref-all -CPP 'cpp -E -C -dD'
produces in the current directory an HTML file maxline.c.html containing cross reference table for all functions used in maxline.c program. You can browse this file by Netscape using File menu and then Open File option. You can use cxref without modifying the program.
Note: If you include in you mccabe program the header file <ctype.h> or any other header file which in turn includes <endian.h> or <bytesex.h>, cxref will fail and will give an esoteric message like
"architecture not supported"
This is because, in <bytesex.h> some macros are defined which require the architecture of the machine to be specified.
You can also refer to the online manual for cxref.
One way out of the problem is to specifically define the macro at the command line. For e.g. if maxline.c is your program then you can give the following command
cxref maxline.c -html -D__i386__ -xref-all -CPP 'cpp -E -C -dD'
The output file that is created is maxline.c.html
.
lclint
Type in the command on the command line
lclint maxline.c
The typical output is something like this :
singapore:[9] lclint maxline.c
LCLint 2.4b --- 18 Apr 98
maxline.c: (in function main)
maxline.c:30:26: Value *save used before definition
An rvalue is used that may not be initialized to a value on
some execution
path. (-usedef will suppress message)
maxline.c:31:2: Return value (type int) ignored: fflush(NULL)
Result returned by function call is not used. If this is intended,
can cast
result to (void) to eliminate message. (-retvalint will suppress
message)
maxline.c:32:2: Path with no return in function declared to return
int
There is a path through a function declared to return a value
on which there
is no return statement. This means the execution may fall through
without
returning a meaningful result to the caller. (-noret will suppress
message)
maxline.c: (in function getline)
maxline.c:41:59: Operands of != have incompatible types (int, char):
c != '\n'
A character constant is used as an int. Use +charintliteral
to allow
character constants to be used as ints. (This is safe
since the actual type
of a char constant is int.)
maxline.c:42:2: Assignment of int to char: s[i] = c
To make char and int types equivalent, use +charint.
maxline.c:41:59: Incompatible types for != (int, char) (in post loop
test):
c != '\n'
maxline.c:43:9: Operands of == have incompatible types (int, char):
c == '\n'
maxline.c:45:4: Assignment of int to char: s[i] = c
maxline.c:9:5: Function exported but not used outside maxline: getline
A declaration is exported, but not used outside this module.
Declaration can
use static qualifier. (-exportlocal will suppress message)
maxline.c:50:1: Definition of getline
maxline.c:10:6: Function exported but not used outside maxline: copy
maxline.c:63:1: Definition of copy
Finished LCLint checking --- 10 code errors found
singapore:[10]
lclint warning messages are not too significant in this case,
but in general could provide very useful hints about potential errors in
the program. It makes sense to devote some time to read carefully the list
of warnings that lclint generates after checking your source code.
Assignment (10 points)
Use the C code preprocessing tools to reformat and check your McCabe
Complexity C program from lab3.
if (...)
{
code
}