Debian Codegraph @

gcc/testsuite/libgdiagnostics.dg/libgdiagnostics.exp

raw
1 # Test code for libgdiagnostics.so
2 #
3 # We will compile each of libgdiagnostics.dg/test-*.{c,cc} into an executable
4 # dynamically linked against libgdiagnostics.so, and then run each
5 # such executable.
6 #
7 # These executables call into the libgdiagnostics.so API to emit diagnostics,
8 # sometimes in text form, and other times in SARIF form.
9
10 # Kludge alert:
11 # We need g++_init so that it can find the stdlib include path.
12 #
13 # g++_init (in lib/g++.exp) uses g++_maybe_build_wrapper,
14 # which normally comes from the definition of
15 # ${tool}_maybe_build_wrapper within lib/wrapper.exp.
16 #
17 # However, for us, ${tool} is "libgdiagnostics".
18 # Hence we load wrapper.exp with tool == "g++", so that
19 # g++_maybe_build_wrapper is defined.
20 set tool g++
21 load_lib wrapper.exp
22 set tool libgdiagnostics
23
24 load_lib dg.exp
25 load_lib prune.exp
26 load_lib target-supports.exp
27 load_lib gcc-defs.exp
28 load_lib timeout.exp
29 load_lib target-libpath.exp
30 load_lib gcc.exp
31 load_lib g++.exp
32 load_lib dejagnu.exp
33 load_lib target-supports-dg.exp
34 load_lib valgrind.exp
35 load_lib scansarif.exp
36 load_lib dg-test-cleanup.exp
37
38 # The default do-what keyword.
39 set dg-do-what-default compile
40
41 # Adapted from jit.exp.
42 #
43 # Execute the executable file.
44 # Returns:
45 # A "" (empty) string if everything worked, or an error message
46 # if there was a problem.
47 #
48 proc fixed_host_execute {args} {
49 global env
50 global text
51 global spawn_id
52
53 verbose "fixed_host_execute: $args"
54
55 set timeoutmsg "Timed out: Never got started, "
56 set timeout 100
57 set file all
58 set timetol 0
59 set arguments ""
60
61 if { [llength $args] == 0} {
62 set executable $args
63 } else {
64 set executable [lindex $args 0]
65 set params [lindex $args 1]
66 }
67
68 verbose "The executable is $executable" 2
69 if {![file exists ${executable}]} {
70 perror "The executable, \"$executable\" is missing" 0
71 return "No source file found"
72 } elseif {![file executable ${executable}]} {
73 perror "The executable, \"$executable\" is not usable" 0
74 return "Bad executable found"
75 }
76
77 verbose "params: $params" 2
78
79 # spawn the executable and look for the DejaGnu output messages from the
80 # test case.
81 # spawn -noecho -open [open "|./${executable}" "r"]
82
83 # Run under valgrind if RUN_UNDER_VALGRIND is present in the environment.
84 # Note that it's best to configure gcc with --enable-valgrind-annotations
85 # when testing under valgrind.
86 set run_under_valgrind [info exists env(RUN_UNDER_VALGRIND)]
87 if $run_under_valgrind {
88 set valgrind_logfile "${executable}.valgrind.txt"
89 set valgrind_params {"valgrind"}
90 lappend valgrind_params "--leak-check=full"
91 lappend valgrind_params "--log-file=${valgrind_logfile}"
92 } else {
93 set valgrind_params {}
94 }
95 verbose "valgrind_params: $valgrind_params" 2
96
97 set args ${valgrind_params}
98 lappend args "./${executable}"
99 set args [concat $args ${params}]
100 verbose "args: $args" 2
101
102 set status [catch "exec -keepnewline $args" exe_output]
103 verbose "Test program returned $exe_output" 2
104
105 if $run_under_valgrind {
106 upvar 2 name name
107 parse_valgrind_logfile $name $valgrind_logfile fail
108 }
109
110 # We don't do prune_gcc_output here, as we want
111 # to check *exactly* what we get from libgdiagnostics
112
113 return $exe_output
114 }
115
116 # (end of code from dejagnu.exp)
117
118 # GCC_UNDER_TEST is needed by gcc_target_compile
119 global GCC_UNDER_TEST
120 if ![info exists GCC_UNDER_TEST] {
121 set GCC_UNDER_TEST "[find_gcc]"
122 }
123
124 g++_init
125
126 # Initialize dg.
127 dg-init
128
129 # Gather a list of all tests.
130
131 # C and C++ tests within the testsuite: gcc/testsuite/libgdiagnostics.dg/test-*.{c,c++}
132 set c_tests [find $srcdir/$subdir test-*.c]
133 set cxx_tests [find $srcdir/$subdir test-*.cc]
134 set tests [concat $c_tests $cxx_tests]
135
136 verbose "tests: $tests"
137
138 # Expand "SRCDIR" within ARG to the location of the top-level
139 # src directory
140
141 proc diagnostics-expand-vars {arg} {
142 verbose "diagnostics-expand-vars: $arg"
143 global srcdir
144 verbose " srcdir: $srcdir"
145 # "srcdir" is that of the gcc/testsuite directory, so
146 # we need to go up two levels.
147 set arg [string map [list "SRCDIR" $srcdir/../..] $arg]
148 verbose " new arg: $arg"
149 return $arg
150 }
151
152 # Parameters used when invoking the executables built from the test cases.
153
154 global diagnostics-exe-params
155 set diagnostics-exe-params {}
156
157 # Set "diagnostics-exe-params", expanding "SRCDIR" in each arg to the location of
158 # the top-level srcdir.
159
160 proc dg-diagnostics-set-exe-params { args } {
161 verbose "dg-diagnostics-set-exe-params: $args"
162
163 global diagnostics-exe-params
164 set diagnostics-exe-params {}
165 # Skip initial arg (line number)
166 foreach arg [lrange $args 1 [llength $args] ] {
167 lappend diagnostics-exe-params [diagnostics-expand-vars $arg]
168 }
169 }
170
171 proc libgdiagnostics-dg-test { prog do_what extra_tool_flags } {
172 verbose "within libgdiagnostics-dg-test..."
173 verbose " prog: $prog"
174 verbose " do_what: $do_what"
175 verbose " extra_tool_flags: $extra_tool_flags"
176
177 global dg-do-what-default
178 set dg-do-what [list ${dg-do-what-default} "" P]
179
180 # If we're not supposed to try this test on this target, we're done.
181 if { [lindex ${dg-do-what} 1] == "N" } {
182 unsupported "$name"
183 verbose "$name not supported on this target, skipping it" 3
184 return
185 }
186
187 # Determine what to name the built executable.
188 #
189 # We simply append .exe to the filename, e.g.
190 # "test-foo.c.exe"
191 # since some testcases exist in both
192 # "test-foo.c" and
193 # "test-foo.cc"
194 # variants, and we don't want them to clobber each other's
195 # executables.
196 #
197 # This also ensures that the source name makes it into the
198 # pass/fail output, so that we can distinguish e.g. which test-foo
199 # is failing.
200 set output_file "[file tail $prog].exe"
201 verbose "output_file: $output_file"
202
203 # Create the test executable:
204 set extension [file extension $prog]
205 if {$extension == ".cc"} {
206 set compilation_function "g++_target_compile"
207 } else {
208 set compilation_function "gcc_target_compile"
209 }
210 set options "{additional_flags=$extra_tool_flags}"
211 verbose "compilation_function=$compilation_function"
212 verbose "options=$options"
213
214 set comp_output [$compilation_function $prog $output_file \
215 "executable" $options]
216 upvar 1 name name
217 if ![libgdiagnostics_check_compile "$name" "initial compilation" \
218 $output_file $comp_output] then {
219 return
220 }
221
222 # Run the test executable.
223
224 # We need to set LD_LIBRARY_PATH so that the test files can find
225 # libgdiagnostics.so
226 # Do this using set_ld_library_path_env_vars from target-libpath.exp
227 # We will restore the old value later using
228 # restore_ld_library_path_env_vars.
229
230 # Unfortunately this API only supports a single saved value, rather
231 # than a stack, and g++_init has already called into this API,
232 # injecting the appropriate value for LD_LIBRARY_PATH for finding
233 # the built copy of libstdc++.
234 # Hence the call to restore_ld_library_path_env_vars would restore
235 # the *initial* value of LD_LIBRARY_PATH, and attempts to run
236 # a C++ testcase after running any prior testcases would thus look
237 # in the wrong place for libstdc++. This led to failures at startup
238 # of the form:
239 # ./tut01-hello-world.cc.exe: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./tut01-hello-world.cc.exe)
240 # when the built libstdc++ is more recent that the system libstdc++.
241 #
242 # As a workaround, reset the variable "orig_environment_saved" within
243 # target-libpath.exp, so that the {set|restore}_ld_library_path_env_vars
244 # API saves/restores the current value of LD_LIBRARY_PATH (as set up
245 # by g++_init).
246 global orig_environment_saved
247 set orig_environment_saved 0
248
249 global ld_library_path
250 global base_dir
251 set ld_library_path "$base_dir/../../"
252 set_ld_library_path_env_vars
253
254 global diagnostics-exe-params
255 set args ${diagnostics-exe-params}
256 set diagnostics-exe-params {}
257
258 set exe_output [fixed_host_execute $output_file $args ]
259 verbose "exe_output: $exe_output"
260
261 restore_ld_library_path_env_vars
262
263 # Analyze the output from the executable. To some what extent this
264 # is duplicating prune_gcc_output, but we're looking for *precise*
265 # output, so we can't reuse prune_gcc_output.
266
267 global testname_with_flags
268 set testname_with_flags $name
269
270 # Handle any freeform regexps.
271 set exe_output [handle-dg-regexps $exe_output]
272
273 # Call into multiline.exp to handle any multiline output directives.
274 set exe_output [handle-multiline-outputs $exe_output]
275
276 # Normally we would return $exe_output and $output_file to the
277 # caller, which would delete $output_file, the generated executable.
278 # If we need to debug, it's handy to be able to suppress this behavior,
279 # keeping the executable around.
280
281 global env
282 set preserve_executables [info exists env(PRESERVE_EXECUTABLES)]
283 if $preserve_executables {
284 set output_file ""
285 }
286
287 return [list $exe_output $output_file]
288 }
289
290 set DEFAULT_CFLAGS "-I$srcdir/.. -lgdiagnostics -g -Wall -Werror"
291
292 # Main loop. This will invoke jig-dg-test on each test-*.c file.
293 dg-runtest $tests "" $DEFAULT_CFLAGS
294
295 # All done.
296 dg-finish