Bug 81364 - Bogus -Wmultistatement-macros warning
Summary: Bogus -Wmultistatement-macros warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: 8.0
Assignee: Marek Polacek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-07-08 22:04 UTC by H.J. Lu
Modified: 2017-07-25 09:50 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-07-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2017-07-08 22:04:55 UTC
[hjl@gnu-tools-1 tmp]$ cat x.c
#define  TST_IF_RETURN(_s_func_) \
 if (_s_func_) \
 { \
 }  \
 else

#define TST_FUNC_ISW(_FUNC_, _func_) \
int \
tst_isw##_func_ (int debug_flg) \
{ \
  TST_IF_RETURN (_FUNC_) \
  { \
  } \
}

extern int LOWER;

TST_FUNC_ISW (LOWER, lower);
[hjl@gnu-tools-1 tmp]$ /export/build/gnu/gcc-cet/release/usr/gcc-8.0.0-cet/bin/gcc -Wmultistatement-macros -S x.c 
x.c: In function ‘tst_iswlower’:
x.c:12:3: warning: macro expands to multiple statements [-Wmultistatement-macros]
   { \
   ^
x.c:18:1: note: in expansion of macro ‘TST_FUNC_ISW’
 TST_FUNC_ISW (LOWER, lower);
 ^~~~~~~~~~~~
x.c:5:2: note: some parts of macro expansion are not guarded by this ‘else’ clause
  else
  ^
x.c:11:3: note: in expansion of macro ‘TST_IF_RETURN’
   TST_IF_RETURN (_FUNC_) \
   ^~~~~~~~~~~~~
x.c:18:1: note: in expansion of macro ‘TST_FUNC_ISW’
 TST_FUNC_ISW (LOWER, lower);
 ^~~~~~~~~~~~
[hjl@gnu-tools-1 tmp]$
Comment 1 H.J. Lu 2017-07-08 22:09:01 UTC
Another one:

[hjl@gnu-tools-1 tmp]$ cat y.c 
#define TST_DO_REC(rec) \
  for (rec=0; rec < 10; ++rec)

#define TST_FUNC_ISW \
int \
tst_isw##_func_ (int debug_flg) \
{ \
  TST_DO_REC (debug_flg) \
  { \
  } \
}

TST_FUNC_ISW
[hjl@gnu-tools-1 tmp]$ /export/build/gnu/gcc-cet/release/usr/gcc-8.0.0-cet/bin/gcc -Wmultistatement-macros -S y.c 
y.c: In function ‘tst_isw_func_’:
y.c:9:3: warning: macro expands to multiple statements [-Wmultistatement-macros]
   { \
   ^
y.c:13:1: note: in expansion of macro ‘TST_FUNC_ISW’
 TST_FUNC_ISW
 ^~~~~~~~~~~~
y.c:2:3: note: some parts of macro expansion are not guarded by this ‘for’ clause
   for (rec=0; rec < 10; ++rec)
   ^
y.c:8:3: note: in expansion of macro ‘TST_DO_REC’
   TST_DO_REC (debug_flg) \
   ^~~~~~~~~~
y.c:13:1: note: in expansion of macro ‘TST_FUNC_ISW’
 TST_FUNC_ISW
 ^~~~~~~~~~~~
[hjl@gnu-tools-1 tmp]$
Comment 2 Marek Polacek 2017-07-11 11:00:45 UTC
C++ doesn't warn.  The problem is that we shouldn't warn (in the C FE) when a guard is followed by '{'.
Comment 3 Marek Polacek 2017-07-25 09:49:43 UTC
Author: mpolacek
Date: Tue Jul 25 09:49:08 2017
New Revision: 250498

URL: https://gcc.gnu.org/viewcvs?rev=250498&root=gcc&view=rev
Log:
	PR c/81364
	* c-parser.c (c_parser_else_body): Don't warn about multistatement
	macro expansion if the body is in { }.
	(c_parser_while_statement): Likewise.
	(c_parser_for_statement): Likewise.

	* Wmultistatement-macros-12.c: New test.

Added:
    trunk/gcc/testsuite/c-c++-common/Wmultistatement-macros-12.c
Modified:
    trunk/gcc/c/ChangeLog
    trunk/gcc/c/c-parser.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Marek Polacek 2017-07-25 09:50:04 UTC
Fixed.