l_states.c 23 KB

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