Passed
Push — master ( 89e842...fde1fa )
by srz
07:01 queued 11s
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
    def __eval_ppif(self, expr):
272
        expand = self.__expand_ppif_macro(expr)
273
        expand_expr = re.sub(r'([0-9])+L', r'\1', expand)
274
        try:
275
            return self.__eval_expanded_expr(expand_expr)
276
        except Exception as e:
277
            r = -1
278
            if len(expand.split()) > 1:
279
                # r = self.__eval_ppif_unknown_defined(expand_expr)
280
                if r == -1:
281
                    if self.debug:
282
                        print(expr)
283
                        print(expand)
284
                        print(e)
285
                # strip fixed condition
286
                if '(0)' in expand or '(1)' in expand:
287
                    expand = re.sub(r'\s*\(0\)\s*or\s*', '', expand)
288
                    expand = re.sub(r'\s*or\s*\(0\)\s*', '', expand)
289
                    expand = re.sub(r'\s*\(1\)\s*and\s*', '', expand)
290
                    expand = re.sub(r'\s*and\s*\(1\)\s*', '', expand)
291
                    expand = expand.replace('and', '&&')
292
                    expand = expand.replace('or',  '||')
293
                    expand = expand.replace('not',  '!')
294
                    expand = expand.replace('(0)',  '0')
295
                    expand = expand.replace('(1)',  '1')
296
                    expand = expand.replace(' ', '')
297
                    return (r, expand)
298
            return (r, None)
299
300
    def __check_ppif(self, ins, expr):
301
        if ins == "if" or ins == "elif":
302
            return self.__eval_ppif(expr)
303
        else:
304
            def other():
305
                if ins == "ifdef":
306
                    if expr in self.unknowns:
307
                        return -1
308
                    elif expr not in self.macros:
309
                        return -1
310
                    elif expr in self.macros:
311
                        if self.macros[expr] is None:
312
                            return 0
313
                        return 1
314
                elif ins == "ifndef":
315
                    if expr in self.unknowns:
316
                        return -1
317
                    elif expr in self.macros:
318
                        if self.macros[expr] is None:
319
                            return 1
320
                        return 0
321
                    elif expr in self.expands_macros:
322
                        return 0
323
                    else:
324
                        return -1
325
                return -1
326
            return other(), None
327
328
    # return line string or None
329
    def __check_pp(self, line):
330
        def ret(b):
331
            if b:
332
                return line
333
            return None
334
        m = RE_PPIF.match(line)
335
        if m:
336
            expr = m.group(2)
337
            f,expanded_expr = self.__check_ppif(m.group(1), expr)
338
            self.depth.append(f)
339
            self.depth_macros.append({})
340
            self.included_path.append([])
341
            self.brothers.append([])
342
            if expanded_expr is not None:
343
                line = line.replace(expr, expanded_expr)
344
            return ret(all(x != 0 for x in self.depth) and f == -1)
345
        m = RE_PPELIF.match(line)
346
        if m:
347
            brother = self.brothers[-1]
348
            prev_brother_f = self.depth[-1]
349
            if len(brother) == 0 and prev_brother_f == 0:
350
                # Convert to #if if the last is if and the result is False
351
                line = line.replace('#elif', '#if')
352
            else:
353
                brother.append(prev_brother_f)
354
            f = 0
355
            if not any(x == 1 for x in brother):
356
                expr = m.group(1)
357
                f,expanded_expr = self.__check_ppif("elif", expr)
358
                if expanded_expr is not None:
359
                    line = line.replace(expr, expanded_expr)
360
            self.depth[-1] = f
361
            if all(x != 0 for x in self.depth):
362
                if f == -1 or any(x == -1 for x in brother):
363
                    return line
364
            return None
365
        m = RE_PPELSE.match(line)
366
        if m:
367
            brother = self.brothers[-1]
368
            brother.append(self.depth[-1])
369
            f = -1
370
            if f == 1 or any(x == 1 for x in brother):
371
                f = 0
372
            elif all(x == 0 for x in brother):
373
                f = 1
374
            self.depth[-1] = f
375
            return ret(all(x != 0 for x in self.depth) and f == -1)
376
        if RE_PPENDIF.match(line):
377
            brother = self.brothers[-1]
378
            f = self.depth.pop()
379
            self.included_path.pop()
380
            poped_macros = self.depth_macros.pop()
381
            b1 = all(x != 0 for x in self.depth)
382
            b2 = any(x == -1 for x in brother)
383
            self.brothers.pop()
384
            need_endif = b1 and (f == -1 or b2)
385
            if need_endif:
386
                return line
