Break circular dependency between FIR dialect and utilities
[llvm-project.git] / flang / test / Semantics / test_symbols.py
blobb82903fe44f2b059dd057046de9e74a817a7f247
1 #!/usr/bin/env python3
3 """Compiles a source file with "-fdebug-unparse-with-symbols' and verifies
4 we get the right symbols in the output, i.e. the output should be
5 the same as the input, except for the copyright comment.
7 Parameters:
8 sys.argv[1]: a source file with contains the input and expected output
9 sys.argv[2]: the Flang frontend driver
10 sys.argv[3:]: Optional arguments to the Flang frontend driver"""
12 import sys
13 import tempfile
14 import re
15 import subprocess
16 import common as cm
18 from difflib import unified_diff
20 cm.check_args(sys.argv)
21 src = cm.set_source(sys.argv[1])
22 diff1 = ""
23 diff2 = ""
25 flang_fc1 = cm.set_executable(sys.argv[2])
26 flang_fc1_args = sys.argv[3:]
27 flang_fc1_options = "-fdebug-unparse-with-symbols"
29 # Strips out blank lines and all comments except for "!DEF:", "!REF:", "!$acc" and "!$omp"
30 with open(src, 'r') as text_in:
31 for line in text_in:
32 text = re.sub(r"!(?![DR]EF:|\$omp|\$acc).*", "", line)
33 text = re.sub(r"^\s*$", "", text)
34 diff1 += text
36 # Strips out "!DEF:" and "!REF:" comments
37 for line in diff1:
38 text = re.sub(r"![DR]EF:.*", "", line)
39 diff2 += text
41 # Compiles, inserting comments for symbols:
42 cmd = [flang_fc1, *flang_fc1_args, flang_fc1_options]
43 with tempfile.TemporaryDirectory() as tmpdir:
44 diff3 = subprocess.check_output(cmd, input=diff2, universal_newlines=True, cwd=tmpdir)
46 # Removes all whitespace to compare differences in files
47 diff1 = diff1.replace(" ", "")
48 diff3 = diff3.replace(" ", "")
49 diff_check = ""
51 # Compares the input with the output
52 diff_check = "\n".join(unified_diff(diff1.split("\n"), diff3.split("\n"), n=999999,
53 fromfile="Expected_output", tofile="Actual_output"))
55 if diff_check != "":
56 print(diff_check.replace(" ", ""))
57 print()
58 print("FAIL")
59 sys.exit(1)
60 else:
61 print()
62 print("PASS")