From 9a740339a4d66349a3258efb03ef5dd3fff88a24 Mon Sep 17 00:00:00 2001 From: Guy Maurel Date: Wed, 18 Sep 2024 17:28:10 +0200 Subject: [PATCH] test the nl_ options --- src/newlines/brace_pair.cpp | 4 + src/newlines/cleanup.cpp | 4 +- src/newlines/eat_start_end.cpp | 2 + src/newlines/one_liner.cpp | 1 + src/nl_func_leave_one_liners.html | 13 ++ src/output.cpp | 213 ++++++++++-------- src/uncrustify.cpp | 5 + tests/cli/test_cli_options.py | 76 ++++--- .../nl/config/nl_func_leave_one_liners.cfg | 5 + .../nl/config/nl_start_of_file_min.cfg | 18 ++ .../nl/expected/nl_func_leave_one_liners.exp | 13 ++ .../nl/expected/nl_start_of_file_min.exp | 21 ++ tests/cli/tracking/nl/html/README.txt | 3 + .../nl/input/nl_func_leave_one_liners.cpp | 1 + .../nl/input/nl_start_of_file_min.cpp | 4 + tests/cli/tracking/tracking_liste_nl.txt | 3 + 16 files changed, 253 insertions(+), 133 deletions(-) create mode 100644 src/nl_func_leave_one_liners.html create mode 100644 tests/cli/tracking/nl/config/nl_func_leave_one_liners.cfg create mode 100644 tests/cli/tracking/nl/config/nl_start_of_file_min.cfg create mode 100644 tests/cli/tracking/nl/expected/nl_func_leave_one_liners.exp create mode 100644 tests/cli/tracking/nl/expected/nl_start_of_file_min.exp create mode 100644 tests/cli/tracking/nl/html/README.txt create mode 100644 tests/cli/tracking/nl/input/nl_func_leave_one_liners.cpp create mode 100644 tests/cli/tracking/nl/input/nl_start_of_file_min.cpp create mode 100644 tests/cli/tracking/tracking_liste_nl.txt diff --git a/src/newlines/brace_pair.cpp b/src/newlines/brace_pair.cpp index 556c26f1cc..5e5813289c 100644 --- a/src/newlines/brace_pair.cpp +++ b/src/newlines/brace_pair.cpp @@ -45,6 +45,8 @@ using namespace uncrustify; * void foo() { * } */ + + void newlines_brace_pair(Chunk *br_open) { LOG_FUNC_ENTRY(); @@ -313,6 +315,7 @@ void newlines_brace_pair(Chunk *br_open) { // Braces belong to a function definition log_rule_B("nl_collapse_empty_body_functions"); + log_ruleNL("nl_collapse_empty_body_functions", br_open); if (options::nl_collapse_empty_body_functions()) { @@ -323,6 +326,7 @@ void newlines_brace_pair(Chunk *br_open) else { log_rule_B("nl_collapse_empty_body"); + log_ruleNL("nl_collapse_empty_body", br_open); if (options::nl_collapse_empty_body()) { diff --git a/src/newlines/cleanup.cpp b/src/newlines/cleanup.cpp index 90c6209cba..0ae0ad5fbb 100644 --- a/src/newlines/cleanup.cpp +++ b/src/newlines/cleanup.cpp @@ -282,7 +282,7 @@ void newlines_cleanup_braces(bool first) if (pc->GetLevel() == pc->GetBraceLevel()) { log_rule_B("nl_class_brace"); - log_ruleNL("nl_class_brace", pc); // this is still a beta test + log_ruleNL("nl_class_brace", pc); newlines_do_else(pc->GetPrevNnl(), options::nl_class_brace()); } break; @@ -470,6 +470,7 @@ void newlines_cleanup_braces(bool first) && options::nl_type_brace_init_lst_open() == IARF_IGNORE && options::nl_type_brace_init_lst_close() == IARF_IGNORE)) { + log_ruleNL("nl_type_brace_init_lst_close", pc); newlines_brace_pair(pc); } @@ -556,6 +557,7 @@ void newlines_cleanup_braces(bool first) && options::nl_type_brace_init_lst_close() != IARF_IGNORE) { // Handle unnamed temporary direct-list-initialization + log_ruleNL("nl_after_brace_close", pc); newline_iarf_pair(pc->GetPrevNnl(), pc, options::nl_type_brace_init_lst_close(), true); } diff --git a/src/newlines/eat_start_end.cpp b/src/newlines/eat_start_end.cpp index 4c4d29a7d4..a8a5f7ec66 100644 --- a/src/newlines/eat_start_end.cpp +++ b/src/newlines/eat_start_end.cpp @@ -73,6 +73,8 @@ void newlines_eat_start_end() chunk.CopyAndAddBefore(pc); LOG_FMT(LNEWLINE, "%s(%d): %zu:%zu add newline before '%s'\n", __func__, __LINE__, pc->GetOrigLine(), pc->GetOrigCol(), pc->Text()); + Chunk *prev = pc->GetPrev(); + log_ruleNL("nl_start_of_file_min", prev); MARK_CHANGE(); } } diff --git a/src/newlines/one_liner.cpp b/src/newlines/one_liner.cpp index 311a251661..ea2321a17b 100644 --- a/src/newlines/one_liner.cpp +++ b/src/newlines/one_liner.cpp @@ -217,6 +217,7 @@ bool one_liner_nl_ok(Chunk *pc) return(false); } log_rule_B("nl_func_leave_one_liners"); + log_ruleNL("nl_func_leave_one_liners", pc); if ( options::nl_func_leave_one_liners() && ( pc->GetParentType() == CT_FUNC_DEF diff --git a/src/nl_func_leave_one_liners.html b/src/nl_func_leave_one_liners.html new file mode 100644 index 0000000000..4f7cf3a253 --- /dev/null +++ b/src/nl_func_leave_one_liners.html @@ -0,0 +1,13 @@ + + + + Uncrustify: where do the Newlines options work + + +

+

+
+int foo() {Mreturn 0; }
+
+ + diff --git a/src/output.cpp b/src/output.cpp index ba4bdf27ec..2820b18817 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -654,6 +654,109 @@ bool compareTrack(TrackNumber t1, TrackNumber t2) } +void DecodeTrackingData(Chunk *pc) +{ + if (pc->GetTrackingData() == nullptr) + { + return; + } + // insert the HTML code for the tracking + LOG_FMT(LGUY, "%s(%d): Text is %s, orig_line is %zu, column is %zu\n", + __func__, __LINE__, pc->Text(), pc->GetOrigLine(), pc->GetColumn()); + LOG_FMT(LGUY, " Tracking info are: \n"); + LOG_FMT(LGUY, " number of track(s) %zu\n", pc->GetTrackingData()->size()); + // is sorting necessary? + size_t many = pc->GetTrackingData()->size(); + + if (many > 1) // there is only one track + { +#ifdef EXTRA_LOG + LOG_FMT(LGUY, " tracking before\n"); + + // protocol before sort + for (size_t track = 0; track < pc->GetTrackingData()->size(); track++) + { + const TrackList *A = pc->GetTrackingData(); + const TrackNumber B = A->at(track); + size_t Bfirst = B.first; + char *Bsecond = B.second; + + LOG_FMT(LGUY, " %zu, tracking number is %zu\n", track, Bfirst); + LOG_FMT(LGUY, " %zu, rule is %s\n", track, Bsecond); + } +#endif + + if (options::debug_sort_the_tracks()) + { + TrackList *A1 = pc->TrackingData(); + sort(A1->begin(), A1->end(), compareTrack); + } + } + else + { + // sorting is not necessary + } +#ifdef EXTRA_LOG + LOG_FMT(LGUY, " tracking after\n"); + + // protocol ( after sort ) + for (size_t track = 0; track < pc->GetTrackingData()->size(); track++) + { + const TrackList *A = pc->GetTrackingData(); + const TrackNumber B = A->at(track); + size_t Bfirst = B.first; + char *Bsecond = B.second; + + LOG_FMT(LGUY, " %zu, tracking number is %zu\n", track, Bfirst); + LOG_FMT(LGUY, " %zu, rule is %s\n", track, Bsecond); + } +#endif + char *old_one = nullptr; + bool first_text = true; + char tempText[80]; + + add_text("GetTrackingData()->size(); track++) + { + const TrackList *A = pc->GetTrackingData(); + const TrackNumber B = A->at(track); + size_t Bfirst = B.first; + char *Bsecond = B.second; + + if ( old_one == nullptr + || strcmp(old_one, Bsecond) != 0) + { + // first time this option + if (old_one != nullptr) + { + // newline + add_text(" "); + add_text(Bsecond); + add_text(": "); + } + old_one = Bsecond; + + if (first_text) + { + snprintf(tempText, sizeof(tempText), "%s", Bsecond); + add_text(tempText); + add_text(": "); + first_text = false; + } + } + else + { + add_text(", "); + } + snprintf(tempText, sizeof(tempText), "%zu", Bfirst); + add_text(tempText); + } // for (size_t track = 0; track < pc->GetTrackingData()->size(); track++) + + add_text("\">M"); +} // DecodeTrackingData( + + void output_text(FILE *pfile) { // tracking_is_on is "false" if we have the standard output @@ -693,7 +796,15 @@ void output_text(FILE *pfile) add_text("\n"); add_text("\n"); add_text(" \n"); - add_text(" Uncrustify: where do the Spaces options work\n"); + + if (cpd.html_type == tracking_type_e::TT_SPACE) + { + add_text(" Uncrustify: where do the Spaces options work\n"); + } + else if (cpd.html_type == tracking_type_e::TT_NEWLINE) + { + add_text(" Uncrustify: where do the Newlines options work\n"); + } add_text("\n"); add_text("\n"); add_text("

\n"); @@ -727,6 +838,8 @@ void output_text(FILE *pfile) if (pc->Is(CT_NEWLINE)) { + DecodeTrackingData(pc); + for (size_t cnt = 0; cnt < pc->GetNlCount(); cnt++) { if ( cnt > 0 @@ -988,104 +1101,8 @@ void output_text(FILE *pfile) { add_text(pc->GetStr(), false, pc->Is(CT_STRING)); } - // insert the HTML code for the tracking - if (pc->GetTrackingData() != nullptr) - { - LOG_FMT(LGUY, "%s(%d): Text is %s, orig_line is %zu, column is %zu\n", - __func__, __LINE__, pc->Text(), pc->GetOrigLine(), pc->GetColumn()); - LOG_FMT(LGUY, " Tracking info are: \n"); - LOG_FMT(LGUY, " number of track(s) %zu\n", pc->GetTrackingData()->size()); - // is sorting necessary? - size_t many = pc->GetTrackingData()->size(); - - if (many > 1) // there is only one track - { -#ifdef EXTRA_LOG - LOG_FMT(LGUY, " tracking before\n"); - - // protocol before sort - for (size_t track = 0; track < pc->GetTrackingData()->size(); track++) - { - const TrackList *A = pc->GetTrackingData(); - const TrackNumber B = A->at(track); - size_t Bfirst = B.first; - char *Bsecond = B.second; - - LOG_FMT(LGUY, " %zu, tracking number is %zu\n", track, Bfirst); - LOG_FMT(LGUY, " %zu, rule is %s\n", track, Bsecond); - } -#endif - - if (options::debug_sort_the_tracks()) - { - TrackList *A1 = pc->TrackingData(); - sort(A1->begin(), A1->end(), compareTrack); - } - } - else - { - // sorting is not necessary - } -#ifdef EXTRA_LOG - LOG_FMT(LGUY, " tracking after\n"); - - // protocol ( after sort ) - for (size_t track = 0; track < pc->GetTrackingData()->size(); track++) - { - const TrackList *A = pc->GetTrackingData(); - const TrackNumber B = A->at(track); - size_t Bfirst = B.first; - char *Bsecond = B.second; - - LOG_FMT(LGUY, " %zu, tracking number is %zu\n", track, Bfirst); - LOG_FMT(LGUY, " %zu, rule is %s\n", track, Bsecond); - } -#endif - char *old_one = nullptr; - bool first_text = true; - char tempText[80]; - - add_text("GetTrackingData()->size(); track++) - { - const TrackList *A = pc->GetTrackingData(); - const TrackNumber B = A->at(track); - size_t Bfirst = B.first; - char *Bsecond = B.second; - - if ( old_one == nullptr - || strcmp(old_one, Bsecond) != 0) - { - // first time this option - if (old_one != nullptr) - { - // newline - add_text(" "); - add_text(Bsecond); - add_text(": "); - } - old_one = Bsecond; - - if (first_text) - { - snprintf(tempText, sizeof(tempText), "%s", Bsecond); - add_text(tempText); - add_text(": "); - first_text = false; - } - } - else - { - add_text(", "); - } - snprintf(tempText, sizeof(tempText), "%zu", Bfirst); - add_text(tempText); - } // for (size_t track = 0; track < pc->GetTrackingData()->size(); track++) - - add_text("\">M"); - } + DecodeTrackingData(pc); } else // standard output { diff --git a/src/uncrustify.cpp b/src/uncrustify.cpp index 21fc7aa46f..a7b16d19be 100644 --- a/src/uncrustify.cpp +++ b/src/uncrustify.cpp @@ -2417,8 +2417,13 @@ void uncrustify_file(const file_mem &fm, FILE *pfout, const char *parsed_file, __func__, cpd.html_file, strerror(errno), errno); exit(EX_IOERR); } + else + { + LOG_FMT(LDATA, "tracking is opened = %s\n", (cpd.html_file != nullptr) ? cpd.html_file : "null"); + } output_text(t_file); fclose(t_file); + LOG_FMT(LDATA, "tracking is closed = %s\n", (cpd.html_file != nullptr) ? cpd.html_file : "null"); exit(EX_OK); } diff --git a/tests/cli/test_cli_options.py b/tests/cli/test_cli_options.py index 34491ba740..4fde79b52a 100755 --- a/tests/cli/test_cli_options.py +++ b/tests/cli/test_cli_options.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 """ test_cli_options.py @@ -7,6 +7,7 @@ (excluding actual source code formatting) :author: DanielChumak +:author: GuyMaurel :license: GPL v2+ """ @@ -177,24 +178,14 @@ def check_generated_output(gen_expected_path, gen_result_path, True """ - #print("2-1: gen_expected_path is "+gen_expected_path) - #print("2-2: gen_result_path is "+gen_result_path) gen_exp_txt = get_file_content(gen_expected_path) - #print("2-3: gen_exp_txt is ") - #+gen_exp_txt) if gen_exp_txt is None: return False gen_res_txt = get_file_content(gen_result_path) - #print("2-4: gen_res_txt is "+gen_res_txt) if gen_res_txt is None: return False - #print("2: result_manip is "+result_manip) - #if result_manip is not None: - # print("2-5: result_manip is "+result_manip) - #else: - # print("2-6: result_manip is None") if result_manip is not None: if type(result_manip) is list or type(result_manip) is tuple: for m in result_manip: @@ -202,10 +193,6 @@ def check_generated_output(gen_expected_path, gen_result_path, else: gen_res_txt = result_manip(gen_res_txt) - #print("2-7: gen_res_txt is ") - #print(gen_res_txt) - #print("2-8: gen_exp_txt is ") - #print(gen_exp_txt) if gen_res_txt != gen_exp_txt: with open(gen_result_path, 'w', encoding="utf-8", newline="") as f: f.write(gen_res_txt) @@ -232,7 +219,6 @@ def check_generated_output(gen_expected_path, gen_result_path, return False remove(gen_result_path) - #print(abspath(gen_result_path)) return True @@ -380,6 +366,7 @@ def check_uncrustify_output( eprint("Set up 'gen_result_path' if 'gen_result_manip' is used") out_res_txt, err_res_txt = proc(uncr_bin, args_arr) + #print(err_res_txt) ret_flag = True @@ -403,13 +390,6 @@ def check_uncrustify_output( program_args=program_args): ret_flag = False - #print("1: gen_expected_path is "+gen_expected_path) - #print("1: gen_result_path is "+gen_result_path) - #if gen_result_manip is not None: - # print("1: gen_result_manip is "+gen_result_manip) - #else: - # print("1: gen_result_manip is None") - #print("program_args is "+program_args) if gen_expected_path and not check_generated_output( gen_expected_path, gen_result_path, result_manip=gen_result_manip, @@ -553,6 +533,45 @@ def main(args): return_flag = True + print("Test nl tracking ...") + # test option(s) nl tracking + + with open("tracking/tracking_liste_nl.txt") as file: + lines = [line.rstrip() for line in file] + file.close() + + for L in lines: + first_char = L[0] + if first_char == '#' : + continue + tr_input = "tracking/nl/input/" + L + ".cpp" + tr_config = "tracking/nl/config/" + L + ".cfg" + tr_expected = "tracking/nl/expected/" + L + ".exp" + tr_html_nl = "tracking/nl/html/" + L + ".html" + + p_config = s_path_join(script_dir, tr_config) + p_input = s_path_join(script_dir, tr_input) + p_expected = s_path_join(script_dir, tr_expected) + p_html = s_path_join(script_dir, tr_html_nl) + + return_value = check_uncrustify_output( + uncr_bin, + parsed_args, + args_arr=['-L', 'A', + '-c', p_config, + '-f', p_input, + '--tracking', "nl:" + p_html, + ], + gen_expected_path = tr_expected, + gen_result_path = p_html, + ) + + if not return_value: + sys_exit() + + print("Test nl tracking is OK") + #sys_exit() + print("Test too big ...") # test option(s) too big against the option 'nl_max' Ls_B = ['nl_var_def_blk_end_func_top', @@ -855,21 +874,10 @@ def main(args): '-f', s_path_join(script_dir, 'input/tracking_space.cpp'), '--tracking', abc, - #s_path_join(script_dir, 'results/Debug_tracking_space.html') - #temp_result_path - # '-L' - #, 'A' - #, '2' - #, '>' - #, '/home/guy/A-T.txt' ], gen_expected_path=s_path_join(script_dir, 'output/Debug_tracking_space.html'), - #gen_result_path=s_path_join(script_dir, 'results/Debug_tracking_space.html') gen_result_path=temp_result_path ): - #print("/home/guy/A-T.txt") - #else: - #print("/home/guy/A-T.txt") return_flag = False print("Test --tracking space:FILE is OK") diff --git a/tests/cli/tracking/nl/config/nl_func_leave_one_liners.cfg b/tests/cli/tracking/nl/config/nl_func_leave_one_liners.cfg new file mode 100644 index 0000000000..f47006d650 --- /dev/null +++ b/tests/cli/tracking/nl/config/nl_func_leave_one_liners.cfg @@ -0,0 +1,5 @@ +#(405)Don't split one-line function definitions, as in 'int foo() { return 0; }'. +# might modify nl_func_type_name +nl_func_leave_one_liners = true +indent_columns = 3 +indent_with_tabs = 0 diff --git a/tests/cli/tracking/nl/config/nl_start_of_file_min.cfg b/tests/cli/tracking/nl/config/nl_start_of_file_min.cfg new file mode 100644 index 0000000000..8767230789 --- /dev/null +++ b/tests/cli/tracking/nl/config/nl_start_of_file_min.cfg @@ -0,0 +1,18 @@ +nl_assign_brace = force +nl_enum_brace = force +nl_if_brace = force +nl_while_brace = force +nl_do_brace = force +nl_for_brace = force + +#nl_assign_leave_one_liners = true +nl_enum_leave_one_liners = true +nl_func_leave_one_liners = true +nl_if_leave_one_liners = true +nl_while_leave_one_liners = true +nl_do_leave_one_liners = true +nl_for_leave_one_liners = true +# +nl_start_of_file = force +nl_start_of_file_min = 5 + diff --git a/tests/cli/tracking/nl/expected/nl_func_leave_one_liners.exp b/tests/cli/tracking/nl/expected/nl_func_leave_one_liners.exp new file mode 100644 index 0000000000..4f7cf3a253 --- /dev/null +++ b/tests/cli/tracking/nl/expected/nl_func_leave_one_liners.exp @@ -0,0 +1,13 @@ + + + + Uncrustify: where do the Newlines options work + + +

+

+
+int foo() {Mreturn 0; }
+
+ + diff --git a/tests/cli/tracking/nl/expected/nl_start_of_file_min.exp b/tests/cli/tracking/nl/expected/nl_start_of_file_min.exp new file mode 100644 index 0000000000..eb4443ae0e --- /dev/null +++ b/tests/cli/tracking/nl/expected/nl_start_of_file_min.exp @@ -0,0 +1,21 @@ + + + + Uncrustify: where do the Newlines options work + + +

+

+
+M
+
+
+
+
+int main()
+{M
+	int a = 1;
+}
+
+ + diff --git a/tests/cli/tracking/nl/html/README.txt b/tests/cli/tracking/nl/html/README.txt new file mode 100644 index 0000000000..0c1be2eb60 --- /dev/null +++ b/tests/cli/tracking/nl/html/README.txt @@ -0,0 +1,3 @@ +This directory is used to save the results. +If the test of comparation is TRUE, the file(s) will be removed. +If the test of comparation is FALSE, the file(s) will be saved, making a loging possible. diff --git a/tests/cli/tracking/nl/input/nl_func_leave_one_liners.cpp b/tests/cli/tracking/nl/input/nl_func_leave_one_liners.cpp new file mode 100644 index 0000000000..9fe07f82f9 --- /dev/null +++ b/tests/cli/tracking/nl/input/nl_func_leave_one_liners.cpp @@ -0,0 +1 @@ +int foo() { return 0; } diff --git a/tests/cli/tracking/nl/input/nl_start_of_file_min.cpp b/tests/cli/tracking/nl/input/nl_start_of_file_min.cpp new file mode 100644 index 0000000000..7541900e3e --- /dev/null +++ b/tests/cli/tracking/nl/input/nl_start_of_file_min.cpp @@ -0,0 +1,4 @@ +int main() +{ +int a = 1; +} diff --git a/tests/cli/tracking/tracking_liste_nl.txt b/tests/cli/tracking/tracking_liste_nl.txt new file mode 100644 index 0000000000..69aebc62f0 --- /dev/null +++ b/tests/cli/tracking/tracking_liste_nl.txt @@ -0,0 +1,3 @@ +# is a comment line +nl_start_of_file_min +nl_func_leave_one_liners