387
            if len(self.depth_macros) > 0:
388
                current_depth_macros = self.depth_macros[-1]
389
                current_depth_macros.update(poped_macros)
390
            return None
391
        return ret(len(self.depth) == 0 or all(x != 0 for x in self.depth))
392
393
    def __check_include(self, line):
394
        m = RE_SYSTEM_INCLUDE_REGEX.match(line)
395
        if m:
396
            path = m.group(1)
397
            for include_paths in self.included_path:
398
                if path in include_paths:
399
                    return False
400
            self.included_path[-1].append(path)
401
        return True
402
403
    def __reduction(self, line):
404
        reduction_macros = {
405
            'IP_INC':    'IP_I',
406
            'IP_DEC':    'IP_D',
407
            'IP_BOOL':   'IP_B',
408
            'IP_ENUM':   'IP_E',
409
            'IP_REPEAT': 'IP_R',
410
            'IP_IS_EMPTY_': 'IP_IS_EMP_',
411
            'IP_EMPTY_TAG': 'IP_EMP_T',
412
            'IP_IS_BEGIN_PARENS': 'IP_IS_BGN_P',
413
            'IP_E_PARAMS_MACRO_': 'IP_E_P_M_',
414
            'IP_E_P_M_IN_BINARY_': 'IP_E_P_M_B_',
415
            'IP_E_BINARY_PARAMS_': 'IP_E_B_P_',
416
            'IP_E_B_P_MACRO_': 'IP_E_B_P_M_',
417
            'IP_R_PARAMS_MACRO_': 'IP_R_P_M_',
418
            'IP_R_P_M_IN_BINARY_': 'IP_R_P_M_B_',
419
            'IP_R_BINARY_PARAMS_': 'IP_R_B_P_',
420
            'IP_R_B_P_MACRO_': 'IP_R_B_P_M_',
421
            'II_SHOW_MACRO':  'II_S_M',
422
            'II_SHOW_ENABLE_MACRO':   'II_S_E_M',
423
            'II_SHOW_DISABLE_MACRO':  'II_S_D_M',
424
            'II_SHOW_FEATURE_MACROS': 'II_S_F_M',
425
            'II_ELEMENTSARE': 'II_EA',
426
            'II_EA_MATCHER_NAME': 'II_EA_M_N',
427
            'II_ANYOF_AND_ALLOF_MATCHER_NAME': 'II_AAA_M_N',
428
            'II_DECL_VALUEARRAY_': 'II_D_VA_',
429
            'II_DECL_CARTESIAN_PRODUCT_': 'II_D_C_P_',
430
            'II_DECL_PAIRWISE_': 'II_D_PW_',
431
            'II_DECL_IS_FUNCTION_PTR_': 'II_D_IS_FP_',
432
            'II_DECL_IS_MEMBER_FUNCTION_PTR_': 'II_D_IS_M_FP_',
433
            'II_DECL_FUNCTION_RETURN_TYPE_': 'II_D_F_R_T_',
434
            'II_DECL_EXPRESSION_': 'II_D_EP_',
435
            'II_DECL_ELEMENTSARE': 'II_D_EA',
436
            'II_DECL_TUPLE_PRINTTO': 'II_D_T_PT',
437
            'II_DECL_ANYOF_AND_ALLOF': 'II_D_AAA',
438
            'II_DECL_COMPARE_HELPER_': 'II_D_C_H_',
439
            'II_DECL_COMBINE_':   'II_D_C_',
440
            'II_DECL_VALUES_':    'II_D_V_',
441
            'II_DECL_TYPES_':     'II_D_T_',
442
            'II_DECL_TEMPLATES_': 'II_D_TPL_',
443
            'II_DECL_TYPELIST_':  'II_D_TL_',
444
            'II_DECL_TEMPLATETYPELIST_': 'II_D_TTL_',
445
            'II_DECL_PEEP_MEMBER_FUNC_': 'II_D_PE_M_F_',
446
            'II_DECL_COMPARE_MATCHER': 'II_D_COMP_M',
447
            'II_DECL_STR_COMPARE_MATCHER': 'II_D_S_COMP_M',
448
            'II_DECL_ALLOF_MATCHER': 'II_D_ALL_M',
449
            'II_DECL_ANYOF_MATCHER': 'II_D_ANY_M',
450
            'II_DECL_DEFAULT_ARG_': 'II_D_DEF_A_',
451
            'II_DECL_SPEC_NONE_': 'II_D_S_NN_',
452
            'II_SUCCEED': 'II_S',
453
            'II_FAIL': 'II_F',
454
            'II_ADD_FAILURE': 'II_ADD_F',
455
            'II_SCOPED_MESSAGE': 'II_S_MSG',
456
            'II_D_C_P_GENERATOR_': 'II_D_C_P_GEN_',
457
            'II_D_C_P_HOLDER_': 'II_D_C_P_HLR_',
458
            'II_D_PW_GENERATOR_': 'II_D_PW_GEN_',
459
            'II_D_PW_HOLDER_': 'II_D_PW_HLR_',
460
            'II_CONCAT_PACKAGE': 'II_CC_PKG',
461
            'II_PACKAGE_': 'II_PKG_',
462
            'II_PKG_CURRENT_NAMESPACE_': 'II_PKG_C_NS_',
463
            'II_PKG_PARENT_NAMESPACE_':  'II_PKG_P_NS_',
464
            'II_TEST_CLASS_NAME_': 'II_T_C_N_',
465
            'II_TEST_INSTANCE_NAME_': 'II_T_INST_N_',
466
            'II_TO_VARNAME_': 'II_TO_VN_',
467
            'II_TO_NAME_': 'II_TO_N_',
468
            'II_CHECK_TESTFIXTURE': 'II_CK_TF',
469
            'II_PMZ_TEST_CLASS_NAME_': 'II_PMZ_T_C_N_',
470
            'II_GETTESTCASEPATTERNHOLDER': 'II_GTCPH',
471
            'II_INSTANTIATE_TEST_CASE_P_': 'II_INST_TC_P_',
472
            'II_TEST_P_EVALGENERATOR_NAME_':    'II_T_P_EGEN_N_',
473
            'II_TEST_P_PARAMGENERATOR_NAME_':   'II_T_P_PGEN_N_',
474
            'II_TEST_P_INSTANTIATIONREGISTER_': 'II_T_P_INST_R_',
475
            'II_TEST_P_FIXTURE_DECL_': 'II_T_P_FX_D_',
476
            'II_TEST_P_BASE_FIXTURE': 'II_T_P_B_FX',
477
            'II_T_P_INST_R_NAME_': 'II_T_P_INST_R_N_',
478
            'II_ALIAS_TESTNAME_PP_': 'II_A_TN_PP_',
479
            'II_TEST_EXPRESSION_': 'II_T_EXPR_',
480
            'II_T_EXPR_EXPAND_EXPRESSION': 'II_T_EXPR_E_E',
481
            'II_EXPRESSION_DECOMPOSE': 'II_EXPR_DEC',
482
            'II_D_EP_RESULT_OP': 'II_D_EP_R_OP',
483
            'II_TYPED_TEST_': 'II_T_T_',
484
            'II_T_T_CASE_': 'II_T_TC_',
485
            'II_T_TC_PSTATE_NAME_': 'II_T_TC_PS_N_',
486
            'II_T_T_P_NAMESPACE_': 'II_T_T_P_NS_',
487
            'II_T_T_P_ADDTESTNAME': 'II_T_T_P_ADD_TN',
488
            'II_T_T_PARAMS_': 'II_T_T_PRMS_',
489
            'II_REGISTER_TYPED_TEST_CASE_P_':    'II_R_T_TC_P_',
490
            'II_INSTANTIATE_TYPED_TEST_CASE_P_': 'II_INST_T_TC_P_',
491
            'II_PEEP_TAG_NAME_':    'II_PE_T_N_',
492
            'II_PEEP_SETTER_NAME_': 'II_PE_S_N_',
493
            'II_GeTypeNameSpecialization': 'II_GTNS',
494
            'II_WORKAROUND_GENRAND': 'II_WA_GENRAND',
495
        }
