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: warn about 'extern' declarations i...



details:   https://anonhg.NetBSD.org/src/rev/42219d30ec79
branches:  trunk
changeset: 374050:42219d30ec79
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Mar 28 20:04:51 2023 +0000

description:
lint: warn about 'extern' declarations inside function bodies

https://mail-index.netbsd.org/tech-userlevel/2023/03/15/msg013727.html

The previous attempt (message 351 about 'extern' declarations outside
headers) did not cover the proposal from the tech-userlevel mailing list
but instead warns about a different usage pattern of the 'extern'
keyword.

diffstat:

 distrib/sets/lists/tests/mi         |   3 ++-
 tests/usr.bin/xlint/lint1/msg_090.c |   3 ++-
 tests/usr.bin/xlint/lint1/msg_352.c |  22 ++++++++++++++++++++++
 usr.bin/xlint/lint1/decl.c          |   9 +++++++--
 usr.bin/xlint/lint1/err.c           |   5 +++--
 5 files changed, 36 insertions(+), 6 deletions(-)

diffs (116 lines):

diff -r 9b282fc7d4ea -r 42219d30ec79 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Tue Mar 28 20:01:57 2023 +0000
+++ b/distrib/sets/lists/tests/mi       Tue Mar 28 20:04:51 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1255 2023/03/28 14:44:34 rillig Exp $
+# $NetBSD: mi,v 1.1256 2023/03/28 20:04:51 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -7342,6 +7342,7 @@
 ./usr/tests/usr.bin/xlint/lint1/msg_349.exp                    tests-obsolete          obsolete,atf
 ./usr/tests/usr.bin/xlint/lint1/msg_350.c                      tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/msg_351.c                      tests-usr.bin-tests     compattestfile,atf
+./usr/tests/usr.bin/xlint/lint1/msg_352.c                      tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/op_colon.c                     tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/op_colon.exp                   tests-obsolete          obsolete,atf
 ./usr/tests/usr.bin/xlint/lint1/op_shl_lp64.c                  tests-usr.bin-tests     compattestfile,atf
diff -r 9b282fc7d4ea -r 42219d30ec79 tests/usr.bin/xlint/lint1/msg_090.c
--- a/tests/usr.bin/xlint/lint1/msg_090.c       Tue Mar 28 20:01:57 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_090.c       Tue Mar 28 20:04:51 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_090.c,v 1.5 2023/03/28 14:44:34 rillig Exp $       */
+/*     $NetBSD: msg_090.c,v 1.6 2023/03/28 20:04:52 rillig Exp $       */
 # 3 "msg_090.c"
 
 // Test for message: inconsistent redeclaration of extern '%s' [90]
@@ -10,6 +10,7 @@ extern int random_number(void);
 void
 use(void)
 {
+       /* expect+2: warning: nested 'extern' declaration of 'random_number' [352] */
        /* expect+1: warning: inconsistent redeclaration of extern 'random_number' [90] */
        extern int random_number(int);
 }
diff -r 9b282fc7d4ea -r 42219d30ec79 tests/usr.bin/xlint/lint1/msg_352.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_352.c       Tue Mar 28 20:04:51 2023 +0000
@@ -0,0 +1,22 @@
+/*     $NetBSD: msg_352.c,v 1.1 2023/03/28 20:04:52 rillig Exp $       */
+# 3 "msg_352.c"
+
+// Test for message 352: nested 'extern' declaration of '%s' [352]
+
+/*
+ * C allows to declare external functions or objects inside function bodies,
+ * which invites inconsistent types.
+ *
+ * Instead, any external functions or objects should be declared in headers.
+ */
+
+int
+function(void)
+{
+       /* expect+1: warning: nested 'extern' declaration of 'external_func' [352] */
+       extern int external_func(void);
+       /* expect+1: warning: nested 'extern' declaration of 'external_var' [352] */
+       extern int external_var;
+
+       return external_func() + external_var;
+}
diff -r 9b282fc7d4ea -r 42219d30ec79 usr.bin/xlint/lint1/decl.c
--- a/usr.bin/xlint/lint1/decl.c        Tue Mar 28 20:01:57 2023 +0000
+++ b/usr.bin/xlint/lint1/decl.c        Tue Mar 28 20:04:51 2023 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.306 2023/03/28 14:44:35 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.307 2023/03/28 20:04:52 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.306 2023/03/28 14:44:35 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.307 2023/03/28 20:04:52 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -2728,6 +2728,11 @@ declare_local(sym_t *dsym, bool initflg)
                        dsym->s_scl = EXTERN;
        }
 
+       if (dsym->s_scl == EXTERN) {
+               /* nested 'extern' declaration of '%s' */
+               warning(352, dsym->s_name);
+       }
+
        if (dsym->s_type->t_tspec == FUNC) {
                if (dsym->s_scl == STATIC) {
                        /* dubious static function '%s' at block level */
diff -r 9b282fc7d4ea -r 42219d30ec79 usr.bin/xlint/lint1/err.c
--- a/usr.bin/xlint/lint1/err.c Tue Mar 28 20:01:57 2023 +0000
+++ b/usr.bin/xlint/lint1/err.c Tue Mar 28 20:04:51 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: err.c,v 1.190 2023/03/28 14:44:35 rillig Exp $ */
+/*     $NetBSD: err.c,v 1.191 2023/03/28 20:04:52 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.190 2023/03/28 14:44:35 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.191 2023/03/28 20:04:52 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -407,6 +407,7 @@ static const char *const msgs[] = {
        "non type argument to alignof is a GCC extension",            /* 349 */
        "'_Atomic' requires C11 or later",                            /* 350 */
        "'extern' declaration of '%s' outside a header",              /* 351 */
+       "nested 'extern' declaration of '%s'",                        /* 352 */
 };
 
 static bool    is_suppressed[sizeof(msgs) / sizeof(msgs[0])];



Home | Main Index | Thread Index | Old Index