Passed
Push — feature/fused_src_compress ( 45b9a9 )
by srz
13:54
created

IutestPreprocessor.__eval_expanded_expr()   B

Complexity

Conditions 7

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nop 2
dl 0
loc 21
rs 8
c 0
b 0
f 0
1
#!/usr/bin/env python
2
#
3
# iutest_pp_strip.py
4
#
5
# Copyright (C) 2018, Takazumi Shirayanagi
6
# This software is released under the new BSD License,
7
# see LICENSE
8
#
9
10
import re
11
import collections
12
13
RE_MACRO_SPLIT = re.compile('([\(\):;{} /%+\-=<>!&\|*#]+)')
14
RE_SPLIT_PAREN = re.compile('([\(\)])')
15
RE_FUNC_MACRO = re.compile('([\w_]+)\((.*?)\)')
16
RE_DEFINE = re.compile('#\s*define (\S+)\s*(.*)$')
17
RE_DEFINE_PARSE = re.compile('(.*)defined\((.*?)\)(.*)')
18
RE_HAS_INCLUDE = re.compile('(.*)__has_include\((.*?)\)(.*)')
19
RE_HAS_FEATURE = re.compile('(.*)__has_feature\((.*?)\)(.*)')
20
RE_SPLIT_OP = re.compile('(&&|\|\||!)')
21
RE_SYMBOLMARK = re.compile('([+\-=<>\(\)]+)')
22
RE_PPIF = re.compile('#\s*(ifdef|ifndef|if)\s*(.*)$')
23
RE_PPELIF = re.compile('#\s*elif\s*(.*)$')
24
RE_PPELSE = re.compile('#\s*else\s*$')
25
RE_PPENDIF = re.compile('#\s*endif')
26
RE_AND = re.compile('and')
27
RE_CPP_COMMENT = re.compile('^//.*')
28
RE_SYSTEM_INCLUDE_REGEX = re.compile(r'^\s*#\s*include\s*<(.*)>')
29
30
RE_STRIP_INCG_REGEX = re.compile(r'^INCG_\S*_[IH]PP_\S+\Z')
31
32
RE_EVAL_UNDEFINED_EXCEPTION = re.compile(r'^name \'(defined_.*)\' is not defined\Z')
33
34
35
class IutestPreprocessor:
36
    macros = {}
37
    depth_macros = [ {} ]
38
    expands_macros = []
39
    expand_function_macros = []
40
    iutest_config_macro = []
41
    included_path = [ [] ]
42
    has_include = {}
43
    has_features = {}
44
    unknowns = []
45
    depth = []
46
    brothers = []
47
    debug = False
48
    prev_line = None
49
50
    def __init__(self
51
            , predefined_macros
52
            , iutest_config_macro
53
            , expand_function_macros
54
            , expands_macros
55
            , has_features
56
            , has_include):
57
        self.set_predefined_macros(predefined_macros)
58
        self.set_iutest_config_macro(iutest_config_macro)
59
        self.set_expand_function_macros(expand_function_macros)
60
        self.set_expands_macros(expands_macros)
61
        self.set_has_features(has_features)
62
        self.set_has_include(has_include)
63
64
    def set_predefined_macros(self, predefined_macros):
65
        self.macros = predefined_macros
66
67
    def set_iutest_config_macro(self, iutest_config_macro):
68
        self.iutest_config_macro = iutest_config_macro
69
        self.macros.update(iutest_config_macro)
70
71
    def set_expands_macros(self, expands_macros):
72
        self.expands_macros = expands_macros
73
74
    def set_expand_function_macros(self, expand_function_macros):
75
        self.expand_function_macros = expand_function_macros
76
77
    def set_has_include(self, has_include):
78
        self.has_include = has_include
79
80
    def set_has_features(self, has_features):
81
        self.has_features = has_features
82
83
    def set_debug_flag(self, flag):
84
        self.debug = flag
85
86
    def __expand_macro(self, line):
87
        dst = ""
88
        for s in RE_MACRO_SPLIT.split(line):
89
            if s in self.expands_macros:
