This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug other/83048] wrap multi-statement macros in do {} while (0)


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

--- Comment #2 from Tom de Vries <vries at gcc dot gnu.org> ---
I wonder if we could use a macro like this:
...
#define SAFE_MACRO_STMT(stmt)                   \
  do {                                          \
    if (1)                                      \
      stmt;                                     \
    else                                        \
      {}                                        \
    if (0)                                      \
      stmt;                                     \
  } while (0)


#if ERROR == 0

// No warning or error
#define foo return

#elif ERROR == 1

// error: else without a previous if
#define foo return;

#elif ERROR == 2

// error: else without a previous if
#define foo return; return

#elif ERROR == 3

// warning: suggest explicit braces to avoid ambiguous else [-Wparentheses]
#define foo if (1) return; else return

#else

#error "Invalid value of ERROR"

#endif

void
bar (void)
{
  SAFE_MACRO_STMT (foo);
}
...

If we wrap all target macro calls in SAFE_MACRO_STMT we trigger an error or a
warning if the target macro implementation is not equivalent to a single stmt.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]