aboutsummaryrefslogtreecommitdiff
path: root/scripts/testsuite-filter
blob: 3d9fb272b47c0e40549b4aea191fa97ef961a692 (plain)
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
#!/usr/bin/env python3


from __future__ import print_function
import sys
import os
import re
import collections

debug = False


class Arch:
    def __init__(self, arch):
        arch = re.sub("^rv(32|64|128)g", 'rv\\1imafd', arch)
        m = re.match('rv(32|64|128)', arch)
        self.base_arch = m.group(0)
        self.ext = []
        i = len(self.base_arch)

        while i < len(arch):
            ext = arch[i]
            if ext in ['x', 's', 'z']:
                extlen = 1
                while (i+extlen) < len(arch) and arch[i+extlen] != '_':
                    extlen += 1
                self.ext.append(arch[i:i + extlen])
                i += extlen
            elif ext == '_':
                i += 1
            else:
                self.ext.append(ext)
                i += 1


def usage():
    print("%s <toolname> <libc>" \
          " <white-list-base-dir> <testsuite.sum>" % sys.argv[0])


def get_white_list_files(raw_arch, abi, libc, white_list_base_dir):
    """ Return white file list according the arch, abi, libc name and component.
    """
    white_list_files = []
    arch = Arch(raw_arch)

    def append_if_exist(filename):
        if debug:
            print ("Try append: %s" % filename)
        filepath = os.path.join(white_list_base_dir, filename)
        if os.path.exists(filepath):
            if debug:
                print ("Got: %s" % filename)
            white_list_files.append(filepath)

    libc_filename = "common.log"
    append_if_exist(libc_filename)

    libc_filename = "%s.log" % (libc)
    append_if_exist(libc_filename)

    filename = "%s.log" % (arch.base_arch)
    append_if_exist(filename)

    filename = "%s.log" % (abi)
    append_if_exist(filename)

    filename = "%s.%s.log" % (arch.base_arch, abi)
    append_if_exist(filename)

    filename = "%s.%s.log" % (libc, arch.base_arch)
    append_if_exist(filename)

    filename = "%s.%s.log" % (libc, abi)
    append_if_exist(filename)

    filename = "%s.%s.%s.log" % (libc, arch.base_arch, abi)
    append_if_exist(filename)

    for ext in arch.ext:
        filename = "%s.log" % (ext)
        append_if_exist(filename)

        filename = "%s.%s.log" % (arch.base_arch, ext)
        append_if_exist(filename)

        filename = "%s.%s.log" % (ext, abi)
        append_if_exist(filename)

        filename = "%s.%s.%s.log" % (arch.base_arch, ext, abi)
        append_if_exist(filename)

        filename = "%s.%s.log" % (libc, ext)
        append_if_exist(filename)

        filename = "%s.%s.%s.log" % (libc, arch.base_arch, ext)
        append_if_exist(filename)

        filename = "%s.%s.%s.log" % (libc, ext, abi)
        append_if_exist(filename)

        filename = "%s.%s.%s.%s.log" % (libc, arch.base_arch, ext, abi)
        append_if_exist(filename)

    return white_list_files


def read_white_lists(white_list_files, is_gcc):
    if is_gcc:
        white_lists = dict()
    else:
        white_lists = set()
    for fname in white_list_files:
        with open(fname) as f:
            content = f.readlines()
            for l in content:
                l = l.strip()
                if len(l) == 0:
                    continue
                if l[0] == '#':
                    continue

                if is_gcc:
                    try:
                        key = l.split(' ')[1]
                    except:
                        print ("Corrupt allowlist file?")
                        print ("Each line must contail <STATUS>: .*")
                        print ("e.g. FAIL: g++.dg/pr83239.C")
                        print ("Or starts with # for comment")
                    if key not in white_lists:
                        white_lists[key] = []
                    white_lists[key].append(l)
                else:
                    white_lists.add(l)

    return white_lists


def read_sum(sum_files):
    unexpected_results = dict()
    for sum_file in sum_files:
        with open(sum_file) as f:
            content = f.readlines()
            current_target = None
            variations = []
            scan_variations = False
            unexpected_result = dict()
            tool = os.path.basename(sum_file).split(".")[0]
            for l in content:
                if l.startswith("Schedule of variations"):
                    scan_variations = True
                    continue
                if scan_variations and l.startswith("    "):
                    variations.append(l.strip())
                    continue
                scan_variations = False

                if l.startswith("Running target"):
                    # Parsing current running target.
                    current_target = l.split(" ")[-1].strip()
                    unexpected_result[current_target] = list()
                elif l.startswith("FAIL") or l.startswith("XPASS") \
                     or l.startswith("ERROR"):
                    unexpected_result[current_target].append(l.strip())
            unexpected_results[tool] = unexpected_result
    # tool -> variation(target) -> list of unexpected result
    return unexpected_results


