bitwise enums

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

 



I have a feature request relating to bitwise and enums.  The following
does not work the way I would like it to:

typedef __attribute__((bitwise)) enum {
	FOO,
	BAR,
} my_t;

static my_t f(int a)
{
	if (a)
		return FOO;
	return BAR;
};

sparse complains:

test.c:10:16: warning: incorrect type in return expression (different base types)
test.c:10:16:    expected restricted my_t
test.c:10:16:    got int

(line 10 is, of course 'return BAR'; since FOO has value 0, it does not warn
on line 9).

I think it would be appropriate for the __attribute__((bitwise)) to
apply not just to 'my_t' but also to 'FOO' and 'BAR'.  ie parse this
as if it were written:

typedef __attribute__((bitwise)) enum {
	FOO = ((__force my_t))0,
	BAR = ((__force my_t))1,
} my_t;

... except of course you can't write that because my_t doesn't exist
until the end of the block.

sparse doesn't whinge about this:

typedef __attribute__((bitwise)) unsigned int my_t;
  
typedef enum {
        FOO = (__attribute__((force)) my_t)0,
        BAR = (__attribute__((force)) my_t)1,
} my_t;

although GCC does complain about redefining a typedef.  Maybe sparse
should also warn about that?

Oh, and I'd also like sparse to permit this:

typedef __attribute__((bitwise)) enum {
	FOO = 1,
	BAR = 2,
	BAZ = 4,
	QUUX = 8,
	WIBBLE = 0x10,
} my_t;

(if you're defining enums for a bitwise type, you're almost certainly
defining individual bits, so you want to do explicit assignment of values)

I can work around this for now by writing:

typedef __attribute__((bitwise)) unsigned int my_t;
  
enum {
        FOO = (__attribute__((force)) my_t)0,
        BAR = (__attribute__((force)) my_t)1,
};

but that's less elegant than the syntax I suggested above.
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[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