Module Name:    src
Committed By:   rillig
Date:           Sun Feb 27 18:29:14 UTC 2022

Modified Files:
        src/usr.bin/xlint/lint1: ckgetopt.c debug.c emit1.c lex.c lint1.h
            tree.c

Log Message:
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".


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/usr.bin/xlint/lint1/ckgetopt.c
cvs rdiff -u -r1.7 -r1.8 src/usr.bin/xlint/lint1/debug.c
cvs rdiff -u -r1.60 -r1.61 src/usr.bin/xlint/lint1/emit1.c
cvs rdiff -u -r1.102 -r1.103 src/usr.bin/xlint/lint1/lex.c
cvs rdiff -u -r1.141 -r1.142 src/usr.bin/xlint/lint1/lint1.h
cvs rdiff -u -r1.409 -r1.410 src/usr.bin/xlint/lint1/tree.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/xlint/lint1/ckgetopt.c
diff -u src/usr.bin/xlint/lint1/ckgetopt.c:1.14 src/usr.bin/xlint/lint1/ckgetopt.c:1.15
--- src/usr.bin/xlint/lint1/ckgetopt.c:1.14	Mon Nov  1 19:48:51 2021
+++ src/usr.bin/xlint/lint1/ckgetopt.c	Sun Feb 27 18:29:14 2022
@@ -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 @@ is_getopt_condition(const tnode_t *tn, c
 	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;
 }
 

Index: src/usr.bin/xlint/lint1/debug.c
diff -u src/usr.bin/xlint/lint1/debug.c:1.7 src/usr.bin/xlint/lint1/debug.c:1.8
--- src/usr.bin/xlint/lint1/debug.c:1.7	Tue Dec 21 21:04:08 2021
+++ src/usr.bin/xlint/lint1/debug.c	Sun Feb 27 18:29:14 2022
@@ -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 @@ debug_node(const tnode_t *tn)
 		    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);

Index: src/usr.bin/xlint/lint1/emit1.c
diff -u src/usr.bin/xlint/lint1/emit1.c:1.60 src/usr.bin/xlint/lint1/emit1.c:1.61
--- src/usr.bin/xlint/lint1/emit1.c:1.60	Sun Nov 28 10:01:36 2021
+++ src/usr.bin/xlint/lint1/emit1.c	Sun Feb 27 18:29:14 2022
@@ -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 @@ outcall(const tnode_t *tn, bool retval_u
 			}
 		} 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 @@ outfstrg(strg_t *strg)
 	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('"');
 

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.102 src/usr.bin/xlint/lint1/lex.c:1.103
--- src/usr.bin/xlint/lint1/lex.c:1.102	Sun Feb 27 10:44:45 2022
+++ src/usr.bin/xlint/lint1/lex.c	Sun Feb 27 18:29:14 2022
@@ -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 @@ lex_string(void)
 		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 @@ lex_wide_string(void)
 	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 @@ freeyyv(void *sp, int tok)
 		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);
 	}
 }

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.141 src/usr.bin/xlint/lint1/lint1.h:1.142
--- src/usr.bin/xlint/lint1/lint1.h:1.141	Sun Feb 27 11:14:42 2022
+++ src/usr.bin/xlint/lint1/lint1.h	Sun Feb 27 18:29:14 2022
@@ -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 @@ typedef struct {
  * 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)
  */

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.409 src/usr.bin/xlint/lint1/tree.c:1.410
--- src/usr.bin/xlint/lint1/tree.c:1.409	Sun Feb 27 11:40:29 2022
+++ src/usr.bin/xlint/lint1/tree.c	Sun Feb 27 18:29:14 2022
@@ -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 @@ build_string(strg_t *strg)
 
 	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 @@ build_string(strg_t *strg)
 	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 @@ constant_addr(const tnode_t *tn, const s
 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);

Reply via email to