<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 --- - Global char* assigned to other global not initialized in statically-executed code in PCH"
   href="http://llvm.org/bugs/show_bug.cgi?id=18979">18979</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Global char* assigned to other global not initialized in statically-executed code in PCH
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.3
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </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>akluck@barracuda.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>Created <span class=""><a href="attachment.cgi?id=12155" name="attach_12155" title="Simple header that will cause static execution that references a global variable">attachment 12155</a> <a href="attachment.cgi?id=12155&action=edit" title="Simple header that will cause static execution that references a global variable">[details]</a></span>
Simple header that will cause static execution that references a global
variable

A global const char pointer assigned to another const char pointer may not be
properly assigned if referenced in statically executing code, such as the
constructor of some global class object, if all these objects exist in a
precompiled header.


Consider the pasted header below. If compiled without PCH using an empty main()
function, the output of the statically-allocated ScopedPrinter object, as
expected, is:

"constructing from header ; ints: 7 7 7 ; strs: foo-bar foo-bar foo-bar"

However, if compiled with a PCH, g_str2 and g_str3, which are assigned to be
equal to g_str1, are NULL. The output of the statically-allocated object is:

"constructing from header ; ints: 7 7 7 ; strs: foo-bar (null) (null)"

In any code not executing statically, there is no issue, PCH or not. There also
doesn't seem to be a problem with the global int in the example, just the char
pointer.

We haven't run into any similar issue with precompiled headers using gcc or
MSVC in Linux or Windows, respectively.

My clang --version output is:
"Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)"

To compile *without* PCH, I ran:
clang++ -include test.h testMain.cpp -o test

To compile *with* PCH, I ran:
clang++ -x c++-header test.h -o test.h.pch
clang++ -include test.h testMain.cpp -o test

~~~~~~ test.h ~~~~~~

#ifndef _test_h_
#define _test_h_

#include <stdio.h>

const unsigned int g_int1 = 7;
const unsigned int g_int2 = g_int1;
const unsigned int g_int3 = g_int2;
const char *const g_str1 = "foo-bar";
const char *const g_str2 = g_str1;
const char *const g_str3 = g_str2;

struct ScopedPrinter
{
    ScopedPrinter(const char *input)
    {
        printf("constructing from %s ; ints: %u %u %u ; strs: %s %s %s\n",
            input, g_int1, g_int2, g_int3, g_str1, g_str2, g_str3);
    }
};

static ScopedPrinter g_headerGlob("header");

#endif

~~~~~~ testMain.cpp ~~~~~~

int main(int argc, char **argv)
{
    return 0;
}</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>