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: apply strict bool mode to lex.c



details:   https://anonhg.NetBSD.org/src/rev/8931a044eba3
branches:  trunk
changeset: 958871:8931a044eba3
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Jan 23 23:11:40 2021 +0000

description:
lint: apply strict bool mode to lex.c

There are 2 remaining expressions:

In line 244, !(kw->kw_deco & deco) is a bitwise and.  This is already
allowed for enums, it needs to be allowed for arbitrary integer
expressions as well.  This covers the many places where plain integers
are used for bit fields, together with #define.  This pattern is not as
typesafe as using enums, still it is common practice.

In line 769, the expression !finite(f) is a legitimate use of a function
that has return type int for traditional reasons.  It's the same as for
ferror.

There are several other functions like unlink, open or strcmp that have
return type int as well, but with a different meaning.  It is not yet
clear what the best way is to handle these different meanings.  Having
to write finite(f) == 0 in strict bool mode doesn't look idiomatic, on
the other hand, !strcmp(s1, s2) is exactly the pattern that strict bool
mode wants to avoid.

diffstat:

 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c   |  18 +++++-
 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp |   2 +
 usr.bin/xlint/lint1/externs1.h                         |   3 +-
 usr.bin/xlint/lint1/lex.c                              |  54 +++++++++---------
 4 files changed, 47 insertions(+), 30 deletions(-)

diffs (175 lines):

diff -r fde3eb975546 -r 8931a044eba3 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c
--- a/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c      Sat Jan 23 22:34:01 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c      Sat Jan 23 23:11:40 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: d_c99_bool_strict_syshdr.c,v 1.5 2021/01/23 22:34:01 rillig Exp $      */
+/*     $NetBSD: d_c99_bool_strict_syshdr.c,v 1.6 2021/01/23 23:11:40 rillig Exp $      */
 # 3 "d_c99_bool_strict_syshdr.c"
 
 /*
@@ -120,3 +120,19 @@
 # 121 "d_c99_bool_strict_syshdr.c"
            != 0;
 }
+
+/*
+ * If a function from a system header has return type int, which has
+ * traditionally been used for the missing type bool, it may be used
+ * in controlling expressions.
+ */
+
+# 1 "math.h" 3 4
+extern int finite(double);
+# 133 "d_c99_bool_strict_syshdr.c"
+
+_Bool
+call_finite(double d)          /*FIXME*//* expect: 231 */
+{
+       return finite(d);       /*FIXME*//* expect: 211 */
+}
diff -r fde3eb975546 -r 8931a044eba3 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp
--- a/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp    Sat Jan 23 22:34:01 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp    Sat Jan 23 23:11:40 2021 +0000
@@ -1,3 +1,5 @@
 d_c99_bool_strict_syshdr.c(32): controlling expression must be bool, not 'int' [333]
 d_c99_bool_strict_syshdr.c(42): controlling expression must be bool, not 'int' [333]
 d_c99_bool_strict_syshdr.c(76): operands of '=' have incompatible types (_Bool != int) [107]
+d_c99_bool_strict_syshdr.c(137): return value type mismatch (_Bool) and (int) [211]
+d_c99_bool_strict_syshdr.c(135): warning: argument d unused in function call_finite [231]
diff -r fde3eb975546 -r 8931a044eba3 usr.bin/xlint/lint1/externs1.h
--- a/usr.bin/xlint/lint1/externs1.h    Sat Jan 23 22:34:01 2021 +0000
+++ b/usr.bin/xlint/lint1/externs1.h    Sat Jan 23 23:11:40 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: externs1.h,v 1.61 2021/01/23 22:20:17 rillig Exp $     */
+/*     $NetBSD: externs1.h,v 1.62 2021/01/23 23:11:40 rillig Exp $     */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -77,7 +77,6 @@
 extern uint64_t qbmasks[], qlmasks[], qumasks[];
 
 extern void    initscan(void);
-extern int     sign(int64_t, tspec_t, int);
 extern int     msb(int64_t, tspec_t, int);
 extern int64_t xsign(int64_t, tspec_t, int);
 extern void    clear_warn_flags(void);
