fused_iutest_files.IutestFused.StoreMinimze()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 2
dl 0
loc 14
rs 9.95
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
# fuse_iutest_files.py
5
#
6
# Copyright (C) 2014-2016, Takazumi Shirayanagi
7
# This software is released under the new BSD License,
8
# see LICENSE
9
#
10
11
""" fuse_iutest_files.py
12
fuse_iutest_files.py [IUTEST_ROOT_DIR] OUTPUT_DIR
13
"""
14
15
import os
16
import sys
17
import re
18
import codecs
19
20
IUTEST_INCLUDE_DIR = os.path.join(os.path.dirname(__file__), '../../include')
21
IUTEST_APPROVAL_INCLUDE_GUARD = {
22
    'INCG_IRIS_IUTEST_HPP_',
23
    'INCG_IRIS_IUTEST_SWITCH_HPP_',
24
    'INCG_IRIS_IUTEST_SPI_HPP_'
25
}
26
27
28
class IutestFused:
29
    INCLUDE_REGEX = re.compile(r'^\s*#\s*include\s*"(.*iutest.*)"')
30
    IFDEF_REGEX = re.compile(r'^\s*#\s*if(def\s*\S*|\s*defined\s*\(.*\))')
31
    PREPRO_REGEX = re.compile(r'^\s*#')
32
    COMMENT_REGEX = re.compile(r'^\s*//.*')
33
    C_COMMENT_BEGIN_REGEX = re.compile(r'^\s*(.*)/\*.*')
34
    C_COMMENT_END_REGEX = re.compile(r'^\s*\*/(.*)')
35
    STRING_REGEX = re.compile(r'(".*?")')
36
    INCG_REGEX = re.compile(r'^\s*#\s*(ifndef|define|endif)[/\s]*(INCG_\S*)\s*\Z')
37
    c_comment = False
38
    store_line = ""
39
    line_buffer = ""
40
41
    def IsUnnecessaryIncludeGuard(self, line):
42
        m = self.INCG_REGEX.match(line)
43
        if m:
44
            incg = m.group(2)
45
            if m.group(1) == 'define':
46
                return False
47
            if incg not in IUTEST_APPROVAL_INCLUDE_GUARD:
48
                return True
49
        return False
50
51
    def StoreStrings(self, src, list):
52
        tmp = src.replace(r'\"', '$$')
53
        prev = 0
54
        dst = ""
55
        for m in self.STRING_REGEX.finditer(tmp):
56
            dst += tmp[prev:m.start()]
57
            dst += '"@STRING@"'
58
            prev = m.end()
59
            list.append(m.group(0))
60
        dst += tmp[prev:]
61
        return dst
62
63
    def RestoreStrings(self, src, list):
64
        dst = src
65
        for s in list:
66
            dst = dst.replace('"@STRING@"', s, 1)
67
        dst = dst.replace('$$', r'\"')
68
        return dst
69
70
    def StoreMinimze(self, line):
71
        store = self.store_line + line
72
        # string store
73
        str_l = []
74
        store = self.StoreStrings(store, str_l)
75
76
        store = re.sub('"@STRING@""@STRING@"', '"@STRING@"##+##"@STRING@"', store)
77
        store = re.sub('\):\s+', '):', store)
78
79
        # string restore
80
        store = self.RestoreStrings(store, str_l)
81
82
        store = store.replace('"##+##"', '')
83
        self.store_line = store
84
85
    def Minimze(self, line):
86
        # if defined -> ifdef
87
        line = re.sub(r'\s*#\s*if\s*defined\((\S*)\)\s*\Z', r'#ifdef \1', line)
88
        # if !defined -> ifndef
89
        line = re.sub(r'\s*#\s*if\s*!defined\((\S*)\)\s*\Z', r'#ifndef \1', line)
90
91
        # C comment
92
        if self.c_comment:
93
            m = self.C_COMMENT_END_REGEX.match(line)
94
            if m:
95
                self.c_comment = False
96
                line = m.group(1)
97
            else:
98
                return ""
99
        m = self.C_COMMENT_BEGIN_REGEX.match(line)
100
        if m:
101
            self.c_comment = True
102
            line = m.group(1)
103
        line = line.strip()
104
        if len(line) == 0:
105
            return ""
106
107
        # C++ comment
108
        if self.COMMENT_REGEX.match(line):
109
            return ""
110
        # include guard
111
        if self.IsUnnecessaryIncludeGuard(line):
112
            return ""
113
114
        # string store
115
        str_l = []
116
        line = self.StoreStrings(line, str_l)
117
118
        # remove comment and strip
119
        line = re.sub('//[\S \t]*', '', line)
120
        line = line.strip(' \t')
121
        # remvoe \r
122
        line = line.rstrip()
123
        line += '\n'
124
        # remove preprocessor directive unnecessary whitespace
125
        line = re.sub('^\s*#\s*', '#', line)
126
        line = re.sub('^\s*#(.+?)[ \t]+', r'#\1 ', line)
127
128
        # remove unnecessary whitespace
129
        line = re.sub('\s+(".*?")', r' \1', line)
130
        line = re.sub(r'\)\s+>', r')>', line)
131
        line = re.sub(';[ \t]+', ';', line)
132
        line = re.sub('[ \t]+', ' ', line)
133
        line = re.sub('([\w)\]]+)\s+([&|\+\-<>=\?]+)[ \t]+([^>])', r'\1\2\3', line)
134
        line = re.sub('\s*([{\+\-\*/%=<>&|!]+=)[ \t]*', r'\1', line)
135
        line = re.sub('<\s+(\w)', r'<\1', line)
