Module Name: src
Committed By: rillig
Date: Thu Nov 28 22:32:53 UTC 2024
Modified Files:
src/tests/usr.bin/xlint/lint1: queries.c t_usage.sh
src/usr.bin/xlint/lint1: decl.c err.c
Log Message:
lint: add queries for typedefs to struct/union and their pointers
As suggested in share/misc/style since 1.75 from August 2023.
These are queries instead of warnings as the number of false positives or
historical practice is too high; these queries are only intended for
detecting these typedefs on newly added types.
To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/tests/usr.bin/xlint/lint1/queries.c
cvs rdiff -u -r1.24 -r1.25 src/tests/usr.bin/xlint/lint1/t_usage.sh
cvs rdiff -u -r1.408 -r1.409 src/usr.bin/xlint/lint1/decl.c
cvs rdiff -u -r1.255 -r1.256 src/usr.bin/xlint/lint1/err.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/queries.c
diff -u src/tests/usr.bin/xlint/lint1/queries.c:1.30 src/tests/usr.bin/xlint/lint1/queries.c:1.31
--- src/tests/usr.bin/xlint/lint1/queries.c:1.30 Sat Nov 23 00:01:48 2024
+++ src/tests/usr.bin/xlint/lint1/queries.c Thu Nov 28 22:32:53 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: queries.c,v 1.30 2024/11/23 00:01:48 rillig Exp $ */
+/* $NetBSD: queries.c,v 1.31 2024/11/28 22:32:53 rillig Exp $ */
# 3 "queries.c"
/*
@@ -17,6 +17,7 @@
/* lint1-extra-flags: -q 1,2,3,4,5,6,7,8,9,10 */
/* lint1-extra-flags: -q 11,12,13,14,15,16,17,18,19,20 */
+/* lint1-extra-flags: -q 21,22,23,24 */
/* lint1-extra-flags: -X 351 */
typedef unsigned char u8_t;
@@ -369,9 +370,9 @@ Q9(int x)
return (0.0);
case 9:
return
-# 373 "queries.c" 3 4
+# 374 "queries.c" 3 4
((void *)0)
-# 375 "queries.c"
+# 376 "queries.c"
/* expect+1: warning: illegal combination of integer 'int' and pointer 'pointer to void' [183] */
;
case 10:
@@ -543,3 +544,23 @@ Q20_void_pointer_conversion(void)
int_ptr = (void *)0;
}
+
+/*
+ * Q21, Q22, Q23 and Q24 detect typedefs for struct and union types and
+ * pointers to them. By using the tagged types directly instead of their
+ * typedefs, it may be possible to save including some system headers.
+ */
+
+struct struct_tag {
+};
+union union_tag {
+};
+
+/* expect+2: typedef 'struct_typedef' of struct type 'struct struct_tag' [Q21] */
+/* expect+1: typedef 'struct_ptr' of pointer to struct type 'pointer to struct struct_tag' [Q23] */
+typedef struct struct_tag struct_typedef, *struct_ptr;
+/* expect+2: typedef 'union_typedef' of union type 'union union_tag' [Q22] */
+/* expect+1: typedef 'union_ptr' of pointer to union type 'pointer to union union_tag' [Q24] */
+typedef union union_tag union_typedef, *union_ptr;
+typedef int int_typedef, *int_pointer;
+typedef void (function_typedef)(int), (*function_ptr)(int);
Index: src/tests/usr.bin/xlint/lint1/t_usage.sh
diff -u src/tests/usr.bin/xlint/lint1/t_usage.sh:1.24 src/tests/usr.bin/xlint/lint1/t_usage.sh:1.25
--- src/tests/usr.bin/xlint/lint1/t_usage.sh:1.24 Sat Nov 23 16:48:35 2024
+++ src/tests/usr.bin/xlint/lint1/t_usage.sh Thu Nov 28 22:32:53 2024
@@ -1,4 +1,4 @@
-# $NetBSD: t_usage.sh,v 1.24 2024/11/23 16:48:35 rillig Exp $
+# $NetBSD: t_usage.sh,v 1.25 2024/11/28 22:32:53 rillig Exp $
#
# Copyright (c) 2023 The NetBSD Foundation, Inc.
# All rights reserved.
@@ -89,13 +89,13 @@ enable_queries_body()
# The largest known query.
atf_check \
- "$lint1" -q 20 code.c /dev/null
+ "$lint1" -q 24 code.c /dev/null
# Larger than the largest known query.
atf_check \
-s 'exit:1' \
- -e "inline:lint1: invalid query ID '21'\n" \
- "$lint1" -q 21 code.c /dev/null
+ -e "inline:lint1: invalid query ID '25'\n" \
+ "$lint1" -q 25 code.c /dev/null
# Whitespace is not allowed before a query ID.
atf_check \
Index: src/usr.bin/xlint/lint1/decl.c
diff -u src/usr.bin/xlint/lint1/decl.c:1.408 src/usr.bin/xlint/lint1/decl.c:1.409
--- src/usr.bin/xlint/lint1/decl.c:1.408 Wed Nov 13 03:43:00 2024
+++ src/usr.bin/xlint/lint1/decl.c Thu Nov 28 22:32:53 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.408 2024/11/13 03:43:00 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.409 2024/11/28 22:32:53 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.408 2024/11/13 03:43:00 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.409 2024/11/28 22:32:53 rillig Exp $");
#endif
#include <sys/param.h>
@@ -2022,6 +2022,24 @@ declare_extern(sym_t *dsym, bool has_ini
dsym->s_type = block_dup_type(dsym->s_type);
dsym->s_type->t_typedef = true;
set_first_typedef(dsym->s_type, dsym);
+ if (dsym->s_type->t_tspec == STRUCT)
+ /* typedef '%s' of struct type '%s' */
+ query_message(21, dsym->s_name,
+ type_name(dsym->s_type));
+ else if (dsym->s_type->t_tspec == UNION)
+ /* typedef '%s' of union type '%s' */
+ query_message(22, dsym->s_name,
+ type_name(dsym->s_type));
+ else if (dsym->s_type->t_tspec == PTR
+ && dsym->s_type->t_subt->t_tspec == STRUCT)
+ /* typedef '%s' of pointer to struct type '%s' */
+ query_message(23, dsym->s_name,
+ type_name(dsym->s_type));
+ else if (dsym->s_type->t_tspec == PTR
+ && dsym->s_type->t_subt->t_tspec == UNION)
+ /* typedef '%s' of pointer to union type '%s' */
+ query_message(24, dsym->s_name,
+ type_name(dsym->s_type));
}
debug_printf("%s: ", __func__);
debug_sym("", dsym, "\n");
Index: src/usr.bin/xlint/lint1/err.c
diff -u src/usr.bin/xlint/lint1/err.c:1.255 src/usr.bin/xlint/lint1/err.c:1.256
--- src/usr.bin/xlint/lint1/err.c:1.255 Sat Nov 23 16:48:35 2024
+++ src/usr.bin/xlint/lint1/err.c Thu Nov 28 22:32:53 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: err.c,v 1.255 2024/11/23 16:48:35 rillig Exp $ */
+/* $NetBSD: err.c,v 1.256 2024/11/28 22:32:53 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.255 2024/11/23 16:48:35 rillig Exp $");
+__RCSID("$NetBSD: err.c,v 1.256 2024/11/28 22:32:53 rillig Exp $");
#endif
#include <limits.h>
@@ -749,6 +749,10 @@ static const char *queries[] = {
"const automatic variable '%s'", // Q18
"implicit conversion from integer '%s' to floating point '%s'", // Q19
"implicit narrowing conversion from void pointer to '%s'", // Q20
+ "typedef '%s' of struct type '%s'", // Q21
+ "typedef '%s' of union type '%s'", // Q22
+ "typedef '%s' of pointer to struct type '%s'", // Q23
+ "typedef '%s' of pointer to union type '%s'", // Q24
};
bool any_query_enabled; /* for optimizing non-query scenarios */