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: merge duplicate code for handling ...



details:   https://anonhg.NetBSD.org/src/rev/903158bd991b
branches:  trunk
changeset: 362449:903158bd991b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Feb 27 18:29:14 2022 +0000

description:
lint: merge duplicate code for handling plain and wide strings

No functional change.  As before, the string literals "1" "2" "3" are
not concatenated from left to right, instead concatenation starts with
"23" and then proceeds to "123".

diffstat:

 usr.bin/xlint/lint1/ckgetopt.c |   9 +++----
 usr.bin/xlint/lint1/debug.c    |  19 +++++++--------
 usr.bin/xlint/lint1/emit1.c    |  11 ++++-----
 usr.bin/xlint/lint1/lex.c      |  19 +++++----------
 usr.bin/xlint/lint1/lint1.h    |  14 +++--------
 usr.bin/xlint/lint1/tree.c     |  50 +++++++++++++++--------------------------
 6 files changed, 48 insertions(+), 74 deletions(-)

diffs (289 lines):

diff -r 87c75225b49d -r 903158bd991b usr.bin/xlint/lint1/ckgetopt.c
--- a/usr.bin/xlint/lint1/ckgetopt.c    Sun Feb 27 17:12:06 2022 +0000
+++ b/usr.bin/xlint/lint1/ckgetopt.c    Sun Feb 27 18:29:14 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: ckgetopt.c,v 1.14 2021/11/01 19:48:51 rillig Exp $ */
+/* $NetBSD: ckgetopt.c,v 1.15 2022/02/27 18:29:14 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: ckgetopt.c,v 1.14 2021/11/01 19:48:51 rillig Exp $");
+__RCSID("$NetBSD: ckgetopt.c,v 1.15 2022/02/27 18:29:14 rillig Exp $");
 #endif
 
 #include <stdbool.h>
@@ -103,10 +103,9 @@
        NEED(last_arg->tn_op == CVT);
        NEED(last_arg->tn_left->tn_op == ADDR);
        NEED(last_arg->tn_left->tn_left->tn_op == STRING);
-       NEED(last_arg->tn_left->tn_left->tn_string->st_tspec == CHAR);
+       NEED(last_arg->tn_left->tn_left->tn_string->st_char);
 
-       *out_options = xstrdup(
-           (const char *)last_arg->tn_left->tn_left->tn_string->st_cp);
+       *out_options = xstrdup(last_arg->tn_left->tn_left->tn_string->st_mem);
        return true;
 }
 
diff -r 87c75225b49d -r 903158bd991b usr.bin/xlint/lint1/debug.c
--- a/usr.bin/xlint/lint1/debug.c       Sun Feb 27 17:12:06 2022 +0000
+++ b/usr.bin/xlint/lint1/debug.c       Sun Feb 27 18:29:14 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.7 2021/12/21 21:04:08 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.8 2022/02/27 18:29:14 rillig Exp $ */
 
 /*-
  * Copyright (c) 2021 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: debug.c,v 1.7 2021/12/21 21:04:08 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.8 2022/02/27 18:29:14 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -137,15 +137,14 @@
                    tn->tn_val->v_quad != 0 ? "true" : "false");
        else if (op == CON)
                debug_printf(", unknown value\n");
-       else if (op == STRING && tn->tn_string->st_tspec == CHAR)
+       else if (op == STRING && tn->tn_string->st_char)
                debug_printf(", length %zu, \"%s\"\n",
-                   tn->tn_string->st_len, tn->tn_string->st_cp);
-       else if (op == STRING && tn->tn_string->st_tspec == WCHAR) {
-               char *s;
-               size_t n;
-               n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
-               s = xmalloc(n);
-               (void)wcstombs(s, tn->tn_string->st_wcp, n);
+                   tn->tn_string->st_len,
+                   (const char *)tn->tn_string->st_mem);
+       else if (op == STRING) {
+               size_t n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
+               char *s = xmalloc(n);
+               (void)wcstombs(s, tn->tn_string->st_mem, n);
                debug_printf(", length %zu, L\"%s\"",
                    tn->tn_string->st_len, s);
                free(s);
diff -r 87c75225b49d -r 903158bd991b usr.bin/xlint/lint1/emit1.c
--- a/usr.bin/xlint/lint1/emit1.c       Sun Feb 27 17:12:06 2022 +0000
+++ b/usr.bin/xlint/lint1/emit1.c       Sun Feb 27 18:29:14 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: emit1.c,v 1.60 2021/11/28 10:01:36 rillig Exp $ */
+/* $NetBSD: emit1.c,v 1.61 2022/02/27 18:29:14 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: emit1.c,v 1.60 2021/11/28 10:01:36 rillig Exp $");
+__RCSID("$NetBSD: emit1.c,v 1.61 2022/02/27 18:29:14 rillig Exp $");
 #endif
 
 #include "lint1.h"
@@ -404,7 +404,7 @@
                        }
                } else if (arg->tn_op == ADDR &&
                           arg->tn_left->tn_op == STRING &&
-                          arg->tn_left->tn_string->st_tspec == CHAR) {
+                          arg->tn_left->tn_string->st_char) {
                        /* constant string, write all format specifiers */
                        outchar('s');
                        outint(n);
