Module Name:    src
Committed By:   rillig
Date:           Thu Jul 13 07:19:25 UTC 2023

Modified Files:
        src/usr.bin/xlint/lint1: cgram.y externs1.h func.c lex.c lint1.h

Log Message:
lint: reduce code size for handling lint comments


To generate a diff of this commit:
cvs rdiff -u -r1.458 -r1.459 src/usr.bin/xlint/lint1/cgram.y
cvs rdiff -u -r1.192 -r1.193 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.168 -r1.169 src/usr.bin/xlint/lint1/func.c
cvs rdiff -u -r1.179 -r1.180 src/usr.bin/xlint/lint1/lex.c
cvs rdiff -u -r1.187 -r1.188 src/usr.bin/xlint/lint1/lint1.h

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

Modified files:

Index: src/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.458 src/usr.bin/xlint/lint1/cgram.y:1.459
--- src/usr.bin/xlint/lint1/cgram.y:1.458	Thu Jul 13 06:41:27 2023
+++ src/usr.bin/xlint/lint1/cgram.y	Thu Jul 13 07:19:24 2023
@@ -1,5 +1,5 @@
 %{
-/* $NetBSD: cgram.y,v 1.458 2023/07/13 06:41:27 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.459 2023/07/13 07:19:24 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -35,7 +35,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: cgram.y,v 1.458 2023/07/13 06:41:27 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.459 2023/07/13 07:19:24 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -2205,7 +2205,7 @@ gcc_attribute:
 		    is_either(name, "unused", "__unused__"))
 			dcs_set_used();
 		else if (is_either(name, "fallthrough", "__fallthrough__"))
-			fallthru(1);
+			suppress_fallthrough = true;
 	}
 |	T_NAME T_LPAREN T_RPAREN
 |	T_NAME T_LPAREN gcc_attribute_parameters T_RPAREN

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.192 src/usr.bin/xlint/lint1/externs1.h:1.193
--- src/usr.bin/xlint/lint1/externs1.h:1.192	Thu Jul 13 06:41:27 2023
+++ src/usr.bin/xlint/lint1/externs1.h	Thu Jul 13 07:19:24 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.192 2023/07/13 06:41:27 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.193 2023/07/13 07:19:24 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -333,18 +333,7 @@ void	stmt_continue(void);
 void	stmt_break(void);
 void	stmt_return(bool, tnode_t *);
 void	global_clean_up_decl(bool);
-void	argsused(int);
-void	constcond(int);
-void	fallthru(int);
-void	not_reached(int);
-void	lintlib(int);
-void	linted(int);
-void	varargs(int);
-void	printflike(int);
-void	scanflike(int);
-void	protolib(int);
-void	longlong(int);
-void	bitfieldtype(int);
+void	handle_lint_comment(lint_comment, int);
 
 /*
  * init.c

Index: src/usr.bin/xlint/lint1/func.c
diff -u src/usr.bin/xlint/lint1/func.c:1.168 src/usr.bin/xlint/lint1/func.c:1.169
--- src/usr.bin/xlint/lint1/func.c:1.168	Thu Jul 13 06:41:27 2023
+++ src/usr.bin/xlint/lint1/func.c	Thu Jul 13 07:19:24 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: func.c,v 1.168 2023/07/13 06:41:27 rillig Exp $	*/
+/*	$NetBSD: func.c,v 1.169 2023/07/13 07:19:24 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: func.c,v 1.168 2023/07/13 06:41:27 rillig Exp $");
+__RCSID("$NetBSD: func.c,v 1.169 2023/07/13 07:19:24 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -1123,12 +1123,10 @@ global_clean_up_decl(bool silent)
 }
 
 /*
- * ARGSUSED comment
- *
- * Only the first n arguments of the following function are checked
- * for usage. A missing argument is taken to be 0.
+ * Only the first n arguments of the following function are checked for usage.
+ * A missing argument is taken to be 0.
  */
-void
+static void
 argsused(int n)
 {
 
@@ -1148,7 +1146,7 @@ argsused(int n)
 	argsused_pos = curr_pos;
 }
 
-void
+static void
 varargs(int n)
 {
 
@@ -1172,7 +1170,7 @@ varargs(int n)
  * Check all arguments until the (n-1)-th as usual. The n-th argument is
  * used the check the types of remaining arguments.
  */
-void
+static void
 printflike(int n)
 {
 
@@ -1196,7 +1194,7 @@ printflike(int n)
  * Check all arguments until the (n-1)-th as usual. The n-th argument is
  * used the check the types of remaining arguments.
  */
-void
+static void
 scanflike(int n)
 {
 
@@ -1216,34 +1214,8 @@ scanflike(int n)
 	scanflike_pos = curr_pos;
 }
 
-/* ARGSUSED */
-void
-constcond(int n)
-{
-
-	suppress_constcond = true;
-}
-
-/* ARGSUSED */
-void
-fallthru(int n)
-{
-
-	suppress_fallthrough = true;
-}
-
-/* ARGSUSED */
-void
-not_reached(int n)
-{
-
-	set_reached(false);
-	warn_about_unreachable = false;
-}
-
-/* ARGSUSED */
-void
-lintlib(int n)
+static void
+lintlib(void)
 {
 
 	if (dcs->d_kind != DLK_EXTERN) {
@@ -1255,30 +1227,12 @@ lintlib(int n)
 	vflag = true;
 }
 
-void
-linted(int n)
-{
-
-	debug_step("set lwarn %d", n);
-	lwarn = n;
-}
-
-/* ARGSUSED */
-void
-bitfieldtype(int n)
-{
-
-	debug_step("%s:%d: suppress_bitfieldtype = true",
-	    curr_pos.p_file, curr_pos.p_line);
-	suppress_bitfieldtype = true;
-}
-
 /*
  * PROTOLIB in conjunction with LINTLIBRARY can be used to handle
  * prototypes like function definitions. This is done if the argument
  * to PROTOLIB is nonzero. Otherwise, prototypes are handled normally.
  */
-void
+static void
 protolib(int n)
 {
 
@@ -1290,10 +1244,23 @@ protolib(int n)
 	plibflg = n != 0;
 }
 
-/* ARGSUSED */
 void
-longlong(int n)
+handle_lint_comment(lint_comment comment, int arg)
 {
-
-	suppress_longlong = true;
+	switch (comment) {
+	case LC_ARGSUSED:	argsused(arg);			break;
+	case LC_BITFIELDTYPE:	suppress_bitfieldtype = true;	break;
+	case LC_CONSTCOND:	suppress_constcond = true;	break;
+	case LC_FALLTHROUGH:	suppress_fallthrough = true;	break;
+	case LC_LINTLIBRARY:	lintlib();			break;
+	case LC_LINTED:		debug_step("set lwarn %d", arg);
+				lwarn = arg;			break;
+	case LC_LONGLONG:	suppress_longlong = true;	break;
+	case LC_NOTREACHED:	set_reached(false);
+				warn_about_unreachable = false;	break;
+	case LC_PRINTFLIKE:	printflike(arg);		break;
+	case LC_PROTOLIB:	protolib(arg);			break;
+	case LC_SCANFLIKE:	scanflike(arg);			break;
+	case LC_VARARGS:	varargs(arg);			break;
+	}
 }

Index: src/usr.bin/xlint/lint1/lex.c
diff -u src/usr.bin/xlint/lint1/lex.c:1.179 src/usr.bin/xlint/lint1/lex.c:1.180
--- src/usr.bin/xlint/lint1/lex.c:1.179	Thu Jul 13 06:41:27 2023
+++ src/usr.bin/xlint/lint1/lex.c	Thu Jul 13 07:19:24 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lex.c,v 1.179 2023/07/13 06:41:27 rillig Exp $ */
+/* $NetBSD: lex.c,v 1.180 2023/07/13 07:19:24 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: lex.c,v 1.179 2023/07/13 06:41:27 rillig Exp $");
+__RCSID("$NetBSD: lex.c,v 1.180 2023/07/13 07:19:24 rillig Exp $");
 #endif
 
 #include <ctype.h>
@@ -1050,39 +1050,34 @@ lex_directive(const char *yytext)
 	}
 }
 
-/*
- * Handle lint comments such as ARGSUSED.
- *
- * If one of these comments is recognized, the argument, if any, is
- * parsed and a function which handles this comment is called.
- */
+/* Handle lint comments such as ARGSUSED. */
 void
 lex_comment(void)
 {
 	int c;
 	static const struct {
-		const	char *keywd;
+		const	char name[18];
 		bool	arg;
-		void	(*func)(int);
+		lint_comment comment;
 	} keywtab[] = {
-		{ "ARGSUSED",		true,	argsused	},
-		{ "BITFIELDTYPE",	false,	bitfieldtype	},
-		{ "CONSTCOND",		false,	constcond	},
-		{ "CONSTANTCOND",	false,	constcond	},
-		{ "CONSTANTCONDITION",	false,	constcond	},
-		{ "FALLTHRU",		false,	fallthru	},
-		{ "FALLTHROUGH",	false,	fallthru	},
-		{ "FALL THROUGH",	false,	fallthru	},
-		{ "fallthrough",	false,	fallthru	},
-		{ "LINTLIBRARY",	false,	lintlib		},
-		{ "LINTED",		true,	linted		},
-		{ "LONGLONG",		false,	longlong	},
-		{ "NOSTRICT",		true,	linted		},
-		{ "NOTREACHED",		false,	not_reached	},
-		{ "PRINTFLIKE",		true,	printflike	},
-		{ "PROTOLIB",		true,	protolib	},
-		{ "SCANFLIKE",		true,	scanflike	},
-		{ "VARARGS",		true,	varargs		},
+		{ "ARGSUSED",		true,	LC_ARGSUSED	},
+		{ "BITFIELDTYPE",	false,	LC_BITFIELDTYPE	},
+		{ "CONSTCOND",		false,	LC_CONSTCOND	},
+		{ "CONSTANTCOND",	false,	LC_CONSTCOND	},
+		{ "CONSTANTCONDITION",	false,	LC_CONSTCOND	},
+		{ "FALLTHRU",		false,	LC_FALLTHROUGH	},
+		{ "FALLTHROUGH",	false,	LC_FALLTHROUGH	},
+		{ "FALL THROUGH",	false,	LC_FALLTHROUGH	},
+		{ "fallthrough",	false,	LC_FALLTHROUGH	},
+		{ "LINTLIBRARY",	false,	LC_LINTLIBRARY	},
+		{ "LINTED",		true,	LC_LINTED	},
+		{ "LONGLONG",		false,	LC_LONGLONG	},
+		{ "NOSTRICT",		true,	LC_LINTED	},
+		{ "NOTREACHED",		false,	LC_NOTREACHED	},
+		{ "PRINTFLIKE",		true,	LC_PRINTFLIKE	},
+		{ "PROTOLIB",		true,	LC_PROTOLIB	},
+		{ "SCANFLIKE",		true,	LC_SCANFLIKE	},
+		{ "VARARGS",		true,	LC_VARARGS	},
 	};
 	char keywd[32];
 	char arg[32];
@@ -1110,7 +1105,7 @@ lex_comment(void)
 
 	/* look for the keyword */
 	for (i = 0; i < sizeof(keywtab) / sizeof(keywtab[0]); i++) {
-		if (strcmp(keywtab[i].keywd, keywd) == 0)
+		if (strcmp(keywtab[i].name, keywd) == 0)
 			break;
 	}
 	if (i == sizeof(keywtab) / sizeof(keywtab[0]))
@@ -1136,12 +1131,11 @@ lex_comment(void)
 		c = read_byte();
 
 	seen_end_of_comment = c == '*' && (c = read_byte()) == '/';
-	if (!seen_end_of_comment && keywtab[i].func != linted)
+	if (!seen_end_of_comment && keywtab[i].comment != LC_LINTED)
 		/* extra characters in lint comment */
 		warning(257);
 
-	if (keywtab[i].func != NULL)
-		keywtab[i].func(a);
+	handle_lint_comment(keywtab[i].comment, a);
 
 skip_rest:
 	while (!seen_end_of_comment) {

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.187 src/usr.bin/xlint/lint1/lint1.h:1.188
--- src/usr.bin/xlint/lint1/lint1.h:1.187	Wed Jul 12 18:26:04 2023
+++ src/usr.bin/xlint/lint1/lint1.h	Thu Jul 13 07:19:24 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.187 2023/07/12 18:26:04 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.188 2023/07/13 07:19:24 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -457,6 +457,21 @@ typedef struct {
 	size_t hi;			/* inclusive */
 } range_t;
 
+typedef enum {
+	LC_ARGSUSED,
+	LC_BITFIELDTYPE,
+	LC_CONSTCOND,
+	LC_FALLTHROUGH,
+	LC_LINTLIBRARY,
+	LC_LINTED,
+	LC_LONGLONG,
+	LC_NOTREACHED,
+	LC_PRINTFLIKE,
+	LC_PROTOLIB,
+	LC_SCANFLIKE,
+	LC_VARARGS,
+} lint_comment;
+
 #include "externs1.h"
 
 #define lint_assert(cond)						\

Reply via email to