4 Set, used and unused variables
6 We make a distinction between two classes of variables:
7 the class of automatic variables (including register variables)
8 and the other variables.
9 The other variables, global variables, static variables, formal
10 parameters et cetera, are assumed to have a defined value.
11 Global variables e.g., are initialized by the compiled code at
12 zeros; formal parameters have a value which is equal to the value
13 of the corresponding actual parameter.
14 These variables can be used without explicitly initializing them.
15 The initial value of automatic variables is undefined (if they are
16 not initialized at declaration).
17 These variables should be set before they are used.
22 an assignment (including an initialization)
27 The first case is clear. The second case is plausible.
28 It would take to much effort (if at all possible) to check
29 if a variable is set through one of its aliases.
32 should not warn about correct constructs, it does this conservative
34 Structures (and unions) can also be set by setting at
36 Again a conservative approach.
37 An array can be set by using its name (e.g. as actual parameter
42 of automatic variables which are not set.
53 Arrays and structures (and unions) are also used if one entry
54 or one member respectively is used.
57 When a variable is never used in the part of the program where it is
58 visible, a warning is given.
59 For variables declared at the beginning of a compound statement,
60 a check is made at the end of this statement.
61 For formal parameters a check is made at the end of the function
63 At the end of a file this is done for global static definitions.
64 For external variables a warning can be given when all the files
71 keeps track of the flow of control is best explained by means of
73 See the program of figure 1.
78 /* a statement which is executed if cond is true,
91 After evaluation of \f(CWcond\fP, two things can happen.
92 The if-part is executed or the else-part is executed (but not both).
93 Variables which are set in the if-part but not in the else-part,
94 need not be set after the if statement, and vice versa.
96 detects this and assumes these variables after the if statement to
113 use(i); /* i may be used before set */
114 use(j); /* maybe j used before set */
124 If both the if-part and the else-part are never left (i.e. they
125 contain an endless loop or a return statement),
127 knows that the if statement is never left too.
128 Besides the if statement,
130 knows the possible flows of control in while, do, for and
132 It also detects some endless loops like \f(CWwhile(1)\fP,
133 \f(CWdo ... while (1)\fP, \f(CWfor (;;)\fP.
137 Most C compilers will not complain if a function is called with actual
138 parameters of a different type than the function expects.
139 Using a function in one file as a function of
142 while defining it in another file as a function of type
144 is also allowed by most compilers.
145 It needs no explanation that this can lead to serious trouble.
148 checks if functions are called with the correct number of arguments,
149 if the types of the actual parameters correspond with the types of
150 the formal parameters and if function values are used in a way
151 consistently with their declaration.
152 When the result of a function is used, a check is made to see if
153 the function returns a value.
154 When a function returns a value,
156 checks if the values of all calls of this function are used.
158 Undefined evaluation order
160 The semantics of C do not define evaluation orders for some
161 constructs, which, at first sight, seem well defined.
162 The evaluation order of the expression
167 It can be translated to something with the semantics of
171 which is what probably was meant, or
175 An easier example to explain why, is
179 `\f(CW+\fR' Is a so called
181 operator (with respect to the evaluation order) , as is `\f(CW=\fR'.
182 This allows the compiler to choose which term to evaluate first.
183 It is easy to see, that it makes a difference for the value of
187 which order is chosen.
196 It affects the value of
200 Because this value is used in the other term, this gives a conflict.
202 A function call with reference to a variable as argument can have
204 Therefor, the evaluation order of
213 When a function is called with an array as argument, this array
214 can be affected by the function, because only the address of the
215 array is passed to the function.
216 (In Pascal a copy of the array is passed to the function if the
217 formal parameter is not declared \fIvar\fP.)
218 So the evaluation order of
227 This one is not yet detected by
230 Global variables can still cause trouble.
235 affects the global variable
239 the value of the expression
243 is undefined, because the evaluation order of \f(CWi\fP is undefined.
245 The evaluation order of the arguments of a function is not
246 defined, so the expression
252 i evaluation order undefined.
255 Pointer alignment problems
257 For pointers to objects of different types there are different
258 alignment restrictions.
259 On some machines pointers to type char can have both odd and even
260 values, whereas pointers to type int should contain an even address.
262 could warn for all pointer conversions.
267 assumes that some pointers are more restricted than others, and
268 that pointers of some types can safely be converted to a pointer
269 of a less restrictive type.
270 The order of restriction is as follows (`\(<=' means
271 `is not more restricted than') :
274 char \(<= short \(<= int \(<= long
280 C is a small language.
281 As a matter of fact it has no i/o routines.
282 To make it a useful language, C is supported by libraries.
283 These libraries contain functions and variables that can be used by any
286 knows some libraries too.
287 At this moment it knows the `-\fIlc\fR', `-\fIlm\fR' and
288 `-\fIlcurses\fR' libraries.
289 The `-\fIlc\fR' library, containing definitions for functions from
290 chapter two and three of the \s-2UNIX\s+2 programmers manual, is default.
292 warns for definitions of functions or global variables with the
293 same name as a function definition in a library.