Fixed const qualifiers on named structs/unions members that should prevent assignments to the whole structs/unions.

Added warning on ignored qualifiers on anonymous structs/unions.
This commit is contained in:
acqn
2023-11-13 17:17:46 +08:00
parent d7d1d89698
commit 0eb38770bd
5 changed files with 163 additions and 23 deletions

25
test/err/bug2018.c Normal file
View File

@@ -0,0 +1,25 @@
/* Bug #2018 - Compiler has problems with const struct fields */
struct X {
struct {
int a;
} a;
union {
int a;
const int b;
} b;
};
struct X f(void)
{
struct X x = { 42 };
return x;
}
int main(void)
{
struct X x = { 0 };
x = f(); /* Error since X is read only */
return 0;
}

61
test/val/bug2018-ok.c Normal file
View File

@@ -0,0 +1,61 @@
/* Bug #2018 - Compiler has problems with const struct fields */
#include <stdio.h>
unsigned failures;
struct X {
const struct { /* Qualifier ignored in cc65 */
int a;
};
const union { /* Qualifier ignored in cc65 */
int b;
};
};
union Y {
const struct { /* Qualifier ignored in cc65 */
int a;
};
const union { /* Qualifier ignored in cc65 */
int b;
};
};
struct X f(struct X a)
{
struct X x = { 42 };
return --a.a ? a : x;
}
union Y g(union Y a)
{
union Y y = { 42 };
return --a.a ? a : y;
}
int main(void)
{
struct X x = { 1 };
union Y y = { 1 };
x = f(x); /* Allowed in cc65 since X is not read only */
y = g(y); /* Allowed in cc65 since Y is not read only */
if (x.a != 42)
{
++failures;
}
if (y.a != 42)
{
++failures;
}
if (failures > 0)
{
printf("failures: %u\n", failures);
}
return failures;
}