Module Name:    src
Committed By:   rillig
Date:           Sat Nov 30 10:43:49 UTC 2024

Modified Files:
        src/tests/usr.bin/xlint/lint1: decl.c
        src/usr.bin/xlint/lint1: decl.c err.c externs1.h func.c lint1.h tree.c

Log Message:
lint: add back optimization for non-query mode

The main point of the optimization is to skip the expensive calls to
type_name when preparing the message details.  This was not spelled out
explicitly in the comment.

While here, fix the stray 'previous declaration' message that occurred
whenever a function was first declared as 'static' and later defined
without 'static', see query 16.


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/tests/usr.bin/xlint/lint1/decl.c
cvs rdiff -u -r1.410 -r1.411 src/usr.bin/xlint/lint1/decl.c
cvs rdiff -u -r1.257 -r1.258 src/usr.bin/xlint/lint1/err.c
cvs rdiff -u -r1.236 -r1.237 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.190 -r1.191 src/usr.bin/xlint/lint1/func.c
cvs rdiff -u -r1.230 -r1.231 src/usr.bin/xlint/lint1/lint1.h
cvs rdiff -u -r1.661 -r1.662 src/usr.bin/xlint/lint1/tree.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/decl.c
diff -u src/tests/usr.bin/xlint/lint1/decl.c:1.31 src/tests/usr.bin/xlint/lint1/decl.c:1.32
--- src/tests/usr.bin/xlint/lint1/decl.c:1.31	Sat Jun  8 09:09:20 2024
+++ src/tests/usr.bin/xlint/lint1/decl.c	Sat Nov 30 10:43:49 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: decl.c,v 1.31 2024/06/08 09:09:20 rillig Exp $	*/
+/*	$NetBSD: decl.c,v 1.32 2024/11/30 10:43:49 rillig Exp $	*/
 # 3 "decl.c"
 
 /*
@@ -300,3 +300,14 @@ type_name_as_member_name(void)
 		} fl;
 	};
 }
+
+
+// When query 16 is not enabled, don't produce a 'previous declaration' message
+// without a preceding main diagnostic.
+static void static_function(void) __attribute__((__used__));
+
+// The definition is without 'static'.
+void
+static_function(void)
+{
+}

Index: src/usr.bin/xlint/lint1/decl.c
diff -u src/usr.bin/xlint/lint1/decl.c:1.410 src/usr.bin/xlint/lint1/decl.c:1.411
--- src/usr.bin/xlint/lint1/decl.c:1.410	Fri Nov 29 06:57:43 2024
+++ src/usr.bin/xlint/lint1/decl.c	Sat Nov 30 10:43:48 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.410 2024/11/29 06:57:43 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.411 2024/11/30 10:43:48 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.410 2024/11/29 06:57:43 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.411 2024/11/30 10:43:48 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -1454,7 +1454,8 @@ declarator_name(sym_t *sym)
 		dcs->d_redeclared_symbol = NULL;
 	} else {
 		dcs->d_redeclared_symbol = sym;
-		if (sym->s_scl == STATIC && dcs->d_scl != STATIC) {
+		if (is_query_enabled[16]
+		    && sym->s_scl == STATIC && dcs->d_scl != STATIC) {
 			/* '%s' was declared 'static', now non-'static' */
 			query_message(16, sym->s_name);
 			print_previous_declaration(sym);

