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: add query for static variables in ...



details:   https://anonhg.NetBSD.org/src/rev/6686dcc92124
branches:  trunk
changeset: 376174:6686dcc92124
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Jun 03 21:08:06 2023 +0000

description:
lint: add query for static variables in functions

This query allows finding hidden global variables, as an easier-to-read
alternative to 'objdump -t'.

diffstat:

 tests/usr.bin/xlint/lint1/queries.c  |  16 ++++++++++++++--
 tests/usr.bin/xlint/lint1/t_usage.sh |   8 ++++----
 usr.bin/xlint/lint1/decl.c           |  13 ++++++-------
 usr.bin/xlint/lint1/err.c            |   5 +++--
 4 files changed, 27 insertions(+), 15 deletions(-)

diffs (130 lines):

diff -r 862d98818f6a -r 6686dcc92124 tests/usr.bin/xlint/lint1/queries.c
--- a/tests/usr.bin/xlint/lint1/queries.c       Sat Jun 03 20:58:00 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/queries.c       Sat Jun 03 21:08:06 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: queries.c,v 1.13 2023/05/13 20:55:44 rillig Exp $      */
+/*     $NetBSD: queries.c,v 1.14 2023/06/03 21:08:06 rillig Exp $      */
 # 3 "queries.c"
 
 /*
@@ -15,7 +15,7 @@
  *     such as casts between arithmetic types.
  */
 
-/* lint1-extra-flags: -q 1,2,3,4,5,6,7,8,9,10 -X 351 */
+/* lint1-extra-flags: -q 1,2,3,4,5,6,7,8,9,10,11 -X 351 */
 
 typedef unsigned char u8_t;
 typedef unsigned short u16_t;
@@ -367,6 +367,18 @@ Q10(void)
        a += b *= c -= 0;
 }
 
+void
+Q11(void)
+{
+       /* expect+1: static variable 'static_var_no_init' in function [Q11] */
+       static int static_var_no_init;
+       /* expect+1: static variable 'static_var_init' in function [Q11] */
+       static int static_var_init = 1;
+
+       static_var_no_init++;
+       static_var_init++;
+}
+
 /*
  * Since queries do not affect the exit status, force a warning to make this
  * test conform to the general expectation that a test that produces output
diff -r 862d98818f6a -r 6686dcc92124 tests/usr.bin/xlint/lint1/t_usage.sh
--- a/tests/usr.bin/xlint/lint1/t_usage.sh      Sat Jun 03 20:58:00 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/t_usage.sh      Sat Jun 03 21:08:06 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: t_usage.sh,v 1.3 2023/05/13 20:55:44 rillig Exp $
+# $NetBSD: t_usage.sh,v 1.4 2023/06/03 21:08:06 rillig Exp $
 #
 # Copyright (c) 2023 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -97,13 +97,13 @@ enable_queries_body()
 
        # The largest known query.
        atf_check \
-           "$lint1" -q 10 code.c /dev/null
+           "$lint1" -q 11 code.c /dev/null
 
        # Larger than the largest known query.
        atf_check \
            -s 'exit:1' \
-           -e "inline:lint1: invalid query ID '11'\n" \
-           "$lint1" -q 11 code.c /dev/null
+           -e "inline:lint1: invalid query ID '12'\n" \
+           "$lint1" -q 12 code.c /dev/null
 
        # Whitespace is not allowed before a query ID.
        atf_check \
diff -r 862d98818f6a -r 6686dcc92124 usr.bin/xlint/lint1/decl.c
--- a/usr.bin/xlint/lint1/decl.c        Sat Jun 03 20:58:00 2023 +0000
+++ b/usr.bin/xlint/lint1/decl.c        Sat Jun 03 21:08:06 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.314 2023/05/22 18:10:57 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.315 2023/06/03 21:08:06 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: decl.c,v 1.314 2023/05/22 18:10:57 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.315 2023/06/03 21:08:06 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -2780,7 +2780,6 @@ declare_local(sym_t *dsym, bool has_init
                        outsym(dsym, EXTERN, dsym->s_def);
                else
                        outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
-
        }
 
        if (dcs->d_redeclared_symbol != NULL)
@@ -2797,10 +2796,10 @@ declare_local(sym_t *dsym, bool has_init
                set_first_typedef(dsym->s_type, dsym);
        }
 
-       /*
-        * Before we can check the size we must wait for an initialization
-        * that may follow.
-        */
+       if (dsym->s_scl == STATIC && any_query_enabled) {
+               /* static variable '%s' in function */
+               query_message(11, dsym->s_name);
+       }
 }
 
 /* Processes (re)declarations of external symbols inside blocks. */
diff -r 862d98818f6a -r 6686dcc92124 usr.bin/xlint/lint1/err.c
--- a/usr.bin/xlint/lint1/err.c Sat Jun 03 20:58:00 2023 +0000
+++ b/usr.bin/xlint/lint1/err.c Sat Jun 03 21:08:06 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: err.c,v 1.196 2023/05/13 20:55:44 rillig Exp $ */
+/*     $NetBSD: err.c,v 1.197 2023/06/03 21:08:06 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: err.c,v 1.196 2023/05/13 20:55:44 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.197 2023/06/03 21:08:06 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -706,6 +706,7 @@ static const char *queries[] = {
        "octal number '%.*s'",                                        /* Q8 */
        "parenthesized return value",                                 /* Q9 */
        "chained assignment with '%s' and '%s'",                      /* Q10 */
+       "static variable '%s' in function",                           /* Q11 */
 };
 
 bool any_query_enabled;                /* for optimizing non-query scenarios */



Home | Main Index | Thread Index | Old Index