/** * Figures out where to split a function def/proto/call * * For fcn protos and defs. Also fcn calls where level == brace_level: * - find the open fparen * + if it doesn't have a newline right after it * * see if all parameters will fit individually after the paren * * if not, throw a newline after the open paren & return * - scan backwards to the open fparen or comma * + if there isn't a newline after that item, add one & return * + otherwise, add a newline before the start token * * @param start the offending token * @return the token that should have a newline * inserted before it */ static void split_fcn_params(chunk_t *start) { LOG_FMT(LSPLIT, " %s: ", __func__); chunk_t *next; chunk_t *prev; chunk_t *fpo; chunk_t *pc; /* Find the opening fparen */ fpo = start; while (((fpo = chunk_get_prev(fpo)) != NULL) && (fpo->type != CT_FPAREN_OPEN)) { /* do nothing */ } pc = chunk_get_next(fpo); if (!chunk_is_newline(pc)) { int min_col = pc->column; int max_width = 0; int cur_width = 0; int last_col = -1; LOG_FMT(LSPLIT, " mincol=%d, max_width=%d ", min_col, cpd.settings[UO_code_width].n - min_col); while (pc != NULL) { if (chunk_is_newline(pc)) { last_col = -1; } else { if (last_col < 0) { last_col = pc->column; } cur_width += (pc->column - last_col) + pc->len(); last_col = pc->column + pc->len(); if ((pc->type == CT_COMMA) || (pc->type == CT_FPAREN_CLOSE)) { cur_width--; LOG_FMT(LSPLIT, " width=%d ", cur_width); if (cur_width > max_width) { max_width = cur_width; if ((max_width + min_col) > cpd.settings[UO_code_width].n) { break; } } cur_width = 0; last_col = -1; if (pc->type == CT_FPAREN_CLOSE) { break; } } } pc = chunk_get_next(pc); } // don't split function w/o parameters next = chunk_get_next(fpo); if (((max_width + min_col) > cpd.settings[UO_code_width].n) && next->type != CT_FPAREN_CLOSE) { LOG_FMT(LSPLIT, " - A param won't fit, nl after open paren."); split_before_chunk(chunk_get_next(fpo)); return; } } /* back up until the prev is a comma */ prev = pc; while ((prev = chunk_get_prev(prev)) != NULL) { if (chunk_is_newline(prev) || (prev->type == CT_COMMA)) { break; } if (prev->type == CT_FPAREN_OPEN) { /* Don't split "()" */ pc = chunk_get_next(prev); if (pc->type != c_token_t(prev->type + 1)) { break; } } } if (prev != NULL) { LOG_FMT(LSPLIT, " -- ended on [%s] -- ", get_token_name(prev->type)); } if (prev != NULL) { split_before_chunk(chunk_get_next(prev)); } }
static void split_fcn_params(chunk_t *start) { LOG_FUNC_ENTRY(); LOG_FMT(LSPLIT, "%s(%d): '%s'\n", __func__, __LINE__, start->text()); // Find the opening function parenthesis chunk_t *fpo = start; LOG_FMT(LSPLIT, "%s(%d): Find the opening function parenthesis\n", __func__, __LINE__); while ( ((fpo = chunk_get_prev(fpo)) != nullptr) && fpo->type != CT_FPAREN_OPEN) { // do nothing LOG_FMT(LSPLIT, "%s(%d): '%s', orig_col is %zu, level is %zu\n", __func__, __LINE__, fpo->text(), fpo->orig_col, fpo->level); } chunk_t *pc = chunk_get_next_ncnl(fpo); size_t min_col = pc->column; LOG_FMT(LSPLIT, " mincol is %zu, max_width is %zu\n", min_col, cpd.settings[UO_code_width].u - min_col); int cur_width = 0; int last_col = -1; LOG_FMT(LSPLIT, "%s(%d):look forward until CT_COMMA or CT_FPAREN_CLOSE\n", __func__, __LINE__); while (pc != nullptr) { LOG_FMT(LSPLIT, "%s(%d): pc->text() '%s', type is %s\n", __func__, __LINE__, pc->text(), get_token_name(pc->type)); if (chunk_is_newline(pc)) { cur_width = 0; last_col = -1; } else { if (last_col < 0) { last_col = pc->column; LOG_FMT(LSPLIT, "%s(%d): last_col is %d\n", __func__, __LINE__, last_col); } cur_width += (pc->column - last_col) + pc->len(); last_col = pc->column + pc->len(); LOG_FMT(LSPLIT, "%s(%d): last_col is %d\n", __func__, __LINE__, last_col); if (pc->type == CT_COMMA || pc->type == CT_FPAREN_CLOSE) { cur_width--; LOG_FMT(LSPLIT, "%s(%d): cur_width is %d\n", __func__, __LINE__, cur_width); if ( ((last_col - 1) > static_cast<int>(cpd.settings[UO_code_width].u)) || pc->type == CT_FPAREN_CLOSE) { break; } } } pc = chunk_get_next(pc); } // back up until the prev is a comma chunk_t *prev = pc; LOG_FMT(LSPLIT, " %s(%d): back up until the prev is a comma\n", __func__, __LINE__); while ((prev = chunk_get_prev(prev)) != nullptr) { LOG_FMT(LSPLIT, "%s(%d): pc '%s', pc->level is %zu, prev '%s', prev->type is %s\n", __func__, __LINE__, pc->text(), pc->level, prev->text(), get_token_name(prev->type)); if (chunk_is_newline(prev) || prev->type == CT_COMMA) { LOG_FMT(LSPLIT, "%s(%d): found at %zu\n", __func__, __LINE__, prev->orig_col); break; } LOG_FMT(LSPLIT, "%s(%d): last_col is %d\n", __func__, __LINE__, last_col); last_col -= pc->len(); LOG_FMT(LSPLIT, "%s(%d): last_col is %d\n", __func__, __LINE__, last_col); if (prev->type == CT_FPAREN_OPEN) { pc = chunk_get_next(prev); if (!cpd.settings[UO_indent_paren_nl].b) { min_col = pc->brace_level * cpd.settings[UO_indent_columns].u + 1; LOG_FMT(LSPLIT, "%s(%d): min_col is %zu\n", __func__, __LINE__, min_col); if (cpd.settings[UO_indent_continue].n == 0) { min_col += cpd.settings[UO_indent_columns].u; } else { min_col += abs(cpd.settings[UO_indent_continue].n); } LOG_FMT(LSPLIT, "%s(%d): min_col is %zu\n", __func__, __LINE__, min_col); } // Don't split "()" if (pc->type != c_token_t(prev->type + 1)) { break; } } } if (prev != nullptr && !chunk_is_newline(prev)) { LOG_FMT(LSPLIT, "%s(%d): -- ended on [%s] --\n", __func__, __LINE__, get_token_name(prev->type)); LOG_FMT(LSPLIT, "%s(%d): min_col is %zu\n", __func__, __LINE__, min_col); pc = chunk_get_next(prev); newline_add_before(pc); reindent_line(pc, min_col); cpd.changes++; } } // split_fcn_params
/** * Figures out where to split a function def/proto/call * * For fcn protos and defs. Also fcn calls where level == brace_level: * - find the open fparen * + if it doesn't have a newline right after it * * see if all parameters will fit individually after the paren * * if not, throw a newline after the open paren & return * - scan backwards to the open fparen or comma * + if there isn't a newline after that item, add one & return * + otherwise, add a newline before the start token * * @param start the offending token * @return the token that should have a newline * inserted before it */ static void split_fcn_params(chunk_t *start) { LOG_FUNC_ENTRY(); LOG_FMT(LSPLIT, " %s: ", __func__); chunk_t *prev; chunk_t *fpo; chunk_t *pc; int cur_width = 0; int last_col = -1; int min_col; /* Find the opening fparen */ fpo = start; while (((fpo = chunk_get_prev(fpo)) != NULL) && (fpo->type != CT_FPAREN_OPEN)) { /* do nothing */ } pc = chunk_get_next_ncnl(fpo); min_col = pc->column; LOG_FMT(LSPLIT, " mincol=%d, max_width=%d ", min_col, cpd.settings[UO_code_width].n - min_col); while (pc != NULL) { if (chunk_is_newline(pc)) { cur_width = 0; last_col = -1; } else { if (last_col < 0) { last_col = pc->column; } cur_width += (pc->column - last_col) + pc->len(); last_col = pc->column + pc->len(); if ((pc->type == CT_COMMA) || (pc->type == CT_FPAREN_CLOSE)) { cur_width--; LOG_FMT(LSPLIT, " width=%d ", cur_width); if (((last_col - 1) > cpd.settings[UO_code_width].n) || (pc->type == CT_FPAREN_CLOSE)) { break; } } } pc = chunk_get_next(pc); } /* back up until the prev is a comma */ prev = pc; while ((prev = chunk_get_prev(prev)) != NULL) { if (chunk_is_newline(prev) || (prev->type == CT_COMMA)) { break; } last_col -= pc->len(); if (prev->type == CT_FPAREN_OPEN) { pc = chunk_get_next(prev); if (!cpd.settings[UO_indent_paren_nl].b) { min_col = pc->brace_level * cpd.settings[UO_indent_columns].n + 1; if (cpd.settings[UO_indent_continue].n == 0) { min_col += cpd.settings[UO_indent_columns].n; } else { min_col += abs(cpd.settings[UO_indent_continue].n); } } /* Don't split "()" */ if (pc->type != c_token_t(prev->type + 1)) { break; } } } if ((prev != NULL) && !chunk_is_newline(prev)) { LOG_FMT(LSPLIT, " -- ended on [%s] --\n", get_token_name(prev->type)); pc = chunk_get_next(prev); newline_add_before(pc); reindent_line(pc, min_col); cpd.changes++; } } // split_fcn_params
void do_braces(void) { LOG_FUNC_ENTRY(); if (cpd.settings[UO_mod_full_brace_if_chain].b || cpd.settings[UO_mod_full_brace_if_chain_only].b) { mod_full_brace_if_chain(); } if ((cpd.settings[UO_mod_full_brace_if].a | cpd.settings[UO_mod_full_brace_do].a | cpd.settings[UO_mod_full_brace_for].a | cpd.settings[UO_mod_full_brace_using].a | cpd.settings[UO_mod_full_brace_while].a) & AV_REMOVE) { examine_braces(); } /* convert vbraces if needed */ if ((cpd.settings[UO_mod_full_brace_if].a | cpd.settings[UO_mod_full_brace_do].a | cpd.settings[UO_mod_full_brace_for].a | cpd.settings[UO_mod_full_brace_function].a | cpd.settings[UO_mod_full_brace_using].a | cpd.settings[UO_mod_full_brace_while].a) & AV_ADD) { convert_vbrace_to_brace(); } /* Mark one-liners */ chunk_t *pc = chunk_get_head(); while ((pc = chunk_get_next_ncnl(pc)) != NULL) { if ((pc->type != CT_BRACE_OPEN) && (pc->type != CT_VBRACE_OPEN)) { continue; } chunk_t *br_open = pc; c_token_t brc_type = c_token_t(pc->type + 1); /* Detect empty bodies */ chunk_t *tmp = chunk_get_next_ncnl(pc); if ((tmp != NULL) && (tmp->type == brc_type)) { chunk_flags_set(br_open, PCF_EMPTY_BODY); chunk_flags_set(tmp, PCF_EMPTY_BODY); } /* Scan for the brace close or a newline */ tmp = br_open; while ((tmp = chunk_get_next_nc(tmp)) != NULL) { if (chunk_is_newline(tmp)) { break; } if ((tmp->type == brc_type) && (br_open->level == tmp->level)) { flag_series(br_open, tmp, PCF_ONE_LINER); break; } } } if (cpd.settings[UO_mod_case_brace].a != AV_IGNORE) { mod_case_brace(); } if (cpd.settings[UO_mod_move_case_break].b) { move_case_break(); } } // do_braces