Debian Codegraph @

testsuite/lib/libtest.py

raw
1 #!/usr/bin/env python3
2
3 import os
4 import platform
5 import pathlib
6 import re
7 import signal
8 import subprocess
9 import sys
10 import time
11 import tempfile
12 import glob
13
14 RESET = '\033[0m'
15 GREEN = '\033[32m'
16 HGREEN = '\033[0;92m'
17 RED = '\033[91m'
18 HRED = '\033[0;91m'
19 YELLOW = '\033[93m'
20 BLUE = '\033[34m'
21
22 def cleanup_temp_files(files):
23 for f in files:
24 try:
25 os.remove(f)
26 except FileNotFoundError:
27 pass
28
29 # Logs messages into file.
30 class Log:
31 def __init__(self, output_path):
32 self.output_path = output_path
33 self.file = open(output_path, "w")
34
35 def __del__(self):
36 self.file.close()
37
38 def print(self, s):
39 self.file.write(s + '\n')
40
41 class UnitTest:
42 def __init__(self, test_path, log_path, binaries_path):
43 self.log = Log(log_path)
44
45 self.test_path = test_path
46 self.test_folder = self.extract_test_folder()
47 self.file = open(test_path, "r")
48 self.file_content = self.file.read()
49 self.lines = self.file_content.split('\n')
50
51 self.options = self.extract_options()
52 self.must_have = self.extract_must_have()
53 self.must_not_have = self.extract_must_not_have()
54 self.error_msgs = self.extract_error_msgs()
55 self.warning_msgs = self.extract_warning_msgs()
56 self.compile_options = self.extract_must_compile()
57 self.skip_silently = self.should_skip_test_silently()
58 self.no_debuginfo = self.without_debuginfo()
59 self.no_ipa_clones = self.without_ipaclones()
60 self.skip_on_archs = self.should_skip_test_on_archs()
61
62 self.binaries_path = binaries_path
63
64 self.must_have_regexes, self.must_not_have_regexes, self.error_msgs_regexes, self.warning_msgs_regexes = self.compile_regexes()
65
66 # Get folder where the test is.
67 def extract_test_folder(self):
68 last_slash = self.test_path.rfind('/')
69 return self.test_path[:last_slash]
70
71 # Given a string, expand all tokens we find. Currently only the `$test_dir`
72 # token.
73 def expand_tokens_in_string(self, string):
74 x = string
75 x = x.replace("$test_dir", self.test_folder)
76 return x
77
78 # Given a list of strings, expand all tokens we find. Currently only the `$test_dir`
79 # token.
80 def expand_tokens_in_list(self, l):
81 new_list = []
82 for s in l:
83 new_list.append(self.expand_tokens_in_string(s))
84
85 return new_list
86
87 # Extract options passed through dg-options.
88 def extract_options(self):
89 p = re.compile('{ *dg-options "(.*)" *}')
90 matches = re.search(p, self.file_content)
91 if matches is not None:
92 matches = matches.group(1)
93 return self.expand_tokens_in_list(matches.split())
94
95 self.log.print("WARNING: No dg-options given")
96 return []
97
98 # Extract rules that must be in the clang-extract output.
99 def extract_must_have(self):
100 p = re.compile('{ *dg-final *{ *scan-tree-dump *"(.*)" *} *}')
101
102 matches = []
103 for line in self.lines:
104 matched = re.search(p, line)
105 if matched is not None:
106 matches.append(matched.group(1))
107 return matches
108
109 # Extract error message.
110 def extract_error_msgs(self):
111 p = re.compile('{ *dg-error *"(.*)" *}')
112
113 matches = []
114 for line in self.lines:
115 matched = re.search(p, line)
116 if matched is not None:
117 regex = "error: .*" + matched.group(1)
118 matches.append(regex)
119 return matches
120
121 # Extract warning message.
122 def extract_warning_msgs(self):
123 p = re.compile('{ *dg-warning *"(.*)" *}')
124
125 matches = []
126 for line in self.lines:
127 matched = re.search(p, line)
128 if matched is not None:
129 regex = "warning: .*" + matched.group(1)
130 matches.append(regex)
131 return matches
132
133 # Extract rules that must NOT be in the clang-extract output.
134 def extract_must_not_have(self):
135 p = re.compile('{ *dg-final *{ *scan-tree-dump-not *"(.*)" *} *}')
136
137 matches = []
138 for line in self.lines:
139 matched = re.search(p, line)
140 if matched is not None:
141 matches.append(matched.group(1))
142
143 return matches
144
145 # Flag that test must XFAIL
146 def extract_should_xfail(self):
147 p = re.compile('{ *dg-xfail *}')
148 matched = re.search(p, self.file_content)
149 if matched is not None:
150 return True
151
152 return False
153
154 # Flag that e must compile the file.
155 def extract_must_compile(self):
156 p = re.compile('{ *dg-compile "(.*)" *}')
157 matches = re.search(p, self.file_content)
158 if matches is not None:
159 matches = matches.group(1)
160 return self.expand_tokens_in_list(matches.split())
161
162 return None
163
164 def should_skip_test_silently(self):
165 p = re.compile('{ *dg-skip-silent *}')
166 matched = re.search(p, self.file_content)
167 if matched is not None:
168 return True
169
170 return False
171
172 # The architectures should be specified with speaces between them, e.g.
173 # /* { dg-skip-on-archs "x86_64 ppc64le" }*/
174 def should_skip_test_on_archs(self):
175 p = re.compile('{ *dg-skip-on-archs "(.*)" }')
176 matched = re.search(p, self.file_content)
177 if matched is not None:
178 matches = matched.group(1).split(' ')
179 return platform.processor() in matches
180
181 return False
182
183 def without_debuginfo(self):
184 p = re.compile('{ *dg-no-debuginfo *}')
185 matched = re.search(p, self.file_content)
186 if matched is not None:
187 return True
188
189 return False
190
191 def without_ipaclones(self):
192 p = re.compile('{ *dg-no-ipa-clones *}')
193 matched = re.search(p, self.file_content)
194 if matched is not None:
195 return True
196
197 return False
198
199
200 def gcc_compile(self):
201 # Do not compile if dg-compile wasn't specified.
202 if self.compile_options is None:
203 return None
204
205 compiler = '/usr/bin/gcc'
206 output = './' + next(tempfile._get_candidate_names()) + ".out"
207
208 command = [ compiler, '-o', output,
209 self.test_path ]
210 command.extend(self.compile_options)
211
212 tool = subprocess.run(command, timeout=10, stderr=subprocess.STDOUT,
213 stdout=subprocess.PIPE)
214
215 if tool.returncode != 0:
216 print(tool.stdout.decode())
217 exit(99)
218
219 return output
220
221 # Compile the regexes in scan-tree-dump and scan-tree-dump-not.
222 def compile_regexes(self):
223 must_have_regexes = []
224 must_not_have_regexes = []
225 error_msgs_regexes = []
226 warning_msgs_regexes = []
227
228 for x in self.must_have:
229 p = re.compile(x)
230 must_have_regexes.append(x)
231
232 for x in self.must_not_have:
233 p = re.compile(x)
234 must_not_have_regexes.append(x)
235
236 for x in self.error_msgs:
237 p = re.compile(x)
238 error_msgs_regexes.append(x)
239
240 for x in self.warning_msgs:
241 p = re.compile(x)
242 warning_msgs_regexes.append(x)
243
244 return must_have_regexes, must_not_have_regexes, error_msgs_regexes, warning_msgs_regexes
245
246 def print_result(self, result, should_xfail = False):
247 if result == 0:
248 if should_xfail == True:
249 print(HRED, "XSUCCESS:", RESET, self.test_path)
250 self.log.print("XSUCCESS:" + self.test_path)
251 else:
252 print(GREEN, "SUCCESS: ", RESET, self.test_path)
253 self.log.print("SUCCESS: " + self.test_path)
254 elif result == 77:
255 print(YELLOW, "SKIPPED: ", RESET, self.test_path)
256 self.log.print("SKIPPED: " + self.test_path)
257 else:
258 if should_xfail == True:
259 print(HGREEN, "XFAIL: ", RESET, self.test_path)
260 self.log.print("XFAIL: " + self.test_path)
261 else:
262 print(RED, "FAIL: ", RESET, self.test_path)
263 self.log.print("FAIL: " + self.test_path)
264
265 def check_founds(self, founds, not_found_msg, rules):
266 for i in range(len(founds)):
267 x = founds[i]
268 if x is False:
269 self.log.print(not_found_msg + rules[i])
270 return False
271 return True
272
273 def check_terminal_output(self, terminal_output):
274 founds = [False] * len(self.error_msgs_regexes)
275 for i in range(len(self.error_msgs_regexes)):
276 p = self.error_msgs_regexes[i]
277 matched = re.search(p, terminal_output)
278 if matched is not None:
279 founds[i] = True
280
281 if self.check_founds(founds, "Error message not found: ",
282 self.error_msgs) == False:
283 return False
284
285 founds = [False] * len(self.warning_msgs_regexes)
286
287 for i in range(len(self.warning_msgs_regexes)):
288 p = self.error_msgs_regexes[i]
289 matched = re.search(p, terminal_output)
290 if matched is not None:
291 founds[i] = True
292
293 if self.check_founds(founds, "Warning message not found: ",
294 self.warning_msgs) == False:
295 return False
296
297 return True
298
299
300 # Check if clang-extract output matches the rules in the test.
301 def check_output(self, output_file):
302
303 try:
304 founds = [False] * len(self.must_have_regexes)
305 with open(output_file, mode="rt", encoding="utf-8") as file:
306 line = file.read()
307
308 self.log.print("output:")
309 self.log.print(line)
310
311 self.log.print("-----")
312
313 for i in range(len(self.must_have_regexes)):
314 p = self.must_have_regexes[i]
315 matched = re.search(p, line)
316 if matched is not None:
317 founds[i] = True
318
319 for i in range(len(self.must_not_have_regexes)):
320 p = self.must_not_have_regexes[i]
321 matched = re.search(p, line)
322 if matched is not None:
323 self.log.print("Must not have pattern found: " +
324 self.must_not_have[i])
325 return False
326
327 if self.check_founds(founds, "Must have pattern not found: ",
328 self.must_have) == False:
329 return False
330 except FileNotFoundError:
331 return False
332
333 return True
334
335 def get_ipa_clones_path(self, elf):
336 output_folder = os.path.dirname(elf)
337 elf_file = os.path.basename(elf)
338
339 test_file = os.path.basename(self.test_path)
340
341 ipa_file = output_folder + "/" + elf_file + "-" + test_file + ".000i.ipa-clones"
342 # Check if the file exists
343 if os.path.isfile(ipa_file) == True:
344 return ipa_file
345
346 # On older versions of gcc, the name is slightly different.
347 ipa_file = output_folder + "/" + test_file + ".000i.ipa-clones"
348 if os.path.isfile(ipa_file) == True:
349 return ipa_file
350
351 return None
352
353 # Indeed run the test.
354 def run_test(self, lto_test=False):
355 clang_extract = self.binaries_path + 'clang-extract'
356 ce_output_path = '/tmp/' + next(tempfile._get_candidate_names()) + '.CE.c'
357
358 if self.skip_on_archs:
359 self.print_result(77)
360 return 77
361
362 command = [ clang_extract, '-DCE_OUTPUT_FILE=' + ce_output_path,
363 self.test_path ]
364 command.extend(self.options)
365
366 tool = subprocess.run(command, timeout=10, stderr=subprocess.STDOUT,
367 stdout=subprocess.PIPE)
368
369 r = self.check(tool, ce_output_path)
370 cleanup_temp_files([ce_output_path])
371 return r
372
373 def run_inline_test(self, lto_test=False):
374 if self.skip_silently:
375 return 0
376
377 inline = self.binaries_path + 'ce-inline'
378 ce_output_path = '/tmp/' + next(tempfile._get_candidate_names()) + '.txt'
379
380 elf = self.gcc_compile()
381
382 command = [ inline, '-o', ce_output_path ]
383 command.extend(self.options)
384 if elf is not None:
385 if self.no_debuginfo == False:
386 command.append("-debuginfo")
387 command.append(elf)
388 if self.no_ipa_clones == False:
389 command.append("-ipa-files")
390 if lto_test:
391 # Pass the directory with all ipa-clones rather than the
392 # path to the ipa-clone file.
393 command.append(os.path.dirname(self.get_ipa_clones_path(elf)))
394 else:
395 command.append(self.get_ipa_clones_path(elf))
396
397 tool = subprocess.run(command, timeout=10, stderr=subprocess.STDOUT,
398 stdout=subprocess.PIPE)
399
400 r = self.check(tool, ce_output_path)
401 cleanup_temp_files((elf, self.get_ipa_clones_path(elf), ce_output_path))
402 if lto_test == True:
403 filelist = glob.glob('*.ipa-clones')
404 cleanup_temp_files(filelist)
405
406 return r
407
408 def check(self, tool, ce_output_path):
409 self.log.print("terminal output of " + os.path.basename(tool.args[0]) + ":")
410 self.log.print(tool.stdout.decode())
411
412 should_xfail = self.extract_should_xfail()
413
414 if self.check_terminal_output(tool.stdout.decode()) == False:
415 self.print_result(1)
416 return 1
417
418 # Only check the output if there is no error message expected.
419 if len(self.error_msgs) == 0:
420 if self.check_output(ce_output_path) == False:
421 self.print_result(1, should_xfail)
422 return 1
423
424 if tool.returncode != 0:
425 self.print_result(tool.returncode, should_xfail)
426 return tool.returncode
427
428 self.print_result(0, should_xfail)