An enumeration with sequentially-assigned values is used as an index of a for loop. The loop operates properly at optimization levels -O0 and -O1. At level -O2 the optimizer "optimizes out" the test for the exit condition and the loop never exits.
I'll be attaching the *.i file, but below is the test source file which shows the situation. At -O2, the loop itself never exits and a separate counter which is part of the test triggers a break itself--after printing that the test has failed. /* Sample program to demonstrate comparison failure involving sequential enumeration when optimized. To reproduce the failure: g++ -O2 test.cpp && ./a.out Any one of the following changes correct the problem: - Change optimization (omit or use -O0 or -O1). - Set ENUM_VALUE_FIRST to 0. - Delete one of the enum values between first and last (one less value). - Insert an addition enum value between first and last (one more value). agarland at fluke dot com */ // Compile at -O1 or uncomment any one of the following to "fix" the problem. //#define FIX1 // Omit an enum value. //#define FIX2 // Add an extra enum value. //#define FIX3 // Start first value at zero rather than 1. #include <stdio.h> enum ENUM_VALUE { #ifdef FIX3 ENUM_VALUE_FIRST = 0, #else ENUM_VALUE_FIRST = 1, #endif ENUM_VALUE_A, #ifndef FIX1 ENUM_VALUE_B, #endif ENUM_VALUE_C, ENUM_VALUE_D, ENUM_VALUE_E, #ifdef FIX2 ENUM_VALUE_F, #endif ENUM_VALUE_LAST }; main() { printf("Compiled on %s %s\n", __DATE__, __TIME__); unsigned int count = 0; for ( ENUM_VALUE id = ENUM_VALUE_FIRST; id <= ENUM_VALUE_LAST; id = static_cast<ENUM_VALUE>(id+1) ) { printf("id=%d\n", id); if (100 == count++) { // Should never get here. printf("ENUMERATION COMPARISON FAILED!\n"); break; } } } -- Summary: Enumeration with sequential values has its for-loop exit condition optimized out. Product: gcc Version: 4.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: tony3 at GarlandConsulting dot us http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42810