linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christopher Li <sparse@chrisli.org>
To: Ben Pfaff <blp@nicira.com>
Cc: linux-sparse@vger.kernel.org
Subject: Re: [PATCH] evaluate: Allow sizeof(_Bool) to succeed.
Date: Sat, 7 May 2011 13:37:04 -0700	[thread overview]
Message-ID: <BANLkTikHwADf5Kv5=rQosKcNjsMYM-28wQ@mail.gmail.com> (raw)
In-Reply-To: <1304552394-16280-1-git-send-email-blp@nicira.com>

On Wed, May 4, 2011 at 4:39 PM, Ben Pfaff <blp@nicira.com> wrote:
> 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.

Thanks for the patch.

The sizeof _Bool is implementation define. Gcc make sizeof(_Bool) as 1.
I modify your patch to issue an warning. Applied and pushed.

The incremental change follows. Please check that works for you or not.

Chris

diff --git a/evaluate.c b/evaluate.c
index f196dbc..1309001 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -2028,14 +2028,18 @@ 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;
 	}

-	if (is_bool_type(type))
-		size = bits_in_char;
-	else if ((size < 0) || (size & (bits_in_char - 1)))
+
+	if ((size < 0) || (size & (bits_in_char - 1)))
 		expression_error(expr, "cannot size expression");

 	expr->type = EXPR_VALUE;
diff --git a/validation/sizeof-bool.c b/validation/sizeof-bool.c
index dfcb12a..6c68748 100644
--- a/validation/sizeof-bool.c
+++ b/validation/sizeof-bool.c
@@ -6,4 +6,7 @@ static int a(void)
  * 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
  */
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2011-05-07 20:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-04 23:39 [PATCH] evaluate: Allow sizeof(_Bool) to succeed Ben Pfaff
2011-05-07 20:37 ` Christopher Li [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='BANLkTikHwADf5Kv5=rQosKcNjsMYM-28wQ@mail.gmail.com' \
    --to=sparse@chrisli.org \
    --cc=blp@nicira.com \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).