Module Name:    src
Committed By:   rillig
Date:           Sun Feb 28 21:39:17 UTC 2021

Modified Files:
        src/tests/usr.bin/xlint/lint1: msg_132.c msg_132.exp

Log Message:
tests/lint: add test for narrowing conversions

Lint can warn about narrowing conversions, it just doesn't do so by
default.

The option -a (which is included in the default LINTFLAGS in sys.mk)
only reports narrowing conversions from 'long' or larger.  To get
warnings about all possible narrowing conversions, the option -a has to
be given more than once.

PR bin/14531


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/tests/usr.bin/xlint/lint1/msg_132.c
cvs rdiff -u -r1.1 -r1.2 src/tests/usr.bin/xlint/lint1/msg_132.exp

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_132.c
diff -u src/tests/usr.bin/xlint/lint1/msg_132.c:1.2 src/tests/usr.bin/xlint/lint1/msg_132.c:1.3
--- src/tests/usr.bin/xlint/lint1/msg_132.c:1.2	Sun Feb 21 09:07:58 2021
+++ src/tests/usr.bin/xlint/lint1/msg_132.c	Sun Feb 28 21:39:17 2021
@@ -1,7 +1,64 @@
-/*	$NetBSD: msg_132.c,v 1.2 2021/02/21 09:07:58 rillig Exp $	*/
+/*	$NetBSD: msg_132.c,v 1.3 2021/02/28 21:39:17 rillig Exp $	*/
 # 3 "msg_132.c"
 
 // Test for message: conversion from '%s' to '%s' may lose accuracy [132]
 
-TODO: "Add example code that triggers the above message." /* expect: 249 */
-TODO: "Add example code that almost triggers the above message."
+/*
+ * NetBSD's default lint flags only include a single -a, which only flags
+ * narrowing conversions from long.  To get warnings for all narrowing
+ * conversions, -aa needs to be given more than once.
+ *
+ * https://gnats.netbsd.org/14531
+ */
+
+/* lint1-extra-flags: -aa */
+
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned int u32;
+typedef unsigned long long u64;
+
+typedef signed char i8;
+typedef signed short i16;
+typedef signed int i32;
+typedef signed long long i64;
+
+void
+convert_unsigned(u8 v8, u16 v16, u32 v32, u64 v64)
+{
+	v8 = v16;		/* expect: 132 */
+	v8 = v32;		/* expect: 132 */
+	v8 = v64;		/* expect: 132 */
+
+	v16 = v8;
+	v16 = v32;		/* expect: 132 */
+	v16 = v64;		/* expect: 132 */
+
+	v32 = v8;
+	v32 = v16;
+	v32 = v64;		/* expect: 132 */
+
+	v64 = v8;
+	v64 = v16;
+	v64 = v32;
+}
+
+void
+convert_signed(i8 v8, i16 v16, i32 v32, i64 v64)
+{
+	v8 = v16;		/* expect: 132 */
+	v8 = v32;		/* expect: 132 */
+	v8 = v64;		/* expect: 132 */
+
+	v16 = v8;
+	v16 = v32;		/* expect: 132 */
+	v16 = v64;		/* expect: 132 */
+
+	v32 = v8;
+	v32 = v16;
+	v32 = v64;		/* expect: 132 */
+
+	v64 = v8;
+	v64 = v16;
+	v64 = v32;
+}

Index: src/tests/usr.bin/xlint/lint1/msg_132.exp
diff -u src/tests/usr.bin/xlint/lint1/msg_132.exp:1.1 src/tests/usr.bin/xlint/lint1/msg_132.exp:1.2
--- src/tests/usr.bin/xlint/lint1/msg_132.exp:1.1	Sat Jan  2 10:22:43 2021
+++ src/tests/usr.bin/xlint/lint1/msg_132.exp	Sun Feb 28 21:39:17 2021
@@ -1 +1,12 @@
-msg_132.c(6): syntax error ':' [249]
+msg_132.c(29): warning: conversion from 'unsigned short' to 'unsigned char' may lose accuracy [132]
+msg_132.c(30): warning: conversion from 'unsigned int' to 'unsigned char' may lose accuracy [132]
+msg_132.c(31): warning: conversion from 'unsigned long long' to 'unsigned char' may lose accuracy [132]
+msg_132.c(34): warning: conversion from 'unsigned int' to 'unsigned short' may lose accuracy [132]
+msg_132.c(35): warning: conversion from 'unsigned long long' to 'unsigned short' may lose accuracy [132]
+msg_132.c(39): warning: conversion from 'unsigned long long' to 'unsigned int' may lose accuracy [132]
+msg_132.c(49): warning: conversion from 'short' to 'signed char' may lose accuracy [132]
+msg_132.c(50): warning: conversion from 'int' to 'signed char' may lose accuracy [132]
+msg_132.c(51): warning: conversion from 'long long' to 'signed char' may lose accuracy [132]
+msg_132.c(54): warning: conversion from 'int' to 'short' may lose accuracy [132]
+msg_132.c(55): warning: conversion from 'long long' to 'short' may lose accuracy [132]
+msg_132.c(59): warning: conversion from 'long long' to 'int' may lose accuracy [132]

Reply via email to