File: extracttests.py

package info (click to toggle)
cppcheck 2.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,132 kB
  • sloc: cpp: 268,935; python: 20,890; ansic: 8,090; sh: 1,045; makefile: 1,008; xml: 1,005; cs: 291
file content (393 lines) | stat: -rwxr-xr-x 13,213 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env python3
#
# Cppcheck - A tool for static C/C++ code analysis
# Copyright (C) 2007-2021 Cppcheck team.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
Extract test cases information from Cppcheck test
file
"""

import os
import sys
import re


def get_includes(code):
    includes = (('alloca','alloca.h'),
                ('NULL','cstddef'),
                ('size_t','cstddef'),
                ('free','cstdlib'),
                ('malloc','cstdlib'),
                ('realloc','cstdlib'),
                ('memcpy','cstring'),
                ('stdin','cstdio'),
                ('strcat','cstring'),
                ('strchr','cstring'),
                ('strcpy','cstring'),
                ('strlen','cstring'),
                ('strncat','cstring'),
                ('strncpy','cstring'),
                ('std::cout','iostream'),
                ('std::pair','utility'),
                ('std::shared_ptr','memory'),
                ('std::string','string'),
                ('std::unique_ptr','memory'),
                ('std::vector','vector'))

    ret = ''

    for i in includes:
        if i[0] in code:
            include_header = '#include <%s>' % i[1]
            if include_header not in ret:
                ret += include_header + '\n'

    return ret


def tweak_expected(expected, start_code):
    if start_code is None or start_code == '':
        return expected
    res = re.match(r'[^(]*\[([^:\]]+):([0-9]+)\](.*)', expected)
    if res is None:
        return expected
    lines = len(start_code[:-1].split('\n'))
    return '[%s:%i]%s' % (res.group(1), lines + int(res.group(2)), res.group(3))


class Extract:

    """
    Read Cppcheck test file and create data
    representation
    """

    # array that stores all the test cases
    nodes = []

    def parseFile(self, filename):
        """
        parse test file and add info to the nodes
        variable
        """

        name = '[0-9a-zA-Z_]+'
        string = '\\"(.+)\\"'

        testclass = None
        functionName = None
        code = None
        start_code = None
        disable = False

        for line in open(filename, 'r'):
            # testclass starts
            res = re.match('class (' + name + ')', line)
            if res is not None:
                testclass = res.group(1)

            # end of testclass
            if re.match('};', line) is not None:
                testclass = None

            # function start
            res = re.match('\\s+void (' + name + ')\\(\\)', line)
            if res is not None:
                functionName = res.group(1)
                start_code = None

            elif re.match('\\s+}', line) is not None:
                functionName = None

            # extracttests commands..
            res = re.match(r'\s*//\s*extracttests.start:(.*)', line)
            if res is not None:
                start_code = res.group(1).replace('\\n', '\n') + '\n'
            elif line.find('extracttests.disable') >= 0:
                disable = True
            elif line.find('extracttests.enable') >= 0:
                disable = False

            if functionName is None or disable:
                continue

            # check
            for f in check_function:
                res = re.match('\\s+' + f + '\\(' + string, line)
                if res is not None:
                    code = res.group(1)
                    break

            # code..
            if code is not None:
                res = re.match('\\s+' + string, line)
                if res is not None:
                    if line.find('",') > line.find('"'):
                        code = None
                        continue

                    code = code + res.group(1)
                    if res.group(1).find('"') > 0:
                        code = None

            # assert
            res = re.match('\\s+ASSERT_EQUALS\\(\\"([^"]*)\\",', line)
            if res is not None and code is not None:
                if start_code:
                    includes = get_includes(start_code + code)
                    code = includes + start_code + code
                    expected = tweak_expected(res.group(1), includes + start_code)
                else:
                    includes = get_includes(code)
                    code = includes + code
                    expected = tweak_expected(res.group(1), includes)

                node = {'testclass': testclass,
                        'functionName': functionName,
                        'code': code.replace("\\\\", "\\"),
                        'expected': expected}
                self.nodes.append(node)
                code = None
            elif re.match('\\s+[TOD_]*ASSERT', line) is not None:
                code = None

def strtoxml(s):
    """Convert string to xml/html format"""
    return s.replace('&', '&amp;').replace('"', '&quot;').replace('<', '&lt;').replace('>', '&gt;')


def trimname(name):
    """Trim test name. Trailing underscore and digits are removed"""
    while name[-1].isdigit():
        name = name[:-1]
    if name[-1] == '_':
        name = name[:-1]
    return name


def writeHtmlFile(nodes, functionName, filename, errorsOnly):
    """Write html file for a function name"""
    fout = open(filename, 'w')
    fout.write('<html>\n')
    fout.write('<head>\n')
    fout.write('  <style type="text/css">\n')
    fout.write('  body { font-size: 0.8em }\n')
    fout.write(
        '  th { background-color: #A3C159; text-transform: uppercase }\n')
    fout.write('  td { background-color: white; vertical-align: text-top }\n')
    fout.write('  pre { background-color: #EEEEEE }\n')
    fout.write('  </style>\n')
    fout.write('</head>\n')
    fout.write('<body>\n')

    fout.write('<a href="index.htm">Home</a> -- ')
    if errorsOnly:
        fout.write('<a href="all-' + functionName + '.htm">All test cases</a>')
    else:
        fout.write(
            '<a href="errors-' + functionName + '.htm">Error test cases</a>')
    fout.write('<br><br>')

    testclass = None
    num = 0
    for node in nodes:
        if errorsOnly and node['expected'] == '':
            continue
        if trimname(node['functionName']) == functionName:
            num = num + 1

            if not testclass:
                testclass = node['testclass']
                fout.write(
                    '<h1>' + node['testclass'] + '::' + functionName + '</h1>')
                fout.write('<table border="0" cellspacing="0">\n')
                fout.write(
                    '  <tr><th>Nr</th><th>Code</th><th>Expected</th></tr>\n')

            fout.write('  <tr><td>' + str(num) + '</td>')
            fout.write('<td><pre>' + strtoxml(
                node['code']).replace('\\n', '\n') + '</pre></td>')
            fout.write(
                '<td>' + strtoxml(node['expected']).replace('\\n', '<br>') + '</td>')
            fout.write('</tr>\n')

    if testclass is not None:
        fout.write('</table>\n')
    fout.write('</body></html>\n')
    fout.close()


if len(sys.argv) <= 1 or '--help' in sys.argv:
    print('Extract test cases from test file')
    print(
        'Syntax: extracttests.py [--html=folder] [--xml] [--code=folder] [--only-tp] [--check-function=check] path/testfile.cpp')
    sys.exit(0)

# parse command line
xml = False
filename = None
htmldir = None
codedir = None
onlyTP = None
check_function = ['check[A-Za-z0-9_]*']
for arg in sys.argv[1:]:
    if arg == '--xml':
        xml = True
    elif arg == '--only-tp':
        onlyTP = True
    elif arg.startswith('--html='):
        htmldir = arg[7:]
    elif arg.startswith('--code='):
        codedir = arg[7:]
    elif arg.endswith('.cpp'):
        filename = arg
    elif arg.startswith('--check-function='):
        check_function.append(arg[17:])
    else:
        print('Invalid option: ' + arg)
        sys.exit(1)


# extract test cases
if filename is not None:
    # parse test file
    e = Extract()
    e.parseFile(filename)

    # generate output
    if xml:
        print('<?xml version="1.0"?>')
        print('<tree>')
        count = 0
        for node in e.nodes:
            s = '  <node'
            s += ' function="' + node['functionName'] + '"'
            s += ' code="' + strtoxml(node['code']) + '"'
            s += ' expected="' + strtoxml(node['expected']) + '"'
            s += '/>'
            print(s)
        print('</tree>')
    elif htmldir is not None:
        if not htmldir.endswith('/'):
            htmldir += '/'
        if not os.path.exists(htmldir):
            os.mkdir(htmldir)
        findex = open(htmldir + 'index.htm', 'w')
        findex.write('<html>\n')
        findex.write('<head>\n')
        findex.write('  <style type="text/css">\n')
        findex.write('  table { font-size: 0.8em }\n')
        findex.write(
            '  th { background-color: #A3C159; text-transform: uppercase }\n')
        findex.write(
            '  td { background-color: #F0FFE0; vertical-align: text-top }\n')
        findex.write('  A:link { text-decoration: none }\n')
        findex.write('  A:visited { text-decoration: none }\n')
        findex.write('  A:active { text-decoration: none }\n')
        findex.write('  A:hover { text-decoration: underline; color: blue }\n')
        findex.write('  </style>\n')
        findex.write('</head>\n')
        findex.write('<body>\n')
        findex.write('<h1>' + filename + '</h1>\n')

        functionNames = []
        for node in e.nodes:
            functionname = trimname(node['functionName'])
            if functionname not in functionNames:
                functionNames.append(functionname)
        functionNames.sort()

        findex.write('<table border="0" cellspacing="0">\n')
        findex.write('  <tr><th>Name</th><th>Errors</th><th>All</th></tr>\n')
        for functionname in functionNames:
            findex.write('  <tr><td>' + functionname + '</td>')
            numall = 0
            numerr = 0
            for node in e.nodes:
                if trimname(node['functionName']) == functionname:
                    numall = numall + 1
                    if node['expected'] != '':
                        numerr = numerr + 1
            if numerr == 0:
                findex.write('<td><div align="right">0</div></td>')
            else:
                findex.write('<td><a href="errors-' + functionname +
                             '.htm"><div align="right">' + str(numerr) + '</div></a></td>')
            findex.write('<td><a href="all-' + functionname +
                         '.htm"><div align="right">' + str(numall) + '</div></a></td>')
            findex.write('</tr>\n')

        findex.write('</table>\n')

        findex.write('</body></html>')
        findex.close()

        # create files for each functionName
        for functionName in functionNames:
            writeHtmlFile(e.nodes,
                          functionName,
                          htmldir + 'errors-' + functionName + '.htm',
                          True)
            writeHtmlFile(e.nodes,
                          functionName,
                          htmldir + 'all-' + functionName + '.htm',
                          False)

    elif codedir:
        testnum = 0

        if not codedir.endswith('/'):
            codedir = codedir + '/'

        if not os.path.exists(codedir):
            os.mkdir(codedir)

        testfile = os.path.splitext(os.path.basename(filename))[0]

        for node in e.nodes:
            if onlyTP and node['expected'] == '':
                continue

            testnum = testnum + 1

            functionName = node['functionName']
            code = node['code']
            code = code.replace('\\n', '\n')
            code = code.replace('\\"', '"')
            expected = node['expected']
            if expected.endswith('\\n'):
                expected = expected[:-2]

            filename = '%s-%03i-%s.cpp' % (testfile, testnum, functionName)

            # comment error
            res = re.match(r'[^(]*\[([^:\]]+):([0-9]+)\]: \([a-z, ]+\) (.*)', expected)
            if res:
                line_number = int(res.group(2)) - 1
                lines = code.split('\n')
                if len(lines) > line_number:
                    lines[line_number] += ' // ' + res.group(3)
                    code = '\n'.join(lines)
                else:
                    print('filename:%s expected:%s' % (filename, expected))

            # source code
            with open(codedir + filename, 'w') as fout:
                fout.write(code + '\n')
    else:
        for node in e.nodes:
            print(node['functionName'])