90
                expand = self.__get_current_macro(s)
91
                if expand is not None:
92
                    dst += expand
93
                    continue
94
            dst += s
95
        return self.__expand_function_macro(dst)
96
97
    def __expand_function_macro(self, line):
98
        dst = ""
99
        tokens = []
100
        prev = ""
101
        for s in RE_SPLIT_PAREN.split(line):
102
            if s == '(':
103
                tokens.append(prev)
104
            elif s == ')' and len(tokens) > 0:
105
                tokens[-1] += prev + s
106
                s = ""
107
                ss = tokens.pop()
108
                for m in RE_FUNC_MACRO.finditer(ss):
109
                    d = m.group(1)
110
                    if d in self.expand_function_macros:
111
                        if d not in self.macros or self.macros[d] is None:
112
                            ss = ss.replace(m.group(0), '')
113
                if len(tokens) > 0:
114
                    tokens[-1] += ss
115
                else:
116
                    dst += ss
117
            elif len(tokens) > 0:
118
                tokens[-1] += prev
119
            else:
120
                dst += prev
121
            prev = s
122
        for s in tokens:
123
            dst += s
124
        dst += prev
125
        return dst
126
127
    def __has_current_macro(self, name):
128
        if name in self.macros:
129
            return True
130
        for m in self.depth_macros:
131
            if name in m:
132
                return True
133
        return False
134
135
    def __get_current_macro(self, name):
136
        if name in self.macros:
137
            return self.macros[name]
138
        for m in self.depth_macros:
139
            if name in m:
140
                return m[name]
141
        return None
142
143
    def __append_define(self, line):
144
        def append(d, v, depth, macros, unknowns, current):
145
            d = re.sub('\(.*\)', '', d)
146
            if len(v) == 0:
147
                if RE_STRIP_INCG_REGEX.match(d):
148
                    self.expands_macros.append(d)
149
                v = 'defined'
150
            if any(x == -1 for x in depth):
151
                unknowns.append(d)
152
                current[d] = v
153
            else:
154
                macros[d] = v
155
            return d
156
        m = RE_DEFINE.match(line)
157
        if m:
158
            current_depth_macro = self.depth_macros[-1]
159
            return append(m.group(1), m.group(2), self.depth, self.macros, self.unknowns, current_depth_macro)
160
        return None
161
162
    def __expand_ppif_macro(self, expr):
163
        expand = ""
164
        for s in RE_SPLIT_OP.split(expr):
165
            if s == '&&':
166
                expand += ' and '
167
            elif s == '||':
168
                expand += ' or '
169
            elif s == '!':
170
                expand += " not "
171
            else:
172
                m = RE_DEFINE_PARSE.match(s)
173
                if m:
174
                    d = m.group(2)
175
                    if self.__has_current_macro(d):
176
                        expand += m.group(1)
177
                        if self.__get_current_macro(d) is None:
178
                            expand += ' (0) '
179
                        else:
180
                            expand += ' (1) '
181
                        expand += m.group(3)
182
                    elif d in self.unknowns:
183
                        expand += s
184
                    else:
185
                        expand += s
186
                        self.unknowns.append(d)
187
                    continue
188
                m = RE_HAS_INCLUDE.match(s)
189
                if m:
190
                    f = m.group(2)
191
                    if f in self.has_include:
192
                        expand += m.group(1) + self.has_include[f] + m.group(3)
193
                    else:
194
                        expand += s
195
                    continue
196
                m = RE_HAS_FEATURE.match(s)
197
                if m:
198
                    f = m.group(2)
199
                    if f in self.has_features:
200
                        expand += m.group(1) + self.has_features[f] + m.group(3)
201
                        continue
202
                for w in RE_SYMBOLMARK.split(s.strip()):
203
                    if RE_SYMBOLMARK.match(w) or w.isspace():
204
                        expand += w
205
                    elif len(w) > 0:
206
                        if w in self.unknowns:
207
                            expand += w
208
                        elif w in self.macros:
209
                            if self.macros[w] is None:
210
                                expand += '0'
211
                            else:
