aboutsummaryrefslogtreecommitdiff
path: root/gcc/testlib/elna-dg.exp
blob: ddebf58851214148a492859ee2666848dd895eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#   Copyright (C) 2004-2025 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCC; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.

load_lib gcc-dg.exp

# Define elna callbacks for dg.exp.

proc elna-dg-test { prog do_what extra_tool_flags } {
    set result \
    [gcc-dg-test-1 elna_target_compile $prog $do_what $extra_tool_flags]

    set comp_output [lindex $result 0]
    set output_file [lindex $result 1]

    return [list $comp_output $output_file]
}

proc elna-dg-prune { system text } {
    return [gcc-dg-prune $system $text]
}

# Utility routines.

#
# This procedure copies a test file from the source tree to the
# build directory while rewriting elna-native directives to dejagnu
# dg-* equivalents.
#
# Directives (inside (* ... *) comments):
#
#   (* @Error message *)      Expected compiler error on this line.
#   (* @Flags args *)         Extra compiler flags.
#
# base - absolute path to the source directory, e.g. .../testsuite/elna.dg
# test - relative path from testsuite, e.g. fail_compilation/file_name.elna
#
proc elna-convert-test { base test } {
    set type [file dirname $test]
    set target $test

    # Opens a file in the build directory for writing and the source
    # file for reading.
    file mkdir $type
    set fin [open $base/$target r]
    set fout [open $target w]

    # Reads the source test file line by line and replace directives with
    # Elna equivalents using a regular expression.
    while { [gets $fin line] >= 0 } {
        regsub -all {\(\*\s*@Error\s+(.*?)\s*\*\)} $line {(* { dg-error "\1" } *)} line
        regsub -all {\(\*\s*@Flags\s+(.*?)\s*\*\)} $line {(* { dg-additional-options "\1" } *)} line
        puts $fout $line
    }

    close $fin

    # Suppress excess output so stray diagnostics do not fail the test.
    puts $fout "(* { dg-prune-output .* } *)"

    # Emit category-specific expectation.
    switch $type {
        compilable {
            # Assert that an output file was produced.
            puts $fout "(* { dg-final { output-exists } } *)"
        }
        fail_compilation {
            # Assert that no output file was produced.
            puts $fout "(* { dg-final { output-exists-not } } *)"
        }
        # Other cases are handled automatically.
    }

    close $fout

    # Reaches up one stack frame (into elna-dg-runtest) and links the
    # caller's cleanup_extra_files variable to the local name cleanups.
    upvar 1 cleanup_extra_files cleanups
    # This ensures the converted file gets deleted after the test runs.
    lappend cleanups $target

    return $target
}

#
# Main loop for convert each test to dejagnu.
#
proc elna-dg-runtest { testcases } {
    global dg-do-what-default
    # Saves dejagnu's current "what to do" setting so different test
    # types don't leak settings between tests. 
    set saved-dg-do-what-default ${dg-do-what-default}

    foreach test $testcases {
        set type [file tail [file dirname $test]]
        set name [file tail $test]
        set base [file dirname [file dirname $test]]

        set cleanup_extra_files ""
        # Calls the converter, $target receives the relative path of
        # the converted file.
        set target "[elna-convert-test $base $type/$name]"

        switch $type {
            runnable {
                set dg-do-what-default "run"
            }
            compilable -
            fail_compilation {
                set dg-do-what-default "compile"
            }
        }

        dg-test $target "" ""

        foreach srcfile $cleanup_extra_files {
            file delete $srcfile
        }
        file delete $target
    }

    set dg-do-what-default ${saved-dg-do-what-default}
}