Wireshark-dev: Re: [Wireshark-dev] Making gcc less pedantic
From: Guy Harris <guy@xxxxxxxxxxxx>
Date: Fri, 13 Feb 2015 15:00:49 -0800
On Feb 13, 2015, at 12:54 PM, Gerald Combs <gerald@xxxxxxxxxxxxx> wrote:

> Would it make sense to make gcc's "-pedantic" warnings a bit less
> pedantic, e.g. with "-std=c99"?

C90 says

	A bit-field shall have a type that is a qualified or unqualified version of one of int, unsigned int, or signed int. Whether the high-order bit position of a (possibly qualified) “plain” int bit-field is treated as a sign bit is implementation-defined. A bit-field is interpreted as an integral type consisting of the specified number of bits.

in 6.5.2.1 "Structure and union specifiers".

C99 says

	A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type.

in 6.7.2.1 "Structure and union specifiers".

At least as I read C99's 6.2.5 "Types", an enumeration is not any of the specified types, so it would be implementation-defined whether an enumeration is a valid type for a bit-field.

So -std=c99 combined with -pedantic does *not* suppress the warning:

	$ gcc -pedantic -std=c99 enum.c
	enum.c:4:7: warning: type of bit-field ‘twobits’ is a GCC extension [-Wpedantic]
	  enum frobozz twobits:2;

when compiling

	enum frobozz { red, green, blue };

	struct foo {
		enum frobozz twobits:2;
		int hello;
	};

	int
	main(void)
	{
		return 0;
	}

-std=cXX, by itself, does *not* cause warnings when features not in cXX are used; to quote the man page:

           ...  When a base standard is specified, the compiler accepts
           all programs following that standard plus those using GNU
           extensions that do not contradict it.  For example, -std=c90 turns
           off certain features of GCC that are incompatible with ISO C90,
           such as the "asm" and "typeof" keywords, but not other GNU
           extensions that do not have a meaning in ISO C90, such as omitting 
           the middle term of a "?:" expression.

so even -std=c89 prints no "a bit-field is an enum" warning when used without -pedantic.

> In this particular case having a "packet_char_enc" encoding instead of
> an "unsigned int" encoding is useful, otherwise you have to cast back to
> packet_char_enc further down the line. It also compiles under Clang,
> Visual C++, and Solaris Studio without warning.

In practice, I suspect few if any C compilers would refuse to allow enumerations as bit-fields.