212
                                expand += self.__expand_ppif_macro(self.macros[w])
213
                        elif w.isdigit():
214
                            expand += w
215
                        else:
216
                            expand += w
217
218
        expand = expand.replace('0(0)', '(0)')
219
        expand = expand.replace('not =', '!=')
220
        expand = re.sub(r'not\s*\(0\)', '(1)', expand)
221
        return expand.strip()
222
223
    def __eval_ppif_unknown_defined(self, expand):
224
        if 'defined' not in expand:
225
            return -1
226
        expand = re.sub(r'defined\((.*?)\)', 'defined_\\1', expand)
227
        try:
228
            r = eval(expand)
229
        except Exception as e:
230
            r = self.__eval_ppif_unknown_defined_(str(e), expand)
231
        return r
232
233
    def __eval_ppif_unknown_defined_(self, exception_str, expand):
234
        def eval_x(d, x):
235
            try:
236
                expanded = expand.replace(d, str(x))
237
                return eval(expanded)
238
            except Exception:
239
                return -1
240
        m = RE_EVAL_UNDEFINED_EXCEPTION.match(exception_str)
241
        if m:
242
            d = m.group(1)
243
            r0 = eval_x(d, 0)
244
            r1 = eval_x(d, 1)
245
            if r0 == r1:
246
                return r0
247
        return -1
248
249
    def __eval_expanded_expr(self, expand_expr):
250
        error = None
251
        try:
252
            r = eval(expand_expr)
253
            if r:
254
                return (1, '1')
255
            else:
256
                return (0, '0')
257
        except Exception as e:
258
            error = e
259
260
        expand = expand_expr
261
        if 'or' not in expand:
262
            for expr in RE_AND.split(expand):
263
                try:
264
                    r = eval(expr)
265
                    if not r:
266
                        return (0, '0')
267
                except Exception as e:
268
                    error = e
269
        raise error
270
271
272
    def __eval_ppif(self, expr):
273
        expand = self.__expand_ppif_macro(expr)
274
        expand_expr = re.sub(r'([0-9])+L', r'\1', expand)
275
        try:
276
            return self.__eval_expanded_expr(expand_expr)
277
        except Exception as e:
278
            r = -1
279
            if len(expand.split()) > 1:
280
                # r = self.__eval_ppif_unknown_defined(expand_expr)
281
                if r == -1:
282
                    if self.debug:
283
                        print(expr)
284
                        print(expand)
285
                        print(e)
286
                # strip fixed condition
287
                if '(0)' in expand or '(1)' in expand:
288
                    expand = re.sub(r'\s*\(0\)\s*or\s*', '', expand)
289
                    expand = re.sub(r'\s*or\s*\(0\)\s*', '', expand)
290
                    expand = re.sub(r'\s*\(1\)\s*and\s*', '', expand)
291
                    expand = re.sub(r'\s*and\s*\(1\)\s*', '', expand)
292
                    expand = expand.replace('and', '&&')
293
                    expand = expand.replace('or',  '||')
294
                    expand = expand.replace('not',  '!')
295
                    expand = expand.replace('(0)',  '0')
296
                    expand = expand.replace('(1)',  '1')
297
                    expand = expand.replace(' ', '')
298
                    return (r, expand)
299
            return (r, None)
300
301
    def __check_ppif(self, ins, expr):
302
        if ins == "if" or ins == "elif":
303
            return self.__eval_ppif(expr)
304
        else:
305
            def other():
306
                if ins == "ifdef":
307
                    if expr in self.unknowns:
308
                        return -1
309
                    elif expr not in self.macros:
310
                        return -1
311
                    elif expr in self.macros:
312
                        if self.macros[expr] is None:
313
                            return 0
314
                        return 1
315
                elif ins == "ifndef":
316
                    if expr in self.unknowns:
317
                        return -1
318
                    elif expr in self.macros:
319
                        if self.macros[expr] is None:
320
                            return 1
321
                        return 0
322
                    elif expr in self.expands_macros:
323
                        return 0
324
                    else:
325
                        return -1
