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: reorder code for handling initiali...



details:   https://anonhg.NetBSD.org/src/rev/f49de0b3a4be
branches:  trunk
changeset: 960738:f49de0b3a4be
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Mar 28 08:30:22 2021 +0000

description:
lint: reorder code for handling initializations

First the primitives for debug logging, then the functions that do not
access any global variables, finally everything else.

No functional change.

diffstat:

 usr.bin/xlint/lint1/init.c |  344 ++++++++++++++++++++++++--------------------
 1 files changed, 188 insertions(+), 156 deletions(-)

diffs (truncated from 572 to 300 lines):

diff -r ef570c9f92e9 -r f49de0b3a4be usr.bin/xlint/lint1/init.c
--- a/usr.bin/xlint/lint1/init.c        Sun Mar 28 07:59:09 2021 +0000
+++ b/usr.bin/xlint/lint1/init.c        Sun Mar 28 08:30:22 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: init.c,v 1.145 2021/03/28 07:59:09 rillig Exp $        */
+/*     $NetBSD: init.c,v 1.146 2021/03/28 08:30:22 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: init.c,v 1.145 2021/03/28 07:59:09 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.146 2021/03/28 08:30:22 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -318,6 +318,163 @@
 #endif
 
 
+static struct designator *
+designator_new(const char *name)
+{
+       struct designator *d = xcalloc(1, sizeof *d);
+       d->name = name;
+       return d;
+}
+
+static void
+designator_free(struct designator *d)
+{
+       free(d);
+}
+
+
+#ifdef DEBUG
+static void
+designation_debug(const struct designation *dn)
+{
+       const struct designator *p;
+
+       if (dn->head == NULL)
+               return;
+
+       debug_indent();
+       debug_printf("designation: ");
+       for (p = dn->head; p != NULL; p = p->next)
+               debug_printf(".%s", p->name);
+       debug_printf("\n");
+}
+#else
+#define designation_debug(dn) do { } while (false)
+#endif
+
+static void
+designation_add(struct designation *dn, struct designator *dr)
+{
+
+       if (dn->head != NULL) {
+               dn->tail->next = dr;
+               dn->tail = dr;
+       } else {
+               dn->head = dr;
+               dn->tail = dr;
+       }
+
+       designation_debug(dn);
+}
+
+/* TODO: add support for array subscripts, not only named members */
+/*
+ * TODO: This function should not be necessary at all.  There is no need to
+ *  remove the head of the list.
+ */
+static void
+designation_shift_level(struct designation *dn)
+{
+       lint_assert(dn->head != NULL);
+
+       if (dn->head == dn->tail) {
+               designator_free(dn->head);
+               dn->head = NULL;
+               dn->tail = NULL;
+       } else {
+               struct designator *head = dn->head;
+               dn->head = dn->head->next;
+               designator_free(head);
+       }
+
+       designation_debug(dn);
+}
+
+
+#ifdef DEBUG
+/*
+ * TODO: only log the top of the stack after each modifying operation
+ *
+ * TODO: wrap all write accesses to brace_level in setter functions
+ */
+static void
+brace_level_debug(const struct brace_level *level)
+{
+       if (level->bl_type != NULL)
+               debug_printf("type '%s'", type_name(level->bl_type));
+       if (level->bl_type != NULL && level->bl_subtype != NULL)
+               debug_printf(", ");
+       if (level->bl_subtype != NULL)
+               debug_printf("subtype '%s'", type_name(level->bl_subtype));
+
+       if (level->bl_brace)
+               debug_printf(", needs closing brace");
+       if (level->bl_array_of_unknown_size)
+               debug_printf(", array of unknown size");
+       if (level->bl_seen_named_member)
+               debug_printf(", seen named member");
+
+       const type_t *eff_type = level->bl_type != NULL
+           ? level->bl_type : level->bl_subtype;
+       if (eff_type->t_tspec == STRUCT && level->bl_next_member != NULL)
+               debug_printf(", next member '%s'",
+                   level->bl_next_member->s_name);
+
+       debug_printf(", remaining %d\n", level->bl_remaining);
+}
+#else
+#define brace_level_debug(level) do { } while (false)
+#endif
+
+
+static struct initialization *
+initialization_new(sym_t *sym)
+{
+       struct initialization *in = xcalloc(1, sizeof(*in));
+
+       in->initsym = sym;
+
+       return in;
+}
+
+static void
+initialization_free(struct initialization *in)
+{
+       struct brace_level *level, *next;
+
+       for (level = in->brace_level; level != NULL; level = next) {
+               next = level->bl_enclosing;
+               free(level);
+       }
+
+       free(in);
+}
+
+#ifdef DEBUG
+/*
+ * TODO: only call debug_initstack after each push/pop.
+ */
+static void
+initialization_debug(const struct initialization *in)
+{
+       if (in->brace_level == NULL) {
+               debug_step("no brace level in the current initialization");
+               return;
+       }
+
+       size_t i = 0;
+       for (const struct brace_level *level = in->brace_level;
+            level != NULL; level = level->bl_enclosing) {
+               debug_indent();
+               debug_printf("brace level %zu: ", i);
+               brace_level_debug(level);
+               i++;
+       }
+}
+#else
+#define initialization_debug(in) do { } while (false)
+#endif
+
 /* XXX: unnecessary prototype since it is not recursive */
 static bool    init_array_using_string(tnode_t *);
 
