ltcmelo / psychec

A compiler frontend for the C programming language
BSD 3-Clause "New" or "Revised" License
538 stars 39 forks source link

Cannot declare variable called `true` or `false` #29

Closed aytey closed 3 years ago

aytey commented 3 years ago

GCC version:

gcc (SUSE Linux) 10.2.1 20201202 [revision e563687cf9d3d1278f45aaebd03e0f66531076c9]

clang version:

clang version 11.0.1

Output of gcc -Werror -Wall -Wextra -Wpedantic -pedantic -std=c11 -c $FNAME

(return code 0)

Output of clang -Werror -Wall -Wextra -Wpedantic -pedantic -std=c11 -c $FNAME

(return code 0)

Output of cnip $FNAME

bool.c:0:4 error: expected <identifier> or `(' starting direct-declarator, got `false'
int false;
    ^

(return code 0)

Content of file

int false;

psychec version

commit d10dc1ddfb7ea845f59274a4d6ca5d021ab31423
Author: Leandro T. C. Melo <LTCMELO@GMAIL.COM>
Date:   Tue Mar 9 21:18:30 2021 -0300

    fix postfixing on compound literal
aytey commented 3 years ago

Same for true:

GCC version:

gcc (SUSE Linux) 10.2.1 20201202 [revision e563687cf9d3d1278f45aaebd03e0f66531076c9]

clang version:

clang version 11.0.1

Output of gcc -Werror -Wall -Wextra -Wpedantic -pedantic -std=c11 -c $FNAME

(return code 0)

Output of clang -Werror -Wall -Wextra -Wpedantic -pedantic -std=c11 -c $FNAME

(return code 0)

Output of cnip $FNAME

bool.c:0:4 error: expected <identifier> or `(' starting direct-declarator, got `true'
int true;
    ^

(return code 0)

Content of file

int true;

psychec version

commit d10dc1ddfb7ea845f59274a4d6ca5d021ab31423
Author: Leandro T. C. Melo <LTCMELO@GMAIL.COM>
Date:   Tue Mar 9 21:18:30 2021 -0300

    fix postfixing on compound literal
ltcmelo commented 3 years ago

Thanks @andrewvaughanj , I see that you created a good batch of new issues :wink:.

This behavior is expected: cnip invokes psyche-c's parser with the default language extensions options, which, in turn, consider true a keyword (false as well).

Perhaps I should consider changing this specific default then, to keep consistency with GCC/clang… and diagnose an issue only in semantic analysis, it would be with the inclusion of header <stdbool.h>.

Screen Shot 2021-03-13 at 09 37 06
aytey commented 3 years ago

Looking at GCC, if you use <stdbool.h> then true/false get expanded like this:

#define bool    _Bool
#define true    1
#define false   0

Could cnip not have true and false as built-ins, and get these in the same way via the the #defines? You'd need to treat _Bool as a built-in, of course.

But I don't know enough as to why cnip needs to consider true and false as "built-ins".

ltcmelo commented 3 years ago

I don't know enough as to why cnip needs to consider true and false as "built-ins".

It doesn't need to… those just happen to be the defaults; I'll adjust them.

However, I can't rely on the #defines during syntax analysis only, so the content of <stdbool.h> will only take effect after semantic analysis (which I haven't yet finished int he master branch) — or if the according language extensions are explicitly enabled (after I adjust the defaults).

ltcmelo commented 3 years ago

Fixed with https://github.com/ltcmelo/psychec/commit/ca31d32becabd3abf70fc8c82a86f4f57d1b6323