From 15fb694d7b4c8ec3644e33dd0f36da30670aa678 Mon Sep 17 00:00:00 2001 From: AnnaSnoeijs Date: Thu, 14 Aug 2025 21:31:26 +0200 Subject: [PATCH] Added LTO and fixed warnings --- connect4.c | 10 ++++++++-- makefile | 9 +++++---- ui_ncurses.c | 26 ++++++++++++-------------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/connect4.c b/connect4.c index 91710c8..38c3269 100644 --- a/connect4.c +++ b/connect4.c @@ -128,10 +128,10 @@ int main( int argc, char *argv[] ){ } return 0; case 'c': // --columns - playboard.columns = atoi(optarg); + playboard.columns = (columnsint_t)atoi(optarg); break; case 'r': // --rows - playboard.rows = atoi(optarg); + playboard.rows = (rowsint_t)atoi(optarg); break; case 'o': filename = strdup(optarg); @@ -169,6 +169,10 @@ int main( int argc, char *argv[] ){ // Allocate the board playboard.height = calloc( playboard.columns, sizeof(rowsint_t) ); playboard.column = calloc( playboard.columns, sizeof(column_t) ); + if( playboard.height == NULL || playboard.column == NULL ){ + fprintf( stderr, "ERROR: COULD NOT ALLOCATE BOARD" ); + return 1; + } // Initialize wins struct if used if( wins != NULL ){ wins->win0 = calloc( playboard.columns, sizeof(column_t) ); @@ -181,7 +185,9 @@ int main( int argc, char *argv[] ){ wins->same.horizontal4 = calloc( playboard.columns, sizeof(column_t) ); wins->same.diagonalUp4 = calloc( playboard.columns, sizeof(column_t) ); wins->same.diagonalDown4 = calloc( playboard.columns, sizeof(column_t) ); + goto init_ui; } + init_ui: initBoard( playboard ); // Begin loopin if( outputfile != NULL ){ diff --git a/makefile b/makefile index ca2de49..e0366af 100644 --- a/makefile +++ b/makefile @@ -4,7 +4,7 @@ ARCH = native # Optimization level -O = 2 +O = s # Flags for the compiler FLAGS = -std=c23 @@ -17,15 +17,16 @@ endif FLAGS += -pedantic FLAGS += -Wall FLAGS += -Wextra -FLAGS += -Werror +#FLAGS += -Werror FLAGS += -Wconversion FLAGS += -Wmissing-prototypes FLAGS += -Wlogical-op FLAGS += -Wdisabled-optimization FLAGS += -fanalyzer +FLAGS += -flto # These flags are because of the compiler otherwise giving too much warning -FLAGS += -Wno-sign-conversion -FLAGS += -Wformat-truncation=0 +#FLAGS += -Wno-sign-conversion +#FLAGS += -Wformat-truncation=0 # Compile flag for defining GITHASH to put at the end of the version string GITFLAG = -D'GITHASH=$(shell git rev-parse --short=1 HEAD)' diff --git a/ui_ncurses.c b/ui_ncurses.c index bf77740..580286a 100644 --- a/ui_ncurses.c +++ b/ui_ncurses.c @@ -71,7 +71,6 @@ void initBoard( const board_t board ){ keypad(stdscr, TRUE); nonl(); echo(); - __attribute__((assume(board.columns < 999))); for( columnsint_t column = 0; column < board.columns; column++ ){ char colnum[4]; snprintf( @@ -79,37 +78,36 @@ void initBoard( const board_t board ){ ); mvaddstr( BOARD_Y, - BOARD_X + BOARD_DX * ( column + 1 ), + (int)(BOARD_X + BOARD_DX * ( column + 1 )), colnum ); for( rowsint_t row = 0; row < board.rows; row++ ){ mvaddstr( - BOARD_Y + BOARD_DY * ( row + 1 ), - BOARD_X + BOARD_DX * ( column + 1 ), + (int)(BOARD_Y + BOARD_DY * ( row + 1 )), + (int)(BOARD_X + BOARD_DX * ( column + 1 )), "[ ]" ); } mvaddstr( - BOARD_Y + BOARD_DY * ( board.rows + 1 ), - BOARD_X + BOARD_DX * ( column + 1 ), + (int)(BOARD_Y + BOARD_DY * ( board.rows + 1 )), + (int)(BOARD_X + BOARD_DX * ( column + 1 )), colnum ); } for( rowsint_t row = 0; row < board.rows; row++ ){ char rownum[4]; const int intToPrint = board.rows - row + FIRST_NUMBER - 1; - __attribute__((assume(intToPrint <= 256))); snprintf( rownum, sizeof(rownum), "%2d", intToPrint ); mvaddstr( - BOARD_Y + BOARD_DY * ( row + 1 ), + (int)(BOARD_Y + BOARD_DY * ( row + 1 )), BOARD_X, rownum ); mvaddstr( - BOARD_Y + BOARD_DY * ( row + 1 ), - BOARD_X + BOARD_DX * ( board.columns + 1 ), + (int)(BOARD_Y + BOARD_DY * ( row + 1 )), + (int)(BOARD_X + BOARD_DX * ( board.columns + 1 )), rownum ); } @@ -142,8 +140,8 @@ void updateBoard( ){ rowsint_t height = board.height[ column ]; mvaddstr( - BOARD_Y + BOARD_DY * ( board.rows - height + 1 ), - BOARD_X + 1 + BOARD_DX * ( column + 1 ), + (int)(BOARD_Y + BOARD_DY * ( board.rows - height + 1 )), + (int)(BOARD_X + 1 + BOARD_DX * ( column + 1 )), board.column[ column ] & 1 << ( height - 1 ) ? "1" : "0" ); char num[4]; @@ -226,8 +224,8 @@ columnsint_t askColumn( } for(;;){ int ch = mvgetch( - BOARD_Y + BOARD_DY * ( board.rows - board.height[ column ] ), - BOARD_X + 1 + BOARD_DX * ( column + 1 ) + (int)(BOARD_Y + BOARD_DY * ( board.rows - board.height[ column ] )), + (int)(BOARD_X + 1 + BOARD_DX * ( column + 1 )) ); switch( ch ){ case KEY_RIGHT: