Pristine Ack-5.5
[Ack-5.5.git] / doc / lint / chap3
1 .NH
2 What lint checks
3 .NH 2
4 Set, used and unused variables
5 .PP
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.
18 A variable is set by
19 .IP
20 .RS
21 .IP 1.
22 an assignment (including an initialization)
23 .IP 2.
24 taking the address
25 .RE
26 .PP
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.
30 Because
31 .I lint
32 should not warn about correct constructs, it does this conservative
33 approach.
34 Structures (and unions) can also be set by setting at
35 least one member.
36 Again a conservative approach.
37 An array can be set by using its name (e.g. as actual parameter
38 of a function call).
39 .I Lint
40 warns for usage as
41 .I rvalue
42 of automatic variables which are not set.
43 .PP
44 A variable is used if
45 .IP
46 .RS
47 .IP 1.
48 it is used as a
49 .I rvalue
50 .IP 2
51 its address is taken
52 .IP
53 Arrays and structures (and unions) are also used if one entry
54 or one member respectively is used.
55 .RE
56 .PP
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
62 definition.
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
65 are parsed.
66 .NH 2
67 Flow of control
68 .PP
69 The way
70 .I lint
71 keeps track of the flow of control is best explained by means of
72 an example.
73 See the program of figure 1.
74 .KF
75 .DS B
76 .ft CW
77 if (cond)
78         /* a statement which is executed if cond is true,
79          * the if-part
80          */
81 else
82         /* the else-part */
83 .DE
84 .br
85 .ce
86 .I
87 figure\ 1.
88 .R
89 .KE
90 .PP
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.
95 .I Lint
96 detects this and assumes these variables after the if statement to
97 be \fImaybe set\fR.
98 (See figure 2.)
99 .KF
100 .DS B
101 .ft CW
102 int cond;
103
104 main()
105 {
106         int i, j;
107
108         if (cond) {
109                 i = 0;
110                 j = 0;
111         }
112         else
113                 use(i);  /* i may be used before set */
114         use(j);          /* maybe j used before set  */
115 }
116 .DE
117 .br
118 .ce
119 .I
120 figure 2.
121 .R
122 .KE
123 .PP
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),
126 .I lint
127 knows that the if statement is never left too.
128 Besides the if statement,
129 .I lint
130 knows the possible flows of control in while, do, for and
131 switch statements.
132 It also detects some endless loops like \f(CWwhile(1)\fP,
133 \f(CWdo ... while (1)\fP, \f(CWfor (;;)\fP.
134 .NH 2
135 Functions
136 .PP
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
140 type
141 .I A
142 while defining it in another file as a function of type
143 .I B
144 is also allowed by most compilers.
145 It needs no explanation that this can lead to serious trouble.
146 .PP
147 .I Lint
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,
155 .I lint
156 checks if the values of all calls of this function are used.
157 .NH 2
158 Undefined evaluation order
159 .PP
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
163 .ft CW
164 a[i]\ =\ i++;
165 .R
166 e.g., is undefined.
167 It can be translated to something with the semantics of
168 .ft CW
169 a[i]\ =\ i; i++;
170 .R
171 which is what probably was meant, or
172 .ft CW
173 a[i+1]\ =\ i; i++;.
174 .R
175 An easier example to explain why, is
176 .ft CW
177 j\ =\ a[i]\ +\ i++;.
178 .R
179 `\f(CW+\fR' Is a so called
180 .I commutative
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
184 .ft CW
185 j,
186 .R
187 which order is chosen.
188 The expression
189 .ft CW
190 i++
191 .R
192 is said to have
193 .I
194 side effects.
195 .R
196 It affects the value of
197 .ft CW
198 i.
199 .R
200 Because this value is used in the other term, this gives a conflict.
201 .PP
202 A function call with reference to a variable as argument can have
203 side effects to.
204 Therefor, the evaluation order of
205 .ft CW
206 i
207 .R
208 in the expression
209 .ft CW
210 f(&i)\ +\ i
211 .R
212 is undefined.
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
219 .ft CW
220 a
221 .R
222 in the expression
223 .ft CW
224 f(a)\ +\ a[0]
225 .R
226 is undefined.
227 This one is not yet detected by
228 .I lint.
229 .PP
230 Global variables can still cause trouble.
231 If function
232 .ft CW
233 f
234 .R
235 affects the global variable
236 .ft CW
237 i,
238 .R
239 the value of the expression
240 .ft CW
241 f()\ +\ i
242 .R
243 is undefined, because the evaluation order of \f(CWi\fP is undefined.
244 .PP
245 The evaluation order of the arguments of a function is not
246 defined, so the expression
247 .ft CW
248 f(i,\ i++)
249 .R
250 gives a warning
251 .ft CW
252 i evaluation order undefined.
253 .R
254 .NH 2
255 Pointer alignment problems
256 .PP
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.
261 .I Lint
262 could warn for all pointer conversions.
263 This is not what
264 .I lint
265 does.
266 .I Lint
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') :
272 .PP
273 .ce
274 char \(<= short \(<= int \(<= long
275 .ce
276 float \(<= double
277 .NH 2
278 Libraries
279 .PP
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
284 C program.
285 .I Lint
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.
291 .I Lint
292 warns for definitions of functions or global variables with the
293 same name as a function definition in a library.
294 .bp