File: constructor_destructor2.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 (84 lines) | stat: -rw-r--r-- 1,890 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

#include "test.h"


// Attributes defined BEFORE declarations
__attribute__((constructor(200))) void ctor2(void);
__attribute__((constructor(100))) void ctor1(void);
__attribute__((destructor(200))) void dtor2(void);
__attribute__((destructor(100))) void dtor1(void);

int step = 0;

void before_main_low(void) __attribute__((constructor(500)));
void before_main_high(void) __attribute__((constructor(400)));

void after_main_low(void) __attribute__((destructor(500)));
void after_main_high(void) __attribute__((destructor(400)));


// Attributes defined AFTER declarations
void ctor3(void) __attribute__((constructor(300)));
void dtor3(void) __attribute__((destructor(300)));

void ctor1(void) {
    printf("Constructor 100 (first)\n");
    step += 100;
    ASSERT(100, step);
}
void ctor2(void) {
    printf("Constructor 200 (second)\n");
    step +=100;
    ASSERT(200, step);
}
void ctor3(void) {
    printf("Constructor 300 (third)\n");
    step += 100;
    ASSERT(300, step);
}

void dtor1(void) {
    printf("Destructor 100 (first)\n");
    step -= 100;
    ASSERT(0, step);
}
void dtor2(void) {
    printf("Destructor 200 (second)\n");
    step -= 100;
    ASSERT(100, step);
}
void dtor3(void) {
    printf("Destructor 300 (last)\n");
    step -= 100;
    ASSERT(200, step);    
}


void before_main_low(void) {
    printf("Constructor with priority 500 (runs fifth)\n");
    step += 100;
    ASSERT(500, step); 
}

void before_main_high(void) {
    printf("Constructor with priority 400 (runs fourth)\n");
    step += 100;
    ASSERT(400, step); 
}

void after_main_low(void) {
    printf("Destructor with priority 500 (runs fifth)\n");
    step -= 100;
    ASSERT(400, step);  
}

void after_main_high(void) {
    printf("Destructor with priority 400 (runs fourth)\n");
    step -= 100;
    ASSERT(300, step); 
}

int main(void) {
    printf("Inside main()\n");
    return 0;
}