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: model C language levels in a futur...



details:   https://anonhg.NetBSD.org/src/rev/26017167139c
branches:  trunk
changeset: 365254:26017167139c
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Apr 16 13:25:27 2022 +0000

description:
lint: model C language levels in a future-compatible way

The options -t, -s and -S are confusing because they are used
inconsistently.  The option -S enables C99 features, but when using it
instead of -s, it also doesn't enable all checks required by C90 and
later.  Prepare fixing of these inconsistencies by replacing the flag
variables with language levels that can be extended in a
straight-forward way as new C standards arrive.

| option | allow_trad | allow_c90 | allow_c99 | allow_c11 |
|--------|------------|-----------|-----------|-----------|
| -t     | x          | -         | -         | -         |
| (none) | x          | x         | -         | -         |
| -s     | -          | x         | -         | -         |
| -S     | -          | x         | x         | -         |
| -Ac11  | -          | x         | x         | x         |

Each usage of the old flag variables will be inspected and migrated
individually, to clean up the subtle variations in the conditions and to
provide a simpler model.

When lint was created in 1995, its focus was migrating traditional C
code to C90 code.  Lint does not help in migrating from C90 to C99 or
from C99 to C11 since there are only few silent changes, and simply
because nobody took the time to implement these migration aids.  If
necessary, such migration modes could be added separately.

There is a small functional change: when the option -s is combined with
either -S or -Ac11, lint now only keeps the last of these options.
Previously, these options could be combined, leading to a mixture of
language levels, halfway between C90, C99 and C11.  Especially combining
traditional C with C11 doesn't make sense, but xlint currently allows
it.

The 3 tests that accidentally specified multiple language levels have
been adjusted to a single language level.

diffstat:

 tests/usr.bin/xlint/lint1/msg_081.c   |   6 +-
 tests/usr.bin/xlint/lint1/msg_100.c   |   6 +-
 tests/usr.bin/xlint/lint1/msg_340.c   |   4 +-
 tests/usr.bin/xlint/lint1/msg_340.exp |   2 +-
 usr.bin/xlint/lint1/err.c             |  10 +----
 usr.bin/xlint/lint1/externs1.h        |  29 ++++++++++++---
 usr.bin/xlint/lint1/main1.c           |  62 ++++++++++++++++++++--------------
 7 files changed, 71 insertions(+), 48 deletions(-)

diffs (273 lines):

diff -r e18d8c8758a4 -r 26017167139c tests/usr.bin/xlint/lint1/msg_081.c
--- a/tests/usr.bin/xlint/lint1/msg_081.c       Sat Apr 16 12:25:10 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_081.c       Sat Apr 16 13:25:27 2022 +0000
@@ -1,8 +1,8 @@
-/*     $NetBSD: msg_081.c,v 1.3 2021/01/31 11:12:07 rillig Exp $       */
+/*     $NetBSD: msg_081.c,v 1.4 2022/04/16 13:25:27 rillig Exp $       */
 # 3 "msg_081.c"
 
-// Test for message: \a undefined in traditional C [81]
+/* Test for message: \a undefined in traditional C [81] */
 
-/* lint1-flags: -Stw */
+/* lint1-flags: -tw */
 
 char str[] = "The bell\a rings";       /* expect: 81 */
diff -r e18d8c8758a4 -r 26017167139c tests/usr.bin/xlint/lint1/msg_100.c
--- a/tests/usr.bin/xlint/lint1/msg_100.c       Sat Apr 16 12:25:10 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_100.c       Sat Apr 16 13:25:27 2022 +0000
@@ -1,9 +1,9 @@
-/*     $NetBSD: msg_100.c,v 1.3 2021/01/31 11:12:07 rillig Exp $       */
+/*     $NetBSD: msg_100.c,v 1.4 2022/04/16 13:25:27 rillig Exp $       */
 # 3 "msg_100.c"
 
-// Test for message: unary + is illegal in traditional C [100]
+/* Test for message: unary + is illegal in traditional C [100] */
 
-/* lint1-flags: -Stw */
+/* lint1-flags: -tw */
 
 int
 unary_plus(int x)
