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