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

            Bug ID: 101817
           Summary: g++ apply optimization irrespective of -O0 flag is set
           Product: gcc
           Version: 9.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mohan.rajanna at ict dot nl
  Target Milestone: ---

Created attachment 51273
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51273&action=edit
disassemble log captured during gdb session

Hello Team,

I observe g++ applies optimization even when -O0 flag is used for compiling.

I am performing unit testing with CMocka setup. Due to optimization, I am
unable to test stub function call to strncmp().

Please Note: Stub function call to strncmp() will work in some cases but most
of the time, function calls are optimized.

I am performing debug build, cmake build log with verbose argument says source
file is compiled with below example. I believe in this case -O0 is applied by
default.
Example:
std=c++14 -g   -o test.c.o -c test.c

Just for Information:
I manually applied -O0 in CMakeLists.txt with this line 
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") and below was the cmake verbose log.
std=c++14 -g -O0   -o test.c.o -c test.c

Please find the attachment to see disassemble log captured during gdb session. 

Please find below the versions used for g++, cmake, ubuntu:

g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

cmake --version
cmake version 3.16.3

Ubuntu 20.04.2 LTS

This is inconsistent behaviour with -O0 flag. Sometimes this flag disables the
optimization but not always.

Kind Regards
Mohan

Reply via email to