84 lines · python
1#!/usr/bin/env python32 3"""Compiles a source file and checks errors against those listed in the file.4 5Parameters:6 sys.argv[1]: a source file with contains the input and expected output7 sys.argv[2]: the Flang frontend driver8 sys.argv[3:]: Optional arguments to the Flang frontend driver"""9 10import sys11import re12import tempfile13import subprocess14import common as cm15 16from difflib import unified_diff17 18cm.check_args(sys.argv)19srcdir = cm.set_source(sys.argv[1])20with open(srcdir, "r", encoding="utf-8") as f:21 src = f.readlines()22actual = ""23expect = ""24diffs = ""25log = ""26 27flang_fc1 = cm.set_executable(sys.argv[2])28flang_fc1_args = sys.argv[3:]29flang_fc1_options = "-fsyntax-only"30 31# Compiles, and reads in the output from the compilation process32cmd = [flang_fc1, *flang_fc1_args, flang_fc1_options, str(srcdir)]33with tempfile.TemporaryDirectory() as tmpdir:34 try:35 proc = subprocess.run(36 cmd,37 stdout=subprocess.PIPE,38 stderr=subprocess.PIPE,39 check=True,40 universal_newlines=True,41 cwd=tmpdir,42 encoding="utf-8",43 )44 except subprocess.CalledProcessError as e:45 log = e.stderr46 if e.returncode >= 128:47 print(f"{log}")48 sys.exit(1)49 50# Cleans up the output from the compilation process to be easier to process51for line in log.split("\n"):52 m = re.search(r"[^:]*:(\d+:).*(?:error|warning|portability|because):(.*)", line)53 if m:54 if re.search(r"warning: .*fold.*host", line):55 continue # ignore host-dependent folding warnings56 actual += m.expand(r"\1\2\n")57 58# Gets the expected errors and their line numbers59errors = []60for i, line in enumerate(src, 1):61 m = re.search(r"(?:^\s*!\s*(?:ERROR|WARNING|PORTABILITY|BECAUSE): )(.*)", line)62 if m:63 errors.append(m.group(1))64 continue65 if errors:66 for x in errors:67 expect += f"{i}: {x}\n"68 errors = []69 70# Compares the expected errors with the compiler errors71for line in unified_diff(actual.split("\n"), expect.split("\n"), n=0):72 line = re.sub(r"(^\-)(\d+:)", r"\nactual at \g<2>", line)73 line = re.sub(r"(^\+)(\d+:)", r"\nexpect at \g<2>", line)74 diffs += line75 76if diffs != "":77 print(diffs)78 print()79 print("FAIL")80 sys.exit(1)81else:82 print()83 print("PASS")84