@@ -492,9 +492,8 @@
        bool    first;
        const char *cp;
 
-       lint_assert(strg->st_tspec == CHAR);
-
-       cp = (const char *)strg->st_cp;
+       lint_assert(strg->st_char);
+       cp = strg->st_mem;
 
        outchar('"');
 
diff -r 87c75225b49d -r 903158bd991b usr.bin/xlint/lint1/lex.c
--- a/usr.bin/xlint/lint1/lex.c Sun Feb 27 17:12:06 2022 +0000
+++ b/usr.bin/xlint/lint1/lex.c Sun Feb 27 18:29:14 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.102 2022/02/27 10:44:45 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.103 2022/02/27 18:29:14 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: lex.c,v 1.102 2022/02/27 10:44:45 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.103 2022/02/27 18:29:14 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -1229,9 +1229,9 @@
                error(258);
 
        strg = xcalloc(1, sizeof(*strg));
-       strg->st_tspec = CHAR;
+       strg->st_char = true;
        strg->st_len = len;
-       strg->st_cp = s;
+       strg->st_mem = s;
 
        yylval.y_string = strg;
        return T_STRING;
@@ -1286,9 +1286,9 @@
        free(s);
 
        strg = xcalloc(1, sizeof(*strg));
-       strg->st_tspec = WCHAR;
+       strg->st_char = false;
        strg->st_len = wlen;
-       strg->st_wcp = ws;
+       strg->st_mem = ws;
 
        yylval.y_string = strg;
        return T_STRING;
@@ -1531,12 +1531,7 @@
                free(val);
        } else if (tok == T_STRING) {
                strg_t *strg = *(strg_t **)sp;
-               if (strg->st_tspec == CHAR) {
-                       free(strg->st_cp);
-               } else {
-                       lint_assert(strg->st_tspec == WCHAR);
-                       free(strg->st_wcp);
-               }
+               free(strg->st_mem);
                free(strg);
        }
 }
diff -r 87c75225b49d -r 903158bd991b usr.bin/xlint/lint1/lint1.h
--- a/usr.bin/xlint/lint1/lint1.h       Sun Feb 27 17:12:06 2022 +0000
+++ b/usr.bin/xlint/lint1/lint1.h       Sun Feb 27 18:29:14 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.141 2022/02/27 11:14:42 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.142 2022/02/27 18:29:14 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -86,17 +86,11 @@
  * Strings are stored with a trailing NUL.
  */
 typedef        struct strg {
-       tspec_t st_tspec;               /* CHAR or WCHAR */
-       size_t  st_len;                 /* length without trailing NUL */
-       union {
-               unsigned char *_st_cp;
-               wchar_t *_st_wcp;
-       } st_u;
+       bool    st_char;        /* string doesn't have an 'L' prefix */
+       size_t  st_len;         /* length without trailing NUL */
+       void    *st_mem;        /* char[] for st_char, or wchar_t[] */
 } strg_t;
 
