Add translations for various sub-directories
[binutils-gdb.git] / gdb / testsuite / gdb.dwarf2 / macro-source-path.exp
blobfccfabdebb19f7a5f669f6ed3dc91936827fc2a2
1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2022-2024 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 # Generate binaries imitating different ways source file paths can be passed to
19 # compilers.  Test printing macros from those binaries.
21 load_lib dwarf.exp
23 require dwarf2_support
25 standard_testfile .c
27 lassign [function_range main $srcdir/$subdir/$srcfile] \
28     main_start main_len
30 # Run one test.
32 #  - TEST_NAME is the name of the test, used to differentiate the binaries.
33 #  - LINES_VERSION is the version of the version of the .debug_line section to
34 #    generate.
35 #  - DW_AT_NAME is the string to put in the compilation unit's DW_AT_name
36 #    attribute.
37 #  - MAIN_FILE_IDX is the file index the .debug_line and .debug_macro sections
38 #    will use to refer to the main file.
39 #  - DIRECTORIES is a list of directories to put in the .debug_line section
40 #    header
41 #  - FILE_NAMES is a list of {name, directory index} pairs describing the files
42 #    names to put in the .debug_line section header.
44 proc do_test { test_name lines_version DW_AT_name main_file_idx directories
45                file_names } {
46     with_test_prefix "test_name=$test_name" {
47         foreach_with_prefix is_64 {true false} {
48             # So we can access them in Dwarf::assemble...
49             set ::lines_version $lines_version
50             set ::DW_AT_name $DW_AT_name
51             set ::main_file_idx $main_file_idx
52             set ::directories $directories
53             set ::file_names $file_names
54             set ::is_64 $is_64
55             set 32_or_64 [expr $is_64 ? 64 : 32]
57             set asm_file [standard_output_file ${::testfile}-${test_name}-${32_or_64}.S]
58             Dwarf::assemble $asm_file {
59                 declare_labels Llines cu_macros
61                 # DW_AT_comp_dir is always the current working directory
62                 # from which the compiler was invoked.  We pretend the compiler was
63                 # always launched from /tmp/cwd.
64                 set comp_dir "/tmp/cwd"
66                 cu {} {
67                     DW_TAG_compile_unit {
68                             {DW_AT_producer "My C Compiler"}
69                             {DW_AT_language @DW_LANG_C11}
70                             {DW_AT_name $::DW_AT_name}
71                             {DW_AT_comp_dir $comp_dir}
72                             {DW_AT_stmt_list $Llines DW_FORM_sec_offset}
73                             {DW_AT_macros $cu_macros DW_FORM_sec_offset}
74                     } {
75                         declare_labels int_type
77                         int_type: DW_TAG_base_type {
78                             {DW_AT_byte_size 4 DW_FORM_sdata}
79                             {DW_AT_encoding  @DW_ATE_signed}
80                             {DW_AT_name int}
81                         }
83                         DW_TAG_subprogram {
84                             {MACRO_AT_func {main}}
85                             {type :$int_type}
86                         }
87                     }
88                 }
90                 # Define the .debug_line section.
91                 lines [list version $::lines_version] "Llines" {
92                     foreach directory $::directories {
93                         include_dir $directory
94                     }
96                     foreach file_name $::file_names {
97                         lassign $file_name name dir_index
98                         file_name $name $dir_index
99                     }
101                     # A line number program just good enough so that GDB can
102                     # figure out we are stopped in main.
103                     program {
104                         DW_LNS_set_file $::main_file_idx
105                         DW_LNE_set_address $::main_start
106                         line 10
107                         DW_LNS_copy
109                         DW_LNE_set_address "$::main_start + $::main_len"
110                         DW_LNE_end_sequence
111                     }
112                 }
114                 # Define the .debug_macro section.
115                 macro {
116                     cu_macros: unit {
117                         "debug-line-offset-label" $Llines
118                         "is-64" $::is_64
119                     } {
120                         # A macro defined outside the main file, as if it was defined
121                         # on the command line with -D.
122                         #
123                         # Clang has this bug where it puts the macros defined on
124                         # the command-line after the main file portion (see
125                         # PR 29034).  We're not trying to replicate that here,
126                         # this is not in the scope of this test.
127                         define 0 "ONE 1"
128                         define_strp 0 "THREE 3"
129                         start_file 0 $::main_file_idx
130                             # A macro defined at line 1 of the main file.
131                             define 1 "TWO 2"
132                         end_file
133                     }
134                 }
135             }
137             if { [prepare_for_testing "failed to prepare" ${::testfile}-${test_name}-${32_or_64} \
138                       [list $::srcfile $asm_file] {nodebug}] } {
139                 return
140             }
142             with_complaints 5 {
143                 gdb_test_multiple "print main" "no complaints" {
144                     -wrap -re "During symbol reading: .*" {
145                         fail $gdb_test_name
146                     }
147                     -wrap -re "" {
148                         pass $gdb_test_name
149                     }
150                 }
151             }
153             if ![runto_main] {
154                 return
155             }
157             gdb_test "print ONE" " = 1"
158             gdb_test "print TWO" " = 2"
159             gdb_test "print THREE" " = 3"
160         }
161     }
164 # When adding a test here, please consider adding an equivalent case to the test
165 # of the same name in gdb.base.
167 # The following tests are based on the output of `gcc -gdwarf-5 -g3 <file>`,
168 # gcc 11 paired with gas from binutils 2.38.
170 ## test.c
171 do_test gcc11-ld238-dw5-filename 5 "test.c" 1 {
172     "/tmp/cwd"
173 } {
174     {"test.c" 0}
175     {"test.c" 0}
178 ## ./test.c
179 do_test gcc11-ld238-dw5-dot-filename 5 "./test.c" 1 {
180     "/tmp/cwd"
181     "."
182 } {
183     {"test.c" 1}
184     {"test.c" 1}
187 ## ../cwd/test.c
188 do_test gcc11-ld238-dw5-dot-dot-cwd 5 "../cwd/test.c" 1 {
189     "/tmp/cwd"
190     "../cwd"
191 } {
192     {"test.c" 1}
193     {"test.c" 1}
196 ## /tmp/cwd/test.c
197 do_test gcc11-ld238-dw5-absolute-cwd 5 "/tmp/cwd/test.c" 1 {
198     "/tmp/cwd"
199     "/tmp/cwd"
200 } {
201     {"test.c" 1}
202     {"test.c" 0}
205 ## ../other/test.c
206 do_test gcc11-ld238-dw5-dot-dot-other 5 "../other/test.c" 1 {
207     "/tmp/cwd"
208     "../other"
209 } {
210     {"test.c" 1}
211     {"test.c" 1}
214 ## /tmp/other/test.c
215 do_test gcc11-ld238-dw5-absolute-other 5 "/tmp/other/test.c" 1 {
216     "/tmp/cwd"
217     "/tmp/other"
218 } {
219     {"test.c" 1}
220     {"test.c" 1}
223 # The following tests are based on the output of `gcc -gdwarf-4 -g3 <file>`,
224 # gcc 11 paired with gas from binutils 2.38.  With -gdwarf-4, gcc generates a
225 # v4 (pre-standard) .debug_macro section.
227 ## test.c
228 do_test gcc11-ld238-dw4-filename 4 "test.c" 1 {
229 } {
230     {"test.c" 0}
233 ## ./test.c
234 do_test gcc11-ld238-dw4-dot-filename 4 "./test.c" 1 {
235     "."
236 } {
237     {"test.c" 1}
240 ## ../cwd/test.c
241 do_test gcc11-ld238-dw4-dot-dot-cwd 4 "../cwd/test.c" 1 {
242     "../cwd"
243 } {
244     {"test.c" 1}
247 ## /tmp/cwd/test.c
248 do_test gcc11-ld238-dw4-absolute-cwd 4 "/tmp/cwd/test.c" 1 {
249     "/tmp/cwd"
250 } {
251     {"test.c" 1}
254 ## ../other/test.c
255 do_test gcc11-ld238-dw4-dot-dot-other 4 "../other/test.c" 1 {
256     "../other"
257 } {
258     {"test.c" 1}
261 ## /tmp/other/test.c
262 do_test gcc11-ld238-dw4-absolute-other 4 "/tmp/other/test.c" 1 {
263     "/tmp/other"
264 } {
265     {"test.c" 1}
268 # The following tests are based on the output of `clang-14 -gdwarf-5
269 # -fdebug-macro -g3 <file>` (using its built-in assembler)
271 ## test.c
272 do_test clang14-dw5-filename 5 "test.c" 0 {
273     "/tmp/cwd"
274 } {
275     {"test.c" 0}
278 ## ./test.c
279 do_test clang14-dw5-dot-filename 5 "test.c" 1 {
280     "/tmp/cwd"
281     "."
282 } {
283     {"test.c" 0}
284     {"test.c" 1}
287 ## ../cwd/test.c
288 do_test clang14-dw5-dot-dot-cwd 5 "../cwd/test.c" 0 {
289     "/tmp/cwd"
290 } {
291     {"../cwd/test.c" 0}
294 ## /tmp/cwd/test.c
295 do_test clang14-dw5-absolute-cwd  5 "/tmp/cwd/test.c" 1 {
296     "/tmp/cwd"
297 } {
298     {"/tmp/cwd/test.c" 0}
299     {"test.c" 0}
302 ## ../other/test.c
303 do_test clang14-dw5-dot-dot-other 5 "../other/test.c" 0 {
304     "/tmp/cwd"
305 } {
306     {"../other/test.c" 0}
309 ## /tmp/other/test.c
310 do_test clang14-dw5-absolute-other 5 "/tmp/other/test.c" 1 {
311     "/tmp/cwd"
312     "/tmp"
313 } {
314     {"/tmp/other/test.c" 0}
315     {"other/test.c" 1}
318 # The following tests are based on the output of `clang-14 -gdwarf-4
319 # -fdebug-macro -g3 <file>` (using its built-in assembler).  With -gdwarf-4,
320 # clang produces a .debug_macinfo section, not a .debug_macro section.  But
321 # this test still creates a .debug_macro section, that's good enough for what
322 # we want to test.
324 ## test.c
325 do_test clang14-dw4-filename 4 "test.c" 1 {
326 } {
327     {"test.c" 0}
330 ## ./test.c
331 do_test clang14-dw4-dot-filename 4 "test.c" 1 {
332     "."
333 } {
334     {"test.c" 1}
337 ## ../cwd/test.c
338 do_test clang14-dw4-dot-dot-cwd 4 "../cwd/test.c" 1 {
339     "../cwd"
340 } {
341     {"test.c" 1}
344 ## /tmp/cwd/test.c
345 do_test clang14-dw4-absolute-cwd  4 "/tmp/cwd/test.c" 1 {
346 } {
347     {"test.c" 0}
350 ## ../other/test.c
351 do_test clang14-dw4-dot-dot-other 4 "../other/test.c" 1 {
352     "../other"
353 } {
354     {"test.c" 1}
357 ## /tmp/other/test.c
358 do_test clang14-dw4-absolute-other 4 "/tmp/other/test.c" 1 {
359     "/tmp"
360 } {
361     {"other/test.c" 1}
364 # The following tests are based on the output of `gcc -gdwarf-5 -g3 <file>`,
365 # gcc 11 paired with gas from binutils 2.34 (Ubuntu 20.04).  It generates a v5
366 # .debug_macro section, but a v3 .debug_line section.
368 ## test.c
369 do_test gcc11-ld234-dw5-filename 3 "test.c" 1 {
370 } {
371     {"test.c" 0}
374 ## ./test.c
375 do_test gcc11-ld234-dw5-dot-filename 3 "./test.c" 1 {
376     "."
377 } {
378     {"test.c" 1}
381 ## ../cwd/test.c
382 do_test gcc11-ld234-dw5-dot-dot-cwd 3 "../cwd/test.c" 1 {
383     "../cwd"
384 } {
385     {"test.c" 1}
388 ## /tmp/cwd/test.c
389 do_test gcc11-ld234-dw5-absolute-cwd 3 "/tmp/cwd/test.c" 1 {
390     "/tmp/cwd"
391 } {
392     {"test.c" 1}
395 ## ../other/test.c
396 do_test gcc11-ld234-dw5-dot-dot-other 3 "../other/test.c" 1 {
397     "../other"
398 } {
399     {"test.c" 1}
402 ## /tmp/other/test.c
403 do_test gcc11-ld234-dw5-absolute-other 3 "/tmp/other/test.c" 1 {
404     "/tmp/other"
405 } {
406     {"test.c" 1}