@@ -318,21 +318,20 @@ fn maybe_run_command_in_vm(
318318 Ok ( ( ) )
319319}
320320
321- fn no_builtins_tests ( env : & Env , args : & TestArg ) -> Result < ( ) , String > {
322- // Test that the #![no_builtins] attribute prevents GCC from replacing
323- // code patterns (like loops) with calls to builtins (like memset).
324- // See https://github.com/rust-lang/rustc_codegen_gcc/issues/570
325-
321+ /// Compile a source file to an object file and check if it contains a memset reference.
322+ fn object_has_memset (
323+ env : & Env ,
324+ args : & TestArg ,
325+ src_file : & str ,
326+ obj_file_name : & str ,
327+ ) -> Result < bool , String > {
326328 let cargo_target_dir = Path :: new ( & args. config_info . cargo_target_dir ) ;
327-
328- // Test 1: WITH #![no_builtins] - memset should NOT be present
329- println ! ( "[TEST] no_builtins attribute (with #![no_builtins])" ) ;
330- let obj_file = cargo_target_dir. join ( "no_builtins_test.o" ) ;
329+ let obj_file = cargo_target_dir. join ( obj_file_name) ;
331330 let obj_file_str = obj_file. to_str ( ) . expect ( "obj_file to_str" ) ;
332331
333332 let mut command = args. config_info . rustc_command_vec ( ) ;
334333 command. extend_from_slice ( & [
335- & "tests/no_builtins/no_builtins.rs" ,
334+ & src_file ,
336335 & "--emit" ,
337336 & "obj" ,
338337 & "-O" ,
@@ -346,47 +345,34 @@ fn no_builtins_tests(env: &Env, args: &TestArg) -> Result<(), String> {
346345 let nm_output = run_command_with_env ( & [ & "nm" , & obj_file_str] , None , Some ( env) ) ?;
347346 let nm_stdout = String :: from_utf8_lossy ( & nm_output. stdout ) ;
348347
349- if nm_stdout. contains ( "memset" ) {
350- return Err ( format ! (
351- "no_builtins test FAILED: Found 'memset' in object file.\n \
348+ Ok ( nm_stdout. contains ( "memset" ) )
349+ }
350+
351+ fn no_builtins_tests ( env : & Env , args : & TestArg ) -> Result < ( ) , String > {
352+ // Test that the #![no_builtins] attribute prevents GCC from replacing
353+ // code patterns (like loops) with calls to builtins (like memset).
354+ // See https://github.com/rust-lang/rustc_codegen_gcc/issues/570
355+
356+ // Test 1: WITH #![no_builtins] - memset should NOT be present
357+ println ! ( "[TEST] no_builtins attribute (with #![no_builtins])" ) ;
358+ let has_memset =
359+ object_has_memset ( env, args, "tests/no_builtins/no_builtins.rs" , "no_builtins_test.o" ) ?;
360+ if has_memset {
361+ return Err ( "no_builtins test FAILED: Found 'memset' in object file.\n \
352362 The #![no_builtins] attribute should prevent GCC from replacing \n \
353- code patterns with builtin calls.\n \
354- nm output:\n {}",
355- nm_stdout
356- ) ) ;
363+ code patterns with builtin calls."
364+ . to_string ( ) ) ;
357365 }
358- println ! ( "[TEST] no_builtins attribute (with #![no_builtins]): PASSED" ) ;
359366
360367 // Test 2: WITHOUT #![no_builtins] - memset SHOULD be present
361368 println ! ( "[TEST] no_builtins attribute (without #![no_builtins])" ) ;
362- let obj_file = cargo_target_dir. join ( "with_builtins_test.o" ) ;
363- let obj_file_str = obj_file. to_str ( ) . expect ( "obj_file to_str" ) ;
364-
365- let mut command = args. config_info . rustc_command_vec ( ) ;
366- command. extend_from_slice ( & [
367- & "tests/no_builtins/with_builtins.rs" ,
368- & "--emit" ,
369- & "obj" ,
370- & "-O" ,
371- & "--target" ,
372- & args. config_info . target_triple ,
373- & "-o" ,
374- ] ) ;
375- command. push ( & obj_file_str) ;
376- run_command_with_env ( & command, None , Some ( env) ) ?;
377-
378- let nm_output = run_command_with_env ( & [ & "nm" , & obj_file_str] , None , Some ( env) ) ?;
379- let nm_stdout = String :: from_utf8_lossy ( & nm_output. stdout ) ;
380-
381- if !nm_stdout. contains ( "memset" ) {
382- return Err ( format ! (
383- "no_builtins test FAILED: 'memset' NOT found in object file.\n \
384- Without #![no_builtins], GCC should replace the loop with memset.\n \
385- nm output:\n {}",
386- nm_stdout
387- ) ) ;
369+ let has_memset =
370+ object_has_memset ( env, args, "tests/no_builtins/with_builtins.rs" , "with_builtins_test.o" ) ?;
371+ if !has_memset {
372+ return Err ( "no_builtins test FAILED: 'memset' NOT found in object file.\n \
373+ Without #![no_builtins], GCC should replace the loop with memset."
374+ . to_string ( ) ) ;
388375 }
389- println ! ( "[TEST] no_builtins attribute (without #![no_builtins]): PASSED" ) ;
390376
391377 Ok ( ( ) )
392378}
0 commit comments