-#define st_cp  st_u._st_cp
-#define        st_wcp  st_u._st_wcp
-
 /*
  * qualifiers (only for lex/yacc interface)
  */
diff -r 87c75225b49d -r 903158bd991b usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Sun Feb 27 17:12:06 2022 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sun Feb 27 18:29:14 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.409 2022/02/27 11:40:29 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.410 2022/02/27 18:29:14 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: tree.c,v 1.409 2022/02/27 11:40:29 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.410 2022/02/27 18:29:14 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -306,7 +306,7 @@
 
        tp = expr_zero_alloc(sizeof(*tp));
        tp->t_tspec = ARRAY;
-       tp->t_subt = gettyp(strg->st_tspec);
+       tp->t_subt = gettyp(strg->st_char ? CHAR : WCHAR);
        tp->t_dim = (int)(len + 1);
 
        n->tn_op = STRING;
@@ -314,19 +314,14 @@
        n->tn_lvalue = true;
 
        n->tn_string = expr_zero_alloc(sizeof(*n->tn_string));
-       n->tn_string->st_tspec = strg->st_tspec;
+       n->tn_string->st_char = strg->st_char;
        n->tn_string->st_len = len;
 
-       if (strg->st_tspec == CHAR) {
-               n->tn_string->st_cp = expr_zero_alloc(len + 1);
-               (void)memcpy(n->tn_string->st_cp, strg->st_cp, len + 1);
-               free(strg->st_cp);
-       } else {
-               size_t size = (len + 1) * sizeof(*n->tn_string->st_wcp);
-               n->tn_string->st_wcp = expr_zero_alloc(size);
-               (void)memcpy(n->tn_string->st_wcp, strg->st_wcp, size);
-               free(strg->st_wcp);
-       }
+       size_t chsize = strg->st_char ? sizeof(char) : sizeof(wchar_t);
+       size_t size = (len + 1) * chsize;
+       n->tn_string->st_mem = expr_zero_alloc(size);
+       (void)memcpy(n->tn_string->st_mem, strg->st_mem, size);
+       free(strg->st_mem);
        free(strg);
 
        return n;
@@ -4444,28 +4439,21 @@
 strg_t *
 cat_strings(strg_t *s1, strg_t *s2)
 {
-       size_t len1, len2, sz;
-
-       if (s1->st_tspec != s2->st_tspec) {
+
+       if (s1->st_char != s2->st_char) {
                /* cannot concatenate wide and regular string literals */
                error(292);
                return s1;
        }
 
-       len1 = s1->st_len;
-       len2 = s2->st_len;
-
-       if (s1->st_tspec == CHAR) {
-               sz = sizeof(*s1->st_cp);
-               s1->st_cp = xrealloc(s1->st_cp, (len1 + len2 + 1) * sz);
-               memcpy(s1->st_cp + len1, s2->st_cp, (len2 + 1) * sz);
-               free(s2->st_cp);
-       } else {
-               sz = sizeof(*s1->st_wcp);
-               s1->st_wcp = xrealloc(s1->st_wcp, (len1 + len2 + 1) * sz);
-               memcpy(s1->st_wcp + len1, s2->st_wcp, (len2 + 1) * sz);
-               free(s2->st_wcp);
-       }
+       size_t len1 = s1->st_len;
+       size_t len2 = s2->st_len;
+       size_t chsize = s1->st_char ? sizeof(char) : sizeof(wchar_t);
+       size_t size1 = len1 * chsize;
+       size_t size2 = (len2 + 1) * chsize;
+       s1->st_mem = xrealloc(s1->st_mem, size1 + size2);
+       memcpy((char *)s1->st_mem + size1, s2->st_mem, size2);
+       free(s2->st_mem);
 
        s1->st_len = len1 + len2;
        free(s2);



Home | Main Index | Thread Index | Old Index