Index: src/usr.bin/xlint/lint1/err.c
diff -u src/usr.bin/xlint/lint1/err.c:1.257 src/usr.bin/xlint/lint1/err.c:1.258
--- src/usr.bin/xlint/lint1/err.c:1.257	Fri Nov 29 06:57:43 2024
+++ src/usr.bin/xlint/lint1/err.c	Sat Nov 30 10:43:49 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: err.c,v 1.257 2024/11/29 06:57:43 rillig Exp $	*/
+/*	$NetBSD: err.c,v 1.258 2024/11/30 10:43:49 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.257 2024/11/29 06:57:43 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.258 2024/11/30 10:43:49 rillig Exp $");
 #endif
 
 #include <limits.h>
@@ -755,6 +755,10 @@ static const char *queries[] = {
 	"typedef '%s' of pointer to union type '%s'",			// Q24
 };
 
+// Omit any expensive computations in the default mode where none of the
+// queries are enabled.  Function calls in message details don't need to be
+// guarded by this flag, as that happens in the query_message macro already.
+bool any_query_enabled;
 bool is_query_enabled[sizeof(queries) / sizeof(queries[0])];
 
 void
@@ -787,6 +791,7 @@ enable_queries(const char *p)
 		    queries[id][0] == '\0')
 			break;
 
+		any_query_enabled = true;
 		is_query_enabled[id] = true;
 
 		if (*end == '\0')

Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.236 src/usr.bin/xlint/lint1/externs1.h:1.237
--- src/usr.bin/xlint/lint1/externs1.h:1.236	Fri Nov 29 06:57:43 2024
+++ src/usr.bin/xlint/lint1/externs1.h	Sat Nov 30 10:43:49 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: externs1.h,v 1.236 2024/11/29 06:57:43 rillig Exp $	*/
+/*	$NetBSD: externs1.h,v 1.237 2024/11/30 10:43:49 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -183,6 +183,7 @@ void debug_attribute_list(const attribut
 extern bool seen_error;
 extern bool seen_warning;
 extern int sytxerr;
+extern bool any_query_enabled;
 extern bool is_query_enabled[];
 
 void msglist(void);

Index: src/usr.bin/xlint/lint1/func.c
diff -u src/usr.bin/xlint/lint1/func.c:1.190 src/usr.bin/xlint/lint1/func.c:1.191
--- src/usr.bin/xlint/lint1/func.c:1.190	Fri Nov 29 06:57:43 2024
+++ src/usr.bin/xlint/lint1/func.c	Sat Nov 30 10:43:49 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: func.c,v 1.190 2024/11/29 06:57:43 rillig Exp $	*/
+/*	$NetBSD: func.c,v 1.191 2024/11/30 10:43:49 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.190 2024/11/29 06:57:43 rillig Exp $");
+__RCSID("$NetBSD: func.c,v 1.191 2024/11/30 10:43:49 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -921,7 +921,7 @@ is_parenthesized(const tnode_t *tn)
 static void
 check_return_value(bool sys, tnode_t *tn)
 {
-	if (is_parenthesized(tn))
+	if (any_query_enabled && is_parenthesized(tn))
 		/* parenthesized return value */
 		query_message(9);
 

Index: src/usr.bin/xlint/lint1/lint1.h
diff -u src/usr.bin/xlint/lint1/lint1.h:1.230 src/usr.bin/xlint/lint1/lint1.h:1.231
--- src/usr.bin/xlint/lint1/lint1.h:1.230	Fri Nov 29 06:57:43 2024
+++ src/usr.bin/xlint/lint1/lint1.h	Sat Nov 30 10:43:49 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: lint1.h,v 1.230 2024/11/29 06:57:43 rillig Exp $ */
+/* $NetBSD: lint1.h,v 1.231 2024/11/30 10:43:49 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -635,7 +635,8 @@ check_printf(const char *fmt, ...)
 #else
 #  define query_message(...)						\
 	do {								\
-		(query_message)(__VA_ARGS__);				\
+		if (any_query_enabled)					\
+			(query_message)(__VA_ARGS__);			\
 	} while (false)
 #endif
 

Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.661 src/usr.bin/xlint/lint1/tree.c:1.662
--- src/usr.bin/xlint/lint1/tree.c:1.661	Fri Nov 29 06:57:43 2024
+++ src/usr.bin/xlint/lint1/tree.c	Sat Nov 30 10:43:49 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: tree.c,v 1.661 2024/11/29 06:57:43 rillig Exp $	*/
+/*	$NetBSD: tree.c,v 1.662 2024/11/30 10:43:49 rillig Exp $	*/
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID)
-__RCSID("$NetBSD: tree.c,v 1.661 2024/11/29 06:57:43 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.662 2024/11/30 10:43:49 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -708,7 +708,7 @@ check_integer_comparison(op_t op, tnode_
 	if (!is_integer(lt) || !is_integer(rt))
 		return;
 
-	if (!in_system_header) {
+	if (any_query_enabled && !in_system_header) {
 		if (lt == CHAR && rn->tn_op == CON &&
 		    !rn->u.value.v_char_constant) {
 			/* comparison '%s' of 'char' with plain integer %d */
@@ -1532,7 +1532,7 @@ build_assignment(op_t op, bool sys, tnod
 		/* implicit narrowing conversion from void ... */
 		query_message(20, type_name(ln->tn_type));
 
-	if (rn->tn_op == CVT && rn->tn_cast &&
+	if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast &&
 	    types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) &&
 	    is_cast_redundant(rn)) {
 		/* redundant cast from '%s' to '%s' before assignment */
@@ -4301,7 +4301,8 @@ cast(tnode_t *tn, bool sys, type_t *tp)
 	} else
 		goto invalid_cast;
 
-	if (types_compatible(tp, tn->tn_type, false, false, NULL))
+	if (any_query_enabled
+	    && types_compatible(tp, tn->tn_type, false, false, NULL))
 		/* no-op cast from '%s' to '%s' */
 		query_message(6, type_name(tn->tn_type), type_name(tp));
 

Reply via email to