chap3 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. main()
  104. {
  105. int i, j;
  106. if (cond) {
  107. i = 0;
  108. j = 0;
  109. }
  110. else
  111. use(i); /* i may be used before set */
  112. use(j); /* maybe j used before set */
  113. }
  114. .DE
  115. .br
  116. .ce
  117. .I
  118. figure 2.
  119. .R
  120. .KE
  121. .PP
  122. If both the if-part and the else-part are never left (i.e. they
  123. contain an endless loop or a return statement),
  124. .I lint
  125. knows that the if statement is never left too.
  126. Besides the if statement,
  127. .I lint
  128. knows the possible flows of control in while, do, for and
  129. switch statements.
  130. It also detects some endless loops like \f(CWwhile(1)\fP,
  131. \f(CWdo ... while (1)\fP, \f(CWfor (;;)\fP.
  132. .NH 2
  133. Functions
  134. .PP
  135. Most C compilers will not complain if a function is called with actual
  136. parameters of a different type than the function expects.
  137. Using a function in one file as a function of
  138. type
  139. .I A
  140. while defining it in another file as a function of type
  141. .I B
  142. is also allowed by most compilers.
  143. It needs no explanation that this can lead to serious trouble.
  144. .PP
  145. .I Lint
  146. checks if functions are called with the correct number of arguments,
  147. if the types of the actual parameters correspond with the types of
  148. the formal parameters and if function values are used in a way
  149. consistently with their declaration.
  150. When the result of a function is used, a check is made to see if
  151. the function returns a value.
  152. When a function returns a value,
  153. .I lint
  154. checks if the values of all calls of this function are used.
  155. .NH 2
  156. Undefined evaluation order
  157. .PP
  158. The semantics of C do not define evaluation orders for some
  159. constructs, which, at first sight, seem well defined.
  160. The evaluation order of the expression
  161. .ft CW
  162. a[i]\ =\ i++;
  163. .R
  164. e.g., is undefined.
  165. It can be translated to something with the semantics of
  166. .ft CW
  167. a[i]\ =\ i; i++;
  168. .R
  169. which is what probably was meant, or
  170. .ft CW
  171. a[i+1]\ =\ i; i++;.
  172. .R
  173. An easier example to explain why, is
  174. .ft CW
  175. j\ =\ a[i]\ +\ i++;.
  176. .R
  177. `\f(CW+\fR' Is a so called
  178. .I commutative
  179. operator (with respect to the evaluation order) , as is `\f(CW=\fR'.
  180. This allows the compiler to choose which term to evaluate first.
  181. It is easy to see, that it makes a difference for the value of
  182. .ft CW
  183. j,
  184. .R
  185. which order is chosen.
  186. The expression
  187. .ft CW
  188. i++
  189. .R
  190. is said to have
  191. .I
  192. side effects.
  193. .R
  194. It affects the value of
  195. .ft CW
  196. i.
  197. .R
  198. Because this value is used in the other term, this gives a conflict.
  199. .PP
  200. A function call with reference to a variable as argument can have
  201. side effects to.
  202. Therefor, the evaluation order of
  203. .ft CW
  204. i
  205. .R
  206. in the expression
  207. .ft CW
  208. f(&i)\ +\ i
  209. .R
  210. is undefined.
  211. When a function is called with an array as argument, this array
  212. can be affected by the function, because only the address of the
  213. array is passed to the function.
  214. (In Pascal a copy of the array is passed to the function if the
  215. formal parameter is not declared \fIvar\fP.)
  216. So the evaluation order of
  217. .ft CW
  218. a
  219. .R
  220. in the expression
  221. .ft CW
  222. f(a)\ +\ a[0]
  223. .R
  224. is undefined.
  225. This one is not yet detected by
  226. .I lint.
  227. .PP
  228. Global variables can still cause trouble.
  229. If function
  230. .ft CW
  231. f
  232. .R
  233. affects the global variable
  234. .ft CW
  235. i,
  236. .R
  237. the value of the expression
  238. .ft CW
  239. f()\ +\ i
  240. .R
  241. is undefined, because the evaluation order of \f(CWi\fP is undefined.
  242. .PP
  243. The evaluation order of the arguments of a function is not
  244. defined, so the expression
  245. .ft CW
  246. f(i,\ i++)
  247. .R
  248. gives a warning
  249. .ft CW
  250. i evaluation order undefined.
  251. .R
  252. .NH 2
  253. Pointer alignment problems
  254. .PP
  255. For pointers to objects of different types there are different
  256. alignment restrictions.
  257. On some machines pointers to type char can have both odd and even
  258. values, whereas pointers to type int should contain an even address.
  259. .I Lint
  260. could warn for all pointer conversions.
  261. This is not what
  262. .I lint
  263. does.
  264. .I Lint
  265. assumes that some pointers are more restricted than others, and
  266. that pointers of some types can safely be converted to a pointer
  267. of a less restrictive type.
  268. The order of restriction is as follows (`\(<=' means
  269. `is not more restricted than') :
  270. .PP
  271. .ce
  272. char \(<= short \(<= int \(<= long
  273. .ce
  274. float \(<= double
  275. .NH 2
  276. Libraries
  277. .PP
  278. C is a small language.
  279. As a matter of fact it has no i/o routines.
  280. To make it a useful language, C is supported by libraries.
  281. These libraries contain functions and variables that can be used by any
  282. C program.
  283. .I Lint
  284. knows some libraries too.
  285. At this moment it knows the `-\fIlc\fR', `-\fIlm\fR' and
  286. `-\fIlcurses\fR' libraries.
  287. The `-\fIlc\fR' library, containing definitions for functions from
  288. chapter two and three of the \s-2UNIX\s+2 programmers manual, is default.
  289. .I Lint
  290. warns for definitions of functions or global variables with the
  291. same name as a function definition in a library.
  292. .bp