* [PATCH 1/5] bitwise: add testcases
2022-06-27 19:05 ` [PATCH 0/5] allow -1 and compares in bitwise types Luc Van Oostenryck
@ 2022-06-27 19:05 ` Luc Van Oostenryck
2022-06-27 19:05 ` [PATCH 2/5] bitwise: accept all ones as non-restricted value Luc Van Oostenryck
` (5 subsequent siblings)
6 siblings, 0 replies; 18+ messages in thread
From: Luc Van Oostenryck @ 2022-06-27 19:05 UTC (permalink / raw)
To: linux-sparse; +Cc: Linus Torvalds, Bart Van Assche, Luc Van Oostenryck
Currently bitwise types only support bitwise operations
(&, |, ^ and ~) and the constant 0 (since this value is
invariant for all bitwise operations and endianness conversion).
But the incoming series will relax this a little bit.
So, add a few testcases for it.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
validation/bitwise-cmp.c | 32 ++++++++++++++++++++++++++++++++
validation/bitwise-is-signed.c | 22 ++++++++++++++++++++++
validation/linear/bitwise-cmps.c | 18 ++++++++++++++++++
validation/linear/bitwise-cmpu.c | 18 ++++++++++++++++++
4 files changed, 90 insertions(+)
create mode 100644 validation/bitwise-cmp.c
create mode 100644 validation/bitwise-is-signed.c
create mode 100644 validation/linear/bitwise-cmps.c
create mode 100644 validation/linear/bitwise-cmpu.c
diff --git a/validation/bitwise-cmp.c b/validation/bitwise-cmp.c
new file mode 100644
index 000000000000..ca12b5e51e8e
--- /dev/null
+++ b/validation/bitwise-cmp.c
@@ -0,0 +1,32 @@
+#define M 0xffffffff
+
+typedef int __attribute__((bitwise)) b32;
+
+static int eq0(b32 x, b32 y) { return (x == 0); }
+static int eqm(b32 x, b32 y) { return (x == M); }
+static int eqx(b32 x, b32 y) { return (x == y); }
+
+static int ne0(b32 x, b32 y) { return (x != 0); }
+static int nem(b32 x, b32 y) { return (x != M); }
+static int nex(b32 x, b32 y) { return (x != y); }
+
+static int lt0(b32 x, b32 y) { return (x < 0); }
+static int ltm(b32 x, b32 y) { return (x < M); }
+static int ltx(b32 x, b32 y) { return (x < y); }
+
+static int lte0(b32 x, b32 y) { return (x <= 0); }
+static int ltem(b32 x, b32 y) { return (x <= M); }
+static int ltex(b32 x, b32 y) { return (x <= y); }
+
+static int gte0(b32 x, b32 y) { return (x >= 0); }
+static int gtem(b32 x, b32 y) { return (x >= M); }
+static int gtex(b32 x, b32 y) { return (x >= y); }
+
+static int gt0(b32 x, b32 y) { return (x > 0); }
+static int gtm(b32 x, b32 y) { return (x > M); }
+static int gtx(b32 x, b32 y) { return (x > y); }
+
+/*
+ * check-name: bitwise-cmp
+ * check-known-to-fail
+ */
diff --git a/validation/bitwise-is-signed.c b/validation/bitwise-is-signed.c
new file mode 100644
index 000000000000..dd9c147173cd
--- /dev/null
+++ b/validation/bitwise-is-signed.c
@@ -0,0 +1,22 @@
+#define __bitwise __attribute__((bitwise))
+
+#define is_signed_type(type) (((type)-1) <= 0)
+
+typedef signed int __bitwise s;
+typedef unsigned int __bitwise u;
+
+int fos(void);
+int fou(void);
+
+
+int fos(void) { return is_signed_type(s); }
+int fou(void) { return !is_signed_type(u); }
+
+/*
+ * check-name: bitwise-is-signed
+ * check-command: test-linearize -Wno-decl $file
+ * check-known-to-fail
+ *
+ * check-output-ignore
+ * check-output-returns: 1
+ */
diff --git a/validation/linear/bitwise-cmps.c b/validation/linear/bitwise-cmps.c
new file mode 100644
index 000000000000..6122944a42c6
--- /dev/null
+++ b/validation/linear/bitwise-cmps.c
@@ -0,0 +1,18 @@
+typedef signed int __attribute__((bitwise)) bs32;
+
+static int ltu(bs32 x, bs32 y) { return (x < y); }
+static int lteu(bs32 x, bs32 y) { return (x <= y); }
+static int gteu(bs32 x, bs32 y) { return (x >= y); }
+static int gtu(bs32 x, bs32 y) { return (x > y); }
+
+/*
+ * check-name: bitwise-cmps
+ * check-command: test-linearize -Wno-decl $file
+ * check-known-to-fail
+ *
+ * check-output-ignore
+ * check-output-excludes: setb\\.
+ * check-output-excludes: setbe\\.
+ * check-output-excludes: setae\\.
+ * check-output-excludes: seta\\.
+ */
diff --git a/validation/linear/bitwise-cmpu.c b/validation/linear/bitwise-cmpu.c
new file mode 100644
index 000000000000..8932436a7764
--- /dev/null
+++ b/validation/linear/bitwise-cmpu.c
@@ -0,0 +1,18 @@
+typedef unsigned int __attribute__((bitwise)) bu32;
+
+static int ltu(bu32 x, bu32 y) { return (x < y); }
+static int lteu(bu32 x, bu32 y) { return (x <= y); }
+static int gteu(bu32 x, bu32 y) { return (x >= y); }
+static int gtu(bu32 x, bu32 y) { return (x > y); }
+
+/*
+ * check-name: bitwise-cmpu
+ * check-command: test-linearize -Wno-decl $file
+ * check-known-to-fail
+ *
+ * check-output-ignore
+ * check-output-excludes: setlt\\.
+ * check-output-excludes: setlte\\.
+ * check-output-excludes: setgte\\.
+ * check-output-excludes: setgt\\.
+ */
--
2.36.1
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH 2/5] bitwise: accept all ones as non-restricted value
2022-06-27 19:05 ` [PATCH 0/5] allow -1 and compares in bitwise types Luc Van Oostenryck
2022-06-27 19:05 ` [PATCH 1/5] bitwise: add testcases Luc Van Oostenryck
@ 2022-06-27 19:05 ` Luc Van Oostenryck
2022-06-27 23:32 ` Ramsay Jones
2022-06-27 19:05 ` [PATCH 3/5] bitwise: allow compares for bitwise types Luc Van Oostenryck
` (4 subsequent siblings)
6 siblings, 1 reply; 18+ messages in thread
From: Luc Van Oostenryck @ 2022-06-27 19:05 UTC (permalink / raw)
To: linux-sparse; +Cc: Linus Torvalds, Bart Van Assche, Luc Van Oostenryck
Currently, the only value bitwise types can act on is 0
because the this value is anyway invariant for all bitwise
operations and endianness conversions.
But, a bit-pattern of all ones has the same properties and
is also very often used.
So, accept all ones as a valid value for bitwise operations.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
evaluate.c | 2 +-
validation/bitwise-cast.c | 13 +++++++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/evaluate.c b/evaluate.c
index 61f59ee3908e..bcbcdf1ef0cc 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -417,7 +417,7 @@ static int restricted_value(struct expression *v, struct symbol *type)
{
if (v->type != EXPR_VALUE)
return 1;
- if (v->value != 0)
+ if (v->value != 0 && v->value != bits_mask(type->bit_size))
return 1;
return 0;
}
diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c
index 0583461cb745..1075a3e9410c 100644
--- a/validation/bitwise-cast.c
+++ b/validation/bitwise-cast.c
@@ -35,6 +35,19 @@ static __be32 quuy(void)
return (__attribute__((force)) __be32) 1730;
}
+/* Implicit casts of all ones, legal */
+static __be32 foo1(void)
+{
+ __be32 x = 0xffffffff;
+ return x;
+}
+
+/* Explicit cast of all ones, legal */
+static __be32 bar1(void)
+{
+ return (__be32)0xffffffff;
+}
+
/*
* check-name: conversions to bitwise types
* check-command: sparse -Wbitwise $file
--
2.36.1
^ permalink raw reply related [flat|nested] 18+ messages in thread
* Re: [PATCH 2/5] bitwise: accept all ones as non-restricted value
2022-06-27 19:05 ` [PATCH 2/5] bitwise: accept all ones as non-restricted value Luc Van Oostenryck
@ 2022-06-27 23:32 ` Ramsay Jones
0 siblings, 0 replies; 18+ messages in thread
From: Ramsay Jones @ 2022-06-27 23:32 UTC (permalink / raw)
To: Luc Van Oostenryck, linux-sparse; +Cc: Linus Torvalds, Bart Van Assche
On 27/06/2022 20:05, Luc Van Oostenryck wrote:
> Currently, the only value bitwise types can act on is 0
> because the this value is anyway invariant for all bitwise
s/the this/this/
s/anyway//
ATB,
Ramsay Jones
> operations and endianness conversions.
>
> But, a bit-pattern of all ones has the same properties and
> is also very often used.
>
> So, accept all ones as a valid value for bitwise operations.
>
> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
> ---
> evaluate.c | 2 +-
> validation/bitwise-cast.c | 13 +++++++++++++
> 2 files changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/evaluate.c b/evaluate.c
> index 61f59ee3908e..bcbcdf1ef0cc 100644
> --- a/evaluate.c
> +++ b/evaluate.c
> @@ -417,7 +417,7 @@ static int restricted_value(struct expression *v, struct symbol *type)
> {
> if (v->type != EXPR_VALUE)
> return 1;
> - if (v->value != 0)
> + if (v->value != 0 && v->value != bits_mask(type->bit_size))
> return 1;
> return 0;
> }
> diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c
> index 0583461cb745..1075a3e9410c 100644
> --- a/validation/bitwise-cast.c
> +++ b/validation/bitwise-cast.c
> @@ -35,6 +35,19 @@ static __be32 quuy(void)
> return (__attribute__((force)) __be32) 1730;
> }
>
> +/* Implicit casts of all ones, legal */
> +static __be32 foo1(void)
> +{
> + __be32 x = 0xffffffff;
> + return x;
> +}
> +
> +/* Explicit cast of all ones, legal */
> +static __be32 bar1(void)
> +{
> + return (__be32)0xffffffff;
> +}
> +
> /*
> * check-name: conversions to bitwise types
> * check-command: sparse -Wbitwise $file
^ permalink raw reply [flat|nested] 18+ messages in thread
* [PATCH 3/5] bitwise: allow compares for bitwise types
2022-06-27 19:05 ` [PATCH 0/5] allow -1 and compares in bitwise types Luc Van Oostenryck
2022-06-27 19:05 ` [PATCH 1/5] bitwise: add testcases Luc Van Oostenryck
2022-06-27 19:05 ` [PATCH 2/5] bitwise: accept all ones as non-restricted value Luc Van Oostenryck
@ 2022-06-27 19:05 ` Luc Van Oostenryck
2022-06-27 19:20 ` Linus Torvalds
2022-06-27 23:34 ` Ramsay Jones
2022-06-27 19:05 ` [PATCH 4/5] bitwise: do not remove the signedness of " Luc Van Oostenryck
` (3 subsequent siblings)
6 siblings, 2 replies; 18+ messages in thread
From: Luc Van Oostenryck @ 2022-06-27 19:05 UTC (permalink / raw)
To: linux-sparse; +Cc: Linus Torvalds, Bart Van Assche, Luc Van Oostenryck
Currently, bitwise types are restricted to bitwise operations
(&, |, ^ and ~) as well as equality comparisons.
This patch makes the others comparisons valid for bitwise types
too.
Warning: This change make sense in the context of [1] but
doesn't make sense for the 'main' bitwise types:
__be32 and friends.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
evaluate.c | 4 ++++
validation/bitwise-cmp.c | 1 -
validation/linear/bitwise-cmps.c | 1 -
3 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/evaluate.c b/evaluate.c
index bcbcdf1ef0cc..bb8c0caa905a 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -435,6 +435,10 @@ static int restricted_binop(int op, struct symbol *type)
case '^':
case '?':
return 2; /* keep fouled */
+ case '<':
+ case '>':
+ case SPECIAL_LTE:
+ case SPECIAL_GTE:
case SPECIAL_EQUAL:
case SPECIAL_NOTEQUAL:
return 3; /* warn if fouled */
diff --git a/validation/bitwise-cmp.c b/validation/bitwise-cmp.c
index ca12b5e51e8e..8c3e6894072d 100644
--- a/validation/bitwise-cmp.c
+++ b/validation/bitwise-cmp.c
@@ -28,5 +28,4 @@ static int gtx(b32 x, b32 y) { return (x > y); }
/*
* check-name: bitwise-cmp
- * check-known-to-fail
*/
diff --git a/validation/linear/bitwise-cmps.c b/validation/linear/bitwise-cmps.c
index 6122944a42c6..f83ab7fe47db 100644
--- a/validation/linear/bitwise-cmps.c
+++ b/validation/linear/bitwise-cmps.c
@@ -8,7 +8,6 @@ static int gtu(bs32 x, bs32 y) { return (x > y); }
/*
* check-name: bitwise-cmps
* check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
*
* check-output-ignore
* check-output-excludes: setb\\.
--
2.36.1
^ permalink raw reply related [flat|nested] 18+ messages in thread
* Re: [PATCH 3/5] bitwise: allow compares for bitwise types
2022-06-27 19:05 ` [PATCH 3/5] bitwise: allow compares for bitwise types Luc Van Oostenryck
@ 2022-06-27 19:20 ` Linus Torvalds
2022-06-27 23:34 ` Ramsay Jones
1 sibling, 0 replies; 18+ messages in thread
From: Linus Torvalds @ 2022-06-27 19:20 UTC (permalink / raw)
To: Luc Van Oostenryck; +Cc: Sparse Mailing-list, Bart Van Assche
On Mon, Jun 27, 2022 at 12:05 PM Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
>
> Currently, bitwise types are restricted to bitwise operations
> (&, |, ^ and ~) as well as equality comparisons.
>
> This patch makes the others comparisons valid for bitwise types
> too.
>
> Warning: This change make sense in the context of [1] but
> doesn't make sense for the 'main' bitwise types:
> __be32 and friends.
Yeah, this is wrong.
It will literally break one of the use-cases, which is endianness comparisons.
You cannot compare values in the wrong endianness for greater-than or
less-than, because you will get the wrong answer - the ordering is
different in different byte-orders.
But comparing for equality (and inequality) is fine, and we actually
do that in the kernel (ie you can take a big-endian value, and compare
it with another big-endian value for being equal, without converting
it to the local CPU endianness).
Now, comparing the *constants* 0 and all-ones is fine. They are
smaller than (and larger than) all other values, regardless of any
byte/bit order issues.
So I think that really needs to check that one (or both) sides are the
magic constants.
Linus
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH 3/5] bitwise: allow compares for bitwise types
2022-06-27 19:05 ` [PATCH 3/5] bitwise: allow compares for bitwise types Luc Van Oostenryck
2022-06-27 19:20 ` Linus Torvalds
@ 2022-06-27 23:34 ` Ramsay Jones
1 sibling, 0 replies; 18+ messages in thread
From: Ramsay Jones @ 2022-06-27 23:34 UTC (permalink / raw)
To: Luc Van Oostenryck, linux-sparse; +Cc: Linus Torvalds, Bart Van Assche
On 27/06/2022 20:05, Luc Van Oostenryck wrote:
> Currently, bitwise types are restricted to bitwise operations
> (&, |, ^ and ~) as well as equality comparisons.
>
> This patch makes the others comparisons valid for bitwise types
> too.
>
> Warning: This change make sense in the context of [1] but
The [1] reference seems to be missing :)
ATB,
Ramsay Jones
> doesn't make sense for the 'main' bitwise types:
> __be32 and friends.
> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
> ---
> evaluate.c | 4 ++++
> validation/bitwise-cmp.c | 1 -
> validation/linear/bitwise-cmps.c | 1 -
> 3 files changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/evaluate.c b/evaluate.c
> index bcbcdf1ef0cc..bb8c0caa905a 100644
> --- a/evaluate.c
> +++ b/evaluate.c
> @@ -435,6 +435,10 @@ static int restricted_binop(int op, struct symbol *type)
> case '^':
> case '?':
> return 2; /* keep fouled */
> + case '<':
> + case '>':
> + case SPECIAL_LTE:
> + case SPECIAL_GTE:
> case SPECIAL_EQUAL:
> case SPECIAL_NOTEQUAL:
> return 3; /* warn if fouled */
> diff --git a/validation/bitwise-cmp.c b/validation/bitwise-cmp.c
> index ca12b5e51e8e..8c3e6894072d 100644
> --- a/validation/bitwise-cmp.c
> +++ b/validation/bitwise-cmp.c
> @@ -28,5 +28,4 @@ static int gtx(b32 x, b32 y) { return (x > y); }
>
> /*
> * check-name: bitwise-cmp
> - * check-known-to-fail
> */
> diff --git a/validation/linear/bitwise-cmps.c b/validation/linear/bitwise-cmps.c
> index 6122944a42c6..f83ab7fe47db 100644
> --- a/validation/linear/bitwise-cmps.c
> +++ b/validation/linear/bitwise-cmps.c
> @@ -8,7 +8,6 @@ static int gtu(bs32 x, bs32 y) { return (x > y); }
> /*
> * check-name: bitwise-cmps
> * check-command: test-linearize -Wno-decl $file
> - * check-known-to-fail
> *
> * check-output-ignore
> * check-output-excludes: setb\\.
^ permalink raw reply [flat|nested] 18+ messages in thread
* [PATCH 4/5] bitwise: do not remove the signedness of bitwise types
2022-06-27 19:05 ` [PATCH 0/5] allow -1 and compares in bitwise types Luc Van Oostenryck
` (2 preceding siblings ...)
2022-06-27 19:05 ` [PATCH 3/5] bitwise: allow compares for bitwise types Luc Van Oostenryck
@ 2022-06-27 19:05 ` Luc Van Oostenryck
2022-06-27 19:05 ` [PATCH 5/5] bitwise: early expansion of simple constants Luc Van Oostenryck
` (2 subsequent siblings)
6 siblings, 0 replies; 18+ messages in thread
From: Luc Van Oostenryck @ 2022-06-27 19:05 UTC (permalink / raw)
To: linux-sparse; +Cc: Linus Torvalds, Bart Van Assche, Luc Van Oostenryck
When bitwise types were added [1] the signedness modifiers
were removed from them. More exactly, it was MOD_SPECIFIER
which was removed. I suppose this was done because then
MOD_SPECIFIER contained the signedness bits but also MOD_CHAR,
MOD_LONG, ... and those had to be removed.
But currently MOD_SPECIFIER contains only MOD_SIGNEDNESS
and the signedness info can be useful for bitwise types too.
So, do not removed anymore MOD_SPECIFIER from the bitwise
types' modifiers.
[1] commit 032f492af0ac ("[PATCH] __attribute__((bitwise))")
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
parse.c | 1 -
show-parse.c | 2 +-
validation/linear/bitwise-cmpu.c | 1 -
3 files changed, 1 insertion(+), 3 deletions(-)
diff --git a/parse.c b/parse.c
index 3d6fef7cb011..14fe9e15448a 100644
--- a/parse.c
+++ b/parse.c
@@ -1586,7 +1586,6 @@ static struct token *declaration_specifiers(struct token *token, struct decl_sta
}
type = alloc_symbol(token->pos, SYM_BASETYPE);
*type = *ctx->ctype.base_type;
- type->ctype.modifiers &= ~MOD_SPECIFIER;
type->ctype.base_type = ctx->ctype.base_type;
type->type = SYM_RESTRICT;
ctx->ctype.base_type = type;
diff --git a/show-parse.c b/show-parse.c
index e2fc18bb4b3d..2ab2479b8181 100644
--- a/show-parse.c
+++ b/show-parse.c
@@ -318,7 +318,7 @@ deeper:
if (as)
prepend(name, "%s ", show_as(as));
- if (sym && (sym->type == SYM_BASETYPE || sym->type == SYM_ENUM))
+ if (sym && (sym->type == SYM_BASETYPE || sym->type == SYM_ENUM || sym->type == SYM_RESTRICT))
mod &= ~MOD_SPECIFIER;
s = modifier_string(mod);
len = strlen(s);
diff --git a/validation/linear/bitwise-cmpu.c b/validation/linear/bitwise-cmpu.c
index 8932436a7764..e151b9741fa3 100644
--- a/validation/linear/bitwise-cmpu.c
+++ b/validation/linear/bitwise-cmpu.c
@@ -8,7 +8,6 @@ static int gtu(bu32 x, bu32 y) { return (x > y); }
/*
* check-name: bitwise-cmpu
* check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
*
* check-output-ignore
* check-output-excludes: setlt\\.
--
2.36.1
^ permalink raw reply related [flat|nested] 18+ messages in thread
* [PATCH 5/5] bitwise: early expansion of simple constants
2022-06-27 19:05 ` [PATCH 0/5] allow -1 and compares in bitwise types Luc Van Oostenryck
` (3 preceding siblings ...)
2022-06-27 19:05 ` [PATCH 4/5] bitwise: do not remove the signedness of " Luc Van Oostenryck
@ 2022-06-27 19:05 ` Luc Van Oostenryck
2022-06-27 19:14 ` [PATCH 0/5] allow -1 and compares in bitwise types Linus Torvalds
2022-06-27 19:15 ` Bart Van Assche
6 siblings, 0 replies; 18+ messages in thread
From: Luc Van Oostenryck @ 2022-06-27 19:05 UTC (permalink / raw)
To: linux-sparse; +Cc: Linus Torvalds, Bart Van Assche, Luc Van Oostenryck
C has only positive constants: -1 is really an expression,
the unary '-' operator applied to the constant 1. '-1' as
a constant value only exists after the expansion of constant
expressions.
This is rather unfortunate since it inhibits easy testing
of such constants in the evaluation phase, like here for
restricted_value().
So, expand expressions like +CTE, -CTE or ~CTE before
calling restricted_value().
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
evaluate.c | 48 +++++++++++++++++++++++++++++++++-
validation/bitwise-cast.c | 13 +++++++++
validation/bitwise-is-signed.c | 1 -
3 files changed, 60 insertions(+), 2 deletions(-)
diff --git a/evaluate.c b/evaluate.c
index bb8c0caa905a..33cc85c8d40f 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -413,11 +413,56 @@ static struct symbol *bad_expr_type(struct expression *expr)
return expr->ctype = &bad_ctype;
}
+static bool expand_simple_constant(struct expression *expr, struct symbol *type)
+{
+ unsigned long long val, mask;
+ struct expression *pre;
+ struct symbol *ctype;
+ unsigned size;
+
+ if (expr->type != EXPR_PREOP)
+ return false;
+ pre = expr->unop;
+ if (pre->type != EXPR_VALUE)
+ return false;
+
+ ctype = pre->ctype;
+ if (!ctype || ctype != type)
+ return false;
+
+ size = ctype->bit_size;
+ if (!size)
+ return false;
+
+ mask = sign_bit(size);
+ switch (expr->op) {
+ case '+':
+ val = pre->value;
+ break;
+ case '-':
+ val = pre->value;
+ if (val == mask && !(ctype->ctype.modifiers & MOD_UNSIGNED))
+ return false;
+ val = -val;
+ break;
+ case '~':
+ val = pre->value;
+ val = ~val;
+ break;
+ default:
+ return false;
+ }
+ expr->op = 0;
+ expr->type = EXPR_VALUE;
+ expr->value = val & bits_mask(size);
+ return true;
+}
+
static int restricted_value(struct expression *v, struct symbol *type)
{
if (v->type != EXPR_VALUE)
return 1;
- if (v->value != 0 && v->value != bits_mask(type->bit_size))
+ if (v->value != 0 && v->value != bits_mask(v->ctype->bit_size))
return 1;
return 0;
}
@@ -1919,6 +1964,7 @@ Normal:
if (!(class & TYPE_FLOAT)) {
ctype = integer_promotion(ctype);
expr->unop = cast_to(expr->unop, ctype);
+ expand_simple_constant(expr, ctype);
} else if (expr->op != '~') {
/* no conversions needed */
} else {
diff --git a/validation/bitwise-cast.c b/validation/bitwise-cast.c
index 1075a3e9410c..01af56c73751 100644
--- a/validation/bitwise-cast.c
+++ b/validation/bitwise-cast.c
@@ -48,6 +48,19 @@ static __be32 bar1(void)
return (__be32)0xffffffff;
}
+/* Implicit casts of minus one, legal */
+static __be32 foom(void)
+{
+ __be32 x = -1;
+ return x;
+}
+
+/* Explicit cast of minus one, legal */
+static __be32 barm(void)
+{
+ return (__be32)-1;
+}
+
/*
* check-name: conversions to bitwise types
* check-command: sparse -Wbitwise $file
diff --git a/validation/bitwise-is-signed.c b/validation/bitwise-is-signed.c
index dd9c147173cd..99d16bd20f9b 100644
--- a/validation/bitwise-is-signed.c
+++ b/validation/bitwise-is-signed.c
@@ -15,7 +15,6 @@ int fou(void) { return !is_signed_type(u); }
/*
* check-name: bitwise-is-signed
* check-command: test-linearize -Wno-decl $file
- * check-known-to-fail
*
* check-output-ignore
* check-output-returns: 1
--
2.36.1
^ permalink raw reply related [flat|nested] 18+ messages in thread
* Re: [PATCH 0/5] allow -1 and compares in bitwise types
2022-06-27 19:05 ` [PATCH 0/5] allow -1 and compares in bitwise types Luc Van Oostenryck
` (4 preceding siblings ...)
2022-06-27 19:05 ` [PATCH 5/5] bitwise: early expansion of simple constants Luc Van Oostenryck
@ 2022-06-27 19:14 ` Linus Torvalds
2022-06-27 19:15 ` Bart Van Assche
6 siblings, 0 replies; 18+ messages in thread
From: Linus Torvalds @ 2022-06-27 19:14 UTC (permalink / raw)
To: Luc Van Oostenryck; +Cc: Sparse Mailing-list, Bart Van Assche
On Mon, Jun 27, 2022 at 12:05 PM Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
>
> #define is_signed_type(type) (((type)-1) <= 0)
Side note: the reason we *don't* use this form in the kernel is
because broken compilers will complain about compares with zero in
unsigned types.
Using "<=" like you do may be an acceptable way to avoid it (the most
obvious thing is to use "< 0"), but it makes me nervous.
Regardless, I think you need the cast of the zero. I think "type"
might be a pointer, and sparse should be complaining about the horrid
use of a bare 0 as NULL.
Similar issues might happen for enums, where various compilers will
complain about comparing an enum to a non-enum.
So I'm pretty sure you would want casts on both values, instead of
assuming "it's an integer type, I don't need to cast 0".
But yeah, maybe
#define is_signed_type(type) (((type)-1) <= (type)0)
works fine and avoids warnings in all the cases.
Famous last words. Warnings can happen for almost anything, and I
wonder if that use of "1" had some other reason.
Linus
^ permalink raw reply [flat|nested] 18+ messages in thread
* Re: [PATCH 0/5] allow -1 and compares in bitwise types
2022-06-27 19:05 ` [PATCH 0/5] allow -1 and compares in bitwise types Luc Van Oostenryck
` (5 preceding siblings ...)
2022-06-27 19:14 ` [PATCH 0/5] allow -1 and compares in bitwise types Linus Torvalds
@ 2022-06-27 19:15 ` Bart Van Assche
6 siblings, 0 replies; 18+ messages in thread
From: Bart Van Assche @ 2022-06-27 19:15 UTC (permalink / raw)
To: Luc Van Oostenryck, linux-sparse; +Cc: Linus Torvalds
On 6/27/22 12:05, Luc Van Oostenryck wrote:
> Allow using -1 and compare operators with bitwise types
>
> This series is an experiment for allowing constants like
> -1 or ~0 with bitwise types, as well as using compares
> on them.
Will these changes cause sparse to stop complaining about
the following expression?
cpu_to_le32(1) < cpu_to_le32(2)
Shouldn't sparse keep complaining about the above expression?
> @Bart,
> Is there a good reason why the macro compares against 1 and
> not against 0?
Maybe to prevent that gcc or clang issues the following
warning: "warning: comparison of unsigned expression in ‘< 0’
is always false"?
Thanks,
Bart.
^ permalink raw reply [flat|nested] 18+ messages in thread