@@ -366,19 +523,6 @@
 }
 
 static void
-free_initialization(struct initialization *in)
-{
-       struct brace_level *level, *next;
-
-       for (level = in->brace_level; level != NULL; level = next) {
-               next = level->bl_enclosing;
-               free(level);
-       }
-
-       free(in);
-}
-
-static void
 set_initerr(void)
 {
        current_init()->initerr = true;
@@ -397,71 +541,6 @@
 
 #else
 
-static void
-debug_designation(void)
-{
-       const struct designator *head = current_designation().head, *p;
-       if (head == NULL)
-               return;
-
-       debug_indent();
-       debug_printf("designation: ");
-       for (p = head; p != NULL; p = p->next)
-               debug_printf(".%s", p->name);
-       debug_printf("\n");
-}
-
-/*
- * TODO: only log the top of the stack after each modifying operation
- *
- * TODO: wrap all write accesses to brace_level in setter functions
- */
-static void
-debug_brace_level(const struct brace_level *level)
-{
-       if (level->bl_type != NULL)
-               debug_printf("type '%s'", type_name(level->bl_type));
-       if (level->bl_type != NULL && level->bl_subtype != NULL)
-               debug_printf(", ");
-       if (level->bl_subtype != NULL)
-               debug_printf("subtype '%s'", type_name(level->bl_subtype));
-
-       if (level->bl_brace)
-               debug_printf(", needs closing brace");
-       if (level->bl_array_of_unknown_size)
-               debug_printf(", array of unknown size");
-       if (level->bl_seen_named_member)
-               debug_printf(", seen named member");
-
-       const type_t *eff_type = level->bl_type != NULL
-           ? level->bl_type : level->bl_subtype;
-       if (eff_type->t_tspec == STRUCT && level->bl_next_member != NULL)
-               debug_printf(", next member '%s'",
-                   level->bl_next_member->s_name);
-
-       debug_printf(", remaining %d\n", level->bl_remaining);
-}
-
-/*
- * TODO: only call debug_initstack after each push/pop.
- */
-static void
-debug_initstack(void)
-{
-       if (brace_level_rvalue == NULL) {
-               debug_step("no brace level in the current initialization");
-               return;
-       }
-
-       size_t i = 0;
-       for (const struct brace_level *level = brace_level_rvalue;
-            level != NULL; level = level->bl_enclosing) {
-               debug_indent();
-               debug_printf("brace level %zu: ", i);
-               debug_brace_level(level);
-               i++;
-       }
-}
 
 #define debug_enter() debug_enter(__func__)
 #define debug_leave() debug_leave(__func__)
@@ -475,10 +554,9 @@
        struct initialization *curr_init;
 
        debug_step("begin initialization of '%s'", type_name(sym->s_type));
-       curr_init = xcalloc(1, sizeof *curr_init);
+       curr_init = initialization_new(sym);
        curr_init->next = init;
        init = curr_init;
-       initsym = sym;
 }
 
 void
@@ -488,44 +566,17 @@
 
        curr_init = init;
        init = init->next;
-       free_initialization(curr_init);
+       initialization_free(curr_init);
        debug_step("end initialization");
 }
 
-static struct designator *
-designator_new(const char *name)
-{
-       struct designator *d = xcalloc(1, sizeof *d);
-       d->name = name;



Home | Main Index | Thread Index | Old Index