496
        line = line.replace('IIUT_', 'II_')
497
        line = line.replace('II_PP_', 'IP_')
498
        line = line.replace('IUTEST_UNUSED_VAR', '(void)')
499
        line = line.replace('statement', 'st')
500
        line = line.replace('expected_exception', 'exp_e')
501
        line = line.replace('exp_e_value', 'exp_e_v')
502
        line = line.replace('expected_str',   'exp_s')
503
        line = line.replace('expected_value', 'exp_v')
504
        line = line.replace('actual_str',  'act_s')
505
        line = line.replace('regex_str',  'regex_s')
506
        line = line.replace('pred_formatter',  'pd_fmt')
507
        line = line.replace('on_failure',  'on_f')
508
        line = line.replace('testcasename_',  'tcn_')
509
        line = line.replace('testname_',  'tn_')
510
        line = line.replace('testfixture_',  'tf_')
511
        for k,v in reduction_macros.items():
512
            if collections.Counter(reduction_macros.values())[v] > 1:
513
                print('error: duplicated ' + v)
514
                continue
515
            line = line.replace(k, v)
516
        line = re.sub(r'(?<![\w\d_])NULL(?![\w\d_])', '0', line)
517
        # line = re.sub('\s+', ' ', line)
518
        line = re.sub('\s$', '', line)
