Fix problem with using values of enum constants in integer constant expressions.

This could cause spurious errors, or in some cases bad code generation.

The following example illustrates the problem:

#include <stdio.h>

enum {A,B,C};

/* arr was treated as having a size of 1, rather than 3 */
char arr[(int)C+1] = {1,2,3}; /* incorrectly gave an error for initializer */

int main(void) {
        static int i = (int)C+1; /* incorrectly gave an error */

        printf("%zu\n", sizeof(arr));
        printf("%i\n", (int)C+1); /* OK */
        printf("%i\n", i);
}
This commit is contained in:
Stephen Heumann 2019-03-31 18:40:14 -05:00
parent 618188c6b2
commit 17de3914ad

View File

@ -837,6 +837,7 @@ var
end; {else} end; {else}
end {if id = nill} end {if id = nill}
else if id^.itype^.kind = enumConst then begin else if id^.itype^.kind = enumConst then begin
stack^.token.class := intConstant;
stack^.token.kind := intconst; stack^.token.kind := intconst;
stack^.token.ival := id^.itype^.eval; stack^.token.ival := id^.itype^.eval;
end; {else if} end; {else if}