<p dir="ltr"><br>
On Oct 23, 2014 3:31 PM, "Austin Clements" <<a href="mailto:aclements@csail.mit.edu">aclements@csail.mit.edu</a>> wrote:<br>
><br>
> These macros help clarify basic bit-twiddling code and are written to<br>
> be robust against C undefined behavior of shift operators.<br>
> ---<br>
> lib/message.cc | 6 +++---<br>
> lib/notmuch-private.h | 11 +++++++++++<br>
> 2 files changed, 14 insertions(+), 3 deletions(-)<br>
><br>
> diff --git a/lib/message.cc b/lib/message.cc<br>
> index 38bc929..55d2ff6 100644<br>
> --- a/lib/message.cc<br>
> +++ b/lib/message.cc<br>
> @@ -869,7 +869,7 @@ notmuch_bool_t<br>
> notmuch_message_get_flag (notmuch_message_t *message,<br>
> notmuch_message_flag_t flag)<br>
> {<br>
> - return message->flags & (1 << flag);<br>
> + return NOTMUCH_TEST_BIT (message->flags, flag);<br>
> }<br>
><br>
> void<br>
> @@ -877,9 +877,9 @@ notmuch_message_set_flag (notmuch_message_t *message,<br>
> notmuch_message_flag_t flag, notmuch_bool_t enable)<br>
> {<br>
> if (enable)<br>
> - message->flags |= (1 << flag);<br>
> + NOTMUCH_SET_BIT (&message->flags, flag);<br>
> else<br>
> - message->flags &= ~(1 << flag);<br>
> + NOTMUCH_CLEAR_BIT (&message->flags, flag);<br>
> }<br>
><br>
> time_t<br>
> diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h<br>
> index 36cc12b..7250291 100644<br>
> --- a/lib/notmuch-private.h<br>
> +++ b/lib/notmuch-private.h<br>
> @@ -63,6 +63,17 @@ NOTMUCH_BEGIN_DECLS<br>
> #define STRNCMP_LITERAL(var, literal) \<br>
> strncmp ((var), (literal), sizeof (literal) - 1)<br>
><br>
> +/* Robust bit test/set/reset macros */<br>
> +#define NOTMUCH_TEST_BIT(val, bit) \<br>
> + ((bit < 0 || bit >= CHAR_BIT * sizeof (unsigned long long)) ? 0 \<br>
> + : !!((val) & (1ull << bit)))<br>
> +#define NOTMUCH_SET_BIT(valp, bit) \<br>
> + ((bit < 0 || bit >= CHAR_BIT * sizeof (unsigned long long)) ? *(valp) \<br>
> + : (*(valp) |= (1ull << bit)))<br>
> +#define NOTMUCH_CLEAR_BIT(valp, bit) \<br>
> + ((bit < 0 || bit >= CHAR_BIT * sizeof (unsigned long long)) ? *(valp) \<br>
> + : (*(valp) &= ~(1ull << bit)))</p>
<p dir="ltr">bit should be in braces in the above, like valp.</p>
<p dir="ltr">Jani.</p>
<p dir="ltr">> +<br>
> #define unused(x) x __attribute__ ((unused))<br>
><br>
> #ifdef __cplusplus<br>
> --<br>
> 2.1.0<br>
><br>
> _______________________________________________<br>
> notmuch mailing list<br>
> <a href="mailto:notmuch@notmuchmail.org">notmuch@notmuchmail.org</a><br>
> <a href="http://notmuchmail.org/mailman/listinfo/notmuch">http://notmuchmail.org/mailman/listinfo/notmuch</a><br>
</p>