[LLVMbugs] [Bug 10120] New: Wrong vtable for template class instantiated with anonymous type

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Jun 10 13:28:58 PDT 2011


           Summary: Wrong vtable for template class instantiated with
                    anonymous type
           Product: clang
           Version: 2.9
          Platform: Macintosh
        OS/Version: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
        AssignedTo: unassignedclangbugs at nondot.org
        ReportedBy: 691D2C6C-B023-46A5-B1D7-ACCBA4713A87 at uuid-mail.com
                CC: llvmbugs at cs.uiuc.edu, dgregor at apple.com

Created an attachment (id=6726)
 --> (http://llvm.org/bugs/attachment.cgi?id=6726)
Sample project to show problem

If in a header I declare a template class like this:

   // header.h
   #include <assert.h>
   #include <typeinfo>

   template <typename T>
   class klass
       char const* tn1 () const         { return typeid(T()).name(); }
       virtual char const* tn2 () const { return typeid(T()).name(); }
       void test () const               { assert(tn1() == tn2());    }

Then in two different translation units instantiate the template class with
identically named types, but from anonymous namespaces, example:

   // foo.cc
   #include "header.h"
   namespace { struct my_type_t { }; }
   void foo () { klass<my_type_t> foo_instance; foo_instance.test(); }

   // bar.cc
   #include "header.h"
   namespace { struct my_type_t { }; }
   void bar () { klass<my_type_t> bar_instance; bar_instance.test(); }

Then the two instances will share the same vtable, meaning calling tn2() on
either will return the same value, whereas calling tn1() will return different

The assertion in test() will fail for one of the two instances.

To test this, save the 3 files above and add main.cc as:

   // main.cc
   void foo ();
   void bar ();

   int main (int argc, char const *argv[])
      return 0;

Then in Terminal run:

   % clang++ {foo,bar,main}.cc && ./a.out

The result I get is:

   Assertion failed: (tn1() == tn2()), function test, file ./header.h, line 10.

I also attached an archive with the four files + build.sh.

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