@@ -2033,7 +2033,9 @@ static struct symbol *evaluate_sizeof(struct expression *expr)
size = bits_in_char;
}
- if ((size < 0) || (size & (bits_in_char - 1)))
+ if (is_bool_type(type))
+ size = bits_in_char;
+ else if ((size < 0) || (size & (bits_in_char - 1)))
expression_error(expr, "cannot size expression");
expr->type = EXPR_VALUE;
@@ -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;
new file mode 100644
@@ -0,0 +1,9 @@
+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
+ */
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> --- evaluate.c | 4 +++- symbol.h | 7 +++++++ validation/sizeof-bool.c | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletions(-) create mode 100644 validation/sizeof-bool.c