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