/* * See also it's preprocessor counterpart * add_long_preprocessor_conditional_block_comment * in defines.cpp */ void add_long_closebrace_comment(void) { chunk_t *pc; chunk_t *tmp; chunk_t *br_open; chunk_t *br_close; chunk_t *fcn_pc = NULL; chunk_t *sw_pc = NULL; int nl_count; pc = chunk_get_head(); while ((pc = chunk_get_next_ncnl(pc)) != NULL) { if ((pc->type == CT_FUNC_DEF) || (pc->type == CT_OC_MSG_DECL)) { fcn_pc = pc; } else if (pc->type == CT_SWITCH) { /* kinda pointless, since it always has the text "switch" */ sw_pc = pc; } if ((pc->type != CT_BRACE_OPEN) || ((pc->flags & PCF_IN_PREPROC) != 0)) { continue; } br_open = pc; nl_count = 0; tmp = pc; while ((tmp = chunk_get_next(tmp)) != NULL) { if (chunk_is_newline(tmp)) { nl_count += tmp->nl_count; } else if ((tmp->level == br_open->level) && (tmp->type == CT_BRACE_CLOSE)) { br_close = tmp; //LOG_FMT(LSYS, "found brace pair on lines %d and %d, nl_count=%d\n", // br_open->orig_line, br_close->orig_line, nl_count); /* Found the matching close brace - make sure a newline is next */ tmp = chunk_get_next(tmp); if ((tmp == NULL) || chunk_is_newline(tmp)) { int nl_min = 0; chunk_t *tag_pc = NULL; if (br_open->parent_type == CT_SWITCH) { nl_min = cpd.settings[UO_mod_add_long_switch_closebrace_comment].n; tag_pc = sw_pc; } else if ((br_open->parent_type == CT_FUNC_DEF) || (br_open->parent_type == CT_OC_MSG_DECL)) { nl_min = cpd.settings[UO_mod_add_long_function_closebrace_comment].n; tag_pc = fcn_pc; } if ((nl_min > 0) && (nl_count >= nl_min) && (tag_pc != NULL)) { /* determine the added comment style */ c_token_t style = (cpd.lang_flags & (LANG_CPP | LANG_CS)) ? CT_COMMENT_CPP : CT_COMMENT; /* Add a comment after the close brace */ insert_comment_after(br_close, style, tag_pc->str); } } break; } } } }
/* * See also it's preprocessor counterpart * add_long_preprocessor_conditional_block_comment * in defines.cpp */ void add_long_closebrace_comment(void) { LOG_FUNC_ENTRY(); chunk_t *br_close; chunk_t *fcn_pc = NULL; chunk_t *sw_pc = NULL; chunk_t *ns_pc = NULL; chunk_t *cl_pc = NULL; chunk_t *cl_semi_pc = NULL; unc_text xstr; for (chunk_t *pc = chunk_get_head(); pc; pc = chunk_get_next_ncnl(pc)) { if ((pc->type == CT_FUNC_DEF) || (pc->type == CT_OC_MSG_DECL)) { fcn_pc = pc; } else if (pc->type == CT_SWITCH) { /* kinda pointless, since it always has the text "switch" */ sw_pc = pc; } else if (pc->type == CT_NAMESPACE) { ns_pc = pc; } else if (pc->type == CT_CLASS) { cl_pc = pc; } if ((pc->type != CT_BRACE_OPEN) || (pc->flags & PCF_IN_PREPROC)) { continue; } chunk_t *br_open = pc; size_t nl_count = 0; chunk_t *tmp = pc; while ((tmp = chunk_get_next(tmp)) != NULL) { if (chunk_is_newline(tmp)) { nl_count += tmp->nl_count; } else if ((tmp->level == br_open->level) && (tmp->type == CT_BRACE_CLOSE)) { br_close = tmp; //LOG_FMT(LSYS, "found brace pair on lines %d and %d, nl_count=%d\n", // br_open->orig_line, br_close->orig_line, nl_count); /* Found the matching close brace - make sure a newline is next */ tmp = chunk_get_next(tmp); // Check for end of class if (tmp != NULL && tmp->parent_type == CT_CLASS && tmp->type == CT_SEMICOLON) { cl_semi_pc = tmp; tmp = chunk_get_next(tmp); if (tmp != NULL && !chunk_is_newline(tmp)) { tmp = cl_semi_pc; cl_semi_pc = NULL; } } if ((tmp == NULL) || chunk_is_newline(tmp)) { size_t nl_min = 0; chunk_t *tag_pc = NULL; if (br_open->parent_type == CT_SWITCH) { nl_min = cpd.settings[UO_mod_add_long_switch_closebrace_comment].u; tag_pc = sw_pc; xstr = sw_pc ? sw_pc->str : NULL; /* \todo NULL is no unc_text structure */ } else if ((br_open->parent_type == CT_FUNC_DEF) || (br_open->parent_type == CT_OC_MSG_DECL)) { nl_min = cpd.settings[UO_mod_add_long_function_closebrace_comment].u; // 76006 Explicit null dereferenced, 2016-03-17 tag_pc = fcn_pc; xstr.clear(); append_tag_name(xstr, tag_pc); } else if (br_open->parent_type == CT_NAMESPACE) { nl_min = cpd.settings[UO_mod_add_long_namespace_closebrace_comment].u; // 76007 Explicit null dereferenced, 2016-03-17 tag_pc = ns_pc; /* obtain the next chunck, normally this is the name of the namespace * and append it to generate "namespace xyz" */ xstr = ns_pc->str; xstr.append(" "); append_tag_name(xstr, chunk_get_next(ns_pc)); } else if (br_open->parent_type == CT_CLASS && cl_semi_pc && cl_pc) { nl_min = cpd.settings[UO_mod_add_long_class_closebrace_comment].u; tag_pc = cl_pc; xstr = tag_pc->str; xstr.append(" "); append_tag_name(xstr, chunk_get_next(cl_pc)); br_close = cl_semi_pc; cl_semi_pc = NULL; cl_pc = NULL; } if ((nl_min > 0) && (nl_count >= nl_min) && (tag_pc != NULL)) { /* determine the added comment style */ c_token_t style = (cpd.lang_flags & (LANG_CPP | LANG_CS)) ? CT_COMMENT_CPP : CT_COMMENT; /* Add a comment after the close brace */ insert_comment_after(br_close, style, xstr); } } break; } } } } // add_long_closebrace_comment
/* * See also it's preprocessor counterpart * add_long_closebrace_comment * in braces.cpp * * Note: since this concerns itself with the preprocessor -- which is line-oriented -- * it turns out that just looking at pc->pp_level is NOT the right thing to do. * See a --parsed dump if you don't believe this: an '#endif' will be one level * UP from the corresponding #ifdef when you look at the tokens 'ifdef' versus 'endif', * but it's a whole another story when you look at their CT_PREPROC ('#') tokens! * * Hence we need to track and seek matching CT_PREPROC pp_levels here, which complicates * things a little bit, but not much. */ void add_long_preprocessor_conditional_block_comment(void) { chunk_t *pc; chunk_t *tmp; chunk_t *br_open; chunk_t *br_close; chunk_t *pp_start = NULL; chunk_t *pp_end = NULL; int nl_count; for (pc = chunk_get_head(); pc; pc = chunk_get_next_ncnl(pc)) { /* just track the preproc level: */ if (pc->type == CT_PREPROC) { pp_end = pp_start = pc; } if (pc->type != CT_PP_IF) { continue; } #if 0 if ((pc->flags & PCF_IN_PREPROC) != 0) { continue; } #endif br_open = pc; nl_count = 0; tmp = pc; while ((tmp = chunk_get_next(tmp)) != NULL) { /* just track the preproc level: */ if (tmp->type == CT_PREPROC) { pp_end = tmp; } if (chunk_is_newline(tmp)) { nl_count += tmp->nl_count; } else if ((pp_end->pp_level == pp_start->pp_level) && ((tmp->type == CT_PP_ENDIF) || (br_open->type == CT_PP_IF ? tmp->type == CT_PP_ELSE : 0))) { br_close = tmp; LOG_FMT(LPPIF, "found #if / %s section on lines %d and %d, nl_count=%d\n", (tmp->type == CT_PP_ENDIF ? "#endif" : "#else"), br_open->orig_line, br_close->orig_line, nl_count); /* Found the matching #else or #endif - make sure a newline is next */ tmp = chunk_get_next(tmp); LOG_FMT(LPPIF, "next item type %d (is %s)\n", (tmp ? tmp->type : -1), (tmp ? chunk_is_newline(tmp) ? "newline" : chunk_is_comment(tmp) ? "comment" : "other" : "---")); if ((tmp == NULL) || (tmp->type == CT_NEWLINE) /* chunk_is_newline(tmp) */) { int nl_min; if (br_close->type == CT_PP_ENDIF) { nl_min = cpd.settings[UO_mod_add_long_ifdef_endif_comment].n; } else { nl_min = cpd.settings[UO_mod_add_long_ifdef_else_comment].n; } const char *txt = !tmp ? "EOF" : ((tmp->type == CT_PP_ENDIF) ? "#endif" : "#else"); LOG_FMT(LPPIF, "#if / %s section candidate for augmenting when over NL threshold %d != 0 (nl_count=%d)\n", txt, nl_min, nl_count); if ((nl_min > 0) && (nl_count > nl_min)) /* nl_count is 1 too large at all times as #if line was counted too */ { /* determine the added comment style */ c_token_t style = (cpd.lang_flags & (LANG_CPP | LANG_CS)) ? CT_COMMENT_CPP : CT_COMMENT; unc_text str; generate_if_conditional_as_text(str, br_open); LOG_FMT(LPPIF, "#if / %s section over threshold %d (nl_count=%d) --> insert comment after the %s: %s\n", txt, nl_min, nl_count, txt, str.c_str()); /* Add a comment after the close brace */ insert_comment_after(br_close, style, str); } } /* checks both the #else and #endif for a given level, only then look further in the main loop */ if (br_close->type == CT_PP_ENDIF) { break; } } } } }