1#!/usr/bin/env python3
2
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 output
7    sys.argv[2]: the Flang frontend driver
8    sys.argv[3:]: Optional arguments to the Flang frontend driver"""
9
10import sys
11import re
12import tempfile
13import subprocess
14import common as cm
15
16from difflib import unified_diff
17
18cm.check_args(sys.argv)
19srcdir = cm.set_source(sys.argv[1])
20with open(srcdir, 'r') 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 process
32cmd = [flang_fc1, *flang_fc1_args, flang_fc1_options, str(srcdir)]
33with tempfile.TemporaryDirectory() as tmpdir:
34    try:
35        proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
36                              check=True, universal_newlines=True, cwd=tmpdir)
37    except subprocess.CalledProcessError as e:
38        log = e.stderr
39        if e.returncode >= 128:
40            print(f"{log}")
41            sys.exit(1)
42
43# Cleans up the output from the compilation process to be easier to process
44for line in log.split('\n'):
45    m = re.search(r"[^:]*:(\d+:).*(?:error:)(.*)", line)
46    if m:
47        actual += m.expand(r"\1\2\n")
48
49# Gets the expected errors and their line number
50errors = []
51for i, line in enumerate(src, 1):
52    m = re.search(r"(?:^\s*!\s*ERROR: )(.*)", line)
53    if m:
54        errors.append(m.group(1))
55        continue
56    if errors:
57        for x in errors:
58            expect += f"{i}: {x}\n"
59        errors = []
60
61# Compares the expected errors with the compiler errors
62for line in unified_diff(actual.split("\n"), expect.split("\n"), n=0):
63    line = re.sub(r"(^\-)(\d+:)", r"\nactual at \g<2>", line)
64    line = re.sub(r"(^\+)(\d+:)", r"\nexpect at \g<2>", line)
65    diffs += line
66
67if diffs != "":
68    print(diffs)
69    print()
70    print("FAIL")
71    sys.exit(1)
72else:
73    print()
74    print("PASS")
75
76