public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/42810]  New: Enumeration with sequential values has its for-loop exit condition optimized out.
@ 2010-01-19 22:52 tony3 at GarlandConsulting dot us
  2010-01-19 22:53 ` [Bug c++/42810] " tony3 at GarlandConsulting dot us
                   ` (24 more replies)
  0 siblings, 25 replies; 26+ messages in thread
From: tony3 at GarlandConsulting dot us @ 2010-01-19 22:52 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2010-05-04 16:07 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-01-19 22:52 [Bug c++/42810] New: Enumeration with sequential values has its for-loop exit condition optimized out tony3 at GarlandConsulting dot us
2010-01-19 22:53 ` [Bug c++/42810] " tony3 at GarlandConsulting dot us
2010-01-19 22:54 ` tony3 at GarlandConsulting dot us
2010-01-19 22:54 ` tony3 at GarlandConsulting dot us
2010-01-19 22:57 ` tony3 at GarlandConsulting dot us
2010-01-19 22:59 ` tony3 at GarlandConsulting dot us
2010-01-19 23:05 ` tony3 at GarlandConsulting dot us
2010-01-19 23:56 ` jakub at gcc dot gnu dot org
2010-01-20  0:19 ` tony3 at GarlandConsulting dot us
2010-01-20 15:42 ` tony3 at GarlandConsulting dot us
2010-01-20 15:48 ` paolo dot carlini at oracle dot com
2010-01-20 17:40 ` rguenth at gcc dot gnu dot org
2010-01-27 19:02 ` tony3 at GarlandConsulting dot us
2010-01-27 19:10 ` pinskia at gcc dot gnu dot org
2010-01-27 20:03 ` tony3 at GarlandConsulting dot us
2010-01-27 20:24 ` pinskia at gcc dot gnu dot org
2010-01-27 20:40 ` tony3 at GarlandConsulting dot us
2010-01-27 20:44 ` pinskia at gcc dot gnu dot org
2010-01-27 20:56 ` tony3 at GarlandConsulting dot us
2010-01-27 21:08 ` pinskia at gcc dot gnu dot org
2010-01-27 21:10 ` pinskia at gcc dot gnu dot org
2010-01-27 22:25 ` tony3 at GarlandConsulting dot us
2010-01-28 10:14 ` jwakely dot gcc at gmail dot com
2010-01-28 17:17 ` tony3 at GarlandConsulting dot us
2010-05-04  4:47 ` jason at gcc dot gnu dot org
2010-05-04 16:07 ` tony3 at GarlandConsulting dot us

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).