switch over constant results in strange constructor/destructor ordering

Hello,

I’m seeing an unexpected construction/destruction order in the program below with clang 3.9.0. Is this considered a bug? Thank you.

clang asm - https://godbolt.org/g/uGlpFa

gcc asm - https://godbolt.org/g/WDgcZP

printf Output from clang (Unexpected):

obj entered 0
obj entered 1
obj entered 2
obj exited 2
obj exited 1
obj exited 0

printf Output from gcc (Expected):

obj entered 0
obj exited 0
obj entered 1
obj exited 1
obj entered 2
obj exited 2

Program:

#include <stdint.h>

#include <stdio.h>

class SomeClass
{
uint32_t k;
public:
SomeClass(uint32_t x)
{ k = x; printf(“obj entered %d\n”, k); }
~SomeClass()
{ printf(“obj exited %d\n”, k); }

};

int main()
{
uint32_t ii = 0;

switch(1)
{
default:
{

{
SomeClass someClass(ii++);
}

{
SomeClass someClass2(ii++);
}

{
SomeClass someClass3(ii++);
}
}
}
}

Wow, yes, that’s a (pretty scary) wrong-code bug. Working on it…

Thanks for letting us know, fixed in r281797.

Richard

I see the same faulty output with Clang 3.6.0 and 3.8.0.

Is there anything which should be done about this pending an updated release?

Thanks

John

I don’t know if we’re planning on another patch release for 3.8, but I’ve already asked our patch release manager to put this into 3.9.1.

Richard

I can report that my build of Clang and LLVM from a fresh download of SVN does not show the bug.

John