88from dataclasses import dataclass
99from pathlib import Path
1010from typing import NamedTuple , Optional
11+ import atexit
1112import difflib
1213import functools
1314import json
@@ -91,12 +92,12 @@ class TestContext:
9192 rust_result : Optional [RunResult ] = None
9293
9394 @classmethod
94- def setup (cls , test ):
95+ def setup (cls , test , model ):
9596 cc_input = Path (test .getFilePath ())
9697 is_multi = is_multi_file_test (cc_input )
97- model = test .getSourcePath ().split ("/" )[- 1 ]
9898 fname = cc_input .name if is_multi else cc_input .stem
99- tmp_dir = get_temp_dir () / f"{ fname } -{ model } "
99+ # Tests in different directories may share a file name.
100+ tmp_dir = get_temp_dir () / f"{ cc_input .parent .name } -{ fname } -{ model } "
100101
101102 shutil .rmtree (tmp_dir , ignore_errors = True )
102103 tmp_dir .mkdir (parents = True )
@@ -191,6 +192,8 @@ def check_expected(self):
191192
192193 def build_cpp (self ):
193194 exp = self .expectations
195+ if self .cpp_bin is not None :
196+ return None
194197 if self .build_dir is not None :
195198 cmd = ["cmake" , "--build" , str (self .build_dir )]
196199 _ , _ , rc = lit .util .executeCommand (cmd )
@@ -203,7 +206,6 @@ def build_cpp(self):
203206 self .cpp_bin = self .tmp_dir / "cpp"
204207 cmd = [
205208 cc ,
206- "-O3" ,
207209 "-o" ,
208210 str (self .cpp_bin ),
209211 str (self .cc_input ),
@@ -229,7 +231,9 @@ def build_rust(self):
229231 "-A" ,
230232 "warnings" ,
231233 "-C" ,
232- "opt-level=3" , # Equivalent to --release
234+ "opt-level=0" ,
235+ "-C" ,
236+ "debug-assertions=off" , # On by default at opt-level=0
233237 "-C" ,
234238 "strip=symbols" ,
235239 "-C" ,
@@ -255,9 +259,10 @@ def build_rust(self):
255259 def run_cpp (self ):
256260 if self .skip_run :
257261 return None
258- self .cpp_result = RunResult (
259- * lit .util .executeCommand (str (self .cpp_bin ), str (self .tmp_dir ))
260- )
262+ if self .cpp_result is None :
263+ self .cpp_result = RunResult (
264+ * lit .util .executeCommand (str (self .cpp_bin ), str (self .tmp_dir ))
265+ )
261266
262267 if (
263268 not self .expectations .should_panic_ub
@@ -325,50 +330,69 @@ def success_result(self):
325330 return (lit .Test .FAIL , "did not fail as expected" )
326331 return (lit .Test .PASS , "" )
327332
328- def finalize (self , result ):
333+ def finalize (self ):
329334 shutil .rmtree (self .tmp_dir , ignore_errors = True )
330- return result
331335
332336
333337class Cpp2RustTest (TestFormat ):
334338 def __init__ (self ):
335339 os .environ ["RUSTFLAGS" ] = "-Awarnings"
340+ # Runs once, in the main lit process. Each test removes its own dirs,
341+ # so this only removes the top-level dir.
342+ atexit .register (shutil .rmtree , get_temp_dir (), ignore_errors = True )
336343
337344 def execute (self , test , litConfig ):
338- ctx = TestContext .setup (test )
339- result = (
340- ctx .translate ()
341- or ctx .check_expected ()
342- or ctx .build_cpp ()
343- or ctx .build_rust ()
344- or ctx .run_cpp ()
345- or ctx .run_rust ()
346- or ctx .compare ()
347- or ctx .success_result ()
348- )
349- return ctx .finalize (result )
350-
351- def getTestsForPath (self , testSuite , path_in_suite , litConfig , localConfig ):
352- source_path = testSuite .getSourcePath (path_in_suite )
345+ contexts = []
346+ results = []
353347 for model in MODELS :
354- yield lit .Test .Test (
355- testSuite , path_in_suite + (model ,), localConfig , source_path
348+ ctx = TestContext .setup (test , model )
349+ if contexts :
350+ # Reuse the C++ binary and its output from the previous model.
351+ ctx .cpp_bin = contexts [- 1 ].cpp_bin
352+ ctx .cpp_result = contexts [- 1 ].cpp_result
353+ contexts .append (ctx )
354+ result = (
355+ ctx .translate ()
356+ or ctx .check_expected ()
357+ or ctx .build_cpp ()
358+ or ctx .build_rust ()
359+ or ctx .run_cpp ()
360+ or ctx .run_rust ()
361+ or ctx .compare ()
362+ or ctx .success_result ()
356363 )
364+ results .append ((model , result ))
365+ # The C++ binary lives in the first model's dir, so clean up at the end.
366+ for ctx in contexts :
367+ ctx .finalize ()
368+ return combine_results (results )
357369
358370 def getTestsInDirectory (self , testSuite , path_in_suite , litConfig , localConfig ):
359371 source_path = Path (testSuite .getSourcePath (path_in_suite ))
360372 if is_multi_file_test (source_path ):
361- for t in self .getTestsForPath (
362- testSuite , path_in_suite , litConfig , localConfig
363- ):
364- yield t
373+ yield lit .Test .Test (testSuite , path_in_suite , localConfig )
365374 return
366375 for entry in source_path .iterdir ():
367376 if entry .is_file () and entry .suffix in (".cpp" , ".c" ):
368- for t in self .getTestsForPath (
369- testSuite , path_in_suite + (entry .name ,), litConfig , localConfig
370- ):
371- yield t
377+ yield lit .Test .Test (
378+ testSuite , path_in_suite + (entry .name ,), localConfig
379+ )
380+
381+
382+ def combine_results (results ):
383+ """Reports the first model that failed; otherwise XFAIL if some model failed
384+ as expected, and PASS if none did."""
385+ codes = [code for _ , (code , _ ) in results ]
386+ code = next (
387+ (c for c in codes if c .isFailure ),
388+ lit .Test .XFAIL if lit .Test .XFAIL in codes else lit .Test .PASS ,
389+ )
390+ output = "" .join (
391+ f"{ model } : { c .name } \n { out } \n "
392+ for model , (c , out ) in results
393+ if c != lit .Test .PASS
394+ )
395+ return (code , output )
372396
373397
374398def read_rust_version ():
0 commit comments