/** * Split right after the chunk */ static void split_before_chunk(chunk_t *pc) { LOG_FMT(LSPLIT, "%s: %s\n", __func__, pc->str.c_str()); if (!chunk_is_newline(pc) && !chunk_is_newline(chunk_get_prev(pc))) { newline_add_before(pc); reindent_line(pc, pc->brace_level * cpd.settings[UO_indent_columns].n); cpd.changes++; } }
/** * Split right after the chunk */ static void split_before_chunk(chunk_t *pc) { LOG_FMT(LSPLIT, "%s: %s\n", __func__, pc->str.c_str()); if (!chunk_is_newline(pc) && !chunk_is_newline(chunk_get_prev(pc))) { newline_add_before(pc); // reindent needs to include the indent_continue value and was off by one reindent_line(pc, pc->brace_level * cpd.settings[UO_indent_columns].n + abs(cpd.settings[UO_indent_continue].n) + 1); cpd.changes++; } }
static void split_template(chunk_t *start) { LOG_FUNC_ENTRY(); LOG_FMT(LSPLIT, " %s(%d): start %s\n", __func__, __LINE__, start->text()); LOG_FMT(LSPLIT, " %s(%d): back up until the prev is a comma\n", __func__, __LINE__); // back up until the prev is a comma chunk_t *prev = start; while ((prev = chunk_get_prev(prev)) != nullptr) { LOG_FMT(LSPLIT, " %s(%d): prev '%s'\n", __func__, __LINE__, prev->text()); if (chunk_is_newline(prev) || prev->type == CT_COMMA) { break; } } if (prev != nullptr && !chunk_is_newline(prev)) { LOG_FMT(LSPLIT, " %s(%d):", __func__, __LINE__); LOG_FMT(LSPLIT, " -- ended on [%s] --\n", get_token_name(prev->type)); chunk_t *pc = chunk_get_next(prev); newline_add_before(pc); size_t min_col = 1; 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); } reindent_line(pc, min_col); cpd.changes++; } } // split_templatefcn_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
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
static chunk_t *mod_case_brace_add(chunk_t *cl_colon) { LOG_FUNC_ENTRY(); chunk_t *pc = cl_colon; chunk_t *last = NULL; chunk_t *next = chunk_get_next_ncnl(cl_colon, CNAV_PREPROC); LOG_FMT(LMCB, "%s: line %zu", __func__, pc->orig_line); while ((pc = chunk_get_next_ncnl(pc, CNAV_PREPROC)) != NULL) { if (pc->level < cl_colon->level) { LOG_FMT(LMCB, " - level drop\n"); return(next); } if ((pc->level == cl_colon->level) && ((pc->type == CT_CASE) || (pc->type == CT_BREAK))) { last = pc; //if (pc->type == CT_BREAK) //{ // /* Step past the semicolon */ // last = chunk_get_next_ncnl(chunk_get_next_ncnl(last)); //} break; } } if (last == NULL) { LOG_FMT(LMCB, " - NULL last\n"); return(next); } LOG_FMT(LMCB, " - adding before '%s' on line %zu\n", last->text(), last->orig_line); chunk_t chunk; chunk.type = CT_BRACE_OPEN; chunk.orig_line = cl_colon->orig_line; chunk.parent_type = CT_CASE; chunk.level = cl_colon->level; chunk.brace_level = cl_colon->brace_level; chunk.flags = pc->flags & PCF_COPY_FLAGS; chunk.str = "{"; chunk_t *br_open = chunk_add_after(&chunk, cl_colon); chunk.type = CT_BRACE_CLOSE; chunk.orig_line = last->orig_line; chunk.str = "}"; chunk_t *br_close = chunk_add_before(&chunk, last); newline_add_before(last); for (pc = chunk_get_next(br_open, CNAV_PREPROC); pc != br_close; pc = chunk_get_next(pc, CNAV_PREPROC)) { pc->level++; pc->brace_level++; } return(br_open); } // mod_case_brace_add