def get_white_list(arch, abi, libc, white_list_base_dir, is_gcc):
    white_list_files = \
        get_white_list_files(arch, abi, libc, white_list_base_dir)
    white_list = read_white_lists(white_list_files, is_gcc)
    return white_list



def filter_result(tool, libc, white_list_base_dir, unexpected_results):
    summary = dict()
    any_fail = False
    is_gcc = tool == 'gcc'
    # Filter with white list.
    for testtool, variation_unexpected_result in unexpected_results.items():
        for variation, unexpected_result in variation_unexpected_result.items():
            # Extract variation to arch/abi
            arch = ""
            abi = ""
            cmodel = ""
            for info in variation.split('/'):
                if info.startswith('-march'):
                    arch = info[7:]
                elif info.startswith('-mabi'):
                    abi = info[6:]
                elif info.startswith('-mcmodel'):
                    cmodel = info[9:]

            white_list = \
                get_white_list(arch, abi, libc,
                               os.path.join(white_list_base_dir, tool),
                               is_gcc)
            # filter!
            config = (arch, abi, cmodel)
            fail_count = 0
            unexpected_result_list = []
            if is_gcc:
                case_count = set()
                for ur in unexpected_result:
                    key = ur.split(' ')[1]
                    if key in white_list and \
                              any(map(lambda x:ur.startswith(x),
                                      white_list[key])):
                        # This item can be ignored
                        continue
                    else:
                        unexpected_result_list.append(ur)
                        fail_count += 1
                        case_count.add(key)
                        any_fail = True

                if config not in summary:
                    summary[config] = dict()
                summary[config][testtool] = (fail_count, len(case_count))
            else:
                for ur in unexpected_result:
                    if ur not in white_list:
                        unexpected_result_list.append(ur)
                        fail_count += 1
                        any_fail = True

                if config not in summary:
                    summary[config] = dict()
                summary[config][testtool] = fail_count


            if len(unexpected_result_list) != 0:
                print ("\t\t=== %s: Unexpected fails for %s %s %s ===" \
                       % (testtool, arch, abi, cmodel))
                for ur in unexpected_result_list:
                    print (ur)


    # Generate summary report.
    if tool == 'gcc':
        toollist = ['gcc', 'g++', 'gfortran']
    elif tool == 'binutils':
        toollist = ['binutils', 'ld', 'gas']
    else:
        raise Exception("Unsupported tool `%s`" % tool)

    bar_item = map(lambda x: "%13s" % x, toollist)
    bar = " |".join(bar_item)
    print ("\n               ========= Summary of %s testsuite =========" % tool)
    if is_gcc:
        print ("                            | # of unexpected case / # of unique unexpected case")
    else:
        print ("                            | # of unexpected case")
    print ("                            |%s |" % bar)
    for config, result in summary.items():
        arch, abi, cmodel = config
        print (" %10s/ %6s/ %6s |" % (arch, abi, cmodel), end='')
        for tool in toollist:
            if tool not in summary[config]:
                print ("%7s |" % '-', end='')
                continue

            if is_gcc:
                fail_count, case_count = summary[config][tool]
                print ("%5d / %5d |" % (fail_count, case_count), end='')
            else:
                fail_count = summary[config][tool]
                print ("%13d |" % fail_count, end='')
        print ("")
    if any_fail or len(summary.items()) == 0:
        return 1
    else:
        return 0


def main():
    if len(sys.argv) < 5:
        usage()
        sys.exit()
    tool, libc, white_list_base_dir, sum_files = sys.argv[1:5]

    rv = 0

    sum_files = sum_files.split(',')
    unexpected_results = read_sum(sum_files)
    if tool in ['gcc', 'binutils']:
        rv = filter_result(tool, libc, white_list_base_dir,
                           unexpected_results)
    else:
        print ("Unsupported tool: `%s`" % tool)
        rv = 1

    sys.exit(rv)


if __name__ == '__main__':
    main()