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: allow initialization of struct wit...



details:   https://anonhg.NetBSD.org/src/rev/dc6e5c25a2a7
branches:  trunk
changeset: 1022906:dc6e5c25a2a7
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Aug 14 12:46:23 2021 +0000

description:
lint: allow initialization of struct with constant member

The operator INIT, just like RETURN and FARG, initializes an object with
an expression.  The target object of such an initialization may be a
struct with constant members.

The operator ASSIGN, on the other hand, is entirely different.  It
overwrites the existing value of the object, and this is not allowed for
structs that have a constant member.  Therefore it was wrong to use the
operator ASSIGN for initialization.

diffstat:

 tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp         |   8 +++---
 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp  |   2 +-
 tests/usr.bin/xlint/lint1/d_init_array_using_string.exp |   8 +++---
 tests/usr.bin/xlint/lint1/msg_115.c                     |   8 +++---
 tests/usr.bin/xlint/lint1/msg_115.exp                   |   1 -
 tests/usr.bin/xlint/lint1/msg_124.exp                   |   6 ++--
 tests/usr.bin/xlint/lint1/msg_164.c                     |   5 ++-
 tests/usr.bin/xlint/lint1/msg_164.exp                   |   6 ++--
 tests/usr.bin/xlint/lint1/msg_242.c                     |  13 +++++++++--
 tests/usr.bin/xlint/lint1/msg_242.exp                   |   6 +++-
 usr.bin/xlint/lint1/init.c                              |  18 +++++++---------
 usr.bin/xlint/lint1/ops.def                             |   4 +-
 usr.bin/xlint/lint1/tree.c                              |  11 +++++----
 13 files changed, 52 insertions(+), 44 deletions(-)

diffs (294 lines):

diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp
--- a/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp   Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool_strict.exp   Sat Aug 14 12:46:23 2021 +0000
@@ -144,10 +144,10 @@
 d_c99_bool_strict.c(583): error: operands of ':' have incompatible types (unsigned int != _Bool) [107]
 d_c99_bool_strict.c(589): warning: expression has null effect [129]
 d_c99_bool_strict.c(590): warning: expression has null effect [129]
-d_c99_bool_strict.c(603): error: operands of '=' have incompatible types (char != _Bool) [107]
-d_c99_bool_strict.c(604): error: operands of '=' have incompatible types (int != _Bool) [107]
-d_c99_bool_strict.c(605): error: operands of '=' have incompatible types (double != _Bool) [107]
-d_c99_bool_strict.c(606): error: operands of '=' have incompatible types (pointer != _Bool) [107]
+d_c99_bool_strict.c(603): error: operands of 'init' have incompatible types (char != _Bool) [107]
+d_c99_bool_strict.c(604): error: operands of 'init' have incompatible types (int != _Bool) [107]
+d_c99_bool_strict.c(605): error: operands of 'init' have incompatible types (double != _Bool) [107]
+d_c99_bool_strict.c(606): error: operands of 'init' have incompatible types (pointer != _Bool) [107]
 d_c99_bool_strict.c(623): error: operands of '=' have incompatible types (int != _Bool) [107]
 d_c99_bool_strict.c(624): error: operands of '=' have incompatible types (int != _Bool) [107]
 d_c99_bool_strict.c(625): error: operands of '=' have incompatible types (int != _Bool) [107]
diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp
--- a/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp    Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_c99_bool_strict_syshdr.exp    Sat Aug 14 12:46:23 2021 +0000
@@ -1,6 +1,6 @@
 d_c99_bool_strict_syshdr.c(43): error: controlling expression must be bool, not 'int' [333]
 d_c99_bool_strict_syshdr.c(53): error: controlling expression must be bool, not 'int' [333]
-d_c99_bool_strict_syshdr.c(87): error: operands of '=' have incompatible types (_Bool != int) [107]
+d_c99_bool_strict_syshdr.c(87): error: operands of 'init' have incompatible types (_Bool != int) [107]
 d_c99_bool_strict_syshdr.c(162): error: return value type mismatch (_Bool) and (int) [211]
 d_c99_bool_strict_syshdr.c(175): error: operand of '!' must be bool, not 'int' [330]
 d_c99_bool_strict_syshdr.c(175): warning: function 'str_equal_bad' expects to return value [214]
diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/d_init_array_using_string.exp
--- a/tests/usr.bin/xlint/lint1/d_init_array_using_string.exp   Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/d_init_array_using_string.exp   Sat Aug 14 12:46:23 2021 +0000
@@ -1,7 +1,7 @@
-d_init_array_using_string.c(16): warning: illegal pointer combination (pointer to const char) and (pointer to int), op = [124]
-d_init_array_using_string.c(17): warning: illegal pointer combination (pointer to const int) and (pointer to char), op = [124]
-d_init_array_using_string.c(34): warning: illegal pointer combination [184]
-d_init_array_using_string.c(35): warning: illegal pointer combination [184]
+d_init_array_using_string.c(16): warning: illegal pointer combination (pointer to const char) and (pointer to int), op init [124]
+d_init_array_using_string.c(17): warning: illegal pointer combination (pointer to const int) and (pointer to char), op init [124]
+d_init_array_using_string.c(34): warning: illegal pointer combination (pointer to const char) and (pointer to int), op init [124]
+d_init_array_using_string.c(35): warning: illegal pointer combination (pointer to const int) and (pointer to char), op init [124]
 d_init_array_using_string.c(59): error: cannot initialize 'array[10] of const char' from 'pointer to int' [185]
 d_init_array_using_string.c(60): error: cannot initialize 'array[10] of const int' from 'pointer to char' [185]
 d_init_array_using_string.c(69): warning: non-null byte ignored in string initializer [187]
diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/msg_115.c
--- a/tests/usr.bin/xlint/lint1/msg_115.c       Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_115.c       Sat Aug 14 12:46:23 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_115.c,v 1.7 2021/08/10 20:43:13 rillig Exp $       */
+/*     $NetBSD: msg_115.c,v 1.8 2021/08/14 12:46:24 rillig Exp $       */
 # 3 "msg_115.c"
 
 // Test for message: %soperand of '%s' must be modifiable lvalue [115]