136
        line = re.sub('\s+:[ \t]+(\w)', r':\1', line)
137
        line = re.sub('\s*,[ \t]*', ',', line)
138
        line = re.sub('\s*\)', ')', line)
139
        line = re.sub('\)\s+{', '){', line)
140
        line = re.sub('\)\s+const', ')const', line)
141
        if not re.match('#define\s+.*\s+{.*', line):
142
            line = re.sub('\s*{\s*', '{', line)
143
            line = re.sub('\s*}\s*', '}', line)
144
145
        # define HOGE(x) vs define HOGE (x)
146
        m = re.match('^#define\s+(\w+)\s+(\([^)]*?\))$', line)
147
        line = re.sub('\s*\([ \t]*', '(', line)
148
        if m:
149
            line = re.sub('^#define\s+(\w+)(\([^)]*?\))$', r'#define \1 \2', line)
150
        else:
151
            line = re.sub('^#define\s+(\w+\([^)]*?\))(\S+)', r'#define \1 \2', line)
152
153
        # 0x00000X => 0xX
154
        line = re.sub('0x0+([0-9A-Fa-f])', r'0x\1', line)
155
        # 0x0 => 0
156
        line = re.sub('0x([0-9])([^0-9A-Fa-f])', r'\1\2', line)
157
158
        # string restore
159
        line = self.RestoreStrings(line, str_l)
160
161
        line = re.sub('^#define\s+(\w+)=', r'#define \1 =', line)
162
        return line
163
164
    def Flush(self, output_file):
165
        if len(self.store_line) > 0:
166
            line = self.store_line.strip()
167
            self.Write(output_file, line + '\n')
168
            self.store_line = ""
169
170
    def Translate(self, root, filename, output, output_dir, minimum):
171
        output_file = codecs.open(os.path.join(output_dir, output), 'w', 'utf-8-sig')
172
        processed_files = set()
173
        # fused-min not support gtest switch
174
        if minimum:
175
            processed_files.add(os.path.normpath(os.path.join(root, "gtest/iutest_switch.hpp")))
176
177
        def ProcessFile(curr, filename, fileset, minimum):
178
            path = os.path.join(root, filename)
179
            if not os.path.exists(path):
180
                path = os.path.join(curr, filename)
181
182
            path = os.path.normpath(path)
183
            if path in fileset:
184
                return
185
186
            find_ifdef = False
187
            fileset.add(path)
188
            # print(filename)
189
            for line in codecs.open(path, 'r', 'utf-8-sig'):
190
                line = re.sub('/\*.*?\*/', '', line)
191
                m = self.INCLUDE_REGEX.match(line)
192
                if m:
193
                    self.Flush(output_file)
194
                    include_file = m.group(1)
195
                    if find_ifdef:
196
                        s = set(fileset)
197
                        ProcessFile(os.path.dirname(path), include_file, s, minimum)
198
                    else:
199
                        ProcessFile(os.path.dirname(path), include_file, fileset, minimum)
200
                else:
201
                    find_ifdef = bool(self.IFDEF_REGEX.match(line))
202
                    if minimum:
203
                        line = self.Minimze(line)
204
                        if len(line):
205
                            if self.PREPRO_REGEX.match(self.store_line):
206
                                line = line.strip()
207
                                if line.endswith('\\'):
208
                                    self.StoreMinimze(line.rstrip(r'\\'))
209
                                else:
210
                                    self.StoreMinimze(line)
211
                                    self.Flush(output_file)
212
                            elif self.PREPRO_REGEX.match(line):
213
                                self.Flush(output_file)
214
                                if line.strip().endswith('\\'):
215
                                    self.StoreMinimze(line.strip().rstrip(r'\\'))
216
                                else:
217
                                    self.Write(output_file, line)
218
                            else:
219
                                strip_line = line.strip()
220
                                self.StoreMinimze(strip_line)
221
                                #if not re.match('.*[{};\(\)<>]$', strip_line):
222
                                #   self.Flush(output_file)
223
                                if re.match('.*(:|IUTEST_CXX_DEFAULT_FUNCTION)$', strip_line):
224
                                    self.store_line += " "
225
                    else:
226
                        self.Write(output_file, line)
227
            self.Flush(output_file)
228
229
        ProcessFile(root, filename, processed_files, minimum)
230
        output_file.close()
231
232
    def Write(self, output_file, line):
233
        if len(line) > 10000:
234
            idx = line.rfind('}', 0, 10000)
235
            if idx > 0:
236
                output_file.write(line[0:idx])
237
                output_file.write('\n')
238
                line = line[idx:]
239
        output_file.write(line)
240
241
def FusedSrc(root, filename, output, output_dir, minimum):
242
    f = IutestFused()
243
    f.Translate(root, filename, output, output_dir, minimum)
244
245
246
def FusedAll(root, output_dir):
247
    if not os.path.exists(output_dir):
248
        os.makedirs(output_dir, True)
249
    FusedSrc(root, 'iutest.hpp', 'iutest.hpp', output_dir, False)
250
    FusedSrc(root, 'iutest.hpp', 'iutest.min.hpp', output_dir, True)
251
252
253
def main():
254
    argc = len(sys.argv)
255
    if argc == 2:
256
        FusedAll(IUTEST_INCLUDE_DIR, sys.argv[1])
257
    elif argc == 3:
258
        FusedAll(os.path.join(sys.argv[1], "include"), sys.argv[2])
259
    else:
260
        print(__doc__)
261
        sys.exit(1)
262
263
264
if __name__ == '__main__':
265
    main()
266