From 7a4622714184f196d8592ee6188945fb0528bd06 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 4 Aug 2012 13:02:44 -0700 Subject: [PATCH] More warning fixes and switching from int to long or size_t --- expand.cpp | 37 ++++++++++++++++---------------- expand.h | 2 +- highlight.cpp | 34 ++++++++++++----------------- highlight.h | 4 ++-- parser.cpp | 41 +++++++++++++++++------------------ reader.cpp | 59 +++++++++++++++++++++++---------------------------- reader.h | 2 +- 7 files changed, 82 insertions(+), 97 deletions(-) diff --git a/expand.cpp b/expand.cpp index 9ba0bdb6e..9a7c77175 100644 --- a/expand.cpp +++ b/expand.cpp @@ -788,9 +788,9 @@ static int expand_pid( const wcstring &instr_with_sep, } -void expand_variable_error( parser_t &parser, const wchar_t *token, int token_pos, int error_pos ) +void expand_variable_error( parser_t &parser, const wchar_t *token, size_t token_pos, int error_pos ) { - int stop_pos = token_pos+1; + size_t stop_pos = token_pos+1; switch( token[stop_pos] ) { @@ -873,11 +873,12 @@ void expand_variable_error( parser_t &parser, const wchar_t *token, int token_po /** Parse an array slicing specification */ -static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector &idx, int size ) +static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector &idx, size_t array_size ) { wchar_t *end; - int pos = 1; + const long size = (long)array_size; + size_t pos = 1; //skip past the opening square bracket // debug( 0, L"parse_slice on '%ls'", in ); @@ -902,7 +903,7 @@ static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector } // debug( 0, L"Push idx %d", tmp ); - long i1 = tmp>-1 ? tmp : size+tmp+1; + long i1 = tmp>-1 ? tmp : (long)array_size+tmp+1; pos = end-in; while( in[pos]==INTERNAL_SEPARATOR ) pos++; @@ -959,16 +960,16 @@ static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector happens, don't edit it unless you know exactly what you are doing, and do proper testing afterwards. */ -static int expand_variables_internal( parser_t &parser, wchar_t * const in, std::vector &out, int last_idx ); +static int expand_variables_internal( parser_t &parser, wchar_t * const in, std::vector &out, long last_idx ); -static int expand_variables2( parser_t &parser, const wcstring &instr, std::vector &out, int last_idx ) { +static int expand_variables2( parser_t &parser, const wcstring &instr, std::vector &out, long last_idx ) { wchar_t *in = wcsdup(instr.c_str()); int result = expand_variables_internal(parser, in, out, last_idx); free(in); return result; } -static int expand_variables_internal( parser_t &parser, wchar_t * const in, std::vector &out, int last_idx ) +static int expand_variables_internal( parser_t &parser, wchar_t * const in, std::vector &out, long last_idx ) { int is_ok= 1; int empty=0; @@ -978,14 +979,14 @@ static int expand_variables_internal( parser_t &parser, wchar_t * const in, std: // CHECK( out, 0 ); - for( int i=last_idx; (i>=0) && is_ok && !empty; i-- ) + for( long i=last_idx; (i>=0) && is_ok && !empty; i-- ) { const wchar_t c = in[i]; if( ( c == VARIABLE_EXPAND ) || (c == VARIABLE_EXPAND_SINGLE ) ) { - int start_pos = i+1; - int stop_pos; - int var_len; + long start_pos = i+1; + long stop_pos; + long var_len; int is_single = (c==VARIABLE_EXPAND_SINGLE); stop_pos = start_pos; @@ -1185,7 +1186,7 @@ static int expand_brackets(parser_t &parser, const wchar_t *in, int flags, std:: const wchar_t *last_sep=0; const wchar_t *item_begin; - int len1, len2, tot_len; + size_t len1, len2, tot_len; CHECK( in, 0 ); // CHECK( out, 0 ); @@ -1276,7 +1277,8 @@ static int expand_brackets(parser_t &parser, const wchar_t *in, int flags, std:: if( (*pos == BRACKET_SEP) || (pos==bracket_end) ) { wchar_t *whole_item; - int item_len = pos-item_begin; + assert(pos >= item_begin); + size_t item_len = pos-item_begin; whole_item = (wchar_t *)malloc( sizeof(wchar_t)*(tot_len + item_len + 1) ); wcslcpy( whole_item, in, len1+1 ); @@ -1310,7 +1312,6 @@ static int expand_brackets(parser_t &parser, const wchar_t *in, int flags, std:: static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector &outList ) { wchar_t *paran_begin=0, *paran_end=0; - int len1; std::vector sub_res; size_t i, j; wchar_t *tail_begin = 0; @@ -1335,9 +1336,7 @@ static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector break; } - - len1 = (paran_begin-in); - + const wcstring subcmd(paran_begin + 1, paran_end-paran_begin - 1); if( exec_subshell( subcmd, sub_res) == -1 ) @@ -1407,7 +1406,7 @@ static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector wcstring tail_item = tail_expand.at(j).completion; //sb_append_substring( &whole_item, in, len1 ); - whole_item.append(in, len1); + whole_item.append(in, paran_begin-in); //sb_append_char( &whole_item, INTERNAL_SEPARATOR ); whole_item.push_back(INTERNAL_SEPARATOR); diff --git a/expand.h b/expand.h index 1181570c1..a5a976c7d 100644 --- a/expand.h +++ b/expand.h @@ -190,7 +190,7 @@ int expand_is_clean( const wchar_t *in ); \param token_pos The position where the expansion begins \param error_pos The position on the line to report to the error function. */ -void expand_variable_error( parser_t &parser, const wchar_t *token, int token_pos, int error_pos ); +void expand_variable_error( parser_t &parser, const wchar_t *token, size_t token_pos, int error_pos ); /** Testing function for getting all process names. diff --git a/highlight.cpp b/highlight.cpp index 1d58d702d..b594434cb 100644 --- a/highlight.cpp +++ b/highlight.cpp @@ -40,9 +40,7 @@ */ #define VAR_COUNT ( sizeof(highlight_var)/sizeof(wchar_t *) ) -static void highlight_universal_internal( const wcstring &buff, - std::vector &color, - int pos ); +static void highlight_universal_internal( const wcstring &buff, std::vector &color, size_t pos ); /** The environment variables used to specify the color of different tokens. @@ -358,7 +356,7 @@ rgb_color_t highlight_get_color( int highlight, bool is_background ) /** Highlight operators (such as $, ~, %, as well as escaped characters. */ -static void highlight_param( const wcstring &buffstr, std::vector &colors, int pos, wcstring_list_t *error ) +static void highlight_param( const wcstring &buffstr, std::vector &colors, size_t pos, wcstring_list_t *error ) { const wchar_t * const buff = buffstr.c_str(); enum {e_unquoted, e_single_quoted, e_double_quoted} mode = e_unquoted; @@ -892,13 +890,12 @@ bool autosuggest_validate_from_history(const history_item_t &item, file_detectio } // This function does I/O -static void tokenize( const wchar_t * const buff, std::vector &color, const int pos, wcstring_list_t *error, const wcstring &working_directory, const env_vars_snapshot_t &vars) { +static void tokenize( const wchar_t * const buff, std::vector &color, const size_t pos, wcstring_list_t *error, const wcstring &working_directory, const env_vars_snapshot_t &vars) { ASSERT_IS_BACKGROUND_THREAD(); - wcstring cmd; + wcstring cmd; int had_cmd=0; wcstring last_cmd; - int len; int accept_switches = 1; @@ -908,10 +905,8 @@ static void tokenize( const wchar_t * const buff, std::vector &color, const CHECK( buff, ); - len = wcslen(buff); - - if( !len ) - return; + if (buff[0] == L'\0') + return; std::fill(color.begin(), color.end(), -1); @@ -970,11 +965,12 @@ static void tokenize( const wchar_t * const buff, std::vector &color, const /* Highlight the parameter. highlight_param wants to write one more color than we have characters (hysterical raisins) so allocate one more in the vector. But don't copy it back. */ const wcstring param_str = param; - int tok_pos = tok_get_pos(&tok); + size_t tok_pos = tok_get_pos(&tok); std::vector::const_iterator where = color.begin() + tok_pos; std::vector subcolors(where, where + param_str.size()); - subcolors.push_back(-1); + subcolors.push_back(-1); + assert(pos >= tok_pos); highlight_param(param_str, subcolors, pos-tok_pos, error); /* Copy the subcolors back into our colors array */ @@ -1265,7 +1261,7 @@ static void tokenize( const wchar_t * const buff, std::vector &color, const // PCA This function does I/O, (calls is_potential_path, path_get_path, maybe others) and so ought to only run on a background thread -void highlight_shell( const wcstring &buff, std::vector &color, int pos, wcstring_list_t *error, const env_vars_snapshot_t &vars ) +void highlight_shell( const wcstring &buff, std::vector &color, size_t pos, wcstring_list_t *error, const env_vars_snapshot_t &vars ) { ASSERT_IS_BACKGROUND_THREAD(); @@ -1346,7 +1342,7 @@ void highlight_shell( const wcstring &buff, std::vector &color, int pos, wc are the current token. For reasons that I don't yet understand, it's required that pos be allowed to be length (e.g. when backspacing). */ - if( pos >= 0 && (size_t)pos <= length ) + if( pos <= length ) { const wchar_t *cbuff = buff.c_str(); @@ -1389,12 +1385,10 @@ void highlight_shell( const wcstring &buff, std::vector &color, int pos, wc /** Perform quote and parenthesis highlighting on the specified string. */ -static void highlight_universal_internal( const wcstring &buffstr, - std::vector &color, - int pos ) +static void highlight_universal_internal( const wcstring &buffstr, std::vector &color, size_t pos ) { assert(buffstr.size() == color.size()); - if( (pos >= 0) && ((size_t)pos < buffstr.size()) ) + if( pos < buffstr.size() ) { /* @@ -1498,7 +1492,7 @@ static void highlight_universal_internal( const wcstring &buffstr, } } -void highlight_universal( const wcstring &buff, std::vector &color, int pos, wcstring_list_t *error, const env_vars_snapshot_t &vars ) +void highlight_universal( const wcstring &buff, std::vector &color, size_t pos, wcstring_list_t *error, const env_vars_snapshot_t &vars ) { assert(buff.size() == color.size()); std::fill(color.begin(), color.end(), 0); diff --git a/highlight.h b/highlight.h index 6ad19dfa8..22121e488 100644 --- a/highlight.h +++ b/highlight.h @@ -83,7 +83,7 @@ struct file_detection_context_t; \param pos the cursor position. Used for quote matching, etc. \param error a list in which a description of each error will be inserted. May be 0, in whcich case no error descriptions will be generated. */ -void highlight_shell( const wcstring &buffstr, std::vector &color, int pos, wcstring_list_t *error, const env_vars_snapshot_t &vars ); +void highlight_shell( const wcstring &buffstr, std::vector &color, size_t pos, wcstring_list_t *error, const env_vars_snapshot_t &vars ); /** Perform syntax highlighting for the text in buff. Matching quotes and paranthesis are highlighted. The result is @@ -95,7 +95,7 @@ void highlight_shell( const wcstring &buffstr, std::vector &color, int pos, \param pos the cursor position. Used for quote matching, etc. \param error a list in which a description of each error will be inserted. May be 0, in whcich case no error descriptions will be generated. */ -void highlight_universal( const wcstring &buffstr, std::vector &color, int pos, wcstring_list_t *error, const env_vars_snapshot_t &vars ); +void highlight_universal( const wcstring &buffstr, std::vector &color, size_t pos, wcstring_list_t *error, const env_vars_snapshot_t &vars ); /** Translate from HIGHLIGHT_* to FISH_COLOR_* according to environment diff --git a/parser.cpp b/parser.cpp index 3a455132e..65424f93d 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1226,11 +1226,9 @@ const wchar_t *parser_t::get_buffer() const int parser_t::is_help( const wchar_t *s, int min_match ) const { - int len; - CHECK( s, 0 ); - len = wcslen(s); + size_t len = wcslen(s); min_match = maxi( min_match, 3 ); @@ -2173,14 +2171,13 @@ int parser_t::parse_job( process_t *p, if( make_sub_block ) { - int end_pos = end-tok_string( tok ); + long end_pos = end-tok_string( tok ); const wcstring sub_block(tok_string( tok ) + current_tokenizer_pos, end_pos - current_tokenizer_pos); p->type = INTERNAL_BLOCK; args.at( 0 ) = completion_t(sub_block); - tok_set_pos( tok, - end_pos ); + tok_set_pos( tok, (int)end_pos ); while( prev_block != current_block ) { @@ -2392,8 +2389,8 @@ void parser_t::eval_job( tokenizer *tok ) { t3 = get_time(); profile_item->level=eval_level; - profile_item->parse = t2-t1; - profile_item->exec=t3-t2; + profile_item->parse = (int)(t2-t1); + profile_item->exec=(int)(t3-t2); } if( current_block->type == WHILE ) @@ -2860,6 +2857,8 @@ int parser_t::test( const wchar_t * buff, tokenizer *previous_tokenizer=current_tokenizer; int previous_pos=current_tokenizer_pos; + + // PCA These statics are terrifying - I have no idea whether and why these variables need to be static static int block_pos[BLOCK_MAX_COUNT]; static int block_type[BLOCK_MAX_COUNT]; int res = 0; @@ -2897,9 +2896,8 @@ int parser_t::test( const wchar_t * buff, if( block_level ) { - int i; - int len = wcslen(buff); - for( i=0; i= 0 ) @@ -3558,11 +3555,12 @@ int parser_t::test( const wchar_t * buff, level. This avoid using the wrong indentation level if a new line starts with whitespace. */ - for( j=i-1; j>=0; j-- ) + size_t prev_char_idx = i; + while (prev_char_idx--) { - if( !wcschr( L" \n\t\r", buff[j] ) ) + if( !wcschr( L" \n\t\r", buff[prev_char_idx] ) ) break; - block_level[j] = last_level; + block_level[prev_char_idx] = last_level; } } block_level[i] = last_level; @@ -3573,15 +3571,14 @@ int parser_t::test( const wchar_t * buff, validator had at exit. This makes sure a new line is correctly indented even if it is empty. */ - for( j=len-1; j>=0; j-- ) + size_t suffix_idx = len; + while (suffix_idx--) { - if( !wcschr( L" \n\t\r", buff[j] ) ) + if( !wcschr( L" \n\t\r", buff[suffix_idx] ) ) break; - block_level[j] = count; + block_level[suffix_idx] = count; } - - - } + } /* Calculate exit status diff --git a/reader.cpp b/reader.cpp index 9bfa1b065..a3c19fe23 100644 --- a/reader.cpp +++ b/reader.cpp @@ -369,7 +369,7 @@ static int interrupted=0; */ static struct termios saved_modes; -static void reader_super_highlight_me_plenty( int pos ); +static void reader_super_highlight_me_plenty( size_t pos ); /** Variable to keep track of forced exits - see \c reader_exit_forced(); @@ -465,7 +465,7 @@ static void reader_repaint() /** Internal helper function for handling killing parts of text. */ -static void reader_kill( size_t begin_idx, int length, int mode, int newv ) +static void reader_kill( size_t begin_idx, size_t length, int mode, int newv ) { const wchar_t *begin = data->command_line.c_str() + begin_idx; if( newv ) @@ -773,7 +773,8 @@ static int insert_string(const wcstring &str) data->command_line_changed(); data->suppress_autosuggestion = false; - /* Syntax highlight */ + /* Syntax highlight. Note we must have that buff_pos > 0 because we just added something nonzero to its length */ + assert(data->buff_pos > 0); reader_super_highlight_me_plenty( data->buff_pos-1 ); reader_repaint(); @@ -1959,8 +1960,8 @@ static void move_word( int dir, int erase, int newv ) if( erase ) { - int remove_count = abs(data->buff_pos - end_buff_pos); - int first_char = mini( data->buff_pos, end_buff_pos ); + size_t remove_count = labs(data->buff_pos - end_buff_pos); + long first_char = mini( data->buff_pos, end_buff_pos ); // fwprintf( stderr, L"Remove from %d to %d\n", first_char, first_char+remove_count ); reader_kill( first_char, remove_count, dir?KILL_APPEND:KILL_PREPEND, newv ); @@ -1985,24 +1986,21 @@ history_t *reader_get_history(void) { return data ? data->history : NULL; } -void reader_set_buffer( const wcstring &b, size_t p ) +void reader_set_buffer( const wcstring &b, size_t pos ) { if( !data ) return; /* Callers like to pass us pointers into ourselves, so be careful! I don't know if we can use operator= with a pointer to our interior, so use an intermediate. */ - size_t l = b.size(); + size_t command_line_len = b.size(); data->command_line = b; data->command_line_changed(); - if( p>=0 ) - { - data->buff_pos=mini( (size_t)p, l ); - } - else - { - data->buff_pos=l; - } + /* Don't set a position past the command line length */ + if (pos > command_line_len) + pos = command_line_len; + + data->buff_pos = pos; data->search_mode = NO_SEARCH; data->search_buff.clear(); @@ -2217,7 +2215,7 @@ public: std::vector colors; /** The position to use for bracket matching */ - const int match_highlight_pos; + const size_t match_highlight_pos; /** Function for syntax highlighting */ const highlight_function_t highlight_function; @@ -2231,24 +2229,26 @@ public: /** Gen count at the time the request was made */ const unsigned int generation_count; - background_highlight_context_t(const wcstring &pbuff, int phighlight_pos, highlight_function_t phighlight_func) : + background_highlight_context_t(const wcstring &pbuff, size_t phighlight_pos, highlight_function_t phighlight_func) : string_to_highlight(pbuff), + colors(pbuff.size(), 0), match_highlight_pos(phighlight_pos), highlight_function(phighlight_func), vars(env_vars_snapshot_t::highlighting_keys), when(timef()), generation_count(s_generation_count) { - colors.resize(string_to_highlight.size(), 0); } int threaded_highlight() { - if (generation_count != s_generation_count) { + if (generation_count != s_generation_count) + { // The gen count has changed, so don't do anything return 0; } - if (! string_to_highlight.empty()) { - highlight_function( string_to_highlight.c_str(), colors, match_highlight_pos, NULL /* error */, vars); + if (! string_to_highlight.empty()) + { + highlight_function( string_to_highlight, colors, match_highlight_pos, NULL /* error */, vars); } return 0; } @@ -2262,10 +2262,8 @@ static void highlight_search(void) { const wchar_t *match = wcsstr( buff, data->search_buff.c_str() ); if( match ) { - int start = match-buff; - int count = data->search_buff.size(); - int i; - + size_t start = match-buff; + size_t i, count = data->search_buff.size(); for( i=0; icolors.at(start+i) |= HIGHLIGHT_SEARCH_MATCH<<16; @@ -2308,7 +2306,7 @@ static int threaded_highlight(background_highlight_context_t *ctx) { \param match_highlight_pos the position to use for bracket matching. This need not be the same as the surrent cursor position \param error if non-null, any possible errors in the buffer are further descibed by the strings inserted into the specified arraylist */ -static void reader_super_highlight_me_plenty( int match_highlight_pos ) +static void reader_super_highlight_me_plenty( size_t match_highlight_pos ) { reader_sanity_check(); @@ -2724,7 +2722,6 @@ const wchar_t *reader_readline() const wchar_t *buff = data->command_line.c_str(); const wchar_t *begin = &buff[data->buff_pos]; const wchar_t *end = begin; - long len; while( *end && *end != L'\n' ) end++; @@ -2732,8 +2729,7 @@ const wchar_t *reader_readline() if( end==begin && *end ) end++; - len = end-begin; - + size_t len = end-begin; if( len ) { reader_kill( begin - buff, len, KILL_APPEND, last_char!=R_KILL_LINE ); @@ -2749,7 +2745,6 @@ const wchar_t *reader_readline() const wchar_t *buff = data->command_line.c_str(); const wchar_t *end = &buff[data->buff_pos]; const wchar_t *begin = end; - long len; while( begin > buff && *begin != L'\n' ) begin--; @@ -2757,7 +2752,7 @@ const wchar_t *reader_readline() if( *begin == L'\n' ) begin++; - len = maxi( end-begin, 1L ); + size_t len = maxi( end-begin, 1L ); begin = end - len; reader_kill( begin - buff, len, KILL_PREPEND, last_char!=R_BACKWARD_KILL_LINE ); @@ -2772,7 +2767,7 @@ const wchar_t *reader_readline() const wchar_t *buff = data->command_line.c_str(); const wchar_t *end = &buff[data->buff_pos]; const wchar_t *begin = end; - int len; + size_t len; while( begin > buff && *begin != L'\n' ) begin--; diff --git a/reader.h b/reader.h index 846e0a0ab..a86f3d1b9 100644 --- a/reader.h +++ b/reader.h @@ -147,7 +147,7 @@ void reader_set_complete_function( complete_function_t ); The type of a highlight function. */ class env_vars_snapshot_t; -typedef void (*highlight_function_t)( const wcstring &, std::vector &, int, wcstring_list_t *, const env_vars_snapshot_t &vars ); +typedef void (*highlight_function_t)( const wcstring &, std::vector &, size_t, wcstring_list_t *, const env_vars_snapshot_t &vars ); /** Specify function for syntax highlighting. The function must take these arguments: