Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit 6cf6ea8

Browse files
danielsan901998Vexu
authored andcommitted
Translator: dereference type for sizeof
1 parent 8eafe24 commit 6cf6ea8

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/Translator.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,8 +1549,7 @@ fn transStmt(t: *Translator, scope: *Scope, stmt: Node.Index) TransError!ZigNode
15491549
.goto_stmt, .computed_goto_stmt, .labeled_stmt => {
15501550
return t.fail(error.UnsupportedTranslation, stmt.tok(t.tree), "TODO goto", .{});
15511551
},
1552-
.gnu_asm_simple,
1553-
=> {
1552+
.gnu_asm_simple => {
15541553
return t.fail(error.UnsupportedTranslation, stmt.tok(t.tree), "TODO asm inside function", .{});
15551554
},
15561555
else => return t.transExprCoercing(scope, stmt, .unused),
@@ -3685,6 +3684,10 @@ fn transTypeInfo(
36853684
const operand = operand: {
36863685
if (typeinfo.expr) |expr| {
36873686
const operand = try t.transExpr(scope, expr, .used);
3687+
if (operand.tag() == .string_literal) {
3688+
const deref = try ZigTag.deref.create(t.arena, operand);
3689+
break :operand try ZigTag.typeof.create(t.arena, deref);
3690+
}
36883691
break :operand try ZigTag.typeof.create(t.arena, operand);
36893692
}
36903693
break :operand try t.transType(scope, typeinfo.operand_qt, typeinfo.op_tok);

test/cases/run/sizeof.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdlib.h>
2+
int main(int argc, char **argv) {
3+
char a[]="a";
4+
char b[3]="a";
5+
int c[10];
6+
if (sizeof("a")!=2) abort();
7+
if (sizeof(a)!=2) abort();
8+
if (sizeof(b)!=3) abort();
9+
if (sizeof(c)!=sizeof(int)*10) abort();
10+
if (__alignof("a")!=1) abort();
11+
}
12+
13+
// run

0 commit comments

Comments
 (0)