Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
2.33k
#define BEFORE_STRING ("\n")
54
731
#define AFTER_STRING ("\0")  /* memcpy of 0 chars might choke.  */
55
5.86k
#define BEFORE_SIZE (1)
56
1.47k
#define AFTER_SIZE  (1)
57
58
#ifndef TC_EOL_IN_INSN
59
715
#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
/* 1-origin line number in a source file.  */
103
/* A line ends in '\n' or eof.  */
104
static unsigned int physical_input_line;
105
static unsigned int logical_input_line;
106
107
/* Indicator whether the origin of an update was a .linefile directive. */
108
static bool is_linefile;
109
110
/* Struct used to save the state of the input handler during include files */
111
struct input_save {
112
  char *              buffer_start;
113
  char *              partial_where;
114
  size_t              partial_size;
115
  char                save_source[AFTER_SIZE];
116
  size_t              buffer_length;
117
  const char *        physical_input_file;
118
  const char *        logical_input_file;
119
  unsigned int        physical_input_line;
120
  unsigned int        logical_input_line;
121
  bool                is_linefile;
122
  size_t              sb_index;
123
  sb                  from_sb;
124
  enum expansion      from_sb_expansion; /* Should we do a conditional check?  */
125
  struct input_save * next_saved_file;  /* Chain of input_saves.  */
126
  char *              input_file_save;  /* Saved state of input routines.  */
127
  char *              saved_position; /* Caller's saved position in buf.  */
128
};
129
130
static struct input_save *input_scrub_push (char *saved_position);
131
static char *input_scrub_pop (struct input_save *arg);
132
133
/* Saved information about the file that .include'd this one.  When we hit EOF,
134
   we automatically pop to that file.  */
135
136
static struct input_save *next_saved_file;
137
138
/* Initialize input buffering.  */
139
140
static void
141
input_scrub_reinit (void)
142
2.33k
{
143
2.33k
  input_file_begin ();    /* Reinitialize! */
144
2.33k
  logical_input_line = -1u;
145
2.33k
  logical_input_file = NULL;
146
2.33k
  sb_index = -1;
147
148
2.33k
  buffer_length = input_file_buffer_size () * 2;
149
2.33k
  buffer_start = XNEWVEC (char, BEFORE_SIZE + AFTER_SIZE + 1 + buffer_length);
150
2.33k
  memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
151
2.33k
}
152
153
/* Finish off old buffers.  */
154
155
static void
156
input_scrub_free (void)
157
2.33k
{
158
2.33k
  if (sb_index != (size_t) -1)
159
1.66k
    {
160
1.66k
      sb_kill (&from_sb);
161
1.66k
      sb_index = -1;
162
1.66k
    }
163
2.33k
  free (buffer_start);
164
2.33k
  buffer_start = NULL;
165
2.33k
  input_file_end ();
166
2.33k
}
167
168
/* Push the state of input reading and scrubbing so that we can #include.
169
   The return value is a 'void *' (fudged for old compilers) to a save
170
   area, which can be restored by passing it to input_scrub_pop().  */
