1# Copyright (C) 2005-2021 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with GCC; see the file COPYING3.  If not see
15# <http://www.gnu.org/licenses/>.
16
17# Run GCC with the input file also specified as output file. Check that the
18# compiler prints an error message and does not overwrite the input file.
19
20load_lib gcc-defs.exp
21load_lib target-supports.exp
22
23# These tests don't run runtest_file_p consistently if it
24# doesn't return the same values, so disable parallelization
25# of this *.exp file.  The first parallel runtest to reach
26# this will run all the tests serially.
27if ![gcc_parallel_test_run_p output] {
28    return
29}
30
31# I'm not sure if this is needed here. It was in options.exp.
32gcc_parallel_test_enable 0
33
34proc check_gcc_overwrite_input {} {
35    set filename test-[pid]
36    set fd [open $filename.c w]
37    puts $fd "int main (void) \{ return 0; \}"
38    close $fd
39    remote_download host $filename.c
40    set test "input overwrite test"
41    set compiler cc1
42    set gcc_output [gcc_target_compile $filename.c $filename.c executable ""]
43
44    # Is this right, or do I need to use something like remote_upload?
45    set fd [open $filename.c r]
46    set file_data [read $fd]
47    close $fd
48    remote_file build delete $filename.c
49
50    # check if the contents of the input file has changed
51    if {!($file_data eq "int main (void) \{ return 0; \}\n")} {
52	fail "$test (input overwritten)"
53	return
54    }
55
56    # check if the error message was printed
57    if {![regexp -- "same as output" $gcc_output]} {
58	fail "$test (no error printed)"
59	return
60    }
61    pass $test
62}
63
64check_gcc_overwrite_input
65
66gcc_parallel_test_enable 1
67