From 6434176909002b3183a7c82d2909d8c740407bf4 Mon Sep 17 00:00:00 2001 From: acqn Date: Wed, 29 Nov 2023 12:27:01 +0800 Subject: [PATCH] Fixed constness of bit-fields. --- src/cc65/datatype.c | 4 ++-- src/cc65/datatype.h | 4 ++-- src/cc65/symtab.c | 1 + test/err/bug2018-bitfield.c | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 test/err/bug2018-bitfield.c diff --git a/src/cc65/datatype.c b/src/cc65/datatype.c index 81727e491..9e0652526 100644 --- a/src/cc65/datatype.c +++ b/src/cc65/datatype.c @@ -497,8 +497,8 @@ Type* NewPointerTo (const Type* T) Type* NewBitFieldOf (const Type* T, unsigned BitOffs, unsigned BitWidth) -/* Return a type string that is "T : BitWidth" aligned on BitOffs. The type -** string is allocated on the heap and may be freed after use. +/* Return a type string that is "unqualified T : BitWidth" aligned on BitOffs. +** The type string is allocated on the heap and may be freed after use. */ { Type* P; diff --git a/src/cc65/datatype.h b/src/cc65/datatype.h index 0890c4d12..4f4b6f25e 100644 --- a/src/cc65/datatype.h +++ b/src/cc65/datatype.h @@ -435,8 +435,8 @@ Type* NewPointerTo (const Type* T); */ Type* NewBitFieldOf (const Type* T, unsigned BitOffs, unsigned BitWidth); -/* Return a type string that is "T : BitWidth" aligned on BitOffs. The type -** string is allocated on the heap and may be freed after use. +/* Return a type string that is "unqualified T : BitWidth" aligned on BitOffs. +** The type string is allocated on the heap and may be freed after use. */ const Type* AddressOf (const Type* T); diff --git a/src/cc65/symtab.c b/src/cc65/symtab.c index d30e591c9..a4ca9e23d 100644 --- a/src/cc65/symtab.c +++ b/src/cc65/symtab.c @@ -1023,6 +1023,7 @@ SymEntry* AddBitField (const char* Name, const Type* T, unsigned Offs, } else { Entry->Type = NewBitFieldOf (T, BitOffs, BitWidth); } + Entry->Type[0].C |= GetQualifier (T) & T_MASK_QUAL; /* Add the entry to the symbol table */ AddSymEntry (FieldTab, Entry); diff --git a/test/err/bug2018-bitfield.c b/test/err/bug2018-bitfield.c new file mode 100644 index 000000000..ea2928659 --- /dev/null +++ b/test/err/bug2018-bitfield.c @@ -0,0 +1,14 @@ +/* Bug #2018 - Compiler has problems with const struct fields */ + +typedef union U { + int a : 16; + const int b : 16; +} U; + +int main(void) +{ + U x = { 42 }; + x.b = 0; + + return 0; +}