Source-Changes-HG archive

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

[src/trunk]: src/tests/usr.bin/xlint/lint1 tests/lint: add test for narrowing...



details:   https://anonhg.NetBSD.org/src/rev/73355c87e89c
branches:  trunk
changeset: 953198:73355c87e89c
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Feb 28 21:39:17 2021 +0000

description:
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

diffstat:

 tests/usr.bin/xlint/lint1/msg_132.c   |  63 +++++++++++++++++++++++++++++++++-
 tests/usr.bin/xlint/lint1/msg_132.exp |  13 ++++++-
 2 files changed, 72 insertions(+), 4 deletions(-)

diffs (88 lines):

diff -r 86de8a79fe28 -r 73355c87e89c tests/usr.bin/xlint/lint1/msg_132.c
--- a/tests/usr.bin/xlint/lint1/msg_132.c       Sun Feb 28 21:34:34 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_132.c       Sun Feb 28 21:39:17 2021 +0000
@@ -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;
+}
diff -r 86de8a79fe28 -r 73355c87e89c tests/usr.bin/xlint/lint1/msg_132.exp
--- a/tests/usr.bin/xlint/lint1/msg_132.exp     Sun Feb 28 21:34:34 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_132.exp     Sun Feb 28 21:39:17 2021 +0000
@@ -1,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]



Home | Main Index | Thread Index | Old Index