1-
21import glob
32import os
3+ import shutil
44import subprocess
55import sys
66
7- def cleanup (out ):
8- ret = ''
9- for s in out .decode ('utf-8' ).split ('\n ' ):
10- if len (s ) > 1 and s [0 ] == '#' :
7+
8+ def cleanup (out : str ) -> str :
9+ parts = []
10+ for line in out .splitlines ():
11+ if len (line ) > 1 and line [0 ] == '#' :
1112 continue
12- s = "" .join (s .split ())
13- ret = ret + s
14- return ret
13+ parts .append ("" .join (line .split ()))
14+ return "" .join (parts )
15+
16+
17+ # Check for required compilers and exit if any are missing
18+ CLANG_EXE = shutil .which ('clang' )
19+ if not CLANG_EXE :
20+ sys .exit ('Failed to run tests: clang compiler not found' )
21+
22+ GCC_EXE = shutil .which ('gcc' )
23+ if not GCC_EXE :
24+ sys .exit ('Failed to run tests: gcc compiler not found' )
25+
26+ SIMPLECPP_EXE = './simplecpp'
27+
1528
1629commands = []
1730
@@ -78,6 +91,21 @@ def cleanup(out):
7891 'pr57580.c' ,
7992 ]
8093
94+
95+ def run (compiler_executable : str , compiler_args : list [str ]) -> tuple [int , str , str ]:
96+ """Execute a compiler command and capture its exit code, stdout, and stderr."""
97+ compiler_cmd = [compiler_executable ]
98+ compiler_cmd .extend (compiler_args )
99+
100+ with subprocess .Popen (compiler_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True , encoding = "utf-8" ) as process :
101+ stdout , stderr = process .communicate ()
102+ exit_code = process .returncode
103+
104+ output = cleanup (stdout )
105+ error = (stderr or "" ).strip ()
106+ return (exit_code , output , error )
107+
108+
81109numberOfSkipped = 0
82110numberOfFailed = 0
83111numberOfFixed = 0
@@ -89,26 +117,12 @@ def cleanup(out):
89117 numberOfSkipped = numberOfSkipped + 1
90118 continue
91119
92- clang_cmd = ['clang' ]
93- clang_cmd .extend (cmd .split (' ' ))
94- p = subprocess .Popen (clang_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
95- comm = p .communicate ()
96- clang_output = cleanup (comm [0 ])
120+ _ , clang_output , _ = run (CLANG_EXE , cmd .split (' ' ))
97121
98- gcc_cmd = ['gcc' ]
99- gcc_cmd .extend (cmd .split (' ' ))
100- p = subprocess .Popen (gcc_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
101- comm = p .communicate ()
102- gcc_output = cleanup (comm [0 ])
122+ _ , gcc_output , _ = run (GCC_EXE , cmd .split (' ' ))
103123
104- simplecpp_cmd = ['./simplecpp' ]
105124 # -E is not supported and we bail out on unknown options
106- simplecpp_cmd .extend (cmd .replace ('-E ' , '' , 1 ).split (' ' ))
107- p = subprocess .Popen (simplecpp_cmd , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
108- comm = p .communicate ()
109- simplecpp_ec = p .returncode
110- simplecpp_output = cleanup (comm [0 ])
111- simplecpp_err = comm [0 ].decode ('utf-8' ).strip ()
125+ simplecpp_ec , simplecpp_output , simplecpp_err = run (SIMPLECPP_EXE , cmd .replace ('-E ' , '' , 1 ).split (' ' ))
112126
113127 if simplecpp_output != clang_output and simplecpp_output != gcc_output :
114128 filename = cmd [cmd .rfind ('/' )+ 1 :]
0 commit comments