Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++11 -cxx-abi microsoft -fms-compatibility -fsyntax-only -triple=i386-pc-win32 -verify %s |
| 2 | // RUN: %clang_cc1 -std=c++11 -cxx-abi microsoft -fms-compatibility -fsyntax-only -triple=x86_64-pc-win32 -verify %s |
| 3 | // |
| 4 | // This file should also give no diagnostics when run through cl.exe from MSVS |
| 5 | // 2012, which supports C++11 and static_assert. It should pass for both 64-bit |
| 6 | // and 32-bit x86. |
| 7 | // |
| 8 | // expected-no-diagnostics |
Joao Matos | 679fc93 | 2012-09-04 17:18:12 +0000 | [diff] [blame] | 9 | |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 10 | // Test the size of various member pointer combinations: |
| 11 | // - complete and incomplete |
| 12 | // - single, multiple, and virtual inheritance (and unspecified for incomplete) |
| 13 | // - data and function pointers |
| 14 | // - templated with declared specializations with annotations |
| 15 | // - template that can be instantiated |
Charles Davis | d18f9f9 | 2010-08-16 04:01:50 +0000 | [diff] [blame] | 16 | |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 17 | // http://llvm.org/PR12070 |
| 18 | struct Foo { |
| 19 | typedef int Foo::*FooInt; |
| 20 | int f; |
| 21 | }; |
Charles Davis | f231df3 | 2010-08-16 05:30:44 +0000 | [diff] [blame] | 22 | |
Reid Kleckner | 84e9ab4 | 2013-03-28 20:02:56 +0000 | [diff] [blame] | 23 | enum { |
| 24 | kSingleDataSize = 1 * sizeof(int), |
| 25 | kSingleFunctionSize = 1 * sizeof(void *), |
| 26 | kMultipleDataSize = 1 * sizeof(int), |
| 27 | kMultipleFunctionSize = 2 * sizeof(void *), |
| 28 | kVirtualDataSize = 2 * sizeof(int), |
| 29 | kVirtualFunctionSize = 2 * sizeof(int) + 1 * sizeof(void *), |
| 30 | // Unspecified is weird, it's 1 more slot than virtual. |
| 31 | kUnspecifiedDataSize = kVirtualDataSize + 1 * sizeof(int), |
| 32 | kUnspecifiedFunctionSize = kVirtualFunctionSize + 1 * sizeof(void *), |
| 33 | }; |
| 34 | |
| 35 | // incomplete types |
| 36 | class __single_inheritance IncSingle; |
| 37 | class __multiple_inheritance IncMultiple; |
| 38 | class __virtual_inheritance IncVirtual; |
| 39 | static_assert(sizeof(int IncSingle::*) == kSingleDataSize, ""); |
| 40 | static_assert(sizeof(int IncMultiple::*) == kMultipleDataSize, ""); |
| 41 | static_assert(sizeof(int IncVirtual::*) == kVirtualDataSize, ""); |
| 42 | static_assert(sizeof(void (IncSingle::*)()) == kSingleFunctionSize, ""); |
| 43 | static_assert(sizeof(void (IncMultiple::*)()) == kMultipleFunctionSize, ""); |
| 44 | static_assert(sizeof(void (IncVirtual::*)()) == kVirtualFunctionSize, ""); |
| 45 | |
| 46 | // An incomplete type with an unspecified inheritance model seems to take one |
| 47 | // more slot than virtual. It's not clear what it's used for yet. |
| 48 | class IncUnspecified; |
| 49 | static_assert(sizeof(int IncUnspecified::*) == kUnspecifiedDataSize, ""); |
| 50 | static_assert(sizeof(void (IncUnspecified::*)()) == kUnspecifiedFunctionSize, ""); |
| 51 | |
| 52 | // complete types |
| 53 | struct B1 { }; |
| 54 | struct B2 { }; |
| 55 | struct Single { }; |
| 56 | struct Multiple : B1, B2 { }; |
| 57 | struct Virtual : virtual B1 { }; |
| 58 | static_assert(sizeof(int Single::*) == kSingleDataSize, ""); |
| 59 | static_assert(sizeof(int Multiple::*) == kMultipleDataSize, ""); |
| 60 | static_assert(sizeof(int Virtual::*) == kVirtualDataSize, ""); |
| 61 | static_assert(sizeof(void (Single::*)()) == kSingleFunctionSize, ""); |
| 62 | static_assert(sizeof(void (Multiple::*)()) == kMultipleFunctionSize, ""); |
| 63 | static_assert(sizeof(void (Virtual::*)()) == kVirtualFunctionSize, ""); |
| 64 | |
| 65 | // Test both declared and defined templates. |
| 66 | template <typename T> class X; |
| 67 | template <> class __single_inheritance X<IncSingle>; |
| 68 | template <> class __multiple_inheritance X<IncMultiple>; |
| 69 | template <> class __virtual_inheritance X<IncVirtual>; |
| 70 | // Don't declare X<IncUnspecified>. |
| 71 | static_assert(sizeof(int X<IncSingle>::*) == kSingleDataSize, ""); |
| 72 | static_assert(sizeof(int X<IncMultiple>::*) == kMultipleDataSize, ""); |
| 73 | static_assert(sizeof(int X<IncVirtual>::*) == kVirtualDataSize, ""); |
| 74 | static_assert(sizeof(int X<IncUnspecified>::*) == kUnspecifiedDataSize, ""); |
| 75 | static_assert(sizeof(void (X<IncSingle>::*)()) == kSingleFunctionSize, ""); |
| 76 | static_assert(sizeof(void (X<IncMultiple>::*)()) == kMultipleFunctionSize, ""); |
| 77 | static_assert(sizeof(void (X<IncVirtual>::*)()) == kVirtualFunctionSize, ""); |
| 78 | static_assert(sizeof(void (X<IncUnspecified>::*)()) == kUnspecifiedFunctionSize, ""); |
| 79 | |
| 80 | template <typename T> |
| 81 | struct Y : T { }; |
| 82 | static_assert(sizeof(int Y<Single>::*) == kSingleDataSize, ""); |
| 83 | static_assert(sizeof(int Y<Multiple>::*) == kMultipleDataSize, ""); |
| 84 | static_assert(sizeof(int Y<Virtual>::*) == kVirtualDataSize, ""); |
| 85 | static_assert(sizeof(void (Y<Single>::*)()) == kSingleFunctionSize, ""); |
| 86 | static_assert(sizeof(void (Y<Multiple>::*)()) == kMultipleFunctionSize, ""); |
| 87 | static_assert(sizeof(void (Y<Virtual>::*)()) == kVirtualFunctionSize, ""); |
| 88 | |
| 89 | struct A { int x; void bar(); }; |
| 90 | struct B : A { virtual void foo(); }; |
| 91 | static_assert(sizeof(int B::*) == kSingleDataSize, ""); |
| 92 | // A non-primary base class uses the multiple inheritance model for member |
| 93 | // pointers. |
| 94 | static_assert(sizeof(void (B::*)()) == kMultipleFunctionSize, ""); |
| 95 | |
| 96 | struct AA { int x; virtual void foo(); }; |
| 97 | struct BB : AA { void bar(); }; |
| 98 | struct CC : BB { virtual void baz(); }; |
| 99 | static_assert(sizeof(void (CC::*)()) == kSingleFunctionSize, ""); |
| 100 | |
| 101 | // We start out unspecified. |
| 102 | struct ForwardDecl1; |
| 103 | struct ForwardDecl2; |
| 104 | |
| 105 | // Re-declare to force us to iterate decls when adding attributes. |
| 106 | struct ForwardDecl1; |
| 107 | struct ForwardDecl2; |
| 108 | |
| 109 | typedef int ForwardDecl1::*MemPtr1; |
| 110 | typedef int ForwardDecl2::*MemPtr2; |
| 111 | MemPtr1 variable_forces_sizing; |
| 112 | |
| 113 | struct ForwardDecl1 : B { |
| 114 | virtual void foo(); |
| 115 | }; |
| 116 | struct ForwardDecl2 : B { |
| 117 | virtual void foo(); |
| 118 | }; |
| 119 | |
| 120 | static_assert(sizeof(variable_forces_sizing) == kUnspecifiedDataSize, ""); |
| 121 | static_assert(sizeof(MemPtr1) == kUnspecifiedDataSize, ""); |
| 122 | // FIXME: Clang fails this assert because it locks in the inheritance model at |
| 123 | // the point of the typedef instead of the first usage, while MSVC does not. |
| 124 | //static_assert(sizeof(MemPtr2) == kSingleDataSize, ""); |
| 125 | |
| 126 | struct MemPtrInBody { |
| 127 | typedef int MemPtrInBody::*MemPtr; |
| 128 | int a; |
| 129 | operator MemPtr() const { |
| 130 | return a ? &MemPtrInBody::a : 0; |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | static_assert(sizeof(MemPtrInBody::MemPtr) == kSingleDataSize, ""); |
Reid Kleckner | e93e255 | 2013-03-28 23:11:29 +0000 | [diff] [blame] | 135 | |
| 136 | // Passing a member pointer through a template should get the right size. |
| 137 | template<typename T> |
| 138 | struct SingleTemplate; |
| 139 | template<typename T> |
| 140 | struct SingleTemplate<void (T::*)(void)> { |
| 141 | static_assert(sizeof(int T::*) == kSingleDataSize, ""); |
| 142 | static_assert(sizeof(void (T::*)()) == kSingleFunctionSize, ""); |
| 143 | }; |
| 144 | |
| 145 | template<typename T> |
| 146 | struct UnspecTemplate; |
| 147 | template<typename T> |
| 148 | struct UnspecTemplate<void (T::*)(void)> { |
| 149 | static_assert(sizeof(int T::*) == kUnspecifiedDataSize, ""); |
| 150 | static_assert(sizeof(void (T::*)()) == kUnspecifiedFunctionSize, ""); |
| 151 | }; |
| 152 | |
| 153 | struct NewUnspecified; |
| 154 | SingleTemplate<void (IncSingle::*)()> tmpl_single; |
| 155 | UnspecTemplate<void (NewUnspecified::*)()> tmpl_unspec; |
| 156 | |
| 157 | struct NewUnspecified { }; |
| 158 | |
| 159 | static_assert(sizeof(void (NewUnspecified::*)()) == kUnspecifiedFunctionSize, ""); |
| 160 | |
| 161 | template <typename T> |
| 162 | struct MemPtrInTemplate { |
| 163 | // We can't require that the template arg be complete until we're |
| 164 | // instantiated. |
| 165 | int T::*data_ptr; |
| 166 | void (T::*func_ptr)(); |
| 167 | }; |