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: clean up code for filename management



details:   https://anonhg.NetBSD.org/src/rev/0ba83c7b1b5e
branches:  trunk
changeset: 953994:0ba83c7b1b5e
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Mar 27 12:24:43 2021 +0000

description:
lint: clean up code for filename management

In add_directory_replacement, the expression 'r->repl - r->orig' looked
strange, as if two pointers into separate objects were subtracted.

The code was probably optimized to a particular compiler on a particular
platform to generate fast and simple code.  Since compilers have made
considerable progress over the last 25 years, optimize the code for
human legibility instead.  The compilers will somehow cope with that.

No functional change.

diffstat:

 usr.bin/xlint/lint1/mem1.c |  29 ++++++++++++++++-------------
 1 files changed, 16 insertions(+), 13 deletions(-)

diffs (68 lines):

diff -r 208d4a54a60c -r 0ba83c7b1b5e usr.bin/xlint/lint1/mem1.c
--- a/usr.bin/xlint/lint1/mem1.c        Sat Mar 27 12:17:22 2021 +0000
+++ b/usr.bin/xlint/lint1/mem1.c        Sat Mar 27 12:24:43 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: mem1.c,v 1.35 2021/03/27 12:17:22 rillig Exp $ */
+/*     $NetBSD: mem1.c,v 1.36 2021/03/27 12:24:43 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: mem1.c,v 1.35 2021/03/27 12:17:22 rillig Exp $");
+__RCSID("$NetBSD: mem1.c,v 1.36 2021/03/27 12:24:43 rillig Exp $");
 #endif
 
 #include <sys/types.h>
@@ -87,11 +87,14 @@
 {
        struct filename_replacement *r = xmalloc(sizeof *r);
 
-       r->orig = arg;
-       if ((r->repl = strchr(arg, '=')) == NULL)
+       char *sep = strchr(arg, '=');
+       if (sep == NULL)
                err(1, "Bad replacement directory spec `%s'", arg);
-       r->orig_len = r->repl - r->orig;
-       *(r->repl)++ = '\0';
+       *sep = '\0';
+
+       r->orig = arg;
+       r->orig_len = sep - arg;
+       r->repl = sep + 1;
        r->next = filename_replacements;
        filename_replacements = r;
 }
@@ -117,7 +120,7 @@
  * If the filename is new, it is written to the output file.
  */
 const char *
-fnnalloc(const char *s, size_t len)
+fnnalloc(const char *s, size_t slen)
 {
        const struct filename *existing_fn;
        struct filename *fn;
@@ -127,15 +130,15 @@
        if (s == NULL)
                return NULL;
 
-       if ((existing_fn = search_filename(s, len)) != NULL)
+       if ((existing_fn = search_filename(s, slen)) != NULL)
                return existing_fn->fn_name;
 
-       fn = xmalloc(sizeof *fn);
+       fn = xmalloc(sizeof(*fn));
        /* Do not use strdup() because s is not NUL-terminated.*/
-       fn->fn_name = xmalloc(len + 1);
-       (void)memcpy(fn->fn_name, s, len);
-       fn->fn_name[len] = '\0';
-       fn->fn_len = len;
+       fn->fn_name = xmalloc(slen + 1);
+       (void)memcpy(fn->fn_name, s, slen);
+       fn->fn_name[slen] = '\0';
+       fn->fn_len = slen;
        fn->fn_id = nxt_id++;
        fn->fn_next = filenames;
        filenames = fn;



Home | Main Index | Thread Index | Old Index