linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] evaluate: Allow sizeof(_Bool) to succeed.
@ 2011-05-04 23:39 Ben Pfaff
  2011-05-07 20:37 ` Christopher Li
  0 siblings, 1 reply; 7+ messages in thread
From: Ben Pfaff @ 2011-05-04 23:39 UTC (permalink / raw)
  To: sparse; +Cc: Ben Pfaff, linux-sparse

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

diff --git a/evaluate.c b/evaluate.c
index f8343c2..f196dbc 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -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;
diff --git a/symbol.h b/symbol.h
index e567305..2b8f20e 100644
--- a/symbol.h
+++ b/symbol.h
@@ -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 0000000..dfcb12a
--- /dev/null
+++ b/validation/sizeof-bool.c
@@ -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
+ */
-- 
1.7.4.4


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-05-12 20:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-04 23:39 [PATCH] evaluate: Allow sizeof(_Bool) to succeed Ben Pfaff
2011-05-07 20:37 ` Christopher Li
2011-05-09 20:02   ` Ben Pfaff
2011-05-09 20:31     ` Christopher Li
2011-05-09 20:49       ` Ben Pfaff
2011-05-12  0:09         ` Christopher Li
2011-05-12 20:48           ` Ben Pfaff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).