ocl-include 0.6.0

Simple preprocessor that implements #include mechanism for OpenCL source files
Documentation
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
use std::path::Path;

use indoc::indoc;

use crate::*;

#[test]
fn main_only() {
    let main = indoc! {"
        int main() {
            return RET_CODE;
        }
    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("main.c"), main.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder().add_source(hook).build();
    let node = parser.parse(Path::new("main.c")).unwrap();

    assert_eq!(node.collect().0, main);
}

#[test]
fn single_header() {
    let main = indoc! {"
        #include <header.h>
        #include <header.h>
        // Main function
        int main() {
            return RET_CODE;
        }
    "};
    let header = indoc! {"
        #pragma once
        // Return code
        static const int RET_CODE = 0;
    "};
    let result = indoc! {"


        // Return code
        static const int RET_CODE = 0;

        // Main function
        int main() {
            return RET_CODE;
        }
    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("main.c"), main.to_string())
        .unwrap()
        .add_file(&Path::new("header.h"), header.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder().add_source(hook).build();
    let node = parser.parse(Path::new("main.c")).unwrap();

    assert_eq!(node.collect().0, result);
}

#[test]
#[should_panic]
fn recursion() {
    let first = indoc! {"
        #include <second.h>
    "};
    let second = indoc! {"
        #include <first.h>
    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("first.h"), first.to_string())
        .unwrap()
        .add_file(&Path::new("second.h"), second.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder().add_source(hook).build();
    parser.parse(Path::new("first.h")).unwrap();
}

#[test]
fn recursion_prevented() {
    let first = indoc! {"
        #pragma once
        #include <second.h>
    "};
    let second = indoc! {"
        #pragma once
        #include <first.h>
    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("first.h"), first.to_string())
        .unwrap()
        .add_file(&Path::new("second.h"), second.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder().add_source(hook).build();
    let node = parser.parse(Path::new("first.h")).unwrap();

    assert_eq!(node.collect().0, "\n\n\n\n");
}

#[test]
fn multiple_headers() {
    let main = indoc! {"
        #include <h02.h>
        #include <h01.h>
    "};
    let h01 = indoc! {"
        #pragma once
        #include <h02.h>
        h01
    "};
    let h02 = indoc! {"
        #pragma once
        #include <h01.h>
        h02
    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("main.c"), main.to_string())
        .unwrap()
        .add_file(&Path::new("h01.h"), h01.to_string())
        .unwrap()
        .add_file(&Path::new("h02.h"), h02.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder().add_source(hook).build();
    let node = parser.parse(Path::new("main.c")).unwrap();

    assert_eq!(node.collect().0, "\n\n\n\n\nh01\nh02\n\n");
}

#[test]
fn line_numbers() {
    let main = indoc! {"
        0
        1
        2
        #include <h01.h>
        9
        10
        #include <h03.h>
        15
        16
    "};
    let h01 = indoc! {"
        4
        #include <h02.h>
        8
    "};
    let h02 = indoc! {"
        6
        7
    "};
    let h03 = indoc! {"
        12
        13
        14
    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("main.c"), main.to_string())
        .unwrap()
        .add_file(&Path::new("h01.h"), h01.to_string())
        .unwrap()
        .add_file(&Path::new("h02.h"), h02.to_string())
        .unwrap()
        .add_file(&Path::new("h03.h"), h03.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder().add_source(hook).build();
    let node = parser.parse(Path::new("main.c")).unwrap();

    let source = node.collect().0;
    for (pos, line) in source.lines().enumerate() {
        let tline = line.trim_end();
        if !tline.is_empty() {
            assert_eq!(tline.parse::<usize>().unwrap(), pos);
        }
    }
}

fn assert_line_index(source: &str, index: &Index) {
    for (pos, line) in source.lines().enumerate() {
        let (name, lpos) = index.search(pos).unwrap();
        let tline = line.trim_end();
        if !tline.is_empty() {
            let n = tline.parse::<usize>().unwrap();
            let (f, l) = (n / 10, n % 10);
            assert_eq!(
                match f {
                    0 => String::from("main.c"),
                    x => format!("h0{}.h", x),
                },
                name.to_string_lossy(),
            );
            assert_eq!(l, lpos);
        }
    }
}

#[test]
fn indexing() {
    let main = indoc! {"
        00
        01
        02
        #include <h01.h>
        04
        05
        #include <h03.h>
        07
        08
    "};
    let h01 = indoc! {"
        10
        #include <h02.h>
        12
    "};
    let h02 = indoc! {"
        20
        21
    "};
    let h03 = indoc! {"
        30
        31
        32
    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("main.c"), main.to_string())
        .unwrap()
        .add_file(&Path::new("h01.h"), h01.to_string())
        .unwrap()
        .add_file(&Path::new("h02.h"), h02.to_string())
        .unwrap()
        .add_file(&Path::new("h03.h"), h03.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder().add_source(hook).build();
    let node = parser.parse(Path::new("main.c")).unwrap();

    let (source, index) = node.collect();
    assert_line_index(&source, &index);
}

#[test]
fn indexing_once() {
    let main = indoc! {"
        00
        01
        02
        #include <h01.h>
        04
        05
        #include <h02.h>
        07
        08
    "};
    let h01 = indoc! {"
        #pragma once
        11
        #include <h02.h>
        13
    "};
    let h02 = indoc! {"
        #pragma once
        21
        #include <h01.h>
        23
    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("main.c"), main.to_string())
        .unwrap()
        .add_file(&Path::new("h01.h"), h01.to_string())
        .unwrap()
        .add_file(&Path::new("h02.h"), h02.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder().add_source(hook).build();
    let node = parser.parse(Path::new("main.c")).unwrap();

    let (source, index) = node.collect();
    assert_line_index(&source, &index);
}

#[test]
fn define_gates() {
    let input = indoc! {"
        1
        #ifdef ABC
        2
        #ifdef XYZ
        3
        #ifndef DEF
        4
        #else // DEF
        5
        #endif // DEF
        6
        #else // XYZ
        7
        #endif // XYZ
        8
        #else // !ABC
        9
        #ifdef XYZ
        A
        #else // !XYZ
        B
        #endif // XYZ
        C
        #endif // ABC
        D
        #if defined(ABC)
        E
        #endif // ABC
        F
    "};

    let output = indoc! {"
        1

        2
        #ifdef XYZ
        3



        5

        6
        #else // XYZ
        7
        #endif // XYZ
        8









        D
        #if defined(ABC)
        E
        #endif // ABC
        F
    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("input.c"), input.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder()
        .add_source(hook)
        .add_flag(String::from("ABC"), true)
        .add_flag(String::from("DEF"), true)
        .build();
    let node = parser.parse(Path::new("input.c")).unwrap();

    assert_eq!(node.collect().0, output);
}

#[test]
fn define_gate_include() {
    let main = indoc! {"
        #ifdef ABC
        #include <h0.h>
        #else // !ABC
        #include <h1.h>
        #endif // ABC
    "};
    let h0 = indoc! {"
        H0 0
        H0 1
        H0 2
    "};
    let h1 = indoc! {"
        H1 0
        H1 1
        H1 2
    "};

    let result = indoc! {"




        H1 0
        H1 1
        H1 2

    "};

    let hook = source::Mem::builder()
        .add_file(&Path::new("main.c"), main.to_string())
        .unwrap()
        .add_file(&Path::new("h0.h"), h0.to_string())
        .unwrap()
        .add_file(&Path::new("h1.h"), h1.to_string())
        .unwrap()
        .build();
    let parser = Parser::builder()
        .add_source(hook)
        .add_flag(String::from("ABC"), false)
        .build();
    let node = parser.parse(Path::new("main.c")).unwrap();

    assert_eq!(node.collect().0, result);
}