~mcf/cproc

e8863971221faba91357310b0c8be0fb2951ef22 — Michael Forney 2 years ago 8b10f07
Fix size of incomplete array types initialized with strings

updatearray takes an index, so was allocating an extra byte.
3 files changed, 25 insertions(+), 1 deletions(-)

M init.c
A tests/initializer-string.c
A tests/initializer-string.qbe
M init.c => init.c +1 -1
@@ 249,7 249,7 @@ parseinit(struct scope *s, struct type *t)
					if (!typecompatible(expr->type->base, base))
						error(&tok.loc, "array initializer is string literal with incompatible type");
					if (t->incomplete)
						updatearray(t, expr->string.size + 1);
						updatearray(t, expr->string.size);
					goto add;
				}
				break;

A tests/initializer-string.c => tests/initializer-string.c +4 -0
@@ 0,0 1,4 @@
char x[] = "hello";
void f(void) {
	char y[] = "hello";
}

A tests/initializer-string.qbe => tests/initializer-string.qbe +20 -0
@@ 0,0 1,20 @@
export data $x = align 1 { b "hello", z 1, }
export
function $f() {
@start.1
	%.1 =l alloc4 6
@body.2
	%.2 =l add %.1, 0
	storeb 104, %.2
	%.3 =l add %.1, 1
	storeb 101, %.3
	%.4 =l add %.1, 2
	storeb 108, %.4
	%.5 =l add %.1, 3
	storeb 108, %.5
	%.6 =l add %.1, 4
	storeb 111, %.6
	%.7 =l add %.1, 5
	storeb 0, %.7
	ret
}