-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathCodeGenTypeTests.cs
More file actions
469 lines (395 loc) · 10 KB
/
CodeGenTypeTests.cs
File metadata and controls
469 lines (395 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
// SPDX-FileCopyrightText: 2025 Cesium contributors <https://github.com/ForNeVeR/Cesium>
//
// SPDX-License-Identifier: MIT
using System.Diagnostics.CodeAnalysis;
using Cesium.TestFramework;
using JetBrains.Annotations;
namespace Cesium.CodeGen.Tests;
public class CodeGenTypeTests : CodeGenTestBase
{
[MustUseReturnValue]
private static Task DoTest([StringSyntax("cpp")] string source, string @namespace = "", string globalTypeFqn = "")
{
var assembly = GenerateAssembly(default, @namespace: @namespace, globalTypeFqn: globalTypeFqn, sources: source);
return VerifyTypes(assembly);
}
[Fact]
public Task GlobalVariableTest() => DoTest(@"int x = 50;
int main()
{
x = x + 1;
return x;
}",
"", "TestClass");
[Fact]
public Task GlobalVariableModuleTest() => DoTest(@"int x = 50;
int main()
{
x = x + 1;
return x;
}");
[Fact]
public Task NamespaceTest() => DoTest(@"int foo()
{
return 42;
}
int main()
{
return foo();
}",
"TestNameSpace", "TestClass");
[Fact]
public Task GlobalClassTest() => DoTest(@"int foo()
{
return 42;
}
int main()
{
return foo();
}",
"",
"TestClass");
[Fact]
public Task GlobalClassFqnTest() => DoTest(@"int foo()
{
return 42;
}
int main()
{
return foo();
}",
"",
"MyNameSpace.TestClass");
[Fact]
public Task ConstCharLiteralTest() => DoTest(@"int main()
{
const char *test = ""hellow"";
}");
[Fact]
public Task ConstIntSmallLiteralTest() => DoTest(@"int main()
{
return 42;
}");
[Fact]
public Task TypeConversionTest() => DoTest(@"int main()
{
return (int)42;
}");
[Fact]
public Task ConstIntLargeLiteralTest() => DoTest(@"int main()
{
return 1337;
}");
[Fact]
public Task ConstIntMinLiteralTest() => DoTest(@"int main()
{
return -2147483648;
}");
[Fact]
public Task ConstIntHexLiteralTest() => DoTest(@"int main()
{
return 0x42;
}");
[Fact]
public Task ConstIntOctalLiteralTest() => DoTest(@"int main()
{
return 042;
}");
[Fact]
public Task ConstCharLiteralDeduplication() => DoTest(@"int main()
{
const char *test1 = ""hellow"";
const char *test2 = ""hellow1"";
const char *test3 = ""hellow"";
}");
[Fact, NoVerify]
public void AbsentForwardDeclaration() => DoesNotCompile(@"int foo()
{
return bar();
}
int bar()
{
return 0;
}", "Function \"bar\" was not found.");
[Fact]
public Task FunctionForwardDeclaration() => DoTest(@"int bar(void);
int foo(void)
{
return bar();
}
int bar(void)
{
return 0;
}");
[Fact]
public Task EmptyFunctionDeclaration() => DoTest(@"
void foo(void)
{
}");
[Fact]
public Task SingleFieldStructDefinition() => DoTest("typedef struct { int x; } foo;");
[Fact]
public Task TypeDefStructUsage() => DoTest(@"typedef struct { int x; } foo;
int main(void) { foo x; x.x = 0; return 0; }");
[Fact]
public Task StructUsageWithPointerMemberAccessGet() => DoTest(@"typedef struct { int x; } foo;
int main(void) { foo *x; return x->x; }");
[Fact]
public Task StructUsageWithPointerMemberAccessSet() => DoTest(@"typedef struct { int x; } foo;
int main(void) { foo *x; x->x = 42; return 0; }");
[Fact]
public Task StructAddressWithPointerMemberAccessGet() => DoTest(@"typedef struct { int x; } foo;
int main(void) { foo x; return (&x)->x; }");
[Fact]
public Task StructAddressWithPointerMemberAccessSet() => DoTest(@"typedef struct { int x; } foo;
int main(void) { foo x; (&x)->x = 42; return 0; }");
[Fact]
public Task StructUsageWithMemberAccessGet() => DoTest(@"typedef struct { int x; } foo;
int main(void) { foo x; return x.x; }");
[Fact]
public Task StructUsageWithMemberAccessSet() => DoTest(@"typedef struct { int x; } foo;
int main(void) { foo x; x.x = 42; return 0; }");
[Fact]
public Task StructAndTypeDefHasSeparateNamespaces() => DoTest(@"struct tagFoo { int A; };
typedef struct { int B; } Foo;
int main(void) {
struct tagFoo a;
Foo b;
a.A = 0;
b.B = 0;
}
");
[Fact]
public Task SingleFieldStructWithUnionDefinition() => DoTest(@"typedef struct { union { int x; float f; }; } foo;
int main ()
{
foo bar;
bar.f = 5.2f;
return bar.x;
}");
[Fact, NoVerify]
public void BadStructWithUnionDefinition() => DoesNotCompile(@"typedef struct { union { int x; float f; }; union { int x; float f; }; } foo;
int main ()
{
foo bar;
bar.f = 5.2f;
return bar.x;
}", "Struct has multiple suitable members named \"f\".");
[Fact]
public Task MegaUnionDefinition() => DoTest(@"typedef struct { union { union { int x1; float x2; union { int x2; float f2; union { int x3; float f3; union { int x4; float f4; };};};}; }; } foo;
int main ()
{
foo bar;
bar.f4 = 5.2f;
return bar.x2;
}");
[Fact]
public Task StructWithUnionsAndAnons() => DoTest(@"
typedef struct {
int _1;
struct {
int _2a;
};
union {
long _3u;
int _4u;
};
union {
long _5u;
int _6u;
} uni;
struct {
int _7;
} s;
} foo;
int main() {
foo f;
f._1 = 2;
f._2a = 10;
f._3u = 10;
f.uni._5u = 10;
f.s._7 = 10;
return f._1 + f._2a + f._4u + f.uni._6u + f.s._7;
}
");
[Fact]
public Task MultipleFieldStructWithUnionDefinition() => DoTest("typedef struct { long l; union { int x; float f; }; } foo;");
[Fact]
public Task ArrayDeclaration() => DoTest(@"int main()
{
int i;
int x[1];
return 0;
}");
[Fact]
public Task StructFunctionMemberDeclaration() => DoTest(@"typedef struct { void (*bar)(int unused); } foo;
int main(void) {}");
[Fact]
public Task BasicTypeDef() => DoTest(@"typedef int foo;
int main(void) { foo x = 0; return 0; }");
[Fact]
public Task StructWithArray() => DoTest(@"typedef struct {
int x[4];
} foo;");
[Fact]
public Task NamedStruct() => DoTest(@"struct foo {
int x[4];
};");
[Fact]
public Task FunctionPointer() => DoTest(@"void foo(int) {}
typedef int (*foo_t)(int);
int main(void) {
foo_t x = &foo;
}");
[Fact, NoVerify]
public void NonExistingStructMember() => DoesNotCompile(@"typedef struct { int x; } foo;
int main(void) {
foo x;
return x.nonExisting;
}", "has no member named \"nonExisting\"");
[Fact]
public Task ComplexStructDefinition() => DoTest(@"typedef void(*function)(int, const int*, const int*);
typedef struct {
int a;
int b[5];
unsigned char c[64];
function func;
int array[80][5];
} foo;");
[Fact]
public Task StaticFileScopedVariable() => DoTest(@"static int x = 123;");
[Fact]
public Task StaticStruct() => DoTest(@"struct _foo {
int x[4];
};
static struct _foo foo;");
[Fact, NoVerify]
public void StructAndEnumSameName() => DoesNotCompile(@"enum Token { T };
struct Token {
int x;
};
", "Tag kind struct type Token was already defined as enum");
[Fact, NoVerify]
public void EnumAndStructSameName() => DoesNotCompile(@"
struct Token {
int x;
};
enum Token { T };
", "Tag kind enum type Token was already defined as struct");
[Fact]
public Task StructAndTypeDefWithSameName() => DoTest(@"typedef struct Token Token;
struct Token {
int x;
};
");
[Fact]
public Task StructAndTypeDefWithSameNameSingleDecl() => DoTest(@"
typedef struct Token {
int x;
} Token;
");
[Fact]
public Task StructAndNestedPointer() => DoTest(@"typedef struct Token Token;
struct Token {
Token* x;
};
");
[Fact]
public Task StructInitialization() => DoTest(@"typedef struct Foo { int a; int b; } Foo;
int main() {
Foo f = { 1, 2 };
return f.a + f.b;
}
");
[Fact]
public Task StructZeroInitialization() => DoTest(@"typedef struct Foo { int a; int b; } Foo;
int main() {
Foo f = { };
return f.a + f.b;
}
");
[Fact]
public Task StructNamedInitialization() => DoTest(@"typedef struct Foo { int a; int b; } Foo;
int main() {
Foo f = { .b = 1, .a = 2 };
return f.a + f.b;
}
");
[Fact]
public Task StructWithArrayInitialization1() => DoTest(@"typedef struct Foo { int a; int b[2]; } Foo;
int main() {
Foo f = { .b[1] = 1, .b[0] = 2, .a = 32 };
return f.a + f.b[0] + f.b[1];
}
");
[Fact]
public Task StructWithArrayInitialization2() => DoTest(@"typedef struct Foo { int b[2]; int a; } Foo;
int main() {
Foo f = { { 1, 2 }, 32 };
return f.a + f.b[0] + f.b[1];
}
");
[Fact]
public Task StructVariableInitialization() => DoTest(@"typedef struct Foo { int a; int b; } Foo;
int main() {
int c = 1;
Foo f = { c, 2 };
return f.a + f.b;
}
");
[Fact]
public Task StructCastInitialization() => DoTest(@"typedef struct Foo { int a; int b; } Foo;
int main() {
int c = 1;
Foo f = { (int)c, 2 };
return f.a + f.b;
}
");
[Fact]
public Task SuperHardStructInitialization() => DoTest(@"
typedef struct Foo
{
int a; int b; // 2 + 2 = 4;
struct { long _1; long _2; } inner;
struct { long he; long ha; } other_inner;
union { int integer; float f; };
struct { int anon_int; };
union { int not_anon; float its; } named_union;
struct { struct { int level_3; } level_2; } level_1;
} Foo;
int main() {
Foo f = { .a = 2, 2, {2,2}, { .he = 2, .ha = 2 }, .anon_int = 5, .integer = 5, .named_union.not_anon = 10, .level_1.level_2.level_3 = 10 };
return f.a + f.b + f.inner._1 + f.inner._2 + f.other_inner.ha + f.other_inner.he + f.level_1.level_2.level_3 + f.named_union.not_anon + f.anon_int + f.integer;
}
");
[Fact]
public Task StructForwardDeclaration() => DoTest(@"struct foo;
struct foo { int x; };");
[Fact(Skip = "TODO[#552]: Support local struct types")]
public Task LocalStructTest() => DoTest("""
int main(void) {
struct foo {
int x;
} bar;
bar.x = 42;
return bar.x;
}
""");
[Fact]
public Task ResolutionOfRecursiveTypes() => DoTest(@"
typedef struct Node Node;
struct Node {
Node *parent;
};
Node *root = &(Node){0};
");
[Fact]
public Task AssigningStructsViaPointers() => DoTest("""
typedef struct {
int x;
} foo;
void foo(foo* p1, foo* p2) {
*p1 = *p2;
}
""");
}