<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Wrong behavior with operator new overloading when using O2 for optimization"
   href="http://llvm.org/bugs/show_bug.cgi?id=15541">15541</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong behavior with operator new overloading when using O2 for optimization
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.2
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>terry.yinzhe@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When having the overloaded new operator defined in other cpp or in a lib, clang
with O2 or above with optimize a new operation without assignment, and it will
not call the overloaded new at all.

This behavior is different when the overloaded new operator is defined in the
same cpp file.

The following code will reproduce the problem.

test.cpp
#include <iostream>

#define CHECK(expect, actual) std::cout << "EXPECT:" << expect <<
"\tACTUAL:"<<actual << std::endl
extern bool newCalled;

void testNewWithoutAssignment() {
    newCalled = false;
    new char;
    CHECK(1, newCalled);
}
char *p;
void testNewWithAssignment() {
    newCalled = false;
    p = new char;
    CHECK(1, newCalled);
}

int main() {
    testNewWithoutAssignment();
    testNewWithAssignment();
    return 0;
}

new.cpp
#include <exception>
#include <new>
#include <cstdlib>

bool newCalled = false;
void* operator new (size_t size)  throw (std::bad_alloc)
{
    newCalled = true;
    return malloc(size);
}</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>