From 6132405404860056d13b8525d1c7f83b4aca1565 Mon Sep 17 00:00:00 2001 From: Joseph Poirier Date: Sat, 13 Jun 2015 13:27:01 -0500 Subject: [PATCH] fix: issue #2, for real this time --- expression.c | 10 ++++++---- tests/27_sizeof.c | 39 +++++++++++++++++++++++++++++++++++---- tests/27_sizeof.expect | 4 ++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/expression.c b/expression.c index e7cd522..caab8b8 100644 --- a/expression.c +++ b/expression.c @@ -635,6 +635,7 @@ void ExpressionPrefixOperator(struct ParseState *Parser, { struct Value *Result; union AnyValue *ValPtr; + struct ValueType *Typ; #ifdef DEBUG_EXPRESSIONS printf("ExpressionPrefixOperator()\n"); @@ -663,11 +664,12 @@ void ExpressionPrefixOperator(struct ParseState *Parser, case TokenSizeof: /* return the size of the argument */ if (TopValue->Typ == &Parser->pc->TypeType) - ExpressionPushInt(Parser, StackTop, TypeSize(TopValue->Val->Typ, - TopValue->Val->Typ->ArraySize, true)); + Typ = TopValue->Val->Typ; else - ExpressionPushInt(Parser, StackTop, TypeSize(TopValue->Typ, - TopValue->Typ->ArraySize, true)); + Typ = TopValue->Typ; + if (Typ->FromType != NULL && Typ->FromType->Base == TypeStruct) + Typ = Typ->FromType; + ExpressionPushInt(Parser, StackTop, TypeSize(Typ, Typ->ArraySize, true)); break; default: /* an arithmetic operator */ diff --git a/tests/27_sizeof.c b/tests/27_sizeof.c index 52703aa..ca01000 100644 --- a/tests/27_sizeof.c +++ b/tests/27_sizeof.c @@ -1,12 +1,43 @@ #include +#include char a; int b; double c; -printf("%d\n", sizeof(a)); -printf("%d\n", sizeof(b)); -printf("%d\n", sizeof(c)); +struct P1 { + int a; + int b; + double c; +}; +struct P2 { + int a; + int b; + int c; + int d; + double e; +}; -void main() {} +struct P3 { + int a; + int b; + int c; + int d; + double e; + struct P1 *p1; + struct P1 *p2; + struct P1 *p3; +}; + +void main() { + struct P1 *p1; + struct P2 *p2; + struct P3 *p3; + printf("%d\n", sizeof(*p1)); + printf("%d\n", sizeof(*p2)); + printf("%d\n", sizeof(*p3)); + printf("%d\n", sizeof(a)); + printf("%d\n", sizeof(b)); + printf("%d\n", sizeof(c)); +} diff --git a/tests/27_sizeof.expect b/tests/27_sizeof.expect index 7329e00..0f1040a 100644 --- a/tests/27_sizeof.expect +++ b/tests/27_sizeof.expect @@ -1,3 +1,7 @@ +16 +24 +48 1 4 8 +