l_states.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* Lint status checking */
  7. #include "lint.h"
  8. #ifdef LINT
  9. #include <alloc.h> /* for st_free */
  10. #include "interface.h"
  11. #include "assert.h"
  12. #include "debug.h"
  13. #ifdef ANSI
  14. #include <flt_arith.h>
  15. #endif /* ANSI */
  16. #include "arith.h"
  17. #include "label.h"
  18. #include "expr.h"
  19. #include "idf.h"
  20. #include "def.h"
  21. #include "code.h" /* RVAL etc */
  22. #include "LLlex.h"
  23. #include "Lpars.h"
  24. #include "stack.h"
  25. #include "type.h"
  26. #include "level.h"
  27. #include "l_lint.h"
  28. #include "l_brace.h"
  29. #include "l_state.h"
  30. #include "l_comment.h"
  31. #include "l_outdef.h"
  32. #ifdef DEBUG
  33. #define dbg_lint_stack(m) /*print_lint_stack(m) /* or not */
  34. #else
  35. #define dbg_lint_stack(m)
  36. #endif /* DEBUG */
  37. extern char *symbol2str();
  38. extern char *func_name;
  39. extern struct type *func_type;
  40. extern int func_notypegiven;
  41. extern char loptions[];
  42. extern struct stack_level *local_level;
  43. /* global variables for the lint_stack */
  44. PRIVATE struct lint_stack_entry *top_ls;
  45. /* global variables for the brace stack */
  46. PRIVATE int brace_count;
  47. PRIVATE struct brace *top_br;
  48. /* global variables for the function return */
  49. PRIVATE int valreturned; /* see l_lint.h */
  50. PRIVATE int return_warned;
  51. PRIVATE end_brace();
  52. PRIVATE lint_1_local();
  53. PRIVATE lint_1_global();
  54. PRIVATE start_loop_stmt();
  55. PRIVATE check_autos();
  56. PRIVATE struct auto_def *copy_auto_list();
  57. PRIVATE free_auto_list();
  58. PRIVATE struct state *copy_state();
  59. PRIVATE Free_state();
  60. PRIVATE remove_settings();
  61. PRIVATE struct auto_def *merge_autos();
  62. PRIVATE merge_states();
  63. PRIVATE struct lint_stack_entry *find_wdf(), *find_wdfc(), *find_cs();
  64. PRIVATE cont_merge();
  65. PRIVATE break_merge();
  66. PRIVATE struct lint_stack_entry *mk_lint_stack_entry();
  67. PRIVATE lint_push();
  68. PRIVATE lint_pop();
  69. lint_init_stack()
  70. {
  71. /* Allocate memory for the global lint-stack elements.
  72. */
  73. top_ls = new_lint_stack_entry();
  74. top_ls->ls_current = new_state();
  75. }
  76. lint_start_local()
  77. {
  78. register struct brace *br = new_brace();
  79. dbg_lint_stack("lint_start_local");
  80. brace_count++;
  81. br->br_count = brace_count;
  82. br->br_level = level;
  83. br->next = top_br;
  84. top_br = br;
  85. }
  86. lint_end_local(stl)
  87. struct stack_level *stl;
  88. {
  89. dbg_lint_stack("lint_end_local");
  90. if (s_NOTREACHED) {
  91. top_ls->ls_current->st_notreached = 1;
  92. top_ls->ls_current->st_warned = 0;
  93. s_NOTREACHED = 0;
  94. }
  95. if (top_ls->ls_class == CASE && level == top_ls->ls_level) {
  96. /* supply missing break; at end of switch */
  97. lint_break_stmt();
  98. }
  99. check_autos();
  100. end_brace(stl);
  101. }
  102. PRIVATE
  103. end_brace(stl)
  104. struct stack_level *stl;
  105. {
  106. /* Check if static variables and labels are used and/or set.
  107. Automatic vars have already been checked by check_autos().
  108. */
  109. register struct stack_entry *se = stl->sl_entry;
  110. register struct brace *br;
  111. while (se) {
  112. register struct idf *idf = se->se_idf;
  113. if (idf->id_def) {
  114. lint_1_local(idf, idf->id_def);
  115. }
  116. if (stl->sl_level == L_LOCAL && idf->id_label) {
  117. lint_1_local(idf, idf->id_label);
  118. }
  119. se = se->next;
  120. }
  121. /* remove entry from brace stack */
  122. br = top_br;
  123. top_br = br->next;
  124. free_brace(br);
  125. }
  126. PRIVATE
  127. lint_1_local(idf, def)
  128. struct idf *idf;
  129. struct def *def;
  130. {
  131. register int sc = def->df_sc;
  132. if ( (sc == STATIC || sc == LABEL)
  133. && !def->df_used
  134. && !is_anon_idf(idf)
  135. ) {
  136. def_warning(def, "%s %s not applied anywhere in function %s",
  137. symbol2str(sc), idf->id_text, func_name);
  138. }
  139. if ( loptions['h']
  140. && sc == AUTO
  141. && !def->df_initialized
  142. && def->df_firstbrace != 0
  143. && def->df_minlevel != level
  144. && !is_anon_idf(idf)
  145. ) {
  146. register int diff = def->df_minlevel - level;
  147. def_warning(def,
  148. "local %s could be declared %d level%s deeper",
  149. idf->id_text, diff, (diff == 1 ? "" : "s")
  150. );
  151. }
  152. }
  153. lint_end_global(stl)
  154. struct stack_level *stl;
  155. {
  156. register struct stack_entry *se = stl->sl_entry;
  157. dbg_lint_stack("lint_end_global");
  158. ASSERT(level == L_GLOBAL);
  159. while (se) {
  160. register struct idf *idf = se->se_idf;
  161. register struct def *def = idf->id_def;
  162. if (def) {
  163. lint_1_global(idf, def);
  164. }
  165. se = se->next;
  166. }
  167. }
  168. PRIVATE
  169. lint_1_global(idf, def)
  170. struct idf *idf;
  171. struct def *def;
  172. {
  173. register int sc = def->df_sc;
  174. register struct type *tp = def->df_type;
  175. register int fund = tp->tp_fund;
  176. switch (sc) {
  177. case STATIC:
  178. case EXTERN:
  179. case GLOBAL:
  180. #ifdef IMPLICIT
  181. case IMPLICIT:
  182. #endif /* IMPLICIT */
  183. if (fund == ERRONEOUS)
  184. break;
  185. if (fund == FUNCTION && sc != STATIC) {
  186. output_proto(idf, def);
  187. }
  188. if (def->df_set || def->df_used) {
  189. /* Output a line to the intermediate file for
  190. * used external variables (including functions)
  191. */
  192. output_use(idf);
  193. }
  194. if (sc == STATIC && !def->df_used) {
  195. if (def->df_set) {
  196. if (!is_anon_idf(idf) && fund != ERRONEOUS) {
  197. def_warning(def,
  198. "%s %s %s set but not used",
  199. symbol2str(sc),
  200. symbol2str(fund),
  201. idf->id_text);
  202. }
  203. }
  204. else {
  205. if (!is_anon_idf(idf) && fund != ERRONEOUS) {
  206. def_warning(def,
  207. "%s %s %s not used anywhere",
  208. symbol2str(sc),
  209. symbol2str(fund),
  210. idf->id_text);
  211. }
  212. }
  213. }
  214. if (loptions['x']) {
  215. register char *fn = def->df_file;
  216. if ( (sc == EXTERN || sc == GLOBAL)
  217. && def->df_alloc == 0
  218. && !def->df_set
  219. && !def->df_initialized
  220. && !def->df_used
  221. && strcmp(&fn[strlen(fn)-2], ".c") == 0
  222. && !is_anon_idf(idf)
  223. && fund != ERRONEOUS
  224. ) {
  225. def_warning(def,
  226. "%s %s %s not used anywhere",
  227. symbol2str(sc),
  228. symbol2str(fund),
  229. idf->id_text);
  230. }
  231. }
  232. break;
  233. }
  234. }
  235. change_state(idf, to_state)
  236. struct idf *idf;
  237. int to_state; /* SET or USED */
  238. {
  239. /* Changes the state of the variable identified by idf in the current state
  240. * on top of the stack.
  241. * The fields in the def-descriptor are set too.
  242. */
  243. register struct def *def = idf->id_def;
  244. register struct auto_def *a = top_ls->ls_current->st_auto_list;
  245. ASSERT(def);
  246. switch (to_state) {
  247. case SET:
  248. def->df_set = 1;
  249. break;
  250. case USED:
  251. def->df_used = 1;
  252. break;
  253. }
  254. /* adjust minimum required brace level */
  255. if (def->df_firstbrace == 0) {
  256. def->df_firstbrace = brace_count;
  257. def->df_minlevel = level;
  258. }
  259. else {
  260. register struct brace *br = top_br;
  261. /* find the smallest brace range from which
  262. firstbrace is visible
  263. */
  264. while (br && br->br_count > def->df_firstbrace) {
  265. br = br->next;
  266. }
  267. ASSERT(br && def->df_minlevel >= br->br_level);
  268. def->df_minlevel = br->br_level;
  269. }
  270. /* search auto_list */
  271. while(a && a->ad_idf != idf)
  272. a = a->next;
  273. if (a == 0) /* identifier not in list, global definition */
  274. return;
  275. switch (to_state) {
  276. case SET:
  277. a->ad_maybe_set = 0;
  278. a->ad_set = 1;
  279. break;
  280. case USED:
  281. if (!a->ad_set) {
  282. if (!is_anon_idf(idf)) {
  283. warning("variable %s%s uninitialized",
  284. idf->id_text,
  285. (a->ad_maybe_set ? " possibly" : "")
  286. );
  287. }
  288. a->ad_maybe_set = 0;
  289. a->ad_set = 1; /* one warning */
  290. }
  291. a->ad_used = 1;
  292. break;
  293. }
  294. }
  295. add_auto(idf) /* to current state on top of lint_stack */
  296. struct idf *idf;
  297. {
  298. /* Check if idf's definition is really an auto (or register).
  299. * It could be a static or extern too.
  300. * Watch out for formal parameters.
  301. */
  302. register struct def *def = idf->id_def;
  303. ASSERT(def);
  304. switch (def->df_sc) {
  305. register struct auto_def *a;
  306. case AUTO:
  307. case REGISTER:
  308. if (def->df_level < L_LOCAL)
  309. return; /* a formal */
  310. a = new_auto_def();
  311. a->ad_idf = idf;
  312. a->ad_def = def;
  313. a->ad_used = def->df_used;
  314. a->ad_set = def->df_set;
  315. a->next = top_ls->ls_current->st_auto_list;
  316. top_ls->ls_current->st_auto_list = a;
  317. }
  318. }
  319. PRIVATE
  320. check_autos()
  321. {
  322. /* Before leaving a block, remove the auto_defs of the automatic
  323. * variables on this level and check if they are used
  324. */
  325. register struct auto_def *a = top_ls->ls_current->st_auto_list;
  326. ASSERT(!(a && a->ad_def->df_level > level));
  327. while (a && a->ad_def->df_level == level) {
  328. struct idf *idf = a->ad_idf;
  329. struct def *def = idf->id_def;
  330. if (!def->df_used && !is_anon_idf(idf)) {
  331. if (def->df_set || a->ad_maybe_set) {
  332. def_warning(def,
  333. "%s set but not used in function %s",
  334. idf->id_text, func_name);
  335. }
  336. else {
  337. def_warning(def,
  338. "%s not used anywhere in function %s",
  339. idf->id_text, func_name);
  340. }
  341. }
  342. { /* free a */
  343. register struct auto_def *aux = a;
  344. a = a->next;
  345. free_auto_def(aux);
  346. }
  347. }
  348. top_ls->ls_current->st_auto_list = a;
  349. }
  350. lint_end_formals()
  351. {
  352. register struct stack_entry *se = local_level->sl_entry;
  353. dbg_lint_stack("lint_end_formals");
  354. ASSERT(level == L_FORMAL1);
  355. while (se) {
  356. register struct def *def = se->se_idf->id_def;
  357. if ( (def && !def->df_used)
  358. && !(f_ARGSUSED || LINTLIB)
  359. && !is_anon_idf(se->se_idf)
  360. ) {
  361. def_warning(def, "argument %s not used in function %s",
  362. se->se_idf->id_text, func_name);
  363. }
  364. se = se->next;
  365. }
  366. }
  367. PRIVATE struct auto_def *
  368. copy_auto_list(from_al, lvl)
  369. struct auto_def *from_al;
  370. int lvl;
  371. {
  372. struct auto_def *start = 0;
  373. register struct auto_def **hook = &start;
  374. /* skip too high levels */
  375. while (from_al && from_al->ad_def->df_level > lvl) {
  376. from_al = from_al->next;
  377. }
  378. while (from_al) {
  379. register struct auto_def *a = new_auto_def();
  380. *hook = a;
  381. *a = *from_al;
  382. hook = &a->next;
  383. from_al = from_al->next;
  384. }
  385. return start;
  386. }
  387. PRIVATE
  388. free_auto_list(a)
  389. register struct auto_def *a;
  390. {
  391. while (a) {
  392. register struct auto_def *aux = a;
  393. a = a->next;
  394. free_auto_def(aux);
  395. }
  396. }
  397. PRIVATE struct state *
  398. copy_state(from_st, lvl)
  399. struct state *from_st;
  400. int lvl;
  401. {
  402. /* Memory for the struct state and the struct auto_defs is allocated
  403. * by this function
  404. */
  405. register struct state *st = new_state();
  406. st->st_auto_list = copy_auto_list(from_st->st_auto_list, lvl);
  407. st->st_notreached = from_st->st_notreached;
  408. st->st_warned = from_st->st_warned;
  409. return st;
  410. }
  411. PRIVATE
  412. Free_state(stp)
  413. struct state **stp;
  414. {
  415. /* This function also frees the list of auto_defs
  416. */
  417. free_auto_list((*stp)->st_auto_list);
  418. free_state(*stp);
  419. *stp = 0;
  420. }
  421. PRIVATE
  422. remove_settings(st, lvl)
  423. struct state *st;
  424. int lvl;
  425. {
  426. /* The states of all variables on this level are set to 'not set' and
  427. * 'not maybe set'. (I think you have to read this twice.)
  428. */
  429. register struct auto_def *a = st->st_auto_list;
  430. while (a && a->ad_def->df_level == lvl) {
  431. a->ad_set = a->ad_maybe_set = 0;
  432. a = a->next;
  433. }
  434. }
  435. /******** M E R G E ********/
  436. /* modes for merging */
  437. #define NORMAL 0
  438. #define CASE_BREAK 1
  439. #define USE_ONLY 2
  440. PRIVATE
  441. merge_states(st1, st2, lvl, mode)
  442. struct state *st1, *st2;
  443. int lvl;
  444. int mode; /* NORMAL or CASE_BREAK */
  445. {
  446. /* st2 becomes the result.
  447. * st1 is left unchanged.
  448. * The resulting state is the state the program gets in if st1 OR st2
  449. * becomes the state. (E.g. the states at the end of an if-part and an
  450. * end-part are merged by this function.)
  451. */
  452. if (st1->st_notreached) {
  453. if (mode == NORMAL || st2->st_notreached) {
  454. st2->st_auto_list =
  455. merge_autos(st1->st_auto_list,
  456. st2->st_auto_list, lvl, USE_ONLY);
  457. }
  458. }
  459. else
  460. if (st2->st_notreached) {
  461. register struct auto_def *tmp = st2->st_auto_list;
  462. st2->st_auto_list = copy_auto_list(st1->st_auto_list, lvl);
  463. st2->st_notreached = 0;
  464. st2->st_warned = 0;
  465. st2->st_auto_list = merge_autos(tmp, st2->st_auto_list,
  466. lvl, USE_ONLY);
  467. free_auto_list(tmp);
  468. }
  469. else {
  470. /* both st1 and st2 reached */
  471. st2->st_auto_list =
  472. merge_autos(st1->st_auto_list, st2->st_auto_list,
  473. lvl, mode);
  474. }
  475. }
  476. PRIVATE struct auto_def *
  477. merge_autos(a1, a2, lvl, mode)
  478. struct auto_def *a1, *a2;
  479. int lvl;
  480. int mode;
  481. {
  482. /* Returns a pointer to the result.
  483. * a1 is left unchanged.
  484. * a2 is used to create this result.
  485. * The fields are set as follows:
  486. * a1_set + a2_set -> set
  487. * + a?_maybe_set -> maybe set
  488. * ELSE -> NOT set && NOT maybe set
  489. * * + a?_used -> used
  490. *
  491. * For mode == CASE_BREAK:
  492. * First a2 is taken as the result, then
  493. * variables NOT set in a2 and set or maybe set in a1 become 'maybe set'
  494. *
  495. * For mode == USE_ONLY:
  496. * Start with a2 as the result.
  497. * Variables used in a1 become used in a2.
  498. * The rest of the result is not changed.
  499. */
  500. register struct auto_def *a;
  501. /* skip too local entries */
  502. while (a1 && a1->ad_def->df_level > lvl) {
  503. a1 = a1->next;
  504. }
  505. /* discard too local entries */
  506. while (a2 && a2->ad_def->df_level > lvl) {
  507. register struct auto_def *aux = a2;
  508. a2 = a2->next;
  509. free_auto_def(aux);
  510. }
  511. a = a2; /* pointer to the result */
  512. while (a1) {
  513. ASSERT(a2);
  514. /* merge the auto_defs for one idf */
  515. ASSERT(a1->ad_idf == a2->ad_idf);
  516. if (a1->ad_used)
  517. a2->ad_used = 1;
  518. if (mode != USE_ONLY) {
  519. if ( ( !a2->ad_set
  520. && (a1->ad_set || a1->ad_maybe_set)
  521. )
  522. || ( mode == NORMAL
  523. && !a1->ad_set
  524. && (a2->ad_set || a2->ad_maybe_set)
  525. )
  526. ) {
  527. a2->ad_set = 0;
  528. a2->ad_maybe_set = 1;
  529. }
  530. }
  531. a1 = a1->next;
  532. a2 = a2->next;
  533. }
  534. ASSERT(!a2);
  535. return a;
  536. }
  537. /******** L I N T S T A C K S E A R C H I N G ********/
  538. /* The next four find-functions search the lint_stack for an entry.
  539. * The letters mean : w: WHILE; d: DO; f: FOR; s: SWITCH; c: CASE.
  540. */
  541. PRIVATE struct lint_stack_entry *
  542. find_wdf()
  543. {
  544. register struct lint_stack_entry *lse = top_ls;
  545. while (lse) {
  546. switch (lse->ls_class) {
  547. case WHILE:
  548. case DO:
  549. case FOR:
  550. return lse;
  551. }
  552. lse = lse->ls_previous;
  553. }
  554. return 0;
  555. }
  556. PRIVATE struct lint_stack_entry *
  557. find_wdfc()
  558. {
  559. register struct lint_stack_entry *lse = top_ls;
  560. while (lse) {
  561. switch (lse->ls_class) {
  562. case WHILE:
  563. case DO:
  564. case FOR:
  565. case CASE:
  566. return lse;
  567. }
  568. lse = lse->ls_previous;
  569. }
  570. return 0;
  571. }
  572. PRIVATE struct lint_stack_entry *
  573. find_cs()
  574. {
  575. register struct lint_stack_entry *lse = top_ls;
  576. while (lse) {
  577. switch (lse->ls_class) {
  578. case CASE:
  579. case SWITCH:
  580. return lse;
  581. }
  582. lse = lse->ls_previous;
  583. }
  584. return 0;
  585. }
  586. /******** A C T I O N S : I F ********/
  587. start_if_part(cst)
  588. {
  589. register struct lint_stack_entry *new = mk_lint_stack_entry(IF);
  590. dbg_lint_stack("start_if_part");
  591. if (cst)
  592. hwarning("condition in if statement is constant");
  593. lint_push(new);
  594. /* ls_current: the state at the start of the if-part
  595. */
  596. }
  597. start_else_part()
  598. {
  599. /* ls_current: the state at the end of the if-part
  600. ls_previous->ls_current: the state before the if-part
  601. */
  602. dbg_lint_stack("start_else_part");
  603. if (s_NOTREACHED) {
  604. top_ls->ls_current->st_notreached = 1;
  605. top_ls->ls_current->st_warned = 0;
  606. s_NOTREACHED = 0;
  607. }
  608. top_ls->LS_IF = top_ls->ls_current;
  609. /* this is the reason why ls_current is a pointer */
  610. top_ls->ls_current =
  611. copy_state(top_ls->ls_previous->ls_current, level);
  612. top_ls->ls_level = level;
  613. /* ls_current: the state before the if-part and the else-part
  614. LS_IF: the state at the end of the if-part
  615. */
  616. }
  617. end_if_else_stmt()
  618. {
  619. /* ls_current: state at the end of the else-part
  620. LS_IF: state at the end of the if-part
  621. */
  622. dbg_lint_stack("end_if_else_stmt");
  623. merge_states(top_ls->LS_IF, top_ls->ls_current,
  624. top_ls->ls_level, NORMAL);
  625. Free_state(&top_ls->LS_IF);
  626. Free_state(&top_ls->ls_previous->ls_current);
  627. top_ls->ls_previous->ls_current = top_ls->ls_current;
  628. lint_pop();
  629. }
  630. end_if_stmt()
  631. {
  632. /* No else-part met.
  633. ls_current: state at the end of the if-part
  634. */
  635. dbg_lint_stack("end_if_stmt");
  636. merge_states(top_ls->ls_current, top_ls->ls_previous->ls_current,
  637. top_ls->ls_level, NORMAL);
  638. Free_state(&top_ls->ls_current);
  639. lint_pop();
  640. }
  641. /******** A C T I O N S : L O O P S ********/
  642. start_while_stmt(expr)
  643. struct expr *expr;
  644. {
  645. if (is_cp_cst(expr)) {
  646. start_loop_stmt(WHILE, 1, expr->VL_VALUE != (arith)0);
  647. }
  648. else {
  649. start_loop_stmt(WHILE, 0, 0);
  650. }
  651. }
  652. start_do_stmt()
  653. {
  654. start_loop_stmt(DO, 1, 1);
  655. }
  656. start_for_stmt(expr)
  657. struct expr *expr;
  658. {
  659. if (!expr) {
  660. start_loop_stmt(FOR, 1, 1);
  661. }
  662. else
  663. if (is_cp_cst(expr)) {
  664. start_loop_stmt(FOR, 1, expr->VL_VALUE != (arith)0);
  665. }
  666. else {
  667. start_loop_stmt(FOR, 0, 0);
  668. }
  669. }
  670. PRIVATE
  671. start_loop_stmt(looptype, cst, cond)
  672. {
  673. /* If cst, the condition is a constant and its value is cond
  674. */
  675. register struct lint_stack_entry *new = mk_lint_stack_entry(looptype);
  676. dbg_lint_stack("start_loop_stmt");
  677. if (cst && !cond) {
  678. /* while (0) | for (;0;) */
  679. hwarning("condition in %s statement is always false",
  680. symbol2str(looptype));
  681. new->ls_current->st_notreached = 1;
  682. }
  683. if (cst && cond) {
  684. /* while (1) | for (;;) | do */
  685. /* omitting the copy for LS_LOOP will force this loop
  686. to be treated as a do loop
  687. */
  688. top_ls->ls_current->st_notreached = 1;
  689. top_ls->ls_current->st_warned = 0;
  690. }
  691. else {
  692. new->LS_LOOP = copy_state(top_ls->ls_current, level);
  693. }
  694. new->LS_TEST = (!cst ? TEST_VAR : cond ? TEST_TRUE : TEST_FALSE);
  695. lint_push(new);
  696. /* ls_current: the state at the start of the body
  697. LS_TEST: info about the loop test
  698. LS_BODY: 0, the state at the end of the body
  699. LS_LOOP: the state at the end of the loop, or 0 if the loop
  700. does not end
  701. */
  702. }
  703. end_loop_body()
  704. {
  705. register struct lint_stack_entry *lse = find_wdf();
  706. dbg_lint_stack("end_loop_body");
  707. ASSERT(lse == top_ls);
  708. if (!lse->ls_current->st_notreached)
  709. cont_merge(lse);
  710. }
  711. end_loop_stmt()
  712. {
  713. register struct lint_stack_entry *lse = find_wdf();
  714. dbg_lint_stack("end_loop_stmt");
  715. ASSERT(lse == top_ls);
  716. if (lse->LS_TEST != TEST_TRUE)
  717. break_merge(lse);
  718. dbg_lint_stack("end_loop_stmt after break_merge");
  719. if (!top_ls->LS_LOOP) {
  720. /* no break met; this is really an endless loop */
  721. hwarning("endless %s loop", symbol2str(top_ls->ls_class));
  722. Free_state(&top_ls->ls_current);
  723. }
  724. else {
  725. Free_state(&top_ls->ls_current);
  726. Free_state(&top_ls->ls_previous->ls_current);
  727. top_ls->ls_previous->ls_current = top_ls->LS_LOOP;
  728. }
  729. lint_pop();
  730. }
  731. end_do_stmt(cst, cond)
  732. {
  733. register struct lint_stack_entry *lse = find_wdf();
  734. dbg_lint_stack("end_do_stmt");
  735. if (cst && !cond) {
  736. /* do ... while (0) */
  737. hwarning("condition in do statement is always false");
  738. }
  739. lse->LS_TEST = (!cst ? TEST_VAR : cond ? TEST_TRUE : TEST_FALSE);
  740. end_loop_stmt();
  741. }
  742. lint_continue_stmt()
  743. {
  744. register struct lint_stack_entry *lse = find_wdf();
  745. dbg_lint_stack("lint_continue_stmt");
  746. if (!lse)
  747. return; /* not inside a loop statement */
  748. cont_merge(lse);
  749. top_ls->ls_current->st_notreached = 1;
  750. top_ls->ls_current->st_warned = 0;
  751. }
  752. /******** A C T I O N S : S W I T C H ********/
  753. start_switch_part(cst)
  754. {
  755. /* ls_current of a SWITCH entry has different meaning from ls_current of
  756. * other entries. It keeps track of which variables are used in all
  757. * following case parts. (Needed for variables declared in a compound
  758. * switch-block.)
  759. */
  760. register struct lint_stack_entry *new = mk_lint_stack_entry(SWITCH);
  761. dbg_lint_stack("start_switch_part");
  762. if (cst)
  763. hwarning("value in switch statement is constant");
  764. new->LS_CASE = copy_state(top_ls->ls_current, level);
  765. new->ls_current->st_notreached = 1;
  766. new->ls_current->st_warned = 0;
  767. top_ls->ls_current->st_notreached = 1;
  768. top_ls->ls_current->st_warned = 0;
  769. lint_push(new);
  770. }
  771. end_switch_stmt()
  772. {
  773. dbg_lint_stack("end_switch_stmt");
  774. if (top_ls->ls_class == CASE) {
  775. /* no break after last case or default */
  776. lint_break_stmt(); /* introduce break */
  777. }
  778. if (!top_ls->LS_DEFAULT_MET) {
  779. top_ls->ls_current->st_notreached = 0;
  780. if (top_ls->LS_BREAK) {
  781. merge_states(top_ls->ls_current, top_ls->LS_BREAK,
  782. top_ls->ls_level, NORMAL);
  783. Free_state(&top_ls->ls_current);
  784. }
  785. else {
  786. top_ls->LS_BREAK = top_ls->ls_current;
  787. }
  788. }
  789. else {
  790. Free_state(&top_ls->ls_current);
  791. }
  792. if (top_ls->LS_BREAK) {
  793. merge_states(top_ls->LS_CASE, top_ls->LS_BREAK,
  794. top_ls->ls_level, CASE_BREAK);
  795. Free_state(&top_ls->LS_CASE);
  796. }
  797. else {
  798. top_ls->LS_BREAK = top_ls->LS_CASE;
  799. }
  800. top_ls->LS_BREAK->st_notreached =
  801. top_ls->ls_previous->ls_current->st_notreached;
  802. /* yack */
  803. Free_state(&top_ls->ls_previous->ls_current);
  804. if (!top_ls->LS_DEFAULT_MET)
  805. top_ls->LS_BREAK->st_notreached = 0;
  806. top_ls->ls_previous->ls_current = top_ls->LS_BREAK;
  807. lint_pop();
  808. }
  809. lint_case_stmt(dflt)
  810. {
  811. /* A default statement is just a special case statement */
  812. register struct lint_stack_entry *cs_entry = find_cs();
  813. dbg_lint_stack("lint_case_stmt");
  814. if (!cs_entry)
  815. return; /* not inside switch */
  816. if (cs_entry != top_ls) {
  817. warning("%s statement in strange context",
  818. dflt ? "default" : "case");
  819. return;
  820. }
  821. switch (cs_entry->ls_class) {
  822. register struct lint_stack_entry *new;
  823. case SWITCH:
  824. if (dflt) {
  825. cs_entry->LS_DEFAULT_MET = 1;
  826. }
  827. new = mk_lint_stack_entry(CASE);
  828. remove_settings(new->ls_current, level);
  829. lint_push(new);
  830. break;
  831. case CASE:
  832. ASSERT(top_ls->ls_previous->ls_class == SWITCH);
  833. if (dflt) {
  834. cs_entry->ls_previous->LS_DEFAULT_MET = 1;
  835. }
  836. merge_states(top_ls->ls_current, top_ls->ls_previous->LS_CASE,
  837. top_ls->ls_previous->ls_level, NORMAL);
  838. merge_states(top_ls->ls_current,
  839. top_ls->ls_previous->ls_current,
  840. top_ls->ls_previous->ls_level, NORMAL);
  841. Free_state(&top_ls->ls_current);
  842. top_ls->ls_current =
  843. copy_state(top_ls->ls_previous->ls_current,
  844. top_ls->ls_previous->ls_level);
  845. remove_settings(top_ls->ls_current, top_ls->ls_level);
  846. break;
  847. default:
  848. NOTREACHED();
  849. /*NOTREACHED*/
  850. }
  851. }
  852. lint_break_stmt()
  853. {
  854. register struct lint_stack_entry *lse = find_wdfc();
  855. dbg_lint_stack("lint_break_stmt");
  856. if (!lse)
  857. return;
  858. switch (lse->ls_class) {
  859. case WHILE:
  860. case FOR:
  861. case DO:
  862. /* loop break */
  863. lse->ls_previous->ls_current->st_notreached = 0;
  864. break_merge(lse);
  865. break;
  866. case CASE:
  867. /* case break */
  868. if (!top_ls->ls_current->st_notreached) {
  869. lse->ls_previous->ls_previous->ls_current->st_notreached = 0;
  870. }
  871. merge_states(lse->ls_current, lse->ls_previous->ls_current,
  872. lse->ls_previous->ls_level, NORMAL);
  873. if (lse->ls_previous->LS_BREAK) {
  874. merge_states(top_ls->ls_current,
  875. lse->ls_previous->LS_BREAK,
  876. lse->ls_previous->ls_level, NORMAL);
  877. }
  878. else {
  879. lse->ls_previous->LS_BREAK =
  880. copy_state(top_ls->ls_current,
  881. lse->ls_previous->ls_level);
  882. }
  883. if (lse == top_ls) {
  884. Free_state(&lse->ls_current);
  885. lint_pop();
  886. }
  887. break;
  888. default:
  889. NOTREACHED();
  890. /*NOTREACHED*/
  891. }
  892. top_ls->ls_current->st_notreached = 1;
  893. top_ls->ls_current->st_warned = 0;
  894. }
  895. PRIVATE
  896. cont_merge(lse)
  897. struct lint_stack_entry *lse;
  898. {
  899. /* merge for continue statements */
  900. if (lse->LS_BODY) {
  901. merge_states(top_ls->ls_current, lse->LS_BODY,
  902. lse->ls_level, NORMAL);
  903. }
  904. else {
  905. lse->LS_BODY = copy_state(top_ls->ls_current, lse->ls_level);
  906. }
  907. }
  908. PRIVATE
  909. break_merge(lse)
  910. struct lint_stack_entry *lse;
  911. {
  912. /* merge for break statements */
  913. if (lse->LS_LOOP) {
  914. merge_states(top_ls->ls_current, lse->LS_LOOP,
  915. lse->ls_level, NORMAL);
  916. }
  917. else {
  918. lse->LS_LOOP = copy_state(top_ls->ls_current, lse->ls_level);
  919. }
  920. }
  921. /******** A C T I O N S : R E T U R N ********/
  922. lint_start_function()
  923. {
  924. dbg_lint_stack("lint_start_function");
  925. valreturned = NORETURN; /* initialization */
  926. return_warned = 0;
  927. lint_comment_function();
  928. }
  929. lint_end_function()
  930. {
  931. dbg_lint_stack("lint_end_function");
  932. /* write the function definition record */
  933. outdef();
  934. /* At this stage it is possible that top_ls->ls_current is
  935. * pointing to a state with a list of auto_defs.
  936. * These auto_defs must be freed and the state must be filled
  937. * with zeros.
  938. */
  939. ASSERT(!top_ls->ls_previous);
  940. free_auto_list(top_ls->ls_current->st_auto_list);
  941. top_ls->ls_current->st_auto_list = 0;
  942. top_ls->ls_current->st_notreached = 0;
  943. top_ls->ls_current->st_warned = 0;
  944. }
  945. lint_implicit_return()
  946. {
  947. dbg_lint_stack("lint_implicit_return");
  948. if (!top_ls->ls_current->st_notreached) {
  949. lint_return_stmt(NOVALRETURNED);
  950. }
  951. }
  952. lint_return_stmt(e)
  953. int e;
  954. {
  955. dbg_lint_stack("lint_return_stmt");
  956. if (valreturned == NORETURN) {
  957. /* first return met */
  958. register int fund = func_type->tp_fund;
  959. if ( e == NOVALRETURNED
  960. && !func_notypegiven
  961. && fund != VOID
  962. && fund != ERRONEOUS
  963. ) {
  964. warning("function %s declared %s%s but no value returned",
  965. func_name,
  966. (func_type->tp_unsigned && fund != POINTER) ?
  967. "unsigned " : "",
  968. symbol2str(fund)
  969. );
  970. /* adjust */
  971. e = VALRETURNED;
  972. }
  973. valreturned = e;
  974. }
  975. else
  976. if (valreturned != e && !return_warned) {
  977. warning("function %s does not always return a value",
  978. func_name);
  979. return_warned = 1;
  980. }
  981. if (!top_ls->ls_current->st_notreached) {
  982. set_od_valreturned(valreturned);
  983. }
  984. top_ls->ls_current->st_notreached = 1;
  985. top_ls->ls_current->st_warned = 0;
  986. }
  987. /******** A C T I O N S : J U M P ********/
  988. lint_jump_stmt(idf)
  989. struct idf *idf;
  990. {
  991. dbg_lint_stack("lint_jump_stmt");
  992. top_ls->ls_current->st_notreached = 1;
  993. top_ls->ls_current->st_warned = 0;
  994. if (idf->id_label)
  995. idf->id_label->df_used = 1;
  996. }
  997. lint_label()
  998. {
  999. /* When meeting a label, we should take the intersection of all
  1000. settings at all goto's leading this way, but this cannot reasonably
  1001. be done. So we assume that the user knows what he is doing and set
  1002. all automatic variables to set.
  1003. */
  1004. register struct auto_def *a = top_ls->ls_current->st_auto_list;
  1005. dbg_lint_stack("lint_label");
  1006. while (a) {
  1007. a->ad_maybe_set = 0;
  1008. a->ad_set = 1;
  1009. a = a->next;
  1010. }
  1011. }
  1012. /******** A C T I O N S : S T A T E M E N T ********/
  1013. lint_statement()
  1014. {
  1015. /* Check if this statement can be reached
  1016. */
  1017. dbg_lint_stack("lint_statement");
  1018. if (s_NOTREACHED) {
  1019. top_ls->ls_current->st_notreached = 1;
  1020. top_ls->ls_current->st_warned = 0;
  1021. s_NOTREACHED = 0;
  1022. }
  1023. if (DOT == '{' || DOT == ';')
  1024. return;
  1025. if (top_ls->ls_current->st_warned)
  1026. return;
  1027. if (top_ls->ls_current->st_notreached) {
  1028. if (DOT != CASE && DOT != DEFAULT && AHEAD != ':') {
  1029. if (DOT != BREAK || loptions['b'])
  1030. warning("statement cannot be reached");
  1031. top_ls->ls_current->st_warned = 1;
  1032. }
  1033. else {
  1034. top_ls->ls_current->st_notreached = 0;
  1035. top_ls->ls_current->st_warned = 0;
  1036. }
  1037. }
  1038. }
  1039. PRIVATE struct lint_stack_entry *
  1040. mk_lint_stack_entry(cl)
  1041. int cl;
  1042. {
  1043. /* Prepare a new stack entry for the lint_stack with class cl.
  1044. Copy the top ls_current to this entry and set its level.
  1045. */
  1046. register struct lint_stack_entry *new = new_lint_stack_entry();
  1047. new->ls_class = cl;
  1048. new->ls_current = copy_state(top_ls->ls_current, level);
  1049. new->ls_level = level;
  1050. return new;
  1051. }
  1052. PRIVATE
  1053. lint_push(lse)
  1054. struct lint_stack_entry *lse;
  1055. {
  1056. lse->ls_previous = top_ls;
  1057. top_ls->next = lse;
  1058. top_ls = lse;
  1059. }
  1060. PRIVATE
  1061. lint_pop()
  1062. {
  1063. top_ls = top_ls->ls_previous;
  1064. free_lint_stack_entry(top_ls->next);
  1065. }
  1066. #ifdef DEBUG
  1067. /* FOR DEBUGGING */
  1068. PRIVATE
  1069. print_autos(a)
  1070. struct auto_def *a;
  1071. {
  1072. while (a) {
  1073. struct idf *idf = a->ad_idf;
  1074. struct def *def = idf->id_def;
  1075. print("%s", idf->id_text);
  1076. print("(lvl=%d)", a->ad_def->df_level);
  1077. print("(u%ds%dm%d U%dS%d) ",
  1078. a->ad_used, a->ad_set, a->ad_maybe_set,
  1079. def->df_used, def->df_set
  1080. );
  1081. a = a->next;
  1082. }
  1083. }
  1084. PRIVATE
  1085. pr_lint_state(nm, st)
  1086. char *nm;
  1087. struct state *st;
  1088. {
  1089. print("%s: ", nm);
  1090. if (st) {
  1091. print("notreached == %d ", st->st_notreached);
  1092. print_autos(st->st_auto_list);
  1093. }
  1094. else {
  1095. print("NULL");
  1096. }
  1097. print("\n");
  1098. }
  1099. print_lint_stack(msg)
  1100. char *msg;
  1101. {
  1102. register struct lint_stack_entry *lse = top_ls;
  1103. print("Lint stack: %s(level=%d)\n", msg, level);
  1104. while (lse) {
  1105. print(" |-------------- level %d ------------\n",
  1106. lse->ls_level);
  1107. pr_lint_state(" |current", lse->ls_current);
  1108. print(" |class == %s\n",
  1109. lse->ls_class ? symbol2str(lse->ls_class) : "{");
  1110. switch (lse->ls_class) {
  1111. case SWITCH:
  1112. pr_lint_state(" |LS_BREAK", lse->LS_BREAK);
  1113. pr_lint_state(" |LS_CASE", lse->LS_CASE);
  1114. break;
  1115. case DO:
  1116. case WHILE:
  1117. case FOR:
  1118. print(" |LS_TEST == %s\n",
  1119. lse->LS_TEST == TEST_VAR ? "TEST_VAR" :
  1120. lse->LS_TEST == TEST_TRUE ? "TEST_TRUE" :
  1121. lse->LS_TEST == TEST_FALSE ? "TEST_FALSE" :
  1122. "<<BAD VALUE>>"
  1123. );
  1124. pr_lint_state(" |LS_BODY", lse->LS_BODY);
  1125. pr_lint_state(" |LS_LOOP", lse->LS_LOOP);
  1126. break;
  1127. case IF:
  1128. pr_lint_state(" |LS_IF", lse->LS_IF);
  1129. break;
  1130. default:
  1131. break;
  1132. }
  1133. lse = lse->ls_previous;
  1134. }
  1135. print(" |--------------\n\n");
  1136. }
  1137. #endif /* DEBUG */
  1138. #endif /* LINT */