diff options
| author | Ben Pfaff <blp@nicira.com> | 2011-05-04 16:39:54 -0700 |
|---|---|---|
| committer | Christopher Li <sparse@chrisli.org> | 2011-05-07 13:29:40 -0700 |
| commit | f20cbcc7ac20f6cd05f945ed1c09af8532094430 (patch) | |
| tree | f29fc1178e7bcb2364c239279b58e4c398125e8a | |
| parent | 05e666547ee05acadf7fed2d1fc9967c27739e9a (diff) | |
| download | sparse-dev-f20cbcc7ac20f6cd05f945ed1c09af8532094430.tar.gz | |
evaluate: Allow sizeof(_Bool) to succeed.
Without this commit, sizeof(_Bool) provokes an error with "cannot size
expression" because _Bool is a 1-bit type and thus not a multiple of a full
byte in size. But sizeof(_Bool) is valid C that should evaluate to 1, so
this commit fixes the problem and adds a regression test.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Signed-off-by: Christopher Li <sparse@chrisli.org>
| -rw-r--r-- | evaluate.c | 5 | ||||
| -rw-r--r-- | symbol.h | 7 | ||||
| -rw-r--r-- | validation/sizeof-bool.c | 12 |
3 files changed, 24 insertions, 0 deletions
@@ -2028,6 +2028,11 @@ static struct symbol *evaluate_sizeof(struct expression *expr) size = bits_in_char; } + if (size == 1 && is_bool_type(type)) { + warning(expr->pos, "expression using sizeof bool"); + size = bits_in_char; + } + if (is_function(type->ctype.base_type)) { warning(expr->pos, "expression using sizeof on a function"); size = bits_in_char; @@ -346,6 +346,13 @@ static inline int is_void_type(struct symbol *type) return type == &void_ctype; } +static inline int is_bool_type(struct symbol *type) +{ + if (type->type == SYM_NODE) + type = type->ctype.base_type; + return type == &bool_ctype; +} + static inline int is_function(struct symbol *type) { return type && type->type == SYM_FN; diff --git a/validation/sizeof-bool.c b/validation/sizeof-bool.c new file mode 100644 index 00000000..6c68748a --- /dev/null +++ b/validation/sizeof-bool.c @@ -0,0 +1,12 @@ +static int a(void) +{ + return sizeof(_Bool); +} +/* + * check-name: sizeof(_Bool) is valid + * check-description: sizeof(_Bool) was rejected because _Bool is not an even + * number of bytes + * check-error-start +sizeof-bool.c:3:16: warning: expression using sizeof bool + * check-error-end + */ |
