File: variadic_struct.c

package info (click to toggle)
chibicc 1.0.23.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,832 kB
  • sloc: ansic: 62,911; sh: 275; makefile: 92
file content (97 lines) | stat: -rw-r--r-- 1,440 bytes parent folder | download
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
#include <stdarg.h>
#include "test.h"
typedef struct {
  int x;
  int y;
} Point;


typedef struct {
  int i;
  double d;
} Mixed;


typedef union {
  int i;
  float f;
} IntOrFloat;


typedef struct {
  long a;
  long b;
} Pair;


typedef struct {
  int a, b, c, d, e;
} BigStruct;

int test_big_struct(int count, ...) {
  va_list ap;
  va_start(ap, count);

  BigStruct bs = va_arg(ap, BigStruct);

  va_end(ap);

  return bs.a + bs.b + bs.c + bs.d + bs.e;
}


long test_multi(int count, ...) {
  va_list ap;
  va_start(ap, count);

  int x = va_arg(ap, int);
  Pair p = va_arg(ap, Pair);
  double d = va_arg(ap, double);

  va_end(ap);

  return x + p.a + p.b + (long)d;
}


int test_union_int(int count, ...) {
  va_list ap;
  va_start(ap, count);

  IntOrFloat u = va_arg(ap, IntOrFloat);

  va_end(ap);
  return u.i;
}

int test_mixed_struct(int count, ...) {
  va_list ap;
  va_start(ap, count);

  Mixed m = va_arg(ap, Mixed);

  va_end(ap);

  return m.i + (int)m.d;
}


int test_struct_sum(int count, ...) {
  va_list ap;
  va_start(ap, count);

  Point p = va_arg(ap, Point);

  va_end(ap);

  return p.x + p.y;
}

int main() {
    ASSERT(30, test_struct_sum(1, (Point){10, 20}));
    ASSERT(15, test_mixed_struct(1, (Mixed){5, 10.5}));
    ASSERT(42, test_union_int(1, (IntOrFloat){42}));
    ASSERT(1 + 2 + 3 + 4, test_multi(3, 1, (Pair){2,3}, 4.0));
    ASSERT(15, test_big_struct(1, (BigStruct){1,2,3,4,5}));
    return 0;
}