l_states.c 27 KB

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