diff -r fde3eb975546 -r 8931a044eba3 usr.bin/xlint/lint1/lex.c
--- a/usr.bin/xlint/lint1/lex.c Sat Jan 23 22:34:01 2021 +0000
+++ b/usr.bin/xlint/lint1/lex.c Sat Jan 23 23:11:40 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.2 2021/01/23 18:30:29 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.3 2021/01/23 23:11:40 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: lex.c,v 1.2 2021/01/23 18:30:29 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.3 2021/01/23 23:11:40 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -403,7 +403,7 @@
        sb->sb_name = yytext;
        sb->sb_len = yyleng;
        sb->sb_hash = hash(yytext);
-       if ((sym = search(sb)) != NULL && sym->s_keyword) {
+       if ((sym = search(sb)) != NULL && sym->s_keyword != NULL) {
                freesb(sb);
                return keyw(sym);
        }
@@ -434,7 +434,7 @@
 
        for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
                if (strcmp(sym->s_name, sb->sb_name) == 0) {
-                       if (sym->s_keyword) {
+                       if (sym->s_keyword != NULL) {
                                struct kwtab *kw = sym->s_keyword;
                                if (!kw->kw_attr || attron)
                                        return sym;
@@ -658,18 +658,18 @@
 }
 
 /*
- * Returns 1 if t is a signed type and the value is negative.
+ * Returns whether t is a signed type and the value is negative.
  *
  * len is the number of significant bits. If len is -1, len is set
  * to the width of type t.
  */
-int
+static bool
 sign(int64_t q, tspec_t t, int len)
 {
 
        if (t == PTR || is_uinteger(t))
-               return 0;
-       return msb(q, t, len);
+               return false;
+       return msb(q, t, len) != 0;
 }
 
 int
@@ -678,7 +678,7 @@
 
        if (len <= 0)
                len = size(t);
-       return (q & qbmasks[len - 1]) != 0;
+       return (q & qbmasks[len - 1]) != 0 ? 1 : 0;
 }
 
 /*
@@ -962,7 +962,7 @@
                        do {
                                v = (v << 3) + (c - '0');
                                c = inpc();
-                       } while (--n && isdigit(c) && (tflag || c <= '7'));
+                       } while (--n > 0 && isdigit(c) && (tflag || c <= '7'));
                        if (tflag && n > 0 && isdigit(c))
                                /* bad octal digit %c */
                                warning(77, c);
@@ -1125,25 +1125,25 @@
        int     c, lc;
        static const struct {
                const   char *keywd;
-               int     arg;
+               bool    arg;
                void    (*func)(int);
        } keywtab[] = {
-               { "ARGSUSED",           1,      argsused        },
-               { "BITFIELDTYPE",       0,      bitfieldtype    },
-               { "CONSTCOND",          0,      constcond       },
-               { "CONSTANTCOND",       0,      constcond       },
-               { "CONSTANTCONDITION",  0,      constcond       },
-               { "FALLTHRU",           0,      fallthru        },
-               { "FALLTHROUGH",        0,      fallthru        },
-               { "LINTLIBRARY",        0,      lintlib         },
-               { "LINTED",             1,      linted          },
-               { "LONGLONG",           0,      longlong        },
-               { "NOSTRICT",           1,      linted          },
-               { "NOTREACHED",         0,      notreach        },
-               { "PRINTFLIKE",         1,      printflike      },
-               { "PROTOLIB",           1,      protolib        },
-               { "SCANFLIKE",          1,      scanflike       },
-               { "VARARGS",            1,      varargs         },
+               { "ARGSUSED",           true,   argsused        },
+               { "BITFIELDTYPE",       false,  bitfieldtype    },
+               { "CONSTCOND",          false,  constcond       },
+               { "CONSTANTCOND",       false,  constcond       },
+               { "CONSTANTCONDITION",  false,  constcond       },
+               { "FALLTHRU",           false,  fallthru        },
+               { "FALLTHROUGH",        false,  fallthru        },
+               { "LINTLIBRARY",        false,  lintlib         },
+               { "LINTED",             true,   linted          },
+               { "LONGLONG",           false,  longlong        },
+               { "NOSTRICT",           true,   linted          },
+               { "NOTREACHED",         false,  notreach        },
+               { "PRINTFLIKE",         true,   printflike      },
+               { "PROTOLIB",           true,   protolib        },
+               { "SCANFLIKE",          true,   scanflike       },
+               { "VARARGS",            true,   varargs         },
        };
        char    keywd[32];
        char    arg[32];



Home | Main Index | Thread Index | Old Index