Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/xlint/lint1 lint: casting to a struct is not allowed...



details:   https://anonhg.NetBSD.org/src/rev/177d7f403e6f
branches:  trunk
changeset: 1022726:177d7f403e6f
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Aug 03 18:38:02 2021 +0000

description:
lint: casting to a struct is not allowed in C99, only with GCC

diffstat:

 tests/usr.bin/xlint/lint1/expr_cast.c   |  18 +++++++++---------
 tests/usr.bin/xlint/lint1/expr_cast.exp |   3 ++-
 tests/usr.bin/xlint/lint1/msg_214.c     |  11 ++++++++---
 tests/usr.bin/xlint/lint1/msg_214.exp   |   2 +-
 usr.bin/xlint/lint1/decl.c              |   6 +++---
 usr.bin/xlint/lint1/tree.c              |   7 ++++---
 6 files changed, 27 insertions(+), 20 deletions(-)

diffs (128 lines):

diff -r a62cd5f23fdb -r 177d7f403e6f tests/usr.bin/xlint/lint1/expr_cast.c
--- a/tests/usr.bin/xlint/lint1/expr_cast.c     Tue Aug 03 18:03:54 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_cast.c     Tue Aug 03 18:38:02 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expr_cast.c,v 1.1 2021/08/03 18:03:54 rillig Exp $     */
+/*     $NetBSD: expr_cast.c,v 1.2 2021/08/03 18:38:02 rillig Exp $     */
 # 3 "expr_cast.c"
 
 /*
@@ -6,8 +6,14 @@
  *
  * K&R C does not mention any restrictions on the target type.
  * C90 requires both the source type and the target type to be scalar.
+ *
+ * GCC allows casting to a struct type but there is no documentation about
+ * it at https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html.  See
+ * c-typeck.c, function build_c_cast, RECORD_OR_UNION_TYPE_P.
  */
 
+/* lint1-flags: -Sw */
+
 struct S {
        int member;
 };
@@ -20,14 +26,8 @@
        } local = {
                0.0
        };
-       /* expect-3: warning: 'local' set but not used in function 'cast' [191] */
-       /*
-        * ^^ XXX: The variable _is_ used, but only in a semantically wrong
-        * expression.  Lint should rather warn about the invalid cast in the
-        * 'return' statement, but since all C compilers since C90 are
-        * required to detect this already, there is no point in duplicating
-        * that work.
-        */
 
+       /* expect+2: error: invalid cast from 'struct S' to 'struct S' [147] */
+       /* expect+1: warning: function cast expects to return value [214] */
        return (struct S)local;
 }
diff -r a62cd5f23fdb -r 177d7f403e6f tests/usr.bin/xlint/lint1/expr_cast.exp
--- a/tests/usr.bin/xlint/lint1/expr_cast.exp   Tue Aug 03 18:03:54 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/expr_cast.exp   Tue Aug 03 18:38:02 2021 +0000
@@ -1,1 +1,2 @@
-expr_cast.c(20): warning: 'local' set but not used in function 'cast' [191]
+expr_cast.c(32): error: invalid cast from 'struct S' to 'struct S' [147]
+expr_cast.c(32): warning: function cast expects to return value [214]
diff -r a62cd5f23fdb -r 177d7f403e6f tests/usr.bin/xlint/lint1/msg_214.c
--- a/tests/usr.bin/xlint/lint1/msg_214.c       Tue Aug 03 18:03:54 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_214.c       Tue Aug 03 18:38:02 2021 +0000
@@ -1,7 +1,12 @@
-/*     $NetBSD: msg_214.c,v 1.2 2021/02/21 09:07:58 rillig Exp $       */
+/*     $NetBSD: msg_214.c,v 1.3 2021/08/03 18:38:02 rillig Exp $       */
 # 3 "msg_214.c"
 
 // Test for message: function %s expects to return value [214]
 
-TODO: "Add example code that triggers the above message." /* expect: 249 */
-TODO: "Add example code that almost triggers the above message."
+int
+int_function(void)
+{
+       /* TODO: add quotes around '%s' */
+       /* expect+1: warning: function int_function expects to return value [214] */
+       return;
+}
diff -r a62cd5f23fdb -r 177d7f403e6f tests/usr.bin/xlint/lint1/msg_214.exp
--- a/tests/usr.bin/xlint/lint1/msg_214.exp     Tue Aug 03 18:03:54 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_214.exp     Tue Aug 03 18:38:02 2021 +0000
@@ -1,1 +1,1 @@
-msg_214.c(6): error: syntax error ':' [249]
+msg_214.c(11): warning: function int_function expects to return value [214]
diff -r a62cd5f23fdb -r 177d7f403e6f usr.bin/xlint/lint1/decl.c
--- a/usr.bin/xlint/lint1/decl.c        Tue Aug 03 18:03:54 2021 +0000
+++ b/usr.bin/xlint/lint1/decl.c        Tue Aug 03 18:38:02 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: decl.c,v 1.217 2021/08/01 18:37:29 rillig Exp $ */
+/* $NetBSD: decl.c,v 1.218 2021/08/03 18:38:02 rillig Exp $ */
 
 /*
  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: decl.c,v 1.217 2021/08/01 18:37:29 rillig Exp $");
+__RCSID("$NetBSD: decl.c,v 1.218 2021/08/03 18:38:02 rillig Exp $");
 #endif
 
 #include <sys/param.h>
@@ -2230,7 +2230,7 @@
                                return false;
                }
 
-               /* dont check prototypes for traditional */
+               /* don't check prototypes for traditional */
                if (t == FUNC && !tflag) {
                        if (tp1->t_proto && tp2->t_proto) {
                                if (!eqargs(tp1, tp2, dowarn))
diff -r a62cd5f23fdb -r 177d7f403e6f usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Tue Aug 03 18:03:54 2021 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Tue Aug 03 18:38:02 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.327 2021/08/03 17:44:59 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.328 2021/08/03 18:38:02 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: tree.c,v 1.327 2021/08/03 17:44:59 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.328 2021/08/03 18:38:02 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -3414,7 +3414,8 @@
                error(329, type_name(tn->tn_type), type_name(tp));
                return NULL;
        } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
-               if (!Sflag || nt == ARRAY || nt == FUNC)
+               /* Casting to a struct is an undocumented GCC extension. */
+               if (!(gflag && nt == STRUCT))
                        goto invalid_cast;
        } else if (ot == STRUCT || ot == UNION) {
                goto invalid_cast;



Home | Main Index | Thread Index | Old Index