326
                return -1
327
            return other(), None
328
329
    # return line string or None
330
    def __check_pp(self, line):
331
        def ret(b):
332
            if b:
333
                return line
334
            return None
335
        m = RE_PPIF.match(line)
336
        if m:
337
            expr = m.group(2)
338
            f,expanded_expr = self.__check_ppif(m.group(1), expr)
339
            self.depth.append(f)
340
            self.depth_macros.append({})
341
            self.included_path.append([])
342
            self.brothers.append([])
343
            if expanded_expr is not None:
344
                line = line.replace(expr, expanded_expr)
345
            return ret(all(x != 0 for x in self.depth) and f == -1)
346
        m = RE_PPELIF.match(line)
347
        if m:
348
            brother = self.brothers[-1]
349
            prev_brother_f = self.depth[-1]
350
            if len(brother) == 0 and prev_brother_f == 0:
351
                # Convert to #if if the last is if and the result is False
352
                line = line.replace('#elif', '#if')
353
            else:
354
                brother.append(prev_brother_f)
355
            f = 0
356
            if not any(x == 1 for x in brother):
357
                expr = m.group(1)
358
                f,expanded_expr = self.__check_ppif("elif", expr)
359
                if expanded_expr is not None:
360
                    line = line.replace(expr, expanded_expr)
361
            self.depth[-1] = f
362
            if all(x != 0 for x in self.depth):
363
                if f == -1 or any(x == -1 for x in brother):
364
                    return line
365
            return None
366
        m = RE_PPELSE.match(line)
367
        if m:
368
            brother = self.brothers[-1]
369
            brother.append(self.depth[-1])
370
            f = -1
371
            if f == 1 or any(x == 1 for x in brother):
372
                f = 0
373
            elif all(x == 0 for x in brother):
374
                f = 1
375
            self.depth[-1] = f
376
            return ret(all(x != 0 for x in self.depth) and f == -1)
377
        if RE_PPENDIF.match(line):
378
            brother = self.brothers[-1]
379
            f = self.depth.pop()
380
            self.included_path.pop()
381
            poped_macros = self.depth_macros.pop()
382
            b1 = all(x != 0 for x in self.depth)
383
            b2 = any(x == -1 for x in brother)
384
            self.brothers.pop()
385
            need_endif = b1 and (f == -1 or b2)
386
            if need_endif:
387
                return line
388
            if len(self.depth_macros) > 0:
389
                current_depth_macros = self.depth_macros[-1]
390
                current_depth_macros.update(poped_macros)
391
            return None
392
        return ret(len(self.depth) == 0 or all(x != 0 for x in self.depth))
393
394
    def __check_include(self, line):
395
        m = RE_SYSTEM_INCLUDE_REGEX.match(line)
396
        if m:
397
            path = m.group(1)
398
            for include_paths in self.included_path:
399
                if path in include_paths:
400
                    return False
401
            self.included_path[-1].append(path)
402
        return True
403
404
    def __reduction(self, line):