519
        line = line.strip()
520
        return line
521
522
    def __strip_namespace(self, line, ns):
523
        s = ""
524
        for n in ns:
525
            s += "namespace " + n + "{"
526
        e = ""
527
        for n in ns:
528
            e += "}"
529
        def __is_namespace_open_close_line(x):
530
            return x.startswith(s) and x.endswith(e)
531
        if __is_namespace_open_close_line(line) and __is_namespace_open_close_line(self.prev_line):
532
            self.prev_line = self.prev_line[:-len(e)]
533
            line = line[len(s):]
534
        return line
535
536
    def __strip_namespace_iutest_detail(self, line):
537
        ns = ['iutest', 'detail']
538
        return self.__strip_namespace(line, ns)
539
540
    def __strip_namespace_iutest(self, line):
541
        ns = ['iutest']
542
        return self.__strip_namespace(line, ns)
543
544
    def __strip_namespaces(self, line):
545
        line = self.__strip_namespace_iutest_detail(line)
546
        line = self.__strip_namespace_iutest(line)
547
        return line
548
549
    def preprocess(self, code, add_macros):
550
        if add_macros is not None:
551
            self.macros.update(add_macros)
552
        dst = ""
553
        for line in code.splitlines():
554
            # c++ comment
555
            if RE_CPP_COMMENT.match(line):
556
                continue
557
            # if/ifdef/ifndef/elif/endif
558
            line = self.__check_pp(line)
559
            if line:
560
                # include
561
                if not self.__check_include(line):
562
                    continue
563
                # define
564
                d = self.__append_define(line)
565
                if d:
566
                    # config macro insert
567
                    if 'INCG_IRIS_IUTEST_CONFIG_HPP_' in d:
568
                        dst += self.prev_line + '\n'
569
                        self.prev_line = None
570
                        for k,v in self.iutest_config_macro.items():
571
                            dst += '#define ' + k + ' ' + str(v) + '\n'
572
                        self.iutest_config_macro = []
573
                    if d in self.expands_macros or d in self.expand_function_macros:
574
                        continue
575
                    if d in ['IUTEST_UNUSED_VAR']:
576
                        continue
577
                line = self.__expand_macro(line)
578
                if len(line) > 0:
579
                    line = self.__reduction(line)
580
                    if self.prev_line is not None:
581
                        line = self.__strip_namespaces(line)
582
                        if self.prev_line.startswith('#'):
583
                            self.prev_line += '\n'
584
                        elif line.startswith('#'):
585
                            self.prev_line += '\n'
586
                        dst += self.prev_line
587
                    self.prev_line = line
588
        dst += self.prev_line + '\n'
589
        return dst
590
591
    def __get_ppif_type(self, line):
592
        if RE_PPIF.match(line):
593
            return 'if'
594
        elif RE_PPELIF.match(line):
595
            return 'elif'
596
        elif RE_PPELSE.match(line):
597
            return 'else'
598
        elif RE_PPENDIF.match(line):
599
            return 'endif'
600
        return None
601
602
    def remove_empty_ppif(self, code):
603
        dst = ""
604
        prev = None
605
        cache_lines = []
606
        def cach_clear():
607
            ret = ""
608
            for s in cache_lines:
609
                if s is not None:
610
                    ret += s
611
            del cache_lines[:]
612
            return ret
613
614
        for line in code.splitlines():
615
            line += "\n"
616
            t = self.__get_ppif_type(line)
617
            if t == 'endif':
618
                if prev == 'if':
619
                    if len(cache_lines) > 0:
620
                        cache_lines = cache_lines[:-1]
621
                elif prev != 'endif':
622
                    if len(cache_lines) > 0:
623
                        cache_lines[-1] = line
624
                    else:
625
                        cache_lines.append(line)
626
                else:
627
                    dst += cach_clear()
628
                    dst += line
629
            elif t is not None:
630
                if prev is None:
631
                    cache_lines.append(line)
632
                else:
633
                    if t == 'else' and prev == 'elif':
634
                        cache_lines[-1] = line
635
                    elif t == 'elif' and prev == 'elif':
636
                        cache_lines[-1] = line
637
                    else:
638
                        cache_lines.append(line)
639
            else:
640
                dst += cach_clear()
641
                dst += line
642
            prev = t
643
        dst += cach_clear()
644
        return dst
645