171
172
static struct input_save *
173
input_scrub_push (char *saved_position)
174
1.77k
{
175
1.77k
  struct input_save *saved;
176
177
1.77k
  saved = XNEW (struct input_save);
178
179
1.77k
  saved->saved_position = saved_position;
180
1.77k
  saved->buffer_start = buffer_start;
181
1.77k
  saved->partial_where = partial_where;
182
1.77k
  saved->partial_size = partial_size;
183
1.77k
  saved->buffer_length = buffer_length;
184
1.77k
  saved->physical_input_file = physical_input_file;
185
1.77k
  saved->logical_input_file = logical_input_file;
186
1.77k
  saved->physical_input_line = physical_input_line;
187
1.77k
  saved->logical_input_line = logical_input_line;
188
1.77k
  saved->is_linefile = is_linefile;
189
1.77k
  saved->sb_index = sb_index;
190
1.77k
  saved->from_sb = from_sb;
191
1.77k
  saved->from_sb_expansion = from_sb_expansion;
192
1.77k
  memcpy (saved->save_source, save_source, sizeof (save_source));
193
1.77k
  saved->next_saved_file = next_saved_file;
194
1.77k
  saved->input_file_save = input_file_push ();
195
196
1.77k
  input_scrub_reinit ();
197
198
1.77k
  return saved;
199
1.77k
}
200
201
static char *
202
input_scrub_pop (struct input_save *saved)
203
1.77k
{
204
1.77k
  char *saved_position;
205
206
1.77k
  input_scrub_free ();
207
208
1.77k
  input_file_pop (saved->input_file_save);
209
1.77k
  saved_position = saved->saved_position;
210
1.77k
  buffer_start = saved->buffer_start;
211
1.77k
  buffer_length = saved->buffer_length;
212
213
  /* When expanding an #APP / #NO_APP block, original lines are re-
214
     processed, so whatever they did to physical file/line needs
215
     retaining.  If logical file/line weren't changed, the logical
216
     line number will want bumping by a corresponding value.  */
217
1.77k
  if (from_sb_expansion != expanding_app)
218
1.55k
    {
219
1.55k
      if (logical_input_file == 0 && logical_input_line == -1u
220
165
    && saved->logical_input_line != -1u)
221
42
  saved->logical_input_line
222
42
    += physical_input_line - saved->physical_input_line;
223
1.55k
      physical_input_file = saved->physical_input_file;
224
1.55k
      physical_input_line = saved->physical_input_line;
225
1.55k
    }
226
1.77k
  logical_input_file = saved->logical_input_file;
227
1.77k
  logical_input_line = saved->logical_input_line;
228
229
1.77k
  is_linefile = saved->is_linefile;
230
1.77k
  sb_index = saved->sb_index;
231
1.77k
  from_sb = saved->from_sb;
232
1.77k
  from_sb_expansion = saved->from_sb_expansion;
233
1.77k
  partial_where = saved->partial_where;
234
1.77k
  partial_size = saved->partial_size;
235
1.77k
  next_saved_file = saved->next_saved_file;
236
1.77k
  memcpy (save_source, saved->save_source, sizeof (save_source));
237
238
1.77k
  free (saved);
239
1.77k
  return saved_position;
240
1.77k
}
241