405
        reduction_macros = {
406
            'IP_INC':    'IP_I',
407
            'IP_DEC':    'IP_D',
408
            'IP_BOOL':   'IP_B',
409
            'IP_ENUM':   'IP_E',
410
            'IP_REPEAT': 'IP_R',
411
            'IP_IS_EMPTY_': 'IP_IS_EMP_',
412
            'IP_EMPTY_TAG': 'IP_EMP_T',
413
            'IP_IS_BEGIN_PARENS': 'IP_IS_BGN_P',
414
            'IP_E_PARAMS_MACRO_': 'IP_E_P_M_',
415
            'IP_E_P_M_IN_BINARY_': 'IP_E_P_M_B_',
416
            'IP_E_BINARY_PARAMS_': 'IP_E_B_P_',
417
            'IP_E_B_P_MACRO_': 'IP_E_B_P_M_',
418
            'IP_R_PARAMS_MACRO_': 'IP_R_P_M_',
419
            'IP_R_P_M_IN_BINARY_': 'IP_R_P_M_B_',
420
            'IP_R_BINARY_PARAMS_': 'IP_R_B_P_',
421
            'IP_R_B_P_MACRO_': 'IP_R_B_P_M_',
422
            'II_SHOW_MACRO':  'II_S_M',
423
            'II_SHOW_ENABLE_MACRO':   'II_S_E_M',
424
            'II_SHOW_DISABLE_MACRO':  'II_S_D_M',
425
            'II_SHOW_FEATURE_MACROS': 'II_S_F_M',
426
            'II_ELEMENTSARE': 'II_EA',
427
            'II_EA_MATCHER_NAME': 'II_EA_M_N',
428
            'II_ANYOF_AND_ALLOF_MATCHER_NAME': 'II_AAA_M_N',
429
            'II_DECL_VALUEARRAY_': 'II_D_VA_',
430
            'II_DECL_CARTESIAN_PRODUCT_': 'II_D_C_P_',
431
            'II_DECL_PAIRWISE_': 'II_D_PW_',
432
            'II_DECL_IS_FUNCTION_PTR_': 'II_D_IS_FP_',
433
            'II_DECL_IS_MEMBER_FUNCTION_PTR_': 'II_D_IS_M_FP_',
434
            'II_DECL_FUNCTION_RETURN_TYPE_': 'II_D_F_R_T_',
435
            'II_DECL_EXPRESSION_': 'II_D_EP_',
436
            'II_DECL_ELEMENTSARE': 'II_D_EA',
437
            'II_DECL_TUPLE_PRINTTO': 'II_D_T_PT',
438
            'II_DECL_ANYOF_AND_ALLOF': 'II_D_AAA',
439
            'II_DECL_COMPARE_HELPER_': 'II_D_C_H_',
440
            'II_DECL_COMBINE_':   'II_D_C_',
441
            'II_DECL_VALUES_':    'II_D_V_',
442
            'II_DECL_TYPES_':     'II_D_T_',
443
            'II_DECL_TEMPLATES_': 'II_D_TPL_',
444
            'II_DECL_TYPELIST_':  'II_D_TL_',
445
            'II_DECL_TEMPLATETYPELIST_': 'II_D_TTL_',
446
            'II_DECL_PEEP_MEMBER_FUNC_': 'II_D_PE_M_F_',
447
            'II_DECL_COMPARE_MATCHER': 'II_D_COMP_M',
448
            'II_DECL_STR_COMPARE_MATCHER': 'II_D_S_COMP_M',
449
            'II_DECL_ALLOF_MATCHER': 'II_D_ALL_M',
450
            'II_DECL_ANYOF_MATCHER': 'II_D_ANY_M',
451
            'II_DECL_DEFAULT_ARG_': 'II_D_DEF_A_',
452
            'II_DECL_SPEC_NONE_': 'II_D_S_NN_',
453
            'II_SUCCEED': 'II_S',
454
            'II_FAIL': 'II_F',
455
            'II_ADD_FAILURE': 'II_ADD_F',
456
            'II_SCOPED_MESSAGE': 'II_S_MSG',
457
            'II_D_C_P_GENERATOR_': 'II_D_C_P_GEN_',
458
            'II_D_C_P_HOLDER_': 'II_D_C_P_HLR_',
459
            'II_D_PW_GENERATOR_': 'II_D_PW_GEN_',
460
            'II_D_PW_HOLDER_': 'II_D_PW_HLR_',
461
            'II_CONCAT_PACKAGE': 'II_CC_PKG',
462
            'II_PACKAGE_': 'II_PKG_',
463
            'II_PKG_CURRENT_NAMESPACE_': 'II_PKG_C_NS_',
464
            'II_PKG_PARENT_NAMESPACE_':  'II_PKG_P_NS_',
465
            'II_TEST_CLASS_NAME_': 'II_T_C_N_',
466
            'II_TEST_INSTANCE_NAME_': 'II_T_INST_N_',
467
            'II_TO_VARNAME_': 'II_TO_VN_',
468
            'II_TO_NAME_': 'II_TO_N_',
469
            'II_CHECK_TESTFIXTURE': 'II_CK_TF',
470
            'II_PMZ_TEST_CLASS_NAME_': 'II_PMZ_T_C_N_',
471
            'II_GETTESTCASEPATTERNHOLDER': 'II_GTCPH',
472
            'II_INSTANTIATE_TEST_CASE_P_': 'II_INST_TC_P_',
473
            'II_TEST_P_EVALGENERATOR_NAME_':    'II_T_P_EGEN_N_',
474
            'II_TEST_P_PARAMGENERATOR_NAME_':   'II_T_P_PGEN_N_',
475
            'II_TEST_P_INSTANTIATIONREGISTER_': 'II_T_P_INST_R_',
476
            'II_TEST_P_FIXTURE_DECL_': 'II_T_P_FX_D_',
477
            'II_TEST_P_BASE_FIXTURE': 'II_T_P_B_FX',
478
            'II_T_P_INST_R_NAME_': 'II_T_P_INST_R_N_',
479
            'II_ALIAS_TESTNAME_PP_': 'II_A_TN_PP_',
480
            'II_TEST_EXPRESSION_': 'II_T_EXPR_',
481
            'II_T_EXPR_EXPAND_EXPRESSION': 'II_T_EXPR_E_E',
482
            'II_EXPRESSION_DECOMPOSE': 'II_EXPR_DEC',
483
            'II_D_EP_RESULT_OP': 'II_D_EP_R_OP',
484
            'II_TYPED_TEST_': 'II_T_T_',
485
            'II_T_T_CASE_': 'II_T_TC_',
486
            'II_T_TC_PSTATE_NAME_': 'II_T_TC_PS_N_',
487
            'II_T_T_P_NAMESPACE_': 'II_T_T_P_NS_',
488
            'II_T_T_P_ADDTESTNAME': 'II_T_T_P_ADD_TN',
489
            'II_T_T_PARAMS_': 'II_T_T_PRMS_',
490
            'II_REGISTER_TYPED_TEST_CASE_P_':    'II_R_T_TC_P_',
491
            'II_INSTANTIATE_TYPED_TEST_CASE_P_': 'II_INST_T_TC_P_',
492
            'II_PEEP_TAG_NAME_':    'II_PE_T_N_',
493
            'II_PEEP_SETTER_NAME_': 'II_PE_S_N_',
494
            'II_GeTypeNameSpecialization': 'II_GTNS',
495
            'II_WORKAROUND_GENRAND': 'II_WA_GENRAND',
496
        }
