lifetime of conditional temporaries

Dear all

I have found out that in clang conditional temporaries are destroyed not at the end of the full expression as in gcc but apparently at the end of the conditional branch.

For example, consider the program below:

#include <stdio.h>

struct S
{
int i;
S() { puts(“S”); }
~S() { puts("~S"); }
};

void f(int) { puts(“f”); }

volatile bool b = true;

int main()
{
f(b ? S().i : 0);
}

If it is compiled with gcc and run the output is:
S
f
~S

With clang the output is:
S
~S
f

The same applies to && and || operators. Is this intended or needs to be fixed in clang?
In the latter case and if nobody is already working on it I can try fixing it.

Best regards,
Victor

I have found out that in clang conditional temporaries are destroyed not at the end of the full expression as in gcc but apparently at the end of the conditional branch.

Is this intended or needs to be fixed in clang?

Sounds like an unimplemented feature.

In the latter case and if nobody is already working on it I can try fixing it.

I don't think anyone is working on it currently.

17 nov 2009 kl. 04.44 skrev Victor Zverovich:

Dear all

The same applies to && and || operators. Is this intended or needs to be fixed in clang?
In the latter case and if nobody is already working on it I can try fixing it.

Hi Victor,

this should work. Please file a bug.

Anders

I submitted it here http://llvm.org/bugs/show_bug.cgi?id=5539 so that it is not lost until I come up with a fix.

Victor

2009/11/17 Anders Carlsson <andersca@mac.com>

The first patch (cond-temp-test.patch) adds 3 new test cases that check the lifetime of conditional temporaries.
The second patch (cond-temp.patch) fixes the bug making the new test cases pass.

Victor

2009/11/17 Victor Zverovich <victor.zverovich@googlemail.com>

cond-temp-test.patch (798 Bytes)

cond-temp.patch (6.18 KB)

Here are corrected patches (forgot to initialize ConditionalBranchLevel in the previous).

Regards,
Victor

2009/11/18 Victor Zverovich <victor.zverovich@googlemail.com>

cond-temp-test.patch (798 Bytes)

cond-temp.patch (6.71 KB)

Hi Victor,

the patches look great! Thanks for fixing this :slight_smile:

One small comment - could you move the conditional temporaries tests into a separate file? You could call it conditional-temporaries.cpp

Thanks,
Anders

17 nov 2009 kl. 23.20 skrev Victor Zverovich:

Hi Anders,

Thanks for the review.
I have moved the new tests into conditional-temporaries.cpp as you suggested.
The patches are attached.

Regards,
Victor

2009/11/18 Anders Carlsson <andersca@mac.com>

cond-temp.patch (6.71 KB)

cond-temp-test.patch (797 Bytes)