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