497
        line = line.replace('IIUT_', 'II_')
498
        line = line.replace('II_PP_', 'IP_')
499
        line = line.replace('IUTEST_UNUSED_VAR', '(void)')
500
        line = line.replace('statement', 'st')
501
        line = line.replace('expected_exception', 'exp_e')
502
        line = line.replace('exp_e_value', 'exp_e_v')
503
        line = line.replace('expected_str',   'exp_s')
504
        line = line.replace('expected_value', 'exp_v')
505
        line = line.replace('actual_str',  'act_s')
506
        line = line.replace('regex_str',  'regex_s')
507
        line = line.replace('pred_formatter',  'pd_fmt')
508
        line = line.replace('on_failure',  'on_f')
509
        line = line.replace('testcasename_',  'tcn_')
510
        line = line.replace('testname_',  'tn_')
511
        line = line.replace('testfixture_',  'tf_')
512
        for k,v in reduction_macros.items():
513
            if collections.Counter(reduction_macros.values())[v] > 1:
514
                print('error: duplicated ' + v)
515
                continue
516
            line = line.replace(k, v)
517
        line = re.sub(r'(?<![\w\d_])NULL(?![\w\d_])', '0', line)
518
        # line = re.sub('\s+', ' ', line)
519
        line = re.sub('\s$', '', line)
520
        line = line.strip()
521
        return line
522
523
    def __strip_namespace(self, line, ns):
524
        s = ""
525
        for n in ns:
526
            s += "namespace " + n + "{"
527
        e = ""
528
        for n in ns:
529
            e += "}"
