public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "mlg7 at yandex dot ru" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug preprocessor/38179] need a warning: macro parameter is not a whole expression within macro body
Date: Fri, 21 Nov 2008 08:45:00 -0000	[thread overview]
Message-ID: <20081121084434.20444.qmail@sourceware.org> (raw)
In-Reply-To: <bug-38179-16925@http.gcc.gnu.org/bugzilla/>



------- Comment #2 from mlg7 at yandex dot ru  2008-11-21 08:44 -------
(In reply to comment #1)
> The expanded text for the first one is:
> int t = 1|2 & 0xFFFFFF00 ? dothis(1|2) : dothat(1|2);
> 
> Maybe I am missing something here. 
> 
the human who is writing
int t = MYMACRO(FLAGA|FLAGB);
obviously means
int t = (1|2) & 0xFFFFFF00 ? dothis(1|2) : dothat(1|2); // #1
while the compiler treats this as
int t = 1|(2 & 0xFFFFFF00) ? dothis(1|2) : dothat(1|2); // #2

(google for "C Operator Precedence" if you need to check it)

This is a well-known preprocessor gotcha. I propose to add a warning for such
cases.

PS

That is, there are two gotchas: the problem with expressions as macro
parameters and the same problem with expressions in generated text. The Clever
Books tell folks to enclose all macro parameters in parens and enclose the
whole macro body in parens.

So, the recommended way for writing
#define MYMACRO(x) x & 0xFFFFFF00 ? dothis(x) : dothat(x)
is
#define MYMACRO(x) ((x) & 0xFFFFFF00 ? dothis((x)) : dothat((x)))

It looks like LISP but it works. But practice shows that not everybody reads
The Clever Books...


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38179


  parent reply	other threads:[~2008-11-21  8:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-19 18:53 [Bug c/38179] New: " mlg7 at yandex dot ru
2008-11-19 18:59 ` [Bug c/38179] " pinskia at gcc dot gnu dot org
2008-11-21  8:45 ` mlg7 at yandex dot ru [this message]
2009-03-30  0:42 ` [Bug preprocessor/38179] " jsm28 at gcc dot gnu dot org

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=20081121084434.20444.qmail@sourceware.org \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.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 read-only IMAP folder(s) and NNTP newsgroup(s).