[PATCH 0/5] allow -1 and compares in bitwise types

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


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.
the motivation for allowing this is to avoid useless casts
and warnings on using some generic macros on bitwise types.
The case of interest here is:
	#define is_signed_type(T) (((T)(-1)) < (T)1)
for which Sparse reports four warnings when 'T' is a bitwise
type. Two of these warnings can be suppressed by using
'__force' in the casts. But the other two can't be worked
around because explicitly reject using any constants other
than 0 with bitwise type.

The changes in the series allow to use the following testcase
without any warnings:
	#define __bitwise __attribute__((bitwise))
	typedef   signed int __bitwise s;
	typedef unsigned int __bitwise u;

	#define is_signed_type(type)  (((type)-1) <= 0)

	int fs(void) { return ((s)-1) <= 0; }
	int fu(void) { return ((u)-1) <= 0; }

and result in a modest but significant decrease in the number
of warnings issued by sparse (x86 {def,all}config):
   -  2736  2735 cast to restricted type
   +   774   792 cast truncates bits from constant value
   -  3183  3152 incorrect type in assignment (different base types)
   -   162   159 incorrect type in initializer (different base types)
   -   789   661 restricted type degrades to integer
   - 13002 12857 Total

This seems to work correctly and I think it correspond to
what you wished (maybe modulo this 'early constant expansion'
I added) and I think that accepting the -1/all-ones is fine.
OTOH, I don't think we can allow the compares because they
don't make any sense for the 'endian' types. Surely we want
to catch things like:
	typedef unsigned int __bitwise __be32;
	__be32 x, y;
	... (x < y)

Is there a good reason why the macro compares against 1 and
not against 0? Is it just because of -Wtype-limits? If so,
is it possible to use '<= 0' like here above?

The series is available for review and testing at:
  https://git.kernel.org/pub/scm/devel/sparse/sparse.git bitwise-ones

Luc Van Oostenryck (5):
  bitwise: add testcases
  bitwise: accept all ones as non-restricted value
  bitwise: allow compares for bitwise types
  bitwise: do not remove the signedness of bitwise types
  bitwise: early expansion of simple constants

 evaluate.c                       | 52 +++++++++++++++++++++++++++++++-
 parse.c                          |  1 -
 show-parse.c                     |  2 +-
 validation/bitwise-cast.c        | 26 ++++++++++++++++
 validation/bitwise-cmp.c         | 31 +++++++++++++++++++
 validation/bitwise-is-signed.c   | 21 +++++++++++++
 validation/linear/bitwise-cmps.c | 17 +++++++++++
 validation/linear/bitwise-cmpu.c | 17 +++++++++++
 8 files changed, 164 insertions(+), 3 deletions(-)
 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


[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux