Bug 95239 - Unable to ignore -Wattribute-warning in macro
Summary: Unable to ignore -Wattribute-warning in macro
Status: RESOLVED DUPLICATE of bug 97498
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2020-05-20 15:37 UTC by Evan Nemerson
Modified: 2022-10-19 20:51 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Test case (312 bytes, text/plain)
2020-05-20 15:37 UTC, Evan Nemerson
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Evan Nemerson 2020-05-20 15:37:44 UTC
Created attachment 48573 [details]
Test case

I'm trying to create a macro which evaluates an expression while ignoring warnings generated by the warning attribute.  Basically, a slightly simplified version of what I want is:

  #define IGNORE_WARNING_ATTR(expr) (__extension__({ \
    _Pragma("GCC diagnostic push") \
    _Pragma("GCC diagnostic ignored \"-Wattribute-warning\"") \
    int tmp = expr; \
    _Pragma("GCC diagnostic pop") \
    tmp; \
  }))

However, when I use it I still see the warning.

If I don't use a macro, but instead just do 

    int c = (__extension__({
	_Pragma("GCC diagnostic push")
	_Pragma("GCC diagnostic ignored \"-Wattribute-warning\"")
	int tmp = foo(argc);
	_Pragma("GCC diagnostic pop")
	tmp;
      }));

It works as intended; no warning.

If I use the macro version and preprocess the source file first, then compile the preprocessed file separately, it works.

If I compile with g++ it works.

Using the attached test case, I get:

$ gcc -E -o warn-pp.c warn.c && gcc -o warn warn-pp.c
$ g++ -o warn warn.c
$ gcc -o warn warn.c
warn.c: In function ‘main’:
warn.c:23:31: warning: call to ‘foo’ declared with attribute warning: Calling foo [-Wattribute-warning]
   23 |   int b = IGNORE_WARNING_ATTR(foo(argc));
      |                               ^~~~~~~~~
warn.c:4:15: note: in definition of macro ‘IGNORE_WARNING_ATTR’
    4 |     int tmp = expr; \
      |               ^~~~
Comment 1 Lewis Hyatt 2022-10-19 20:51:57 UTC
Same issue as PR97498.

*** This bug has been marked as a duplicate of bug 97498 ***