Module Name: src
Committed By: rillig
Date: Sat Aug 5 10:13:39 UTC 2023
Modified Files:
src/tests/usr.bin/xlint/lint1: expr_sizeof.c
src/usr.bin/xlint/lint1: tree.c
Log Message:
lint: implement __builtin_offsetof for the simplest cases
Cases not covered:
1. C99 allows designators in the offsetof macro.
2. For packed types, the offset is likely to be incorrect.
To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/tests/usr.bin/xlint/lint1/expr_sizeof.c
cvs rdiff -u -r1.575 -r1.576 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/expr_sizeof.c
diff -u src/tests/usr.bin/xlint/lint1/expr_sizeof.c:1.13 src/tests/usr.bin/xlint/lint1/expr_sizeof.c:1.14
--- src/tests/usr.bin/xlint/lint1/expr_sizeof.c:1.13 Sun Jul 9 11:18:55 2023
+++ src/tests/usr.bin/xlint/lint1/expr_sizeof.c Sat Aug 5 10:13:39 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: expr_sizeof.c,v 1.13 2023/07/09 11:18:55 rillig Exp $ */
+/* $NetBSD: expr_sizeof.c,v 1.14 2023/08/05 10:13:39 rillig Exp $ */
# 3 "expr_sizeof.c"
/*
@@ -132,8 +132,7 @@ bit_fields(void)
} mixed;
/* expect+1: error: negative array dimension (-8) [20] */
typedef int sizeof_mixed[-(int)sizeof(mixed)];
- /* FIXME: Implement build_offsetof correctly. */
- /* expect+3: error: negative array dimension (-8) [20] */
+ /* expect+3: error: negative array dimension (-1) [20] */
typedef int offsetof_mixed_ch[
-(int)__builtin_offsetof(struct mixed, ch)
];
Index: src/usr.bin/xlint/lint1/tree.c
diff -u src/usr.bin/xlint/lint1/tree.c:1.575 src/usr.bin/xlint/lint1/tree.c:1.576
--- src/usr.bin/xlint/lint1/tree.c:1.575 Wed Aug 2 18:57:54 2023
+++ src/usr.bin/xlint/lint1/tree.c Sat Aug 5 10:13:39 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: tree.c,v 1.575 2023/08/02 18:57:54 rillig Exp $ */
+/* $NetBSD: tree.c,v 1.576 2023/08/05 10:13:39 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.575 2023/08/02 18:57:54 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.576 2023/08/05 10:13:39 rillig Exp $");
#endif
#include <float.h>
@@ -3960,17 +3960,26 @@ build_sizeof(const type_t *tp)
/*
* Create a constant node for offsetof.
*/
-/* ARGSUSED */ /* FIXME: See implementation comments. */
tnode_t *
build_offsetof(const type_t *tp, const sym_t *sym)
{
+ unsigned int offset_in_bits = 0;
- if (!is_struct_or_union(tp->t_tspec))
+ if (!is_struct_or_union(tp->t_tspec)) {
/* unacceptable operand of '%s' */
error(111, "offsetof");
+ goto proceed;
+ }
+ sym_t *mem = find_member(tp->t_sou, sym->s_name);
+ if (mem == NULL) {
+ /* type '%s' does not have member '%s' */
+ error(101, sym->s_name, type_name(tp));
+ goto proceed;
+ }
+ offset_in_bits = mem->u.s_member.sm_offset_in_bits;
- /* FIXME: Don't wrongly use the size of the whole type, use sym. */
- unsigned int offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
+proceed:;
+ unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
tn->tn_system_dependent = true;
return tn;