summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-05-08 03:23:16 +0200
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-10-05 03:29:20 +0200
commitf2a89f8cadfb253d27a493212ef457127dfd33e3 (patch)
tree4dbc07b883491044ac06f2215a628201bdcdd10a
parentd39fc24d828166e7e2db63b9ab96a26552727f11 (diff)
downloadsparse-f2a89f8cadfb253d27a493212ef457127dfd33e3.tar.gz
enum: default to unsigned
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>
-rw-r--r--parse.c12
-rw-r--r--validation/builtin-overflow.c2
-rw-r--r--validation/enum-mismatch.c4
-rw-r--r--validation/enum-sign-gcc.c1
4 files changed, 9 insertions, 10 deletions
diff --git a/parse.c b/parse.c
index 8ccfdea2..1886495f 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 867eb42f..a3dacc26 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 9a929d24..f698c016 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 a442bf0b..5aa7983c 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
*/