Module Name:    src
Committed By:   rillig
Date:           Sat Jan 23 23:11:40 UTC 2021

Modified Files:
        src/tests/usr.bin/xlint/lint1: d_c99_bool_strict_syshdr.c
            d_c99_bool_strict_syshdr.exp
        src/usr.bin/xlint/lint1: externs1.h lex.c

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 \
    src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c \
    src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp
cvs rdiff -u -r1.61 -r1.62 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/xlint/lint1/lex.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c
diff -u src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c:1.5 src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c:1.6
--- src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c:1.5	Sat Jan 23 22:34:01 2021
+++ src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.c	Sat Jan 23 23:11:40 2021
@@ -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 @@ ch_isspace_sys_bool(char c)
 # 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 */
+}
Index: src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp
diff -u src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp:1.5 src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp:1.6
--- src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp:1.5	Sat Jan 23 22:34:01 2021
+++ src/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp	Sat Jan 23 23:11:40 2021
@@ -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]

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.61 src/usr.bin/xlint/lint1/externs1.h:1.62
--- src/usr.bin/xlint/lint1/externs1.h:1.61	Sat Jan 23 22:20:17 2021
+++ src/usr.bin/xlint/lint1/externs1.h	Sat Jan 23 23:11:40 2021
@@ -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	FILE	*yyin;
 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);

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.2 src/usr.bin/xlint/lint1/lex.c:1.3
--- src/usr.bin/xlint/lint1/lex.c:1.2	Sat Jan 23 18:30:29 2021
+++ src/usr.bin/xlint/lint1/lex.c	Sat Jan 23 23:11:40 2021
@@ -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 @@ lex_name(const char *yytext, size_t yyle
 	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 @@ search(sbuf_t *sb)
 
 	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 @@ lex_icon(const char *yytext, size_t yyle
 }
 
 /*
- * 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 @@ msb(int64_t q, tspec_t t, int len)
 
 	if (len <= 0)
 		len = size(t);
-	return (q & qbmasks[len - 1]) != 0;
+	return (q & qbmasks[len - 1]) != 0 ? 1 : 0;
 }
 
 /*
@@ -962,7 +962,7 @@ getescc(int delim)
 			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 @@ lex_comment(void)
 	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];

Reply via email to