/src/binutils-gdb/gas/input-scrub.c
Line | Count | Source |
1 | | /* input_scrub.c - Break up input buffers into whole numbers of lines. |
2 | | Copyright (C) 1987-2026 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of GAS, the GNU Assembler. |
5 | | |
6 | | GAS is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 3, or (at your option) |
9 | | any later version. |
10 | | |
11 | | GAS is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with GAS; see the file COPYING. If not, write to the Free |
18 | | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | | 02110-1301, USA. */ |
20 | | |
21 | | #include "as.h" |
22 | | #include "filenames.h" |
23 | | #include "input-file.h" |
24 | | #include "sb.h" |
25 | | #include "listing.h" |
26 | | |
27 | | /* |
28 | | * O/S independent module to supply buffers of sanitised source code |
29 | | * to rest of assembler. We get sanitised input data of arbitrary length. |
30 | | * We break these buffers on line boundaries, recombine pieces that |
31 | | * were broken across buffers, and return a buffer of full lines to |
32 | | * the caller. |
33 | | * The last partial line begins the next buffer we build and return to caller. |
34 | | * The buffer returned to caller is preceded by BEFORE_STRING and followed |
35 | | * by AFTER_STRING, as sentinels. The last character before AFTER_STRING |
36 | | * is a newline. |
37 | | * Also looks after line numbers, for e.g. error messages. |
38 | | */ |
39 | | |
40 | | /* |
41 | | * We don't care how filthy our buffers are, but our callers assume |
42 | | * that the following sanitation has already been done. |
43 | | * |
44 | | * No comments, reduce a comment to a space. |
45 | | * Reduce a tab to a space unless it is 1st char of line. |
46 | | * All multiple tabs and spaces collapsed into 1 char. Tab only |
47 | | * legal if 1st char of line. |
48 | | * # line file statements converted to .line x;.file y; statements. |
49 | | * Escaped newlines at end of line: remove them but add as many newlines |
50 | | * to end of statement as you removed in the middle, to synch line numbers. |
51 | | */ |
52 | | |
53 | 3.63k | #define BEFORE_STRING ("\n") |
54 | 421 | #define AFTER_STRING ("\0") /* memcpy of 0 chars might choke. */ |
55 | 5.95k | #define BEFORE_SIZE (1) |
56 | 873 | #define AFTER_SIZE (1) |
57 | | |
58 | | #ifndef TC_EOL_IN_INSN |
59 | 409 | #define TC_EOL_IN_INSN(P) 0 |
60 | | #endif |
61 | | |
62 | | static char *buffer_start; /*->1st char of full buffer area. */ |
63 | | static char *partial_where; /*->after last full line in buffer. */ |
64 | | static size_t partial_size; /* >=0. Number of chars in partial line in buffer. */ |
65 | | |
66 | | /* Because we need AFTER_STRING just after last full line, it clobbers |
67 | | 1st part of partial line. So we preserve 1st part of partial line |
68 | | here. */ |
69 | | static char save_source[AFTER_SIZE]; |
70 | | |
71 | | /* The size of the input buffer we concatenate |
72 | | input_file_give_next_buffer chunks into. Excludes the BEFORE and |
73 | | AFTER counts. */ |
74 | | static size_t buffer_length; |
75 | | |
76 | | /* The index into an sb structure we are reading from. -1 if none. */ |
77 | | static size_t sb_index = -1; |
78 | | |
79 | | /* If we are reading from an sb structure, this is it. */ |
80 | | static sb from_sb; |
81 | | |
82 | | /* Should we do a conditional check on from_sb? */ |
83 | | static enum expansion from_sb_expansion = expanding_none; |
84 | | |
85 | | /* The number of nested sb structures we have included. */ |
86 | | int macro_nest; |
87 | | |
88 | | /* We can have more than one source file open at once, though the info for all |
89 | | but the latest one are saved off in a struct input_save. These files remain |
90 | | open, so we are limited by the number of open files allowed by the |
91 | | underlying OS. We may also sequentially read more than one source file in an |
92 | | assembly. */ |
93 | | |
94 | | /* We must track the physical file and line number for error messages. We also |
95 | | track a "logical" file and line number corresponding to (C?) compiler |
96 | | source line numbers. Whenever we open a file we must fill in |
97 | | physical_input_file. So if it is NULL we have not opened any files yet. */ |
98 | | |
99 | | static const char *physical_input_file; |
100 | | static const char *logical_input_file; |
101 | | |
102 | | static const char *primary_input_file; |
103 | | static const char *primary_logical_file; |
104 | | |
105 | | /* 1-origin line number in a source file. */ |
106 | | /* A line ends in '\n' or eof. */ |
107 | | static unsigned int physical_input_line; |
108 | | static unsigned int logical_input_line; |
109 | | |
110 | | /* Indicator whether the origin of an update was a .linefile directive. */ |
111 | | static bool is_linefile; |
112 | | |
113 | | /* Struct used to save the state of the input handler during include files */ |
114 | | struct input_save { |
115 | | char * buffer_start; |
116 | | char * partial_where; |
117 | | size_t partial_size; |
118 | | char save_source[AFTER_SIZE]; |
119 | | size_t buffer_length; |
120 | | const char * physical_input_file; |
121 | | const char * logical_input_file; |
122 | | unsigned int physical_input_line; |
123 | | unsigned int logical_input_line; |
124 | | bool is_linefile; |
125 | | size_t sb_index; |
126 | | sb from_sb; |
127 | | enum expansion from_sb_expansion; /* Should we do a conditional check? */ |
128 | | struct input_save * next_saved_file; /* Chain of input_saves. */ |
129 | | char * input_file_save; /* Saved state of input routines. */ |
130 | | char * saved_position; /* Caller's saved position in buf. */ |
131 | | }; |
132 | | |
133 | | static struct input_save *input_scrub_push (char *saved_position); |
134 | | static char *input_scrub_pop (struct input_save *arg); |
135 | | |
136 | | /* Saved information about the file that .include'd this one. When we hit EOF, |
137 | | we automatically pop to that file. */ |
138 | | |
139 | | static struct input_save *next_saved_file; |
140 | | |
141 | | /* Initialize input buffering. */ |
142 | | |
143 | | static void |
144 | | input_scrub_reinit (void) |
145 | 3.63k | { |
146 | 3.63k | input_file_begin (); /* Reinitialize! */ |
147 | 3.63k | logical_input_line = -1u; |
148 | 3.63k | logical_input_file = NULL; |
149 | 3.63k | sb_index = -1; |
150 | | |
151 | 3.63k | buffer_length = input_file_buffer_size () * 2; |
152 | 3.63k | buffer_start = XNEWVEC (char, BEFORE_SIZE + AFTER_SIZE + 1 + buffer_length); |
153 | 3.63k | memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE); |
154 | 3.63k | } |
155 | | |
156 | | /* Finish off old buffers. */ |
157 | | |
158 | | static void |
159 | | input_scrub_free (void) |
160 | 3.63k | { |
161 | 3.63k | if (sb_index != (size_t) -1) |
162 | 3.16k | { |
163 | 3.16k | sb_kill (&from_sb); |
164 | 3.16k | sb_index = -1; |
165 | 3.16k | } |
166 | 3.63k | free (buffer_start); |
167 | 3.63k | buffer_start = NULL; |
168 | 3.63k | input_file_end (); |
169 | 3.63k | } |
170 | | |
171 | | /* Push the state of input reading and scrubbing so that we can #include. |
172 | | The return value is a 'void *' (fudged for old compilers) to a save |
173 | | area, which can be restored by passing it to input_scrub_pop(). */ |
174 | | |
175 | | static struct input_save * |
176 | | input_scrub_push (char *saved_position) |
177 | 3.26k | { |
178 | 3.26k | struct input_save *saved; |
179 | | |
180 | 3.26k | saved = XNEW (struct input_save); |
181 | | |
182 | 3.26k | saved->saved_position = saved_position; |
183 | 3.26k | saved->buffer_start = buffer_start; |
184 | 3.26k | saved->partial_where = partial_where; |
185 | 3.26k | saved->partial_size = partial_size; |
186 | 3.26k | saved->buffer_length = buffer_length; |
187 | 3.26k | saved->physical_input_file = physical_input_file; |
188 | 3.26k | saved->logical_input_file = logical_input_file; |
189 | 3.26k | saved->physical_input_line = physical_input_line; |
190 | 3.26k | saved->logical_input_line = logical_input_line; |
191 | 3.26k | saved->is_linefile = is_linefile; |
192 | 3.26k | saved->sb_index = sb_index; |
193 | 3.26k | saved->from_sb = from_sb; |
194 | 3.26k | saved->from_sb_expansion = from_sb_expansion; |
195 | 3.26k | memcpy (saved->save_source, save_source, sizeof (save_source)); |
196 | 3.26k | saved->next_saved_file = next_saved_file; |
197 | 3.26k | saved->input_file_save = input_file_push (); |
198 | | |
199 | 3.26k | input_scrub_reinit (); |
200 | | |
201 | 3.26k | return saved; |
202 | 3.26k | } |
203 | | |
204 | | static char * |
205 | | input_scrub_pop (struct input_save *saved) |
206 | 3.26k | { |
207 | 3.26k | char *saved_position; |
208 | | |
209 | 3.26k | input_scrub_free (); |
210 | | |
211 | 3.26k | input_file_pop (saved->input_file_save); |
212 | 3.26k | saved_position = saved->saved_position; |
213 | 3.26k | buffer_start = saved->buffer_start; |
214 | 3.26k | buffer_length = saved->buffer_length; |
215 | | |
216 | | /* When expanding an #APP / #NO_APP block, original lines are re- |
217 | | processed, so whatever they did to physical file/line needs |
218 | | retaining. If logical file/line weren't changed, the logical |
219 | | line number will want bumping by a corresponding value. */ |
220 | 3.26k | if (from_sb_expansion != expanding_app) |
221 | 3.25k | { |
222 | 3.25k | if (logical_input_file == 0 && logical_input_line == -1u |
223 | 150 | && saved->logical_input_line != -1u) |
224 | 35 | saved->logical_input_line |
225 | 35 | += physical_input_line - saved->physical_input_line; |
226 | 3.25k | physical_input_file = saved->physical_input_file; |
227 | 3.25k | physical_input_line = saved->physical_input_line; |
228 | 3.25k | } |
229 | 3.26k | logical_input_file = saved->logical_input_file; |
230 | 3.26k | logical_input_line = saved->logical_input_line; |
231 | | |
232 | 3.26k | is_linefile = saved->is_linefile; |
233 | 3.26k | sb_index = saved->sb_index; |
234 | 3.26k | from_sb = saved->from_sb; |
235 | 3.26k | from_sb_expansion = saved->from_sb_expansion; |
236 | 3.26k | partial_where = saved->partial_where; |
237 | 3.26k | partial_size = saved->partial_size; |
238 | 3.26k | next_saved_file = saved->next_saved_file; |
239 | 3.26k | memcpy (save_source, saved->save_source, sizeof (save_source)); |
240 | | |
241 | 3.26k | free (saved); |
242 | 3.26k | return saved_position; |
243 | 3.26k | } |
244 | | |
245 | | void |
246 | | input_scrub_begin (void) |
247 | 368 | { |
248 | 368 | know (strlen (BEFORE_STRING) == BEFORE_SIZE); |
249 | 368 | know (strlen (AFTER_STRING) == AFTER_SIZE |
250 | 368 | || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1)); |
251 | | |
252 | 368 | physical_input_file = NULL; /* No file read yet. */ |
253 | 368 | primary_input_file = NULL; |
254 | 368 | primary_logical_file = NULL; |
255 | 368 | next_saved_file = NULL; /* At EOF, don't pop to any other file */ |
256 | 368 | macro_nest = 0; |
257 | 368 | input_scrub_reinit (); |
258 | 368 | do_scrub_begin (flag_m68k_mri); |
259 | 368 | } |
260 | | |
261 | | void |
262 | | input_scrub_end (void) |
263 | 368 | { |
264 | 369 | while (next_saved_file != NULL) |
265 | 1 | input_scrub_pop (next_saved_file); |
266 | 368 | input_scrub_free (); |
267 | 368 | } |
268 | | |
269 | | /* Start reading input from a new file. |
270 | | Return start of caller's part of buffer. */ |
271 | | |
272 | | char * |
273 | | input_scrub_new_file (const char *filename) |
274 | 468 | { |
275 | 468 | input_file_open (filename, !flag_no_comments); |
276 | 468 | physical_input_file = filename[0] ? filename : _("{standard input}"); |
277 | 468 | if (primary_input_file == NULL) |
278 | 368 | primary_input_file = physical_input_file; |
279 | 468 | physical_input_line = 0; |
280 | | |
281 | 468 | partial_size = 0; |
282 | 468 | return (buffer_start + BEFORE_SIZE); |
283 | 468 | } |
284 | | |
285 | | /* Include a file from the current file. Save our state, cause it to |
286 | | be restored on EOF, and begin handling a new file. Same result as |
287 | | input_scrub_new_file. */ |
288 | | |
289 | | char * |
290 | | input_scrub_include_file (const char *filename, char *position) |
291 | 100 | { |
292 | 100 | next_saved_file = input_scrub_push (position); |
293 | 100 | from_sb_expansion = expanding_none; |
294 | 100 | return input_scrub_new_file (filename); |
295 | 100 | } |
296 | | |
297 | | /* Start getting input from an sb structure. This is used when |
298 | | expanding a macro. */ |
299 | | |
300 | | void |
301 | | input_scrub_include_sb (sb *from, char *position, enum expansion expansion) |
302 | 3.16k | { |
303 | 3.16k | int newline; |
304 | | |
305 | 3.16k | if (expansion != expanding_app) |
306 | 3.15k | { |
307 | 3.15k | if (macro_nest > max_macro_nest) |
308 | 0 | as_fatal (_("macros nested too deeply")); |
309 | 3.15k | ++macro_nest; |
310 | 3.15k | } |
311 | | |
312 | | #ifdef md_macro_start |
313 | | if (expansion == expanding_macro) |
314 | | { |
315 | | md_macro_start (); |
316 | | } |
317 | | #endif |
318 | | |
319 | 3.16k | next_saved_file = input_scrub_push (position); |
320 | | |
321 | | /* Allocate sufficient space: from->len plus optional newline |
322 | | plus two ".linefile " directives, plus a little more for other |
323 | | expansion. */ |
324 | 3.16k | newline = from->len >= 1 && from->ptr[0] != '\n'; |
325 | 3.16k | sb_build (&from_sb, from->len + newline + 2 * sizeof (".linefile") + 30); |
326 | 3.16k | from_sb_expansion = expansion; |
327 | 3.16k | if (newline) |
328 | 2.58k | { |
329 | | /* Add the sentinel required by read.c. */ |
330 | 2.58k | sb_add_char (&from_sb, '\n'); |
331 | 2.58k | } |
332 | 3.16k | sb_scrub_and_add_sb (&from_sb, from); |
333 | | |
334 | | /* Make sure the parser looks at defined contents when it scans for |
335 | | e.g. end-of-line at the end of a macro. */ |
336 | 3.16k | sb_terminate (&from_sb); |
337 | | |
338 | 3.16k | sb_index = 1; |
339 | | |
340 | | /* These variables are reset by input_scrub_push. Restore them |
341 | | since we are, after all, still at the same point in the file. */ |
342 | 3.16k | logical_input_line = next_saved_file->logical_input_line; |
343 | 3.16k | logical_input_file = next_saved_file->logical_input_file; |
344 | 3.16k | } |
345 | | |
346 | | void |
347 | | input_scrub_close (void) |
348 | 368 | { |
349 | 368 | input_file_close (); |
350 | 368 | physical_input_line = 0; |
351 | 368 | logical_input_line = -1u; |
352 | 368 | } |
353 | | |
354 | | char * |
355 | | input_scrub_next_buffer (char **bufp) |
356 | 6.64k | { |
357 | 6.64k | char *limit; /*->just after last char of buffer. */ |
358 | | |
359 | 6.64k | if (sb_index != (size_t) -1) |
360 | 5.75k | { |
361 | 5.75k | if (sb_index >= from_sb.len) |
362 | 3.16k | { |
363 | 3.16k | if (from_sb_expansion == expanding_macro) |
364 | 2.14k | { |
365 | 2.14k | cond_finish_check (macro_nest); |
366 | | #ifdef md_macro_end |
367 | | /* Allow the target to clean up per-macro expansion |
368 | | data. */ |
369 | | md_macro_end (); |
370 | | #endif |
371 | 2.14k | } |
372 | 3.16k | if (from_sb_expansion != expanding_app) |
373 | 3.15k | --macro_nest; |
374 | 3.16k | partial_where = NULL; |
375 | 3.16k | partial_size = 0; |
376 | 3.16k | if (next_saved_file != NULL) |
377 | 3.16k | *bufp = input_scrub_pop (next_saved_file); |
378 | 3.16k | return partial_where; |
379 | 3.16k | } |
380 | | |
381 | 2.58k | partial_where = from_sb.ptr + from_sb.len; |
382 | 2.58k | partial_size = 0; |
383 | 2.58k | *bufp = from_sb.ptr + sb_index; |
384 | 2.58k | sb_index = from_sb.len; |
385 | 2.58k | return partial_where; |
386 | 5.75k | } |
387 | | |
388 | 891 | if (partial_size) |
389 | 31 | { |
390 | 31 | memmove (buffer_start + BEFORE_SIZE, partial_where, partial_size); |
391 | 31 | memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE); |
392 | 31 | } |
393 | | |
394 | 891 | while (1) |
395 | 891 | { |
396 | 891 | char *p; |
397 | 891 | char *start = buffer_start + BEFORE_SIZE + partial_size; |
398 | | |
399 | 891 | *bufp = buffer_start + BEFORE_SIZE; |
400 | 891 | limit = input_file_give_next_buffer (start); |
401 | 891 | if (!limit) |
402 | 482 | { |
403 | 482 | if (!partial_size) |
404 | | /* End of this file. */ |
405 | 470 | break; |
406 | | |
407 | 12 | as_warn (_("end of file not at end of a line; newline inserted")); |
408 | 12 | p = buffer_start + BEFORE_SIZE + partial_size; |
409 | 12 | *p++ = '\n'; |
410 | 12 | limit = p; |
411 | 12 | } |
412 | 409 | else |
413 | 409 | { |
414 | | /* Terminate the buffer to avoid confusing TC_EOL_IN_INSN. */ |
415 | 409 | *limit = '\0'; |
416 | | |
417 | | /* Find last newline. */ |
418 | 2.38k | for (p = limit - 1; *p != '\n' || TC_EOL_IN_INSN (p); --p) |
419 | 1.97k | if (p < start) |
420 | 0 | goto read_more; |
421 | 409 | ++p; |
422 | 409 | } |
423 | | |
424 | | /* We found a newline in the newly read chars. */ |
425 | 421 | partial_where = p; |
426 | 421 | partial_size = limit - p; |
427 | | |
428 | | /* Save the fragment after that last newline. */ |
429 | 421 | memcpy (save_source, partial_where, AFTER_SIZE); |
430 | 421 | memcpy (partial_where, AFTER_STRING, AFTER_SIZE); |
431 | 421 | return partial_where; |
432 | | |
433 | 0 | read_more: |
434 | | /* Didn't find a newline. Read more text. */ |
435 | 0 | partial_size = limit - (buffer_start + BEFORE_SIZE); |
436 | 0 | if (buffer_length - input_file_buffer_size () < partial_size) |
437 | 0 | { |
438 | | /* Increase the buffer when it doesn't have room for the |
439 | | next block of input. */ |
440 | 0 | buffer_length *= 2; |
441 | 0 | buffer_start = XRESIZEVEC (char, buffer_start, |
442 | 0 | (buffer_length |
443 | 0 | + BEFORE_SIZE + AFTER_SIZE + 1)); |
444 | 0 | } |
445 | 0 | } |
446 | | |
447 | | /* Tell the listing we've finished the file. */ |
448 | 470 | LISTING_EOF (); |
449 | | |
450 | | /* If we should pop to another file at EOF, do it. */ |
451 | 470 | partial_where = NULL; |
452 | 470 | if (next_saved_file) |
453 | 100 | *bufp = input_scrub_pop (next_saved_file); |
454 | | |
455 | 470 | return partial_where; |
456 | 891 | } |
457 | | |
458 | | /* The remaining part of this file deals with line numbers, error |
459 | | messages and so on. Return TRUE if we opened any file. */ |
460 | | |
461 | | int |
462 | | seen_at_least_1_file (void) |
463 | 0 | { |
464 | 0 | return (physical_input_file != NULL); |
465 | 0 | } |
466 | | |
467 | | void |
468 | | bump_line_counters (void) |
469 | 393k | { |
470 | 393k | if (sb_index == (size_t) -1 || from_sb_expansion == expanding_app) |
471 | 8.27k | ++physical_input_line; |
472 | | |
473 | 393k | if (logical_input_line != -1u) |
474 | 385k | ++logical_input_line; |
475 | 393k | } |
476 | | |
477 | | /* Tells us what the new logical line number and file are. |
478 | | If the line_number is -1, we don't change the current logical line |
479 | | number. |
480 | | If fname is NULL, we don't change the current logical file name, unless |
481 | | bit 3 of flags is set. |
482 | | Returns nonzero if the filename actually changes. */ |
483 | | |
484 | | void |
485 | | new_logical_line_flags (const char *fname, /* DON'T destroy it! We point to it! */ |
486 | | int line_number, |
487 | | int flags) |
488 | 56.9k | { |
489 | 56.9k | switch (flags) |
490 | 56.9k | { |
491 | 56.9k | case 0: |
492 | 56.9k | break; |
493 | 63 | case 1: |
494 | 63 | if (line_number != -1) |
495 | 0 | abort (); |
496 | 63 | break; |
497 | 63 | case 1 << 1: |
498 | 0 | case 1 << 2: |
499 | | /* FIXME: we could check that include nesting is correct. */ |
500 | 0 | break; |
501 | 1 | case 1 << 3: |
502 | | /* s_linefile conditionally decrements the line depending on |
503 | | whether '\n' is seen. */ |
504 | 1 | if (line_number < 0 |
505 | 0 | || (line_number == 0 && input_line_pointer[-1] != '\n')) |
506 | 1 | line_number = physical_input_line; |
507 | 1 | if (fname != NULL) |
508 | 0 | abort (); |
509 | 1 | if (next_saved_file == NULL) |
510 | 1 | fname = physical_input_file; |
511 | 0 | else if (next_saved_file->logical_input_file) |
512 | 0 | fname = next_saved_file->logical_input_file; |
513 | 0 | else |
514 | 0 | fname = next_saved_file->physical_input_file; |
515 | 1 | break; |
516 | 0 | default: |
517 | 0 | abort (); |
518 | 56.9k | } |
519 | | |
520 | 56.9k | is_linefile = flags != 1 && (flags != 0 || fname); |
521 | | |
522 | 56.9k | if (line_number >= 0) |
523 | 56.8k | logical_input_line = line_number; |
524 | 71 | else if (line_number == -1 && fname && !*fname && (flags & (1 << 2))) |
525 | 0 | { |
526 | 0 | logical_input_file = physical_input_file; |
527 | 0 | logical_input_line = physical_input_line; |
528 | 0 | fname = NULL; |
529 | 0 | } |
530 | | |
531 | 56.9k | if (fname) |
532 | 56.9k | { |
533 | 56.9k | if (logical_input_file == NULL |
534 | 56.7k | || filename_cmp (logical_input_file, fname)) |
535 | 600 | logical_input_file = fname; |
536 | | |
537 | | /* The first top-level CPP line marker names the primary source. */ |
538 | 56.9k | if (primary_logical_file == NULL && *fname |
539 | 53.5k | && next_saved_file == NULL |
540 | 70 | && physical_input_file == primary_input_file |
541 | 70 | && flags == 0) |
542 | 11 | primary_logical_file = fname; |
543 | 56.9k | } |
544 | 56.9k | } |
545 | | |
546 | | void |
547 | | new_logical_line (const char *fname, int line_number) |
548 | 8 | { |
549 | 8 | new_logical_line_flags (fname, line_number, 0); |
550 | 8 | } |
551 | | |
552 | | void |
553 | | as_report_context (void) |
554 | 764k | { |
555 | 764k | const struct input_save *saved; |
556 | 764k | enum expansion expansion = from_sb_expansion; |
557 | 764k | int indent = 1; |
558 | | |
559 | 764k | if (!macro_nest) |
560 | 236k | return; |
561 | | |
562 | 1.28M | for (saved = next_saved_file; saved; saved = saved->next_saved_file) |
563 | 752k | { |
564 | 752k | if (expansion != expanding_macro) |
565 | 737k | /* Nothing. */; |
566 | 14.7k | else if (saved->logical_input_file != NULL |
567 | 4.06k | && saved->logical_input_line != -1u) |
568 | 4.06k | as_info_where (saved->logical_input_file, saved->logical_input_line, |
569 | 4.06k | indent, _("macro invoked from here")); |
570 | 10.6k | else |
571 | 10.6k | as_info_where (saved->physical_input_file, saved->physical_input_line, |
572 | 10.6k | indent, _("macro invoked from here")); |
573 | | |
574 | 752k | expansion = saved->from_sb_expansion; |
575 | 752k | ++indent; |
576 | 752k | } |
577 | 527k | } |
578 | | |
579 | | /* Return the primary source file name, if known. */ |
580 | | |
581 | | const char * |
582 | | as_where_primary (void) |
583 | 0 | { |
584 | 0 | return primary_logical_file ? primary_logical_file : primary_input_file; |
585 | 0 | } |
586 | | |
587 | | /* Return the current physical input file name and line number, if known */ |
588 | | |
589 | | const char * |
590 | | as_where_physical (unsigned int *linep) |
591 | 247k | { |
592 | 247k | if (physical_input_file != NULL) |
593 | 247k | { |
594 | 247k | if (linep != NULL) |
595 | 247k | *linep = physical_input_line; |
596 | 247k | return physical_input_file; |
597 | 247k | } |
598 | | |
599 | 0 | if (linep != NULL) |
600 | 0 | *linep = 0; |
601 | 0 | return NULL; |
602 | 247k | } |
603 | | |
604 | | /* Return the file name and line number at the top most macro |
605 | | invocation, unless .file / .line were used inside a macro. */ |
606 | | |
607 | | const char * |
608 | | as_where (unsigned int *linep) |
609 | 84.5k | { |
610 | 84.5k | const char *file = as_where_top (linep); |
611 | | |
612 | 84.5k | if (macro_nest && is_linefile) |
613 | 74.1k | { |
614 | 74.1k | const struct input_save *saved; |
615 | 74.1k | enum expansion expansion = from_sb_expansion; |
616 | | |
617 | 161k | for (saved = next_saved_file; saved; saved = saved->next_saved_file) |
618 | 87.3k | { |
619 | 87.3k | if (expansion != expanding_macro) |
620 | 80.3k | /* Nothing. */; |
621 | 6.97k | else if (saved->logical_input_file != NULL |
622 | 979 | && (linep == NULL || saved->logical_input_line != -1u)) |
623 | 979 | { |
624 | 979 | if (linep != NULL) |
625 | 979 | *linep = saved->logical_input_line; |
626 | 979 | file = saved->logical_input_file; |
627 | 979 | } |
628 | 5.99k | else if (saved->physical_input_file != NULL) |
629 | 5.99k | { |
630 | 5.99k | if (linep != NULL) |
631 | 5.99k | *linep = saved->physical_input_line; |
632 | 5.99k | file = saved->physical_input_file; |
633 | 5.99k | } |
634 | | |
635 | 87.3k | expansion = saved->from_sb_expansion; |
636 | 87.3k | } |
637 | 74.1k | } |
638 | | |
639 | 84.5k | return file; |
640 | 84.5k | } |
641 | | |
642 | | /* Return the current file name and line number. */ |
643 | | |
644 | | const char * |
645 | | as_where_top (unsigned int *linep) |
646 | 850k | { |
647 | 850k | if (logical_input_file != NULL |
648 | 605k | && (linep == NULL || logical_input_line != -1u)) |
649 | 603k | { |
650 | 603k | if (linep != NULL) |
651 | 603k | *linep = logical_input_line; |
652 | 603k | return logical_input_file; |
653 | 603k | } |
654 | | |
655 | 247k | return as_where_physical (linep); |
656 | 850k | } |