Bug 48026 - #pragma optimize ignored for C++ for preprocessor
Summary: #pragma optimize ignored for C++ for preprocessor
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.5.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Keywords: wrong-code
Depends on: 41201
  Show dependency treegraph
Reported: 2011-03-07 19:44 UTC by Sam Morris
Modified: 2021-11-21 00:19 UTC (History)
3 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2021-11-20 00:00:00


Note You need to log in before you can comment on or make changes to this bug.
Description Sam Morris 2011-03-07 19:44:07 UTC
#pragma optimize seems to be ignored for C++.

=== Makefile ===
all: opt_c.s opt_cxx.s
.PHONY: all

opt_c.s: opt.c
	gcc -S -o $@ -O2 $<

opt_cxx.s: opt.c
	g++ -S -o $@ -O2 $<

	rm -f *.s
.PHONY: clean

=== opt.c ===
#pragma GCC optimize ("O0")

#ifdef __OPTIMIZE__
#error help!

int main () {
	int i;
	for (i = 0; i < 2000000000; ++i)

=== test output ===
$ make -k
gcc -S -o opt_c.s -O2 opt.c
g++ -S -o opt_cxx.s -O2 opt.c
opt.c:4:2: error: #error help!
make: *** [opt_cxx.s] Error 1
make: Target `all' not remade because of errors.
Comment 1 Andrew Pinski 2012-02-02 06:24:46 UTC
Confirmed, this is most likely the same issue as PR 41201.
Comment 2 Allan Jensen 2012-07-25 12:40:22 UTC
(In reply to comment #1)
> Confirmed, this is most likely the same issue as PR 41201.

Well, not completely the same. The original issue reported in PR 41201 is about defining the macros, but the possible fix in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41201#c6 is about this bug.
Comment 3 Frank 2014-12-10 17:33:43 UTC
Still existent in 4.8.4
Comment 4 Manuel López-Ibáñez 2014-12-17 12:56:43 UTC
This is probably because the preprocessor runs before the #pragmas are handled. If so, this is the same issue as for #pragma GCC diagnostics in C++ (PR53431).