@@ -26,9 +26,9 @@
 const_member
 initialize_const_struct_member(void)
 {
-       /* FIXME: In an initialization, const members can be assigned. */
-       /* expect+1: warning: left operand of '=' must be modifiable lvalue [115] */
+       /* In a simple initialization, const members can be assigned. */
        const_member cm1 = (const_member) { 12345 };
+
        if (cm1.member != 0)
                /* In a function call, const members can be assigned. */
                take_const_member(cm1);
@@ -36,7 +36,7 @@
        struct {
                const_member member;
        } cm2 = {
-           /* In an initialization, const members can be assigned. */
+           /* In a nested initialization, const members can be assigned. */
            cm1,
        };
        if (cm2.member.member != 0) {
diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/msg_115.exp
--- a/tests/usr.bin/xlint/lint1/msg_115.exp     Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_115.exp     Sat Aug 14 12:46:23 2021 +0000
@@ -5,4 +5,3 @@
 msg_115.c(14): warning: left operand of '/=' must be modifiable lvalue [115]
 msg_115.c(15): warning: left operand of '%=' must be modifiable lvalue [115]
 msg_115.c(16): warning: operand of 'x++' must be modifiable lvalue [115]
-msg_115.c(31): warning: left operand of '=' must be modifiable lvalue [115]
diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/msg_124.exp
--- a/tests/usr.bin/xlint/lint1/msg_124.exp     Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_124.exp     Sat Aug 14 12:46:23 2021 +0000
@@ -1,6 +1,6 @@
-msg_124.c(18): warning: illegal pointer combination (pointer to function(int) returning void) and (pointer to int), op = [124]
-msg_124.c(19): warning: illegal pointer combination (pointer to function(pointer to function(int) returning void) returning pointer to function(int) returning void) and (pointer to int), op = [124]
-msg_124.c(20): warning: illegal pointer combination (pointer to function(pointer to const char, ...) returning int) and (pointer to int), op = [124]
+msg_124.c(18): warning: illegal pointer combination (pointer to function(int) returning void) and (pointer to int), op init [124]
+msg_124.c(19): warning: illegal pointer combination (pointer to function(pointer to function(int) returning void) returning pointer to function(int) returning void) and (pointer to int), op init 
[124]
+msg_124.c(20): warning: illegal pointer combination (pointer to function(pointer to const char, ...) returning int) and (pointer to int), op init [124]
 msg_124.c(32): warning: ANSI C forbids comparison of 'void *' with function pointer [274]
 msg_124.c(33): warning: illegal pointer combination (pointer to const char) and (pointer to const int), op == [124]
 msg_124.c(34): warning: illegal pointer combination (pointer to const char) and (pointer to function(int) returning void), op == [124]
diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/msg_164.c
--- a/tests/usr.bin/xlint/lint1/msg_164.c       Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_164.c       Sat Aug 14 12:46:23 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_164.c,v 1.3 2021/01/31 11:12:07 rillig Exp $       */
+/*     $NetBSD: msg_164.c,v 1.4 2021/08/14 12:46:24 rillig Exp $       */
 # 3 "msg_164.c"
 
 // Test for message: assignment of negative constant to unsigned type [164]
@@ -6,7 +6,8 @@
 void
 example(void)
 {
-       unsigned char uch = -3;         /* expect: 164 */
+       /* expect+1: warning: initialization of unsigned with negative constant [221] */
+       unsigned char uch = -3;
 
        uch = -5;                       /* expect: 164 */
        uch += -7;                      /* expect: 222 */
diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/msg_164.exp
--- a/tests/usr.bin/xlint/lint1/msg_164.exp     Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_164.exp     Sat Aug 14 12:46:23 2021 +0000
@@ -1,4 +1,4 @@
-msg_164.c(9): warning: assignment of negative constant to unsigned type [164]
-msg_164.c(11): warning: assignment of negative constant to unsigned type [164]
-msg_164.c(12): warning: conversion of negative constant to unsigned type [222]
+msg_164.c(10): warning: initialization of unsigned with negative constant [221]
+msg_164.c(12): warning: assignment of negative constant to unsigned type [164]
 msg_164.c(13): warning: conversion of negative constant to unsigned type [222]
+msg_164.c(14): warning: conversion of negative constant to unsigned type [222]
diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/msg_242.c
--- a/tests/usr.bin/xlint/lint1/msg_242.c       Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_242.c       Sat Aug 14 12:46:23 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: msg_242.c,v 1.3 2021/02/27 18:01:29 rillig Exp $       */
+/*     $NetBSD: msg_242.c,v 1.4 2021/08/14 12:46:24 rillig Exp $       */
 # 3 "msg_242.c"
 
 // Test for message: combination of '%s' and '%s', op %s [242]
@@ -16,10 +16,17 @@
 example(enum E e, int i)
 {
        enum E e2 = e;
-       enum E e3 = i;          /* expect: 242 */
-       int i2 = e;             /* expect: 242 */
+       /* expect+1: warning: initialization of 'enum E' with 'int' [277] */
+       enum E e3 = i;
+       /* expect+1: warning: initialization of 'int' with 'enum E' [277] */
+       int i2 = e;
        int i3 = i;
 
+       /* expect+1: warning: combination of 'enum E' and 'int', op = [242] */
+       e3 = i;
+       /* expect+1: warning: combination of 'int' and 'enum E', op = [242] */
+       i2 = e;
+
        sink_enum(e2);
        sink_enum(e3);
        sink_int(i2);
diff -r e705db32f1a4 -r dc6e5c25a2a7 tests/usr.bin/xlint/lint1/msg_242.exp
--- a/tests/usr.bin/xlint/lint1/msg_242.exp     Fri Aug 13 21:04:44 2021 +0000
+++ b/tests/usr.bin/xlint/lint1/msg_242.exp     Sat Aug 14 12:46:23 2021 +0000
@@ -1,2 +1,4 @@
-msg_242.c(19): warning: combination of 'enum E' and 'int', op = [242]
-msg_242.c(20): warning: combination of 'int' and 'enum E', op = [242]
+msg_242.c(20): warning: initialization of 'enum E' with 'int' [277]
+msg_242.c(22): warning: initialization of 'int' with 'enum E' [277]
+msg_242.c(26): warning: combination of 'enum E' and 'int', op = [242]
+msg_242.c(28): warning: combination of 'int' and 'enum E', op = [242]
diff -r e705db32f1a4 -r dc6e5c25a2a7 usr.bin/xlint/lint1/init.c
--- a/usr.bin/xlint/lint1/init.c        Fri Aug 13 21:04:44 2021 +0000
+++ b/usr.bin/xlint/lint1/init.c        Sat Aug 14 12:46:23 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: init.c,v 1.207 2021/08/10 20:43:12 rillig Exp $        */
+/*     $NetBSD: init.c,v 1.208 2021/08/14 12:46:23 rillig Exp $        */
 
 /*
  * Copyright (c) 1994, 1995 Jochen Pohl
@@ -38,7 +38,7 @@
 
 #include <sys/cdefs.h>
 #if defined(__RCSID) && !defined(lint)
-__RCSID("$NetBSD: init.c,v 1.207 2021/08/10 20:43:12 rillig Exp $");
+__RCSID("$NetBSD: init.c,v 1.208 2021/08/14 12:46:23 rillig Exp $");
 #endif
 
 #include <stdlib.h>
@@ -812,12 +812,11 @@
 }
 
 /*
- * An object with automatic storage duration that has a single initializer
- * expression without braces and is not an array is initialized by delegating
- * to the ASSIGN operator.
+ * Initialize an object with automatic storage duration that has an
+ * initializer expression without braces.
  */
 static bool
-initialization_expr_using_assign(struct initialization *in, tnode_t *rn)
+initialization_expr_using_op(struct initialization *in, tnode_t *rn)
 {
        tnode_t *ln, *tn;
 
@@ -828,13 +827,12 @@
        if (in->in_sym->s_type->t_tspec == ARRAY)
                return false;
 
-       debug_step("handing over to ASSIGN");
+       debug_step("handing over to INIT");
 
        ln = build_name(in->in_sym, 0);
        ln->tn_type = expr_unqualified_type(ln->tn_type);
 
-       /* TODO: allow 'const' on the left-hand side; see msg_115.c */
-       tn = build_binary(ln, ASSIGN, rn);
+       tn = build_binary(ln, INIT, rn);
        expr(tn, false, false, false, false);
 
        return true;
@@ -899,7 +897,7 @@
 
        if (tn == NULL)
                goto advance;
-       if (initialization_expr_using_assign(in, tn))
+       if (initialization_expr_using_op(in, tn))
                goto done;
        if (initialization_init_array_using_string(in, tn))
                goto advance;
diff -r e705db32f1a4 -r dc6e5c25a2a7 usr.bin/xlint/lint1/ops.def
--- a/usr.bin/xlint/lint1/ops.def       Fri Aug 13 21:04:44 2021 +0000
+++ b/usr.bin/xlint/lint1/ops.def       Sat Aug 14 12:46:23 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ops.def,v 1.23 2021/08/10 16:59:28 rillig Exp $ */
+/*     $NetBSD: ops.def,v 1.24 2021/08/14 12:46:23 rillig Exp $ */
 
 begin_ops()
 
@@ -106,7 +106,7 @@
 
 /* INIT, CASE and FARG are pseudo operators that don't appear in the tree. */
 /*     name    repr            b l b B i c a s f v t b s l r p c e e = */
-op(    INIT,   "init",         1,-,1,-,-,-,-,-,-,-,-,-,-,-,-,-,-,1,-,-)
+op(    INIT,   "init",         1,-,1,-,-,-,-,-,-,-,-,-,1,-,-,-,-,1,-,-)
 op(    CASE,   "case",         -,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-)
 /*
  * FARG is only used temporarily in check_prototype_argument to check type
diff -r e705db32f1a4 -r dc6e5c25a2a7 usr.bin/xlint/lint1/tree.c
--- a/usr.bin/xlint/lint1/tree.c        Fri Aug 13 21:04:44 2021 +0000
+++ b/usr.bin/xlint/lint1/tree.c        Sat Aug 14 12:46:23 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tree.c,v 1.332 2021/08/10 20:43:12 rillig Exp $        */
+/*     $NetBSD: tree.c,v 1.333 2021/08/14 12:46:23 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.332 2021/08/10 20:43:12 rillig Exp $");
+__RCSID("$NetBSD: tree.c,v 1.333 2021/08/14 12:46:23 rillig Exp $");
 #endif
 
 #include <float.h>
@@ -513,7 +513,7 @@
         * union member, or if it is not to be assigned to the left operand
         */
        if (mp->m_binary && op != ARROW && op != POINT &&
-           op != ASSIGN && op != RETURN) {
+           op != ASSIGN && op != RETURN && op != INIT) {
                rn = promote(op, false, rn);
        }
 
@@ -587,6 +587,7 @@
        case XORASS:
        case ORASS:
        case RETURN:
+       case INIT:
                ntn = build_assignment(op, ln, rn);
                break;
        case COMMA:
@@ -1520,7 +1521,6 @@
 
        if (lt == PTR && rt == PTR) {
                switch (op) {
-               case INIT:
                case RETURN:
                        warn_incompatible_pointers(NULL, ltp, rtp);
                        break;
@@ -2876,7 +2876,8 @@
                        rn = fold(rn);
        }
 
-       if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
+       if ((op == ASSIGN || op == RETURN || op == INIT) &&
+           (lt == STRUCT || rt == STRUCT)) {
                lint_assert(lt == rt);
                lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
                if (is_incomplete(ln->tn_type)) {



Home | Main Index | Thread Index | Old Index