530
        def __is_namespace_open_close_line(x):
531
            return x.startswith(s) and x.endswith(e)
532
        if __is_namespace_open_close_line(line) and __is_namespace_open_close_line(self.prev_line):
533
            self.prev_line = self.prev_line[:-len(e)]
534
            line = line[len(s):]
535
        return line
536
537
    def __strip_namespace_iutest_detail(self, line):
538
        ns = ['iutest', 'detail']
539
        return self.__strip_namespace(line, ns)
540
541
    def __strip_namespace_iutest(self, line):
542
        ns = ['iutest']
543
        return self.__strip_namespace(line, ns)
544
545
    def __strip_namespaces(self, line):
546
        line = self.__strip_namespace_iutest_detail(line)
547
        line = self.__strip_namespace_iutest(line)
548
        return line
549
550
    def preprocess(self, code, add_macros):
551
        if add_macros is not None:
552
            self.macros.update(add_macros)
553
        dst = ""
554
        for line in code.splitlines():
555
            # c++ comment
556
            if RE_CPP_COMMENT.match(line):
557
                continue
558
            # if/ifdef/ifndef/elif/endif
559
            line = self.__check_pp(line)
560
            if line:
561
                # include
562
                if not self.__check_include(line):
563
                    continue
564
                # define
565
                d = self.__append_define(line)
566
                if d:
567
                    # config macro insert
568
                    if 'INCG_IRIS_IUTEST_CONFIG_HPP_' in d:
569
                        dst += self.prev_line + '\n'
570
                        self.prev_line = None
571
                        for k,v in self.iutest_config_macro.items():
572
                            dst += '#define ' + k + ' ' + str(v) + '\n'
573
                        self.iutest_config_macro = []
574
                    if d in self.expands_macros or d in self.expand_function_macros:
575
                        continue
576
                    if d in ['IUTEST_UNUSED_VAR']:
577
                        continue
578
                line = self.__expand_macro(line)
579
                if len(line) > 0:
580
                    line = self.__reduction(line)
581
                    if self.prev_line is not None:
582
                        line = self.__strip_namespaces(line)
583
                        if self.prev_line.startswith('#'):
584
                            self.prev_line += '\n'
585
                        elif line.startswith('#'):
586
                            self.prev_line += '\n'
587
                        dst += self.prev_line
588
                    self.prev_line = line
589
        dst += self.prev_line + '\n'
590
        return dst
591
592
    def __get_ppif_type(self, line):
593
        if RE_PPIF.match(line):
594
            return 'if'
595
        elif RE_PPELIF.match(line):
596
            return 'elif'
597
        elif RE_PPELSE.match(line):
598
            return 'else'
599
        elif RE_PPENDIF.match(line):
600
            return 'endif'
601
        return None
602
603
    def remove_empty_ppif(self, code):
604
        dst = ""
605
        prev = None
606
        cache_lines = []
607
        def cach_clear():
608
            ret = ""
609
            for s in cache_lines:
610
                if s is not None:
611
                    ret += s
612
            del cache_lines[:]
613
            return ret
614
615
        for line in code.splitlines():
616
            line += "\n"
617
            t = self.__get_ppif_type(line)
618
            if t == 'endif':
619
                if prev == 'if':
620
                    if len(cache_lines) > 0:
621
                        cache_lines = cache_lines[:-1]
622
                elif prev != 'endif':
623
                    if len(cache_lines) > 0:
624
                        cache_lines[-1] = line
625
                    else:
626
                        cache_lines.append(line)
627
                else:
628
                    dst += cach_clear()
629
                    dst += line
630
            elif t is not None:
631
                if prev is None:
632
                    cache_lines.append(line)
633
                else:
634
                    if t == 'else' and prev == 'elif':
635
                        cache_lines[-1] = line
636
                    elif t == 'elif' and prev == 'elif':
637
                        cache_lines[-1] = line
638
                    else:
639
                        cache_lines.append(line)
640
            else:
641
                dst += cach_clear()
642
                dst += line
643
            prev = t
644
        dst += cach_clear()
645
        return dst
646