diff mbox series

[2/3] enum: default to unsigned

Message ID 20181005015642.7207-3-luc.vanoostenryck@gmail.com (mailing list archive)
State Mainlined, archived
Headers show
Series fix enum type - finalize | expand

Commit Message

Luc Van Oostenryck Oct. 5, 2018, 1:56 a.m. UTC
GCC uses an unsigned type for enum's basetype unless one of
the enumerators is negative.

Using 'int' for plain simple enumerators and then using the same
rule as for integer constants (int -> unsigned int -> long -> ...)
should be more natural but doing so creates useless warnings when
using sparse on the kernel code.

So, do the same as GCC:
* uses the smaller type that fits all enumerators,
* uses at least int or unsigned int,
* uses an signed type only if one of the enumerators is negative.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 parse.c                       | 12 ++++++------
 validation/builtin-overflow.c |  2 +-
 validation/enum-mismatch.c    |  4 ++--
 validation/enum-sign-gcc.c    |  1 -
 4 files changed, 9 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/parse.c b/parse.c
index 8ccfdea20..1886495f8 100644
--- a/parse.c
+++ b/parse.c
@@ -977,18 +977,18 @@  static struct token *parse_enum_declaration(struct token *token, struct symbol *
 	}
 	else if (!is_int_type(base_type))
 		base_type = base_type;
-	else if (type_is_ok(&int_ctype, range))
-		base_type = &int_ctype;
 	else if (type_is_ok(&uint_ctype, range))
 		base_type = &uint_ctype;
-	else if (type_is_ok(&long_ctype, range))
-		base_type = &long_ctype;
+	else if (type_is_ok(&int_ctype, range))
+		base_type = &int_ctype;
 	else if (type_is_ok(&ulong_ctype, range))
 		base_type = &ulong_ctype;
-	else if (type_is_ok(&llong_ctype, range))
-		base_type = &llong_ctype;
+	else if (type_is_ok(&long_ctype, range))
+		base_type = &long_ctype;
 	else if (type_is_ok(&ullong_ctype, range))
 		base_type = &ullong_ctype;
+	else if (type_is_ok(&llong_ctype, range))
+		base_type = &llong_ctype;
 	else
 		base_type = &bad_ctype;
 	parent->ctype.base_type = base_type;
diff --git a/validation/builtin-overflow.c b/validation/builtin-overflow.c
index 867eb42f5..a3dacc265 100644
--- a/validation/builtin-overflow.c
+++ b/validation/builtin-overflow.c
@@ -1,4 +1,4 @@ 
-enum e { OK };
+enum e { OK, KO = -1 };
 typedef _Bool bool;
 
 static int test(int i, long l, long long ll, enum e e, bool b, void *p)
diff --git a/validation/enum-mismatch.c b/validation/enum-mismatch.c
index 9a929d24c..f698c0161 100644
--- a/validation/enum-mismatch.c
+++ b/validation/enum-mismatch.c
@@ -13,7 +13,7 @@  static enum eb foo(enum ea a)
  *
  * check-error-start
 enum-mismatch.c:7:16: warning: mixing different enum types
-enum-mismatch.c:7:16:     int enum ea  versus
-enum-mismatch.c:7:16:     int enum eb 
+enum-mismatch.c:7:16:     unsigned int enum ea  versus
+enum-mismatch.c:7:16:     unsigned int enum eb 
  * check-error-end
  */
diff --git a/validation/enum-sign-gcc.c b/validation/enum-sign-gcc.c
index a442bf0b8..5aa7983c1 100644
--- a/validation/enum-sign-gcc.c
+++ b/validation/enum-sign-gcc.c
@@ -60,5 +60,4 @@  _Static_assert(is_unsigned(D) == 1, "enum D");
 /*
  * check-name: enum-sign-gcc
  * check-command: sparse -m64 $file
- * check-known-to-fail
  */