[LLVMbugs] [Bug 8652] New: -fno-elide-constructors doesn't prevent all elisions

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Nov 19 13:22:54 PST 2010


           Summary: -fno-elide-constructors doesn't prevent all elisions
           Product: clang
           Version: trunk
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: reid.kleckner at gmail.com
                CC: llvmbugs at cs.uiuc.edu, dgregor at apple.com

I was drafting up a quiz question about C++ copy constructors of the form
"given this program, what does it print?" where the copy ctor keeps a counter
for the number of calls it gets.  The point is that copying objects can be
slow, and it's easy to write C++ code that does it if you're not careful. 
However, I need to be careful because there are circumstances in which a copy
ctor call can be elided by the standard, even if it has side-effects.

Interestingly, both clang and gcc elide copy constructors regardless of the
optimization level, which is not what I expected.  I discovered that gcc has
the -fno-elide-constructors option, which I used to check if my example had
elidable copy constructors by comparing the output of the program with and
without elision.  Clang has the same option, but it doesn't seem to work when
applied to my small sample program:

[rnk at methacholine quiz2]$ cat elision.cpp 
#include <iostream>

static int copies = 0;

struct CopyCounter {
  CopyCounter() { }
  CopyCounter(const CopyCounter &obj) {

CopyCounter foo() {
  return CopyCounter();

int main(int argc, char **argv) {
  CopyCounter a(foo());
  std::cout << copies << '\n';
  return 0;
[rnk at methacholine quiz2]$ g++ elision.cpp -o elision && ./elision 
[rnk at methacholine quiz2]$ g++ -fno-elide-constructors elision.cpp -o elision &&
[rnk at methacholine quiz2]$ clang++ elision.cpp -o elision && ./elision 
[rnk at methacholine quiz2]$ clang++ -fno-elide-constructors elision.cpp -o
elision && ./elision 

Is this a bug?  Is clang missing a check for LangOpts::ElideConstructors
somewhere in CodeGen?

Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.

More information about the llvm-bugs mailing list