public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96266] New: Macro defined as enum in parens resolves to 1
@ 2020-07-21 11:55 ethouris at gmail dot com
  2020-07-21 12:05 ` [Bug c++/96266] " jakub at gcc dot gnu.org
  2020-07-21 12:34 ` redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: ethouris at gmail dot com @ 2020-07-21 11:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96266

            Bug ID: 96266
           Summary: Macro defined as enum in parens resolves to 1
           Product: gcc
           Version: 7.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ethouris at gmail dot com
  Target Milestone: ---

Created attachment 48906
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48906&action=edit
Preprocessed C++ file with example.

I'm attaching a quite long preprocessed file, but all "simplified examples"
didn't reproduce it. I'm not even sure if this is a bug or a feature (maybe
something related to resolving into `defined()` pp function), but it definitely
works not according to the intention.

This can also be my mistake, but if so, I'm completely unable to find it
despite best efforts.

In short:
1. There's an enum defined, one value is, say, `UPDATE = 0x10`
2. There's a macro defined: `#define ETONLY (UPDATE)`
3. The variable is passed as `int x = 0x11`.
4. The expression `x & ETONLY` resolves to... 1 (not 0x10).

Workaround:
1. Define `ETONLY` as `(0 | UPDATE)`
2. Expression `x & int(ETONLY)` resolves correctly to 0x10.

Tested on gcc 7.5 and some older, including 4.4.

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

* [Bug c++/96266] Macro defined as enum in parens resolves to 1
  2020-07-21 11:55 [Bug c++/96266] New: Macro defined as enum in parens resolves to 1 ethouris at gmail dot com
@ 2020-07-21 12:05 ` jakub at gcc dot gnu.org
  2020-07-21 12:34 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-07-21 12:05 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96266

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I think it is just testcase bug.
In particular the
inline bool operator&(int flags, SRT_EPOLL_OPT eflg)
{ 
    return (flags & int(eflg)) != 0;
}
operator is a very bad idea, because then any of the & operations where you
expect it to work like it would without the operator behave differently.

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

* [Bug c++/96266] Macro defined as enum in parens resolves to 1
  2020-07-21 11:55 [Bug c++/96266] New: Macro defined as enum in parens resolves to 1 ethouris at gmail dot com
  2020-07-21 12:05 ` [Bug c++/96266] " jakub at gcc dot gnu.org
@ 2020-07-21 12:34 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2020-07-21 12:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96266

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Michal Malecki from comment #0)
> I'm attaching a quite long preprocessed file, but all "simplified examples"
> didn't reproduce it.

Of course they do. This has nothing to do with macros or parentheses.


#include <iostream>

enum SRT_EPOLL_OPT
{
   SRT_EPOLL_UPDATE = 0x10,
};

inline bool operator&(int flags, SRT_EPOLL_OPT eflg)
{
    return (flags & int(eflg)) != 0;
}

using namespace std;

int main() {

    int evts = 0x11;

    int et_evts = (evts & SRT_EPOLL_UPDATE);

 cout << (SRT_EPOLL_UPDATE) << " " << et_evts << " " << evts << endl;
}


As Jakub said, your operator& is used here and is doing exactly what you told
it to do, returning a bool.

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

end of thread, other threads:[~2020-07-21 12:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-21 11:55 [Bug c++/96266] New: Macro defined as enum in parens resolves to 1 ethouris at gmail dot com
2020-07-21 12:05 ` [Bug c++/96266] " jakub at gcc dot gnu.org
2020-07-21 12:34 ` redi at gcc dot gnu.org

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).