1 module it.c.compile.typedef_; 2 3 import it; 4 5 // 0 children 6 // underlying: UChar("unsigned char") 7 // underlying canonical: UChar("unsigned char") 8 @("unsigned char") 9 unittest { 10 shouldCompile( 11 C( 12 q{ 13 typedef unsigned char __u_char; 14 } 15 ), 16 D( 17 q{ 18 static assert(__u_char.sizeof == 1); 19 } 20 ) 21 ); 22 } 23 24 // 0 children 25 // underlying: Pointer("const char *") 26 // underlying canonical: Pointer("const char *") 27 @("const char*") 28 unittest { 29 shouldCompile( 30 C( 31 q{ 32 typedef const char* mystring; 33 } 34 ), 35 D( 36 q{ 37 const(char)[128] buffer; 38 } 39 ) 40 ); 41 } 42 43 // 1 child: StructDecl(""), Type.Record("Foo") 44 // underlying: Elaborated("struct Foo") 45 // underlying canonical: Record("Foo") 46 @("anonymous struct") 47 unittest { 48 shouldCompile( 49 C( 50 q{ 51 typedef struct { int i; } Foo; 52 } 53 ), 54 D( 55 q{ 56 Foo f; 57 f.i = 42; 58 static assert(!__traits(compiles, _Anonymous_1(42))); 59 } 60 ) 61 ); 62 } 63 64 // 1 child, StructDecl("Foo"), Type.Record("struct Foo") 65 // underlying: Elaborated("struct Foo") 66 // underlying canonical: Record("struct Foo") 67 @("struct") 68 unittest { 69 shouldCompile( 70 C( 71 q{ 72 typedef struct Foo { int i; } Foo; 73 } 74 ), 75 D( 76 q{ 77 Foo f1; 78 f1.i = 42; 79 Foo f2; 80 f2.i = 33; 81 } 82 ) 83 ); 84 } 85 86 // 1 child, UnionDecl("Foo"), Type.Record("union Foo") 87 // underlying: Elaborated("union Foo") 88 // underlying canonical: Record("union Foo") 89 @("union") 90 unittest { 91 shouldCompile( 92 C( 93 q{ 94 typedef union Foo { int i; } Foo; 95 } 96 ), 97 D( 98 q{ 99 Foo f1; 100 f1.i = 42; 101 Foo f2; 102 f2.i = 33; 103 } 104 ) 105 ); 106 } 107 108 // 1 child, EnumDecl("Foo"), Type.Enum("enum Foo") 109 // underlying: Elaborated("enum Foo") 110 // underlying canonical: Enum("enum Foo") 111 @("enum") 112 unittest { 113 shouldCompile( 114 C( 115 q{ 116 typedef enum Foo { i } Foo; 117 } 118 ), 119 D( 120 q{ 121 Foo f = Foo.i; 122 } 123 ) 124 ); 125 } 126 127 128 // 1 child, IntegerLiteral(""), Type.Int("int") 129 // underlying: ConstantArray("int [42]") 130 // underlying canonical: ConstantArray("int [42]") 131 @("array") 132 unittest { 133 shouldCompile( 134 C( 135 q{ 136 typedef int Array[42]; 137 } 138 ), 139 D( 140 q{ 141 Array a; 142 static assert(a.sizeof == 42 * int.sizeof); 143 } 144 ) 145 ); 146 } 147 148 // 0 children 149 // underling: Pointer("int *") 150 // underlying canonical: Pointer("int *") 151 @("pointer") 152 unittest { 153 shouldCompile( 154 C( 155 q{ 156 typedef int* IntPtr; 157 } 158 ), 159 D( 160 q{ 161 int i = 42; 162 IntPtr ptr = &i; 163 } 164 ) 165 ); 166 } 167 168 @("typedef multiple definitions") 169 @safe unittest { 170 // See https://github.com/tpn/winsdk-10/blob/9b69fd26ac0c7d0b83d378dba01080e93349c2ed/Include/10.0.16299.0/um/winscard.h#L504-L528 171 with(immutable IncludeSandbox()) { 172 writeFile("hdr.h", 173 ` 174 typedef struct { 175 int dwStructSize; 176 int lpstrGroupNames; 177 } a, *b, *c; 178 179 typedef struct { 180 int dwStructSize; 181 int lpstrGroupNames; 182 } x, *y, *z; 183 184 #ifdef SOMETHING 185 typedef x X; 186 typedef y Y; 187 typedef z Z; 188 #else 189 typedef a X; 190 typedef b Y; 191 typedef c Z; 192 #endif 193 `); 194 195 writeFile("main.dpp", 196 ` 197 #include "hdr.h" 198 void main() { } 199 `); 200 201 runPreprocessOnly("main.dpp"); 202 203 shouldCompile("main.d"); 204 } 205 } 206 207 208 @("check anonymous struct is defined") 209 @safe unittest { 210 with(immutable IncludeSandbox()) { 211 writeFile("hdr.h", 212 ` 213 typedef struct { 214 void * pad[2]; 215 void * userContext; 216 } * NDR_SCONTEXT; 217 218 typedef struct A { 219 NDR_SCONTEXT k; 220 }; 221 `); 222 223 writeFile("main.dpp", 224 ` 225 #include "hdr.h" 226 void main() { } 227 `); 228 229 runPreprocessOnly("main.dpp"); 230 231 shouldCompile("main.d"); 232 } 233 } 234 235 @("anon struct declaration should be missing") 236 @safe unittest { 237 with(immutable IncludeSandbox()) { 238 writeFile("hdr.h", 239 ` 240 typedef struct { 241 void * pad[2]; 242 void * userContext; 243 } * NDR_SCONTEXT; 244 245 typedef struct A { 246 NDR_SCONTEXT * k; 247 }; 248 249 void test(NDR_SCONTEXT * x); 250 `); 251 252 writeFile("main.dpp", 253 ` 254 #include "hdr.h" 255 void main() { } 256 `); 257 258 runPreprocessOnly("main.dpp"); 259 260 shouldCompile("main.d"); 261 } 262 }