diff -r e18d8c8758a4 -r 26017167139c tests/usr.bin/xlint/lint1/msg_340.c
--- a/tests/usr.bin/xlint/lint1/msg_340.c       Sat Apr 16 12:25:10 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_340.c       Sat Apr 16 13:25:27 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_340.c,v 1.2 2021/07/05 19:53:43 rillig Exp $       */
+/*     $NetBSD: msg_340.c,v 1.3 2022/04/16 13:25:27 rillig Exp $       */
 # 3 "msg_340.c"
 
 // Test for message: initialization with '[a...b]' is a GCC extension [340]
@@ -7,7 +7,7 @@
  * In strict C mode, GCC extensions are flagged as such.
  */
 
-/* lint1-flags: -Ssw */
+/* lint1-flags: -Sw */
 
 int
 example(void)
diff -r e18d8c8758a4 -r 26017167139c tests/usr.bin/xlint/lint1/msg_340.exp
--- a/tests/usr.bin/xlint/lint1/msg_340.exp     Sat Apr 16 12:25:10 2022 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_340.exp     Sat Apr 16 13:25:27 2022 +0000
@@ -1,1 +1,1 @@
-msg_340.c(16): error: initialization with '[a...b]' is a GCC extension [340]
+msg_340.c(16): warning: initialization with '[a...b]' is a GCC extension [340]
diff -r e18d8c8758a4 -r 26017167139c usr.bin/xlint/lint1/err.c
--- a/usr.bin/xlint/lint1/err.c Sat Apr 16 12:25:10 2022 +0000
+++ b/usr.bin/xlint/lint1/err.c Sat Apr 16 13:25:27 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: err.c,v 1.159 2022/04/08 21:29:29 rillig Exp $ */
+/*     $NetBSD: err.c,v 1.160 2022/04/16 13:25:27 rillig Exp $ */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: err.c,v 1.159 2022/04/08 21:29:29 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.160 2022/04/16 13:25:27 rillig Exp $");
 #endif
 
 #include <sys/types.h>
@@ -630,12 +630,6 @@
        va_end(ap);
 }
 
