From b931e658117189cf62662d4919618bb0ffb00144 Mon Sep 17 00:00:00 2001 From: Jesse Rosenstock Date: Sat, 26 Sep 2020 14:56:28 +0200 Subject: [PATCH] Fix ICE for bit-fields with typedef Fixes #1267 Avoid ICE, but treat plain int bit-fields declared via typedef as signed rather than unsigned. It is more efficient to treat them as unsigned, but this requires distinguishing int from signed int, and this is curently not done. --- src/cc65/declare.c | 9 ++++ test/misc/Makefile | 6 --- test/{misc => val}/bug1094.c | 0 test/val/bug1267.c | 79 ++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 6 deletions(-) rename test/{misc => val}/bug1094.c (100%) create mode 100644 test/val/bug1267.c diff --git a/src/cc65/declare.c b/src/cc65/declare.c index 8623fa08f..bf27f9c90 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -1468,6 +1468,15 @@ static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers, /* It's a typedef */ NextToken (); TypeCopy (D->Type, Entry->Type); + /* If it's a typedef, we should actually use whether the signedness was + ** specified on the typedef, but that information has been lost. Treat the + ** signedness as being specified to work around the ICE in #1267. + ** Unforunately, this will cause plain int bit-fields defined via typedefs + ** to be treated as signed rather than unsigned. + */ + if (SignednessSpecified) { + *SignednessSpecified = 1; + } break; } } else { diff --git a/test/misc/Makefile b/test/misc/Makefile index 06046af3c..b8c578405 100644 --- a/test/misc/Makefile +++ b/test/misc/Makefile @@ -100,12 +100,6 @@ $(WORKDIR)/bug1263.$1.$2.prg: bug1263.c | $(WORKDIR) $(if $(QUIET),echo misc/bug1263.$1.$2.prg) $(NOT) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLERR) -# should compile, but gives an error -$(WORKDIR)/bug1094.$1.$2.prg: bug1094.c | $(WORKDIR) - @echo "FIXME: " $$@ "currently does not compile." - $(if $(QUIET),echo misc/bug1094.$1.$2.prg) - $(NOT) $(CC65) -t sim$2 -$1 -o $$@ $$< $(NULLERR) - # this one requires --std=c89, it fails with --std=c99 # it fails currently at runtime $(WORKDIR)/bug1265.$1.$2.prg: bug1265.c | $(WORKDIR) diff --git a/test/misc/bug1094.c b/test/val/bug1094.c similarity index 100% rename from test/misc/bug1094.c rename to test/val/bug1094.c diff --git a/test/val/bug1267.c b/test/val/bug1267.c new file mode 100644 index 000000000..382903594 --- /dev/null +++ b/test/val/bug1267.c @@ -0,0 +1,79 @@ +/* + Copyright 2020 The cc65 Authors + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +/* + Tests of bit-field signedness with typedefs; see https://github.com/cc65/cc65/issues/1267 +*/ + +#include + +static unsigned char failures = 0; + +typedef int i16; +typedef unsigned int u16; +typedef signed int s16; + +static struct ints { + i16 i : 4; + u16 u : 4; + s16 s : 4; +} si = {1, 2, 3}; + +static void test_bitfield_typedefs (void) +{ + if (si.i != 1) { + /* Note that this is another bug that i is signed. */ + printf ("Got si.a = %d, expected 1.\n", si.i); + failures++; + } + if (si.u != 2) { + printf ("Got si.u = %u, expected 2.\n", si.u); + failures++; + } + if (si.s != 3) { + printf ("Got si.s = %d, expected 3.\n", si.s); + failures++; + } + + si.i = -1; + si.u = -2; + si.s = -3; + + /* Note that this is another bug that i is signed. */ + if (si.i != -1) { + printf ("Got si.a = %d, expected -1.\n", si.i); + failures++; + } + if (si.u != 14) { + printf ("Got si.u = %u, expected 14.\n", si.u); + failures++; + } + if (si.s != -3) { + printf ("Got si.s = %d, expected -3.\n", si.s); + failures++; + } +} + +int main (void) +{ + test_bitfield_typedefs (); + printf ("failures: %u\n", failures); + return failures; +}