Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xlint/lint1 lint: split label handling into separate...



details:   https://anonhg.NetBSD.org/src/rev/8936899781e2
branches:  trunk
changeset: 1017589:8936899781e2
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jan 01 10:55:27 2021 +0000

description:
lint: split label handling into separate functions

The only thing these cases have in common is the name "label" and the
"reached = 1" assignment.  That's not reason enough to combine
completely unrelated functions.

diffstat:

 usr.bin/xlint/lint1/cgram.y    |   12 +-
 usr.bin/xlint/lint1/externs1.h |    6 +-
 usr.bin/xlint/lint1/func.c     |  208 ++++++++++++++++++++--------------------
 3 files changed, 113 insertions(+), 113 deletions(-)

diffs (truncated from 310 to 300 lines):

diff -r c59b1381fc54 -r 8936899781e2 usr.bin/xlint/lint1/cgram.y
--- a/usr.bin/xlint/lint1/cgram.y       Fri Jan 01 10:12:25 2021 +0000
+++ b/usr.bin/xlint/lint1/cgram.y       Fri Jan 01 10:55:27 2021 +0000
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.124 2021/01/01 09:28:22 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.125 2021/01/01 10:55:27 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: cgram.y,v 1.124 2021/01/01 09:28:22 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.125 2021/01/01 10:55:27 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -1490,19 +1490,19 @@
 label:
          T_NAME T_COLON {
                symtyp = FLABEL;
-               label(T_NAME, getsym($1), NULL);
+               named_label(getsym($1));
          }
        | T_CASE constant T_COLON {
-               label(T_CASE, NULL, $2);
+               case_label($2);
                ftflg = 1;
        }
        | T_CASE constant T_ELLIPSE constant T_COLON {
                /* XXX: We don't fill all cases */
-               label(T_CASE, NULL, $2);
+               case_label($2);
                ftflg = 1;
        }
        | T_DEFAULT T_COLON {
-               label(T_DEFAULT, NULL, NULL);
+               default_label();
                ftflg = 1;
          }
        ;
diff -r c59b1381fc54 -r 8936899781e2 usr.bin/xlint/lint1/externs1.h
--- a/usr.bin/xlint/lint1/externs1.h    Fri Jan 01 10:12:25 2021 +0000
+++ b/usr.bin/xlint/lint1/externs1.h    Fri Jan 01 10:55:27 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: externs1.h,v 1.42 2020/12/30 13:17:42 rillig Exp $     */
+/*     $NetBSD: externs1.h,v 1.43 2021/01/01 10:55:28 rillig Exp $     */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -248,7 +248,9 @@
 extern void    check_statement_reachable(void);
 extern void    funcdef(sym_t *);
 extern void    funcend(void);
-extern void    label(int, sym_t *, tnode_t *);
+extern void    named_label(sym_t *);
+extern void    case_label(tnode_t *);
+extern void    default_label(void);
 extern void    if1(tnode_t *);
 extern void    if2(void);
 extern void    if3(int);
diff -r c59b1381fc54 -r 8936899781e2 usr.bin/xlint/lint1/func.c
--- a/usr.bin/xlint/lint1/func.c        Fri Jan 01 10:12:25 2021 +0000
+++ b/usr.bin/xlint/lint1/func.c        Fri Jan 01 10:55:27 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: func.c,v 1.41 2021/01/01 09:11:40 rillig Exp $ */
+/*     $NetBSD: func.c,v 1.42 2021/01/01 10:55:28 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: func.c,v 1.41 2021/01/01 09:11:40 rillig Exp $");
+__RCSID("$NetBSD: func.c,v 1.42 2021/01/01 10:55:28 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -399,15 +399,22 @@
        reached = 1;
 }
 
-/*
- * Process a label.
- *
- * typ         type of the label (T_NAME, T_DEFAULT or T_CASE).
- * sym         symbol table entry of label if typ == T_NAME
- * tn          expression if typ == T_CASE
- */
 void
-label(int typ, sym_t *sym, tnode_t *tn)
+named_label(sym_t *sym)
+{
+
+       if (sym->s_set) {
+               /* label %s redefined */
+               error(194, sym->s_name);
+       } else {
+               mark_as_set(sym);
+       }
+
+       reached = 1;
+}
+
+void
+case_label(tnode_t *tn)
 {
        cstk_t  *ci;
        clst_t  *cl;
@@ -415,111 +422,102 @@
        val_t   nv;
        tspec_t t;
 
-       switch (typ) {
-
-       case T_NAME:
-               if (sym->s_set) {
-                       /* label %s redefined */
-                       error(194, sym->s_name);
-               } else {
-                       mark_as_set(sym);
-               }
-               break;
-
-       case T_CASE:
+       /* find the stack entry for the innermost switch statement */
+       for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next)
+               continue;
 
-               /* find the stack entry for the innermost switch statement */
-               for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next)
-                       continue;
+       if (ci == NULL) {
+               /* case not in switch */
+               error(195);
+               tn = NULL;
+       } else if (tn != NULL && tn->tn_op != CON) {
+               /* non-constant case expression */
+               error(197);
+               tn = NULL;
+       } else if (tn != NULL && !tspec_is_int(tn->tn_type->t_tspec)) {
+               /* non-integral case expression */
+               error(198);
+               tn = NULL;
+       }
 
