Skip to content

Commit

Permalink
Fixed several issues relating to the static and const keywords (#123)
Browse files Browse the repository at this point in the history
* fix several miscompilations relating to the const and static keywords, add -g flag to Makefile

* change how __FILE__ and __LINE__ are used

* remove some trailing whitespace

* remove file and line macros

* revert -g flag and ci changes

* revert -g flag and ci changes

* only skip static for globals

* fix typo

* squashed commits

---------

Co-authored-by: Laurent Huberdeau <[email protected]>
  • Loading branch information
VisenDev and laurenthuberdeau authored Jan 8, 2025
1 parent 5e20a86 commit 93bbc5c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
run: |
set -e
check_dir() { # $1 is the file extension, $2 is the directory and $3 the max depth to check
result=$(find $2 -maxdepth ${3:-1000} -name "$1" -type f -exec egrep -l " +$" {} \;)
result=$(find $2 -maxdepth ${3:-1000} -name "$1" -type f -exec grep -l -E " +$" {} \;)
if [ -n "$result" ]; then
echo "Trailing whitespace found in the following files:"
echo "$result"
Expand Down
40 changes: 33 additions & 7 deletions pnut.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#define OPTIMIZE_LONG_LINES
#endif

// Uncomment to cause parse_error() to print which pnut function emitted the error
//#define DEBUG_SHOW_ERR_ORIGIN

// Use positional parameter directly for function parameters that are constants
#define OPTIMIZE_CONSTANT_PARAM_not
#define SUPPORT_ADDRESS_OF_OP_not
Expand Down Expand Up @@ -2057,9 +2060,9 @@ void get_tok() {
#include "debug.c"
#endif

#define parse_error(msg, token) parse_error_internal(msg, token, __FILE__, __LINE__)


void parse_error(char * msg, int token) {
void parse_error_internal(char * msg, int token, char * file, int line) {

#ifdef NICE_ERR_MSG
#define ANSI_RED "\x1b[31m"
Expand Down Expand Up @@ -2095,8 +2098,17 @@ void parse_error(char * msg, int token) {
putint(last_tok_column_number);
putstr(ANSI_RESET"\n");
#else
fatal_error(msg);
putstr(msg);
#endif

#ifdef DEBUG_SHOW_ERR_ORIGIN
putstr("Note, error emitted from ");
putstr(file);
putstr(" line ");
putint(line);
putstr("\n");
#endif

exit(1);
}

Expand Down Expand Up @@ -2190,8 +2202,19 @@ int parse_stars_for_type(int type) {
return type;
}

//defining a const after the * is valid c, ie
// const int * const foo;
void ignore_optional_const() {
if(tok == CONST_KW) {
//skip the const
get_tok();
}
}

int parse_type_with_stars() {
return parse_stars_for_type(parse_type());
int type = parse_stars_for_type(parse_type());
ignore_optional_const();
return type;
}

int is_type_starter(int tok) {
Expand Down Expand Up @@ -2403,6 +2426,11 @@ ast parse_definition(int local) {
ast tail = 0;
ast current_declaration;

//static can be skipped for global definitions without affecting semantics
if(!local && tok == STATIC_KW) {
get_tok();
}

if (is_type_starter(tok)) {
type = parse_type();

Expand All @@ -2418,6 +2446,7 @@ ast parse_definition(int local) {
while (1) {

this_type = parse_stars_for_type(type);
ignore_optional_const();

name = val;

Expand Down Expand Up @@ -2747,9 +2776,6 @@ ast parse_cast_expression() {
if (is_type_starter(tok)) {
type = parse_type_with_stars();

if (get_val(type) == 0 && get_op(type) == VOID_KW)
parse_error("variable with void type", tok);

expect_tok(')');
result = new_ast2(CAST, type, parse_cast_expression());
return result;
Expand Down
6 changes: 6 additions & 0 deletions tests/_all/void_cast.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
int main(int argc, char** argv) {
(void) argc;
(void) argc;

return 0;
}
Empty file added tests/_all/void_cast.golden
Empty file.

0 comments on commit 93bbc5c

Please sign in to comment.