<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - Static variable in inline function having duplicates with shared library (compiled with GCC)"
   href="https://bugs.llvm.org/show_bug.cgi?id=33931">33931</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Static variable in inline function having duplicates with shared library (compiled with GCC)
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>3.9
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </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>mikebentley15@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=18847" name="attach_18847" title="minimal reproducible example">attachment 18847</a> <a href="attachment.cgi?id=18847&action=edit" title="minimal reproducible example">[details]</a></span>
minimal reproducible example

This is a strange use case, but it is my use case.

Suppose you have an inline function with a static variable, such as

inline std::map<std::string, void*>& get_tests() {
  static std::map<std::string, void*> tests;
  return tests;
}

that is defined in a header file for a shared library.  Seems reasonable,
right?  Now suppose the shared library is compiled with GCC 5.4.0, also
reasonable.  Then you create an application that uses this inline function and
compile your application with Clang.  What happens is that the shared library,
when it calls this function, will have one copy of the static variable
different from the one in the application.

I have attached code that can duplicate this problem.  This has been tested
with GCC 5.4.0 (and a little with GCC 6.1).  I notice that this problem is NOT
there with GCC 4.9.  So, I actually do not know if this is a Clang bug or a GCC
bug.

In the example code, I demonstrate that everything is okay when using a static
integer, but it fails when using a static map.  I don't know what other kinds
of objects would have this problem.

Tested with the following versions of Clang:
- 3.9.0 final release
- 3.9.1 final release
- 4.0.0 final release
- 4.0.1 final release
- 5.0.0
  - This is the one I have installed from building cling
  - <a href="http://root.cern.ch/git/clang.git">http://root.cern.ch/git/clang.git</a> 1f8b137c7eb06ed8e321649ef7e3f3e7a96f361c
  - <a href="http://root.cern.ch/git/llvm.git">http://root.cern.ch/git/llvm.git</a> 2a34248cb945d63ded5ee55128e68efd7e5b87c8</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>