[DirectX] Set the EnableRawAndStructuredBuffers shader flag (#122667)
[llvm-project.git] / flang / test / Semantics / test_modfile.py
blob0e7806f27aa90fce2b57a83d437ed1b7d52dc2af
1 #!/usr/bin/env python3
3 """Compiles a source file and compares generated .mod files against expected.
5 Parameters:
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"""
10 import sys
11 import re
12 import os
13 import tempfile
14 import subprocess
15 import glob
16 import common as cm
18 from pathlib import Path
19 from difflib import unified_diff
21 cm.check_args_long(sys.argv)
22 srcdir = Path(sys.argv[1])
23 sources = list(glob.iglob(str(srcdir)))
24 sources = sorted(sources)
25 diffs = ""
27 flang_fc1 = cm.set_executable(sys.argv[2])
28 flang_fc_args = sys.argv[3:]
29 flang_fc1_options = "-fsyntax-only"
31 with tempfile.TemporaryDirectory() as tmpdir:
32 for src in sources:
33 src = Path(src).resolve()
34 actual = ""
35 expect = ""
36 expected_files = set()
37 actual_files = set()
39 if not src.is_file():
40 cm.die(src)
42 prev_files = set(os.listdir(tmpdir))
43 cmd = [flang_fc1, *flang_fc_args, flang_fc1_options, str(src)]
44 proc = subprocess.check_output(
45 cmd, stderr=subprocess.PIPE, universal_newlines=True, cwd=tmpdir
47 actual_files = set(os.listdir(tmpdir)).difference(prev_files)
49 # The first 3 bytes of the files are an UTF-8 BOM
50 with open(src, "r", encoding="utf-8", errors="strict") as f:
51 for line in f:
52 m = re.search(r"^!Expect: (.*)", line)
53 if m:
54 expected_files.add(m.group(1))
56 extra_files = actual_files.difference(expected_files)
57 if extra_files:
58 print(f"Unexpected .mod files produced: {extra_files}")
59 sys.exit(1)
61 for mod in expected_files:
62 mod = Path(tmpdir).joinpath(mod)
63 if not mod.is_file():
64 print(f"Compilation did not produce expected mod file: {mod}")
65 sys.exit(1)
66 with open(mod, "r", encoding="utf-8", errors="strict") as f:
67 for line in f:
68 if "!mod$" in line or "!need$" in line:
69 continue
70 actual += line
72 with open(src, "r", encoding="utf-8", errors="strict") as f:
73 for line in f:
74 if f"!Expect: {mod.name}" in line:
75 for line in f:
76 if re.match(r"^$", line):
77 break
78 m = re.sub(r"^!", "", line.lstrip())
79 expect += m
81 diffs = "\n".join(
82 unified_diff(
83 actual.replace(" ", "").split("\n"),
84 expect.replace(" ", "").split("\n"),
85 fromfile=mod.name,
86 tofile="Expect",
87 n=999999,
91 if diffs != "":
92 print(diffs)
93 print()
94 print("FAIL")
95 sys.exit(1)
97 print()
98 print("PASS")