Module Name:    src
Committed By:   rillig
Date:           Mon Feb 24 19:49:00 UTC 2025

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_153.c msg_177.c msg_309.c

Log Message:
tests/lint: add tests for constants, pointers and lost bits


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/tests/usr.bin/xlint/lint1/msg_153.c
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/xlint/lint1/msg_177.c
cvs rdiff -u -r1.7 -r1.8 src/tests/usr.bin/xlint/lint1/msg_309.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/msg_153.c
diff -u src/tests/usr.bin/xlint/lint1/msg_153.c:1.9 src/tests/usr.bin/xlint/lint1/msg_153.c:1.10
--- src/tests/usr.bin/xlint/lint1/msg_153.c:1.9	Sat Nov 23 16:48:35 2024
+++ src/tests/usr.bin/xlint/lint1/msg_153.c	Mon Feb 24 19:49:00 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_153.c,v 1.9 2024/11/23 16:48:35 rillig Exp $	*/
+/*	$NetBSD: msg_153.c,v 1.10 2025/02/24 19:49:00 rillig Exp $	*/
 # 3 "msg_153.c"
 
 // Test for message: converting '%s' to incompatible '%s' for argument %d [153]
@@ -7,10 +7,14 @@
 
 
 typedef double (*unary_operator)(double);
+typedef unsigned char sixteen_bytes[16];
 
 void sink_function_pointer(unary_operator);
 void sink_int_pointer(int *);
 void sink_qualifiers(char *, const char *, volatile char *, const volatile char *);
+void take_pointer_to_sixteen_bytes(sixteen_bytes *);
+
+sixteen_bytes bytes;
 
 void
 to_function_pointer(int *x)
@@ -36,3 +40,11 @@ qualifiers(char *ptr, const volatile cha
 	/* expect+1: warning: passing 'pointer to const volatile char' to argument 3 discards 'const' [383] */
 	sink_qualifiers(cvptr, cvptr, cvptr, cvptr);
 }
+
+void
+pass_pointer_to_array(void)
+{
+	// FIXME: Must be a pointer to 16 bytes, not to 1 byte.
+	/* expect+1: warning: converting 'pointer to unsigned char' to incompatible 'pointer to array[16] of unsigned char' for argument 1 [153] */
+	take_pointer_to_sixteen_bytes(&bytes);
+}

Index: src/tests/usr.bin/xlint/lint1/msg_177.c
diff -u src/tests/usr.bin/xlint/lint1/msg_177.c:1.4 src/tests/usr.bin/xlint/lint1/msg_177.c:1.5
--- src/tests/usr.bin/xlint/lint1/msg_177.c:1.4	Tue Mar 28 14:44:35 2023
+++ src/tests/usr.bin/xlint/lint1/msg_177.c	Mon Feb 24 19:49:00 2025
@@ -1,4 +1,4 @@
-/*	$NetBSD: msg_177.c,v 1.4 2023/03/28 14:44:35 rillig Exp $	*/
+/*	$NetBSD: msg_177.c,v 1.5 2025/02/24 19:49:00 rillig Exp $	*/
 # 3 "msg_177.c"
 
 // Test for message: non-constant initializer [177]
@@ -14,3 +14,7 @@ const int var = not_a_constant;
 
 /* expect+1: error: non-constant initializer [177] */
 const int calling_function = function();
+
+// A compound expression is not a constant expression.
+/* expect+1: error: non-constant initializer [177] */
+const int compound_expression = (int){ 3 };

Index: src/tests/usr.bin/xlint/lint1/msg_309.c
diff -u src/tests/usr.bin/xlint/lint1/msg_309.c:1.7 src/tests/usr.bin/xlint/lint1/msg_309.c:1.8
--- src/tests/usr.bin/xlint/lint1/msg_309.c:1.7	Fri Jul  7 19:45:22 2023
+++ src/tests/usr.bin/xlint/lint1/msg_309.c	Mon Feb 24 19:49:00 2025
@@ -1,19 +1,28 @@
-/*	$NetBSD: msg_309.c,v 1.7 2023/07/07 19:45:22 rillig Exp $	*/
+/*	$NetBSD: msg_309.c,v 1.8 2025/02/24 19:49:00 rillig Exp $	*/
 # 3 "msg_309.c"
 
 // Test for message: extra bits set to 0 in conversion of '%s' to '%s', op '%s' [309]
 
 /* lint1-extra-flags: -X 351 */
 
-int
-scale(unsigned long long x) {
+typedef unsigned char u8_t;
+typedef unsigned int u32_t;
+typedef unsigned long long u64_t;
+
+u8_t u8;
+u32_t u32;
+u64_t u64;
+
+
+void
+test(void)
+{
 
 	/*
 	 * Both operands of '&' have the same type, therefore no conversion
 	 * is necessary and no bits can get lost.
 	 */
-	if ((x & 0xffffffff00000000ULL) != 0)
-		return 32;
+	u64 = u64 & 0xffffffff00000000ULL;
 
 	/*
 	 * The constant has type 'unsigned 32-bit'.  The usual arithmetic
@@ -23,8 +32,7 @@ scale(unsigned long long x) {
 	 * 32-bit to a 64-bit platform.
 	 */
 	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */
-	if ((x & 0xffff0000) != 0)
-		return 16;
+	u64 = u64 & 0xffff0000;
 
 	/*
 	 * The integer constant is explicitly unsigned.  Even in this case,
@@ -33,8 +41,7 @@ scale(unsigned long long x) {
 	 * bits.
 	 */
 	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */
-	if ((x & 0xffff0000U) != 0)
-		return 16;
+	u64 = u64 & 0xffff0000U;
 
 	/*
 	 * Even if the expression is written as '& ~', which makes the
@@ -43,27 +50,29 @@ scale(unsigned long long x) {
 	 * the code to a 64-bit platform, the upper 32 bits are preserved.
 	 */
 	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned int' to 'unsigned long long', op '&' [309] */
-	if ((x & ~0xffffU) != 0)
-		return 16;
+	u64 = u64 & ~0xffffU;
 
 	/*
 	 * Casting the integer constant to the proper type removes all
 	 * ambiguities about the programmer's intention.
 	 */
-	if ((x & (unsigned long long)~0xffffU) != 0)
-		return 16;
+	u64 = u64 & (unsigned long long)~0xffffU;
 
 	/*
 	 * In the remaining cases, the constant does not have its most
 	 * significant bit set, therefore there is no ambiguity.
 	 */
-	if ((x & 0xff00) != 0)
-		return 8;
-	if ((x & 0xf0) != 0)
-		return 4;
-	if ((x & 0xc) != 0)
-		return 2;
-	if ((x & 0x2) != 0)
-		return 1;
-	return (int)(x & 0x1);
+	u64 = u64 & 0xff00;
+	u64 = u64 & 0xf0;
+	u64 = u64 & 0xc;
+	u64 = u64 & 0x2;
+	u64 = u64 & 0x1;
+
+	u8 = u8 & 0x7f;
+	u8 = u8 & 0x80;
+	u8 = u8 & -0x80;
+	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned char' to 'int', op '&' [309] */
+	u8 = u8 & (u8_t)-0x80;
+	/* expect+1: warning: extra bits set to 0 in conversion of 'unsigned char' to 'int', op '&' [309] */
+	u8 = u8 & (u8_t)-0x80U;
 }

Reply via email to