-/*
- * XXX I think the logic is possibly somewhat screwed up here. The
- * question is, how do we want to interpret the -s and -S flags going
- * forward? We need to answer that and then we can fix this to be
- * "right"... [perry, 2 Nov 2002]
-*/
 void
 (c99ism)(int msgid, ...)
 {
diff -r e18d8c8758a4 -r 26017167139c usr.bin/xlint/lint1/externs1.h
--- a/usr.bin/xlint/lint1/externs1.h    Sat Apr 16 12:25:10 2022 +0000
+++ b/usr.bin/xlint/lint1/externs1.h    Sat Apr 16 13:25:27 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: externs1.h,v 1.154 2022/04/09 23:41:22 rillig Exp $    */
+/*     $NetBSD: externs1.h,v 1.155 2022/04/16 13:25:27 rillig Exp $    */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,24 +37,41 @@
 extern int     aflag;
 extern bool    bflag;
 extern bool    cflag;
-extern bool    c11flag;
 extern bool    eflag;
 extern bool    Fflag;
-extern bool    gflag;
 extern bool    hflag;
 extern bool    pflag;
 extern bool    rflag;
-extern bool    sflag;
-extern bool    tflag;
 extern bool    uflag;
 extern bool    vflag;
 extern bool    yflag;
 extern bool    wflag;
 extern bool    zflag;
-extern bool    Sflag;
 extern bool    Tflag;
 extern bool    Pflag;
 
+extern bool    allow_trad;
+extern bool    allow_c90;
+extern bool    allow_c99;
+extern bool    allow_c11;
+extern bool    allow_gcc;
+
+/*
+ * TODO: Replace the old flags with the new expressions, checking in each
+ * case whether the specific condition still makes sense.  When lint was
+ * invented in 1995, it did not know about C99 mode, which makes all
+ * conditions involving tflag and sflag suspicious.
+ *
+ * In 1995, gflag meant "C90 plus GCC extensions".  That definition needs to
+ * be extended to C99 and later as well to properly match "C99 + GCC" or "C11
+ * + GCC".
+ */
+#define tflag  (allow_trad && !allow_c90)
+#define sflag  (!allow_trad && !allow_c99)
+#define Sflag  (!!allow_c99)
+#define c11flag        (!!allow_c11)
+#define gflag  (!!allow_gcc)
+
 extern void    norecover(void);
 
 /*
diff -r e18d8c8758a4 -r 26017167139c usr.bin/xlint/lint1/main1.c
--- a/usr.bin/xlint/lint1/main1.c       Sat Apr 16 12:25:10 2022 +0000
+++ b/usr.bin/xlint/lint1/main1.c       Sat Apr 16 13:25:27 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main1.c,v 1.59 2022/02/27 11:40:29 rillig Exp $        */
+/*     $NetBSD: main1.c,v 1.60 2022/04/16 13:25:27 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: main1.c,v 1.59 2022/02/27 11:40:29 rillig Exp $");
+__RCSID("$NetBSD: main1.c,v 1.60 2022/04/16 13:25:27 rillig Exp $");
 #endif
 
 #include <sys/types.h>
@@ -68,18 +68,12 @@
 /* Print warnings for pointer casts. */
 bool   cflag;
 
-/* Allow features from C11, C99 and C90. */
-bool   c11flag;
-
 /* Perform stricter checking of enum types and operations on enum types. */
 bool   eflag;
 
 /* Print complete pathnames, not only the basename. */
 bool   Fflag;
 
-/* Enable some extensions of gcc */
-bool   gflag;
-
 /* Treat warnings as errors */
 bool   wflag;
 
@@ -98,17 +92,8 @@
  */
 bool   rflag;
 
-/* Strict ANSI C mode. */
-bool   sflag;
-
 bool   Tflag;
 
-/* Traditional C mode. */
-bool   tflag;
-
-/* Enable C99 extensions */
-bool   Sflag;
-
 /* Picky flag */
 bool   Pflag;
 
@@ -124,6 +109,17 @@
 /* Complain about structures which are never defined. */
 bool   zflag = true;
 
+/*
+ * The default language level is the one that checks for compatibility
+ * between traditional C and C90.  As of 2022, this default is no longer
+ * useful since most traditional C code has already been migrated.
+ */
+bool   allow_trad = true;
+bool   allow_c90 = true;
+bool   allow_c99;
+bool   allow_c11;
+bool   allow_gcc;
+
 err_set        msgset;
 
 sig_atomic_t fpe;
@@ -211,15 +207,30 @@
                case 'c':       cflag = true;   break;
                case 'e':       eflag = true;   break;
                case 'F':       Fflag = true;   break;
-               case 'g':       gflag = true;   break;
+               case 'g':       allow_gcc = true;       break;
                case 'h':       hflag = true;   break;
                case 'p':       pflag = true;   break;
                case 'P':       Pflag = true;   break;
                case 'r':       rflag = true;   break;
-               case 's':       sflag = true;   break;
-               case 'S':       Sflag = true;   break;
+               case 's':
+                       allow_trad = false;
+                       allow_c90 = true;
+                       allow_c99 = false;
+                       allow_c11 = false;
+                       break;
+               case 'S':
+                       allow_trad = false;
+                       allow_c90 = true;
+                       allow_c99 = true;
+                       allow_c11 = false;
+                       break;
                case 'T':       Tflag = true;   break;
-               case 't':       tflag = true;   break;
+               case 't':
+                       allow_trad = true;
+                       allow_c90 = false;
+                       allow_c99 = false;
+                       allow_c11 = false;
+                       break;
                case 'u':       uflag = false;  break;
                case 'w':       wflag = true;   break;
                case 'v':       vflag = false;  break;
@@ -228,9 +239,10 @@
 
                case 'A':
                        if (strcmp(optarg, "c11") == 0) {
-                               c11flag = true;
-                               Sflag = true;
-                               sflag = false;
+                               allow_trad = false;
+                               allow_c90 = true;
+                               allow_c99 = true;
+                               allow_c11 = true;
                        } else
                                usage();
                        break;
@@ -273,7 +285,7 @@
        initdecl();
        initscan();
 
-       if (gflag && !tflag) {
+       if (allow_gcc && !tflag) {
                if ((yyin = gcc_builtins()) == NULL)
                        err(1, "cannot open builtins");
                yyparse();



Home | Main Index | Thread Index | Old Index