1 module it.c.compile.preprocessor;
2 
3 
4 import it;
5 
6 
7 @("simple macro")
8 @safe unittest {
9     shouldCompile(
10         C(
11             `
12                 #define FOO 5
13             `
14         ),
15         D(
16             q{
17                 int[FOO] foos;
18                 static assert(foos.length == 5, "Wrong length for foos");
19             }
20         )
21     );
22 }
23 
24 @("define macro, undefine, then define again")
25 @safe unittest {
26     shouldCompile(
27         C(
28             `
29                 #define FOO foo
30                 #undef FOO
31                 #define FOO bar
32                 int FOO(int i);
33             `
34         ),
35         D(
36             q{
37                 int i = bar(2);
38             }
39         )
40     );
41 }
42 
43 
44 @("include guards")
45 @safe unittest {
46     with(immutable IncludeSandbox()) {
47         writeFile("hdr.h",
48                   `#ifndef HAHA
49                    #    define HAHA
50                    int inc(int);
51                    #endif`);
52         writeFile("foo.dpp",
53                   `#include "hdr.h"
54                    import bar;
55                    int func(int i) { return inc(i) * 2; }`);
56         writeFile("bar.dpp",
57                   `#include "hdr.h";
58                    int func(int i) { return inc(i) * 3; }`);
59 
60         runPreprocessOnly("foo.dpp");
61         runPreprocessOnly("bar.dpp");
62         shouldCompile("foo.d");
63     }
64 }
65 
66 
67 @("octal.whitespace")
68 @safe unittest {
69     shouldCompile(
70         C(
71             `
72                 #define FOO	   00
73             `
74         ),
75         D(
76             q{
77             }
78         )
79     );
80 
81 }
82 
83 
84 @("elaborate")
85 @safe unittest {
86     shouldCompile(
87         C(
88             `
89                 struct Foo {};
90                 #define STRUCT_HEAD \
91                     int count; \
92                     struct Foo *foo;
93             `
94         ),
95         D(
96             q{
97                 static struct Derived {
98                     STRUCT_HEAD
99                 }
100 
101                 auto d = Derived();
102                 d.count = 42;
103                 d.foo = null;
104             }
105         )
106     );
107 }
108 
109 
110 version(Posix)  // FIXME
111 @("multiline")
112 @safe unittest {
113     shouldCompile(
114         C(
115             `
116                 // WARNING: don't attempt to tidy up the formatting here or the
117                 // test is actually changed
118 #define void_to_int_ptr(x) ( \
119     (int *) x \
120 )
121             `
122         ),
123         D(
124             q{
125                 import std.stdio: writeln;
126                 int a = 7;
127                 void *p = &a;
128                 auto intPtr = void_to_int_ptr(p);
129             }
130         )
131     );
132 }