-               if (ci == NULL) {
-                       /* case not in switch */
-                       error(195);
-                       tn = NULL;
-               } else if (tn != NULL && tn->tn_op != CON) {
-                       /* non-constant case expression */
-                       error(197);
-                       tn = NULL;
-               } else if (tn != NULL && !tspec_is_int(tn->tn_type->t_tspec)) {
-                       /* non-integral case expression */
-                       error(198);
-                       tn = NULL;
+       if (tn != NULL) {
+
+               lint_assert(ci->c_swtype != NULL);
+
+               if (reached && !ftflg) {
+                       if (hflag)
+                               /* fallthrough on case statement */
+                               warning(220);
+               }
+
+               t = tn->tn_type->t_tspec;
+               if (t == LONG || t == ULONG ||
+                   t == QUAD || t == UQUAD) {
+                       if (tflag)
+                               /* case label must be of type ... */
+                               warning(203);
                }
 
-               if (tn != NULL) {
-
-                       lint_assert(ci->c_swtype != NULL);
-
-                       if (reached && !ftflg) {
-                               if (hflag)
-                                       /* fallthrough on case statement */
-                                       warning(220);
-                       }
-
-                       t = tn->tn_type->t_tspec;
-                       if (t == LONG || t == ULONG ||
-                           t == QUAD || t == UQUAD) {
-                               if (tflag)
-                                       /* case label must be of type ... */
-                                       warning(203);
-                       }
-
-                       /*
-                        * get the value of the expression and convert it
-                        * to the type of the switch expression
-                        */
-                       v = constant(tn, 1);
-                       (void) memset(&nv, 0, sizeof nv);
-                       cvtcon(CASE, 0, ci->c_swtype, &nv, v);
-                       free(v);
+               /*
+                * get the value of the expression and convert it
+                * to the type of the switch expression
+                */
+               v = constant(tn, 1);
+               (void) memset(&nv, 0, sizeof nv);
+               cvtcon(CASE, 0, ci->c_swtype, &nv, v);
+               free(v);
 
-                       /* look if we had this value already */
-                       for (cl = ci->c_clst; cl != NULL; cl = cl->cl_next) {
-                               if (cl->cl_val.v_quad == nv.v_quad)
-                                       break;
-                       }
-                       if (cl != NULL && tspec_is_uint(nv.v_tspec)) {
-                               /* duplicate case in switch: %lu */
-                               error(200, (u_long)nv.v_quad);
-                       } else if (cl != NULL) {
-                               /* duplicate case in switch: %ld */
-                               error(199, (long)nv.v_quad);
-                       } else {
-                               /*
-                                * append the value to the list of
-                                * case values
-                                */
-                               cl = xcalloc(1, sizeof (clst_t));
-                               cl->cl_val = nv;
-                               cl->cl_next = ci->c_clst;
-                               ci->c_clst = cl;
-                       }
+               /* look if we had this value already */
+               for (cl = ci->c_clst; cl != NULL; cl = cl->cl_next) {
+                       if (cl->cl_val.v_quad == nv.v_quad)
+                               break;
+               }
+               if (cl != NULL && tspec_is_uint(nv.v_tspec)) {
+                       /* duplicate case in switch: %lu */
+                       error(200, (u_long)nv.v_quad);
+               } else if (cl != NULL) {
+                       /* duplicate case in switch: %ld */
+                       error(199, (long)nv.v_quad);
+               } else {
+                       /*
+                        * append the value to the list of
+                        * case values
+                        */
+                       cl = xcalloc(1, sizeof (clst_t));
+                       cl->cl_val = nv;
+                       cl->cl_next = ci->c_clst;
+                       ci->c_clst = cl;
                }
-               tfreeblk();
-               break;
+       }
+       tfreeblk();
+
+       reached = 1;
+}
 
-       case T_DEFAULT:
+void
+default_label(void)
+{
+       cstk_t  *ci;
 
-               /* find the stack entry for the innermost switch statement */
-               for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next)
-                       continue;
+       /* find the stack entry for the innermost switch statement */
+       for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_next)
+               continue;
 
-               if (ci == NULL) {
-                       /* default outside switch */
-                       error(201);
-               } else if (ci->c_default) {
-                       /* duplicate default in switch */
-                       error(202);
-               } else {
-                       if (reached && !ftflg) {
-                               if (hflag)
-                                       /* fallthrough on default statement */
-                                       warning(284);
-                       }
-                       ci->c_default = 1;
+       if (ci == NULL) {
+               /* default outside switch */
+               error(201);
+       } else if (ci->c_default) {
+               /* duplicate default in switch */
+               error(202);
+       } else {
+               if (reached && !ftflg) {
+                       if (hflag)
+                               /* fallthrough on default statement */



Home | Main Index | Thread Index | Old Index