Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src tests/lint: add test for parsing sizeof expressions



details:   https://anonhg.NetBSD.org/src/rev/cea4ac44c142
branches:  trunk
changeset: 373051:cea4ac44c142
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jan 15 00:53:19 2023 +0000

description:
tests/lint: add test for parsing sizeof expressions

diffstat:

 distrib/sets/lists/tests/mi             |   3 +-
 tests/usr.bin/xlint/lint1/Makefile      |   3 +-
 tests/usr.bin/xlint/lint1/expr_sizeof.c |  51 +++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 2 deletions(-)

diffs (89 lines):

diff -r ad60a9f25293 -r cea4ac44c142 distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Sun Jan 15 00:34:10 2023 +0000
+++ b/distrib/sets/lists/tests/mi       Sun Jan 15 00:53:19 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1240 2023/01/15 00:34:10 rillig Exp $
+# $NetBSD: mi,v 1.1241 2023/01/15 00:53:19 rillig Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -6553,6 +6553,7 @@
 ./usr/tests/usr.bin/xlint/lint1/expr_promote_trad.exp-ln       tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/expr_range.c                   tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/expr_range.exp                 tests-obsolete          obsolete,atf
+./usr/tests/usr.bin/xlint/lint1/expr_sizeof.c                  tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/feat_stacktrace.c              tests-usr.bin-tests     compattestfile,atf
 ./usr/tests/usr.bin/xlint/lint1/feat_stacktrace.exp            tests-obsolete          obsolete,atf
 ./usr/tests/usr.bin/xlint/lint1/gcc_attribute.c                        tests-usr.bin-tests     compattestfile,atf
diff -r ad60a9f25293 -r cea4ac44c142 tests/usr.bin/xlint/lint1/Makefile
--- a/tests/usr.bin/xlint/lint1/Makefile        Sun Jan 15 00:34:10 2023 +0000
+++ b/tests/usr.bin/xlint/lint1/Makefile        Sun Jan 15 00:53:19 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.130 2023/01/04 05:25:08 rillig Exp $
+# $NetBSD: Makefile,v 1.131 2023/01/15 00:53:19 rillig Exp $
 
 NOMAN=         # defined
 MAX_MESSAGE=   349             # see lint1/err.c
@@ -111,6 +111,7 @@
 FILES+=                expr_promote_trad.c
 FILES+=                expr_promote_trad.exp-ln
 FILES+=                expr_range.c
+FILES+=                expr_sizeof.c
 FILES+=                feat_stacktrace.c
 FILES+=                gcc_attribute.c
 FILES+=                gcc_attribute_aligned.c
diff -r ad60a9f25293 -r cea4ac44c142 tests/usr.bin/xlint/lint1/expr_sizeof.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_sizeof.c   Sun Jan 15 00:53:19 2023 +0000
@@ -0,0 +1,51 @@
+/*     $NetBSD: expr_sizeof.c,v 1.1 2023/01/15 00:53:19 rillig Exp $   */
+# 3 "expr_sizeof.c"
+
+/*
+ * C99 6.5.3.4 "The sizeof operator"
+ * C11 6.5.3.4 "The sizeof operator"
+ */
+
+/*
+ * A sizeof expression can either take a type name or an expression.
+ */
+void sink(unsigned long);
+
+struct {
+       int member;
+} s, *ps;
+
+/*
+ * In a sizeof expression taking a type name, the type name must be enclosed
+ * in parentheses.
+ */
+/* expect+1: error: negative array dimension (-4) [20] */
+typedef int sizeof_int[-(int)sizeof(int)];
+
+/*
+ * In a sizeof expression taking an expression, the expression may or may not
+ * be enclosed in parentheses, like any other expression.
+ */
+/* expect+1: error: negative array dimension (-4) [20] */
+typedef int sizeof_paren_zero[-(int)sizeof(0)];
+/* expect+1: error: negative array dimension (-4) [20] */
+typedef int sizeof_zero[-(int)sizeof 0];
+
+/*
+ * Even though 's' is not a constant expression, 'sizeof s' is.
+ */
+/* expect+1: error: negative array dimension (-4) [20] */
+typedef int sizeof_global_var[-(int)sizeof s];
+/* expect+1: error: negative array dimension (-4) [20] */
+typedef int sizeof_paren_global_var[-(int)sizeof(s)];
+
+/*
+ * Even though 'sizeof(s)' may look like a function call expression, the
+ * parentheses around 's' are ordinary parentheses and do not influence the
+ * associativity.  Therefore, the '.' following the '(s)' takes precedence
+ * over the 'sizeof'.  Same for the '->' following the '(ps)'.
+ */
+/* expect+1: error: negative array dimension (-4) [20] */
+typedef int sizeof_paren_global_struct_member[-(int)sizeof(s).member];
+/* expect+1: error: negative array dimension (-4) [20] */
+typedef int sizeof_paren_global_ptr_struct_member[-(int)sizeof(ps)->member];



Home | Main Index | Thread Index | Old Index