242
void
243
input_scrub_begin (void)
244
567
{
245
567
  know (strlen (BEFORE_STRING) == BEFORE_SIZE);
246
567
  know (strlen (AFTER_STRING) == AFTER_SIZE
247
567
  || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
248
249
567
  physical_input_file = NULL; /* No file read yet.  */
250
567
  next_saved_file = NULL; /* At EOF, don't pop to any other file */
251
567
  macro_nest = 0;
252
567
  input_scrub_reinit ();
253
567
  do_scrub_begin (flag_m68k_mri);
254
567
}
255
256
void
257
input_scrub_end (void)
258
567
{
259
569
  while (next_saved_file != NULL)
260
2
    input_scrub_pop (next_saved_file);
261
567
  input_scrub_free ();
262
567
}
263
264
/* Start reading input from a new file.
265
   Return start of caller's part of buffer.  */
266
267
char *
268
input_scrub_new_file (const char *filename)
269
668
{
270
668
  input_file_open (filename, !flag_no_comments);
271
668
  physical_input_file = filename[0] ? filename : _("{standard input}");
272
668
  physical_input_line = 0;
273
274
668
  partial_size = 0;
275
668
  return (buffer_start + BEFORE_SIZE);
276
668
}
277
278
/* Include a file from the current file.  Save our state, cause it to
279
   be restored on EOF, and begin handling a new file.  Same result as
280
   input_scrub_new_file.  */
281
282
char *
283
input_scrub_include_file (const char *filename, char *position)
284
101
{
285
101
  next_saved_file = input_scrub_push (position);
286
101
  from_sb_expansion = expanding_none;
287
101
  return input_scrub_new_file (filename);
288
101
}
289
290
/* Start getting input from an sb structure.  This is used when
291
   expanding a macro.  */
292
293
void
294
input_scrub_include_sb (sb *from, char *position, enum expansion expansion)
295
1.66k
{
296
1.66k
  int newline;
297
298
1.66k
  if (expansion != expanding_app)
299
1.45k
    {
300
1.45k
      if (macro_nest > max_macro_nest)
301
0
  as_fatal (_("macros nested too deeply"));
302
1.45k
      ++macro_nest;
303
1.45k
    }
304
305
#ifdef md_macro_start
306
  if (expansion == expanding_macro)
307
    {
308
      md_macro_start ();
309
    }
310
#endif
311
312
1.66k
  next_saved_file = input_scrub_push (position);
313
314
  /* Allocate sufficient space: from->len plus optional newline
315
     plus two ".linefile " directives, plus a little more for other
316
     expansion.  */
317
1.66k
  newline = from->len >= 1 && from->ptr[0] != '\n';
318
1.66k
  sb_build (&from_sb, from->len + newline + 2 * sizeof (".linefile") + 30);
319
1.66k
  from_sb_expansion = expansion;
320
1.66k
  if (newline)
321
1.30k
    {
322
      /* Add the sentinel required by read.c.  */
323
1.30k
      sb_add_char (&from_sb, '\n');
324
1.30k
    }
325
1.66k
  sb_scrub_and_add_sb (&from_sb, from);
326
327
  /* Make sure the parser looks at defined contents when it scans for
328
     e.g. end-of-line at the end of a macro.  */
329
1.66k
  sb_terminate (&from_sb);
330
331
1.66k
  sb_index = 1;
332
333
  /* These variables are reset by input_scrub_push.  Restore them
334
     since we are, after all, still at the same point in the file.  */
335
1.66k
  logical_input_line = next_saved_file->logical_input_line;
336
1.66k
  logical_input_file = next_saved_file->logical_input_file;
337
1.66k
}
338
339
void
340
input_scrub_close (void)
341
567
{
342
567
  input_file_close ();
343
567
  physical_input_line = 0;
344
567
  logical_input_line = -1u;
345
567
}
346
347
char *
348
input_scrub_next_buffer (char **bufp)
349
4.37k
{
350
4.37k
  char *limit;    /*->just after last char of buffer.  */
351
352
4.37k
  if (sb_index != (size_t) -1)
353
2.97k
    {
354
2.97k
      if (sb_index >= from_sb.len)
355
1.66k
  {
356
1.66k
    if (from_sb_expansion == expanding_macro)
357
211
      {
358
211
        cond_finish_check (macro_nest);
359
#ifdef md_macro_end
360
        /* Allow the target to clean up per-macro expansion
361
           data.  */
362
        md_macro_end ();
363
#endif
364
211
      }
365
1.66k
    if (from_sb_expansion != expanding_app)
366
1.45k
      --macro_nest;
367
1.66k
    partial_where = NULL;
368
1.66k
    partial_size = 0;
369
1.66k
    if (next_saved_file != NULL)
370
1.66k
      *bufp = input_scrub_pop (next_saved_file);
371
1.66k
    return partial_where;
372
1.66k
  }
373
374
1.30k
      partial_where = from_sb.ptr + from_sb.len;
375
1.30k
      partial_size = 0;
376
1.30k
      *bufp = from_sb.ptr + sb_index;
377
1.30k
      sb_index = from_sb.len;
378
1.30k
      return partial_where;
379
2.97k
    }
380
381
1.40k
  if (partial_size)
382
16
    {
383
16
      memmove (buffer_start + BEFORE_SIZE, partial_where, partial_size);
384
16
      memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
385
16
    }
386
387
1.40k
  while (1)
388
1.40k
    {
389
1.40k
      char *p;
390
1.40k
      char *start = buffer_start + BEFORE_SIZE + partial_size;
391
392
1.40k
      *bufp = buffer_start + BEFORE_SIZE;
393
1.40k
      limit = input_file_give_next_buffer (start);
394
1.40k
      if (!limit)
395
689
  {
396
689
    if (!partial_size)
397
      /* End of this file.  */
398
673
      break;
399
400
16
    as_warn (_("end of file not at end of a line; newline inserted"));
401
16
    p = buffer_start + BEFORE_SIZE + partial_size;
402
16
    *p++ = '\n';
403
16
    limit = p;
404
16
  }
405
715
      else
406
715
  {
407
    /* Terminate the buffer to avoid confusing TC_EOL_IN_INSN.  */
408
715
    *limit = '\0';
409
410
    /* Find last newline.  */
411
7.09k
    for (p = limit - 1; *p != '\n' || TC_EOL_IN_INSN (p); --p)
412
6.38k
      if (p < start)
413
0
        goto read_more;
414
715
    ++p;
415
715
  }
416
417
      /* We found a newline in the newly read chars.  */
418
731
      partial_where = p;
419
731
      partial_size = limit - p;
420
421
      /* Save the fragment after that last newline.  */
422
731
      memcpy (save_source, partial_where, AFTER_SIZE);
423
731
      memcpy (partial_where, AFTER_STRING, AFTER_SIZE);
424
731
      return partial_where;
425
426
0
    read_more:
427
      /* Didn't find a newline.  Read more text.  */
428
0
      partial_size = limit - (buffer_start + BEFORE_SIZE);
429
0
      if (buffer_length - input_file_buffer_size () < partial_size)
430
0
  {
431
    /* Increase the buffer when it doesn't have room for the
432
       next block of input.  */
433
0
    buffer_length *= 2;
434
0
    buffer_start = XRESIZEVEC (char, buffer_start,
435
0
             (buffer_length
436
0
              + BEFORE_SIZE + AFTER_SIZE + 1));
437
0
  }
438
0
    }
439
440
  /* Tell the listing we've finished the file.  */
441
673
  LISTING_EOF ();
442
443
  /* If we should pop to another file at EOF, do it.  */
444
673
  partial_where = NULL;
445
673
  if (next_saved_file)
446
101
    *bufp = input_scrub_pop (next_saved_file);
447
448
673
  return partial_where;
449
1.40k
}
450

451
/* The remaining part of this file deals with line numbers, error
452
   messages and so on.  Return TRUE if we opened any file.  */
453
454
int
455
seen_at_least_1_file (void)
456
0
{
457
0
  return (physical_input_file != NULL);
458
0
}
459
460
void
461
bump_line_counters (void)
462
1.30M
{
463
1.30M
  if (sb_index == (size_t) -1 || from_sb_expansion == expanding_app)
464
40.5k
    ++physical_input_line;
465
466
1.30M
  if (logical_input_line != -1u)
467
1.29M
    ++logical_input_line;
468
1.30M
}
469

470
/* Tells us what the new logical line number and file are.
471
   If the line_number is -1, we don't change the current logical line
472
   number.
473
   If fname is NULL, we don't change the current logical file name, unless
474
   bit 3 of flags is set.
475
   Returns nonzero if the filename actually changes.  */
476
477
void
478
new_logical_line_flags (const char *fname, /* DON'T destroy it!  We point to it!  */
479
      int line_number,
480
      int flags)
481
52.7k
{
482
52.7k
  switch (flags)
483
52.7k
    {
484
52.5k
    case 0:
485
52.5k
      break;
486
150
    case 1:
487
150
      if (line_number != -1)
488
0
  abort ();
489
150
      break;
490
150
    case 1 << 1:
491
8
    case 1 << 2:
492
      /* FIXME: we could check that include nesting is correct.  */
493
8
      break;
494
22
    case 1 << 3:
495
      /* s_linefile conditionally decrements the line depending on
496
   whether '\n' is seen.  */
497
22
      if (line_number < 0
498
22
    || (line_number == 0 && input_line_pointer[-1] != '\n'))
499
0
  line_number = physical_input_line;
500
22
      if (fname != NULL)
501
0
  abort ();
502
22
      if (next_saved_file == NULL)
503
0
  fname = physical_input_file;
504
22
      else if (next_saved_file->logical_input_file)
505
0
  fname = next_saved_file->logical_input_file;
506
22
      else
507
22
  fname = next_saved_file->physical_input_file;
508
22
      break;
509
0
    default:
510
0
      abort ();
511
52.7k
    }
512
513
52.7k
  is_linefile = flags != 1 && (flags != 0 || fname);
514
515
52.7k
  if (line_number >= 0)
516
52.6k
    logical_input_line = line_number;
517
154
  else if (line_number == -1 && fname && !*fname && (flags & (1 << 2)))
518
0
    {
519
0
      logical_input_file = physical_input_file;
520
0
      logical_input_line = physical_input_line;
521
0
      fname = NULL;
522
0
    }
523
524
52.7k
  if (fname
525
52.7k
      && (logical_input_file == NULL
526
52.2k
    || filename_cmp (logical_input_file, fname)))
527
2.76k
    logical_input_file = fname;
528
52.7k
}
529
530
void
531
new_logical_line (const char *fname, int line_number)
532
22
{
533
22
  new_logical_line_flags (fname, line_number, 0);
534
22
}
535
536
void
537
as_report_context (void)
538
795k
{
539
795k
  const struct input_save *saved;
540
795k
  enum expansion expansion = from_sb_expansion;
541
795k
  int indent = 1;
542
543
795k
  if (!macro_nest)
544
57.3k
    return;
545
546
1.64M
  for (saved = next_saved_file; saved; saved = saved->next_saved_file)
547
907k
    {
548
907k
      if (expansion != expanding_macro)
549
838k
  /* Nothing.  */;
550
69.4k
      else if (saved->logical_input_file != NULL
551
134
         && saved->logical_input_line != -1u)
552
134
  as_info_where (saved->logical_input_file, saved->logical_input_line,
553
134
           indent, _("macro invoked from here"));
554
69.2k
      else
555
69.2k
  as_info_where (saved->physical_input_file, saved->physical_input_line,
556
69.2k
           indent, _("macro invoked from here"));
557
558
907k
      expansion = saved->from_sb_expansion;
559
907k
      ++indent;
560
907k
    }
561
738k
}
562

563
/* Return the current physical input file name and line number, if known  */
564
565
const char *
566
as_where_physical (unsigned int *linep)
567
44.3k
{
568
44.3k
  if (physical_input_file != NULL)
569
44.3k
    {
570
44.3k
      if (linep != NULL)
571
44.3k
  *linep = physical_input_line;
572
44.3k
      return physical_input_file;
573
44.3k
    }
574
575
0
  if (linep != NULL)
576
0
    *linep = 0;
577
0
  return NULL;
578
44.3k
}
579
580
/* Return the file name and line number at the top most macro
581
   invocation, unless .file / .line were used inside a macro.  */
582
583
const char *
584
as_where (unsigned int *linep)
585
75.3k
{
586
75.3k
  const char *file = as_where_top (linep);
587
588
75.3k
  if (macro_nest && is_linefile)
589
63.5k
    {
590
63.5k
      const struct input_save *saved;
591
63.5k
      enum expansion expansion = from_sb_expansion;
592
593
129k
      for (saved = next_saved_file; saved; saved = saved->next_saved_file)
594
65.5k
  {
595
65.5k
    if (expansion != expanding_macro)
596
65.5k
      /* Nothing.  */;
597
17
    else if (saved->logical_input_file != NULL
598
13
       && (linep == NULL || saved->logical_input_line != -1u))
599
13
      {
600
13
        if (linep != NULL)
601
13
    *linep = saved->logical_input_line;
602
13
        file = saved->logical_input_file;
603
13
      }
604
4
    else if (saved->physical_input_file != NULL)
605
4
      {
606
4
        if (linep != NULL)
607
4
    *linep = saved->physical_input_line;
608
4
        file = saved->physical_input_file;
609
4
      }
610
611
65.5k
    expansion = saved->from_sb_expansion;
612
65.5k
  }
613
63.5k
    }
614
615
75.3k
  return file;
616
75.3k
}
617
618
/* Return the current file name and line number.  */
619
620
const char *
621
as_where_top (unsigned int *linep)
622
874k
{
623
874k
  if (logical_input_file != NULL
624
831k
      && (linep == NULL || logical_input_line != -1u))
625
830k
    {
626
830k
      if (linep != NULL)
627
830k
  *linep = logical_input_line;
628
830k
      return logical_input_file;
629
830k
    }
630
631
44.3k
  return as_where_physical (linep);
632
874k
}