History

Correctly support all unary expression with sizeof

Unary expression can start with a parenthesis. Thus, the current test to detect which sizeof form is being parsed is inaccurate. This patch makes tcc able to handle things like sizeof (x)[1] where x is declared as char x[5]; wich is a valid unary expression

Thomas Preud'homme committed 16 years ago

Commit 8de9b7a1 parent 6655e061 file · +19 −6Browse files

Unified · Split

tccgen.c+19−6
@@ -284,6 +284,13 @@ static void vsetc(CType *type, int r, CValue *vc) · 283 unmodified lines
284284 vtop->c = *vc;
285285 }
286286
287+/* push constant of type "type" with useless value */
288+void vpush(CType *type)
289+{
290+ CValue cval;
291+ vsetc(type, VT_CONST, &cval);
292+}
293+
287294 /* push integer constant */
288295 ST_FUNC void vpushi(int v)
289296 {
@@ -3244,11 +3251,14 @@ static void vpush_tokc(int t) · 2954 unmodified lines
32443251
32453252 ST_FUNC void unary(void)
32463253 {
3247− int n, t, align, size, r;
3254+ int n, t, align, size, r, sizeof_caller;
32483255 CType type;
32493256 Sym *s;
32503257 AttributeDef ad;
3258+ static int in_sizeof = 0;
32513259
3260+ sizeof_caller = in_sizeof;
3261+ in_sizeof = 0;
32523262 /* XXX: GCC 2.95.3 does not generate a table although it should be
32533263 better here */
32543264 tok_next:
@@ -3345,6 +3355,10 @@ ST_FUNC void unary(void) · 90 unmodified lines
33453355 memset(&ad, 0, sizeof(AttributeDef));
33463356 decl_initializer_alloc(&type, &ad, r, 1, 0, 0);
33473357 } else {
3358+ if (sizeof_caller) {
3359+ vpush(&type);
3360+ return;
3361+ }
33483362 unary();
33493363 gen_cast(&type);
33503364 }
@@ -3414,11 +3428,8 @@ ST_FUNC void unary(void) · 63 unmodified lines
34143428 case TOK_ALIGNOF2:
34153429 t = tok;
34163430 next();
3417− if (tok == '(') {
3418− parse_expr_type(&type);
3419− } else {
3420− unary_type(&type);
3421− }
3431+ in_sizeof++;
3432+ unary_type(&type); // Perform a in_sizeof = 0;
34223433 size = type_size(&type, &align);
34233434 if (t == TOK_SIZEOF) {
34243435 if (size < 0)
@@ -4040,9 +4051,11 @@ static void expr_type(CType *type) · 615 unmodified lines
40404051 static void unary_type(CType *type)
40414052 {
40424053 int a;
4054+ void *vtop_saved;
40434055
40444056 a = nocode_wanted;
40454057 nocode_wanted = 1;
4058+ vtop_saved = vtop;
40464059 unary();
40474060 *type = vtop->type;
40484061 vpop();

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t