Skip to content

Commit 4d74aab

Browse files
committed
PR29466, APP/NO_APP with .linefile
Commit 53f2b36 exposed a bug in sb_scrub_and_add_sb that could result in losing input. If scrubbing results in expansion past the holding capacity of do_scrub_chars output buffer, then do_scrub_chars stashes the extra input for the next call. That call never came because sb_scrub_and_add_sb wrongly decided it was done. Fix that by allowing sb_scrub_and_add_sb to see whether there is pending input. Also allow a little extra space so that in most cases we won't need to resize the output buffer. sb_scrub_and_add_sb also limited output to the size of the input, rather than the actual output buffer size. Fixing that resulted in a fail of gas/testsuite/macros/dot with an extra warning: "end of file not at end of a line; newline inserted". OK, so the macro in dot.s really does finish without end-of-line. Apparently the macro expansion code relied on do_scrub_chars returning early. So fix that too by adding a newline if needed in macro_expand_body. PR 29466 * app.c (do_scrub_pending): New function. * as.h: Declare it. * input-scrub.c (input_scrub_include_sb): Add extra space for two .linefile directives. * sb.c (sb_scrub_and_add_sb): Take into account pending input. Allow output to max. * macro.c (macro_expand_body): Add terminating newline. * testsuite/config/default.exp (SIZE, SIZEFLAGS): Define. * testsuite/gas/macros/app5.d, * testsuite/gas/macros/app5.s: New test. * testsuite/gas/macros/macros.exp: Run it.
1 parent 5291ecf commit 4d74aab

File tree

9 files changed

+43
-4
lines changed

9 files changed

+43
-4
lines changed

gas/app.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,3 +1537,16 @@ do_scrub_chars (size_t (*get) (char *, size_t), char *tostart, size_t tolen)
15371537
last_char = to[-1];
15381538
return to - tostart;
15391539
}
1540+
1541+
/* Return amount of pending input. */
1542+
1543+
size_t
1544+
do_scrub_pending (void)
1545+
{
1546+
size_t len = 0;
1547+
if (saved_input)
1548+
len += saved_input_len;
1549+
if (state == -1)
1550+
len += strlen (out_string);
1551+
return len;
1552+
}

gas/as.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ void input_scrub_insert_file (char *);
471471
char * input_scrub_new_file (const char *);
472472
char * input_scrub_next_buffer (char **bufp);
473473
size_t do_scrub_chars (size_t (*get) (char *, size_t), char *, size_t);
474+
size_t do_scrub_pending (void);
474475
bool scan_for_multibyte_characters (const unsigned char *, const unsigned char *, bool);
475476
int gen_to_words (LITTLENUM_TYPE *, int, long);
476477
int had_err (void);

gas/input-scrub.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,11 @@ input_scrub_include_sb (sb *from, char *position, enum expansion expansion)
278278

279279
next_saved_file = input_scrub_push (position);
280280

281-
/* Allocate sufficient space: from->len + optional newline. */
281+
/* Allocate sufficient space: from->len plus optional newline
282+
plus two ".linefile " directives, plus a little more for other
283+
expansion. */
282284
newline = from->len >= 1 && from->ptr[0] != '\n';
283-
sb_build (&from_sb, from->len + newline);
285+
sb_build (&from_sb, from->len + newline + 2 * sizeof (".linefile") + 30);
284286
if (expansion == expanding_repeat && from_sb_expansion >= expanding_macro)
285287
expansion = expanding_nested;
286288
from_sb_expansion = expansion;

gas/macro.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,8 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals,
10461046
loclist = f;
10471047
}
10481048

1049+
if (!err && (out->len == 0 || out->ptr[out->len - 1] != '\n'))
1050+
sb_add_char (out, '\n');
10491051
return err;
10501052
}
10511053

gas/sb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,12 @@ sb_scrub_and_add_sb (sb *ptr, sb *s)
119119
So we loop until the input S is consumed. */
120120
while (1)
121121
{
122-
size_t copy = s->len - (scrub_position - s->ptr);
122+
size_t copy = s->len - (scrub_position - s->ptr) + do_scrub_pending ();
123123
if (copy == 0)
124124
break;
125125
sb_check (ptr, copy);
126-
ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, copy);
126+
ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len,
127+
ptr->max - ptr->len);
127128
}
128129

129130
sb_to_scrub = 0;

gas/testsuite/config/default.exp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ if ![info exists NMFLAGS] then {
5252
set NMFLAGS {}
5353
}
5454

55+
if ![info exists SIZE] then {
56+
set SIZE [findfile $base_dir/size]
57+
}
58+
59+
if ![info exists SIZEFLAGS] then {
60+
set SIZEFLAGS ""
61+
}
62+
5563
if ![info exists OBJCOPY] then {
5664
set OBJCOPY [findfile $base_dir/../../binutils/objcopy]
5765
}

gas/testsuite/gas/macros/app5.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#name: APP with linefile
2+
#xfail: tic30-*-*
3+
#size: -G
4+
# pr29466 just check that the test assembles
5+
6+
#pass

gas/testsuite/gas/macros/app5.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#NO_APP
2+
#APP
3+
# 5 "foo.c" 1
4+
# 0 "" 2
5+
#NO_APP

gas/testsuite/gas/macros/macros.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ run_dump_test app2
7070
run_dump_test app3
7171
remote_download host "$srcdir/$subdir/app4b.s"
7272
run_dump_test app4
73+
run_dump_test app5
7374

7475
run_list_test badarg ""
7576

0 commit comments

Comments
 (0)