Module Name: src
Committed By: rillig
Date: Thu Jan 11 23:06:19 UTC 2024
Modified Files:
src/usr.bin/xlint/lint1: cgram.y externs1.h tree.c
Log Message:
lint: correctly set system-header flag on cast-expression
When a cast-expression comes partly from a system header, determine at
the ')' whether the whole cast-expression comes from the system header.
Previously, it was based on the operand, which contradicted the
documentation of tn_sys.
Mainly affects strict bool mode (where expressions from system headers
are handled more leniently), as well as query 9 for parenthesized return
expressions.
Discovered upon manual inspection, as calling expr_alloc_tnode should
never be necessary when creating an expression node with operands;
there's build_op for that purpose.
To generate a diff of this commit:
cvs rdiff -u -r1.478 -r1.479 src/usr.bin/xlint/lint1/cgram.y
cvs rdiff -u -r1.210 -r1.211 src/usr.bin/xlint/lint1/externs1.h
cvs rdiff -u -r1.593 -r1.594 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/usr.bin/xlint/lint1/cgram.y
diff -u src/usr.bin/xlint/lint1/cgram.y:1.478 src/usr.bin/xlint/lint1/cgram.y:1.479
--- src/usr.bin/xlint/lint1/cgram.y:1.478 Tue Jan 9 23:46:54 2024
+++ src/usr.bin/xlint/lint1/cgram.y Thu Jan 11 23:06:19 2024
@@ -1,5 +1,5 @@
%{
-/* $NetBSD: cgram.y,v 1.478 2024/01/09 23:46:54 rillig Exp $ */
+/* $NetBSD: cgram.y,v 1.479 2024/01/11 23:06:19 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.478 2024/01/09 23:46:54 rillig Exp $");
+__RCSID("$NetBSD: cgram.y,v 1.479 2024/01/11 23:06:19 rillig Exp $");
#endif
#include <limits.h>
@@ -724,8 +724,8 @@ unary_expression:
/* K&R 7.2, C90 ???, C99 6.5.4, C11 6.5.4 */
cast_expression:
unary_expression
-| T_LPAREN type_name T_RPAREN cast_expression {
- $$ = cast($4, $2);
+| T_LPAREN type_name T_RPAREN sys cast_expression {
+ $$ = cast($5, $4, $2);
}
;
Index: src/usr.bin/xlint/lint1/externs1.h
diff -u src/usr.bin/xlint/lint1/externs1.h:1.210 src/usr.bin/xlint/lint1/externs1.h:1.211
--- src/usr.bin/xlint/lint1/externs1.h:1.210 Tue Jan 9 23:46:54 2024
+++ src/usr.bin/xlint/lint1/externs1.h Thu Jan 11 23:06:19 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: externs1.h,v 1.210 2024/01/09 23:46:54 rillig Exp $ */
+/* $NetBSD: externs1.h,v 1.211 2024/01/11 23:06:19 rillig Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
@@ -291,7 +291,7 @@ void convert_constant(op_t, int, const t
tnode_t *build_sizeof(const type_t *);
tnode_t *build_offsetof(const type_t *, designation);
tnode_t *build_alignof(const type_t *);
-tnode_t *cast(tnode_t *, type_t *);
+tnode_t *cast(tnode_t *, bool, type_t *);
tnode_t *build_function_argument(tnode_t *, tnode_t *);
tnode_t *build_function_call(tnode_t *, bool, tnode_t *);
val_t *integer_constant(tnode_t *, bool);
Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.593 src/usr.bin/xlint/lint1/tree.c:1.594
--- src/usr.bin/xlint/lint1/tree.c:1.593 Thu Jan 11 20:25:04 2024
+++ src/usr.bin/xlint/lint1/tree.c Thu Jan 11 23:06:19 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: tree.c,v 1.593 2024/01/11 20:25:04 rillig Exp $ */
+/* $NetBSD: tree.c,v 1.594 2024/01/11 23:06:19 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.593 2024/01/11 20:25:04 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.594 2024/01/11 23:06:19 rillig Exp $");
#endif
#include <float.h>
@@ -4017,7 +4017,7 @@ build_alignof(const type_t *tp)
}
static tnode_t *
-cast_to_union(tnode_t *otn, type_t *ntp)
+cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
{
if (!allow_gcc) {
@@ -4030,12 +4030,8 @@ cast_to_union(tnode_t *otn, type_t *ntp)
m != NULL; m = m->s_next) {
if (types_compatible(m->s_type, otn->tn_type,
false, false, NULL)) {
- tnode_t *ntn = expr_alloc_tnode();
- ntn->tn_op = CVT;
- ntn->tn_type = ntp;
+ tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
ntn->tn_cast = true;
- ntn->tn_left = otn;
- ntn->tn_right = NULL;
return ntn;
}
}
@@ -4046,7 +4042,7 @@ cast_to_union(tnode_t *otn, type_t *ntp)
}
tnode_t *
-cast(tnode_t *tn, type_t *tp)
+cast(tnode_t *tn, bool sys, type_t *tp)
{
if (tn == NULL)
@@ -4065,7 +4061,7 @@ cast(tnode_t *tn, type_t *tp)
* scalar type to a scalar type.
*/
} else if (nt == UNION)
- return cast_to_union(tn, tp);
+ return cast_to_union(tn, sys, tp);
else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
/* Casting to a struct is an undocumented GCC extension. */
if (!(allow_gcc && nt == STRUCT))
@@ -4099,6 +4095,7 @@ cast(tnode_t *tn, type_t *tp)
tn = convert(CVT, 0, tp, tn);
tn->tn_cast = true;
+ tn->tn_sys = sys;
return tn;