From 4b33ad392a8ddf1cc480d29498af80f7da1db269 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Thu, 3 Mar 2016 22:05:24 +0200 Subject: Initial changes to improve end of line comments. --- profile.c | 55 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 16 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index 43875600..f55ad287 100644 --- a/profile.c +++ b/profile.c @@ -26,7 +26,7 @@ #include "awk.h" static void pprint(INSTRUCTION *startp, INSTRUCTION *endp, int flags); -static void end_line(INSTRUCTION *ip); +static INSTRUCTION *end_line(INSTRUCTION *ip); static void pp_parenthesize(NODE *n); static void parenthesize(int type, NODE *left, NODE *right); static char *pp_list(int nargs, const char *paren, const char *delim); @@ -207,6 +207,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, int flags) char *tmp; int rule; static int rule_count[MAXRULE]; + static bool skip_comment = false; for (pc = startp; pc != endp; pc = pc->nexti) { if (pc->source_line > 0) @@ -214,8 +215,13 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, int flags) /* skip leading EOL comment as it has already been printed */ if (pc->opcode == Op_comment - && pc->memory->comment_type == EOL_COMMENT) + && pc->memory->comment_type == EOL_COMMENT + && skip_comment) { + skip_comment = false; continue; + } + skip_comment = false; + switch (pc->opcode) { case Op_rule: /* @@ -246,6 +252,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, int flags) } fprintf(prof_fp, "%s {", ruletab[rule]); end_line(pc); + skip_comment = true; } else { if (do_profile && ! rule_count[rule]++) fprintf(prof_fp, _("\t# Rule(s)\n\n")); @@ -268,6 +275,7 @@ pprint(INSTRUCTION *startp, INSTRUCTION *endp, int flags) fprintf(prof_fp, " # %ld", ip1->exec_count); end_line(ip1); + skip_comment = true; } else { fprintf(prof_fp, "{\n"); ip1 = (pc + 1)->firsti; @@ -366,7 +374,7 @@ cleanup: pp_free(t2); pp_free(t1); if ((flags & IN_FOR_HEADER) == 0) - end_line(pc); + pc = end_line(pc); break; default: @@ -492,7 +500,7 @@ cleanup: pp_free(t2); pp_free(t1); if ((flags & IN_FOR_HEADER) == 0) - end_line(pc); + pc = end_line(pc); break; case Op_concat: @@ -513,7 +521,7 @@ cleanup: } else fprintf(prof_fp, "%s %s", op2str(Op_K_delete), array); if ((flags & IN_FOR_HEADER) == 0) - end_line(pc); + pc = end_line(pc); pp_free(t1); } break; @@ -625,7 +633,7 @@ cleanup: fprintf(prof_fp, "%s%s", op2str(pc->opcode), tmp); efree(tmp); if ((flags & IN_FOR_HEADER) == 0) - end_line(pc); + pc = end_line(pc); break; case Op_push_re: @@ -734,7 +742,8 @@ cleanup: case Op_K_break: case Op_K_nextfile: case Op_K_next: - fprintf(prof_fp, "%s\n", op2str(pc->opcode)); + fprintf(prof_fp, "%s", op2str(pc->opcode)); + pc = end_line(pc); break; case Op_K_return: @@ -742,8 +751,10 @@ cleanup: t1 = pp_pop(); if (is_binary(t1->type)) pp_parenthesize(t1); - if (pc->source_line > 0) /* don't print implicit 'return' at end of function */ - fprintf(prof_fp, "%s %s\n", op2str(pc->opcode), t1->pp_str); + if (pc->source_line > 0) { /* don't print implicit 'return' at end of function */ + fprintf(prof_fp, "%s %s", op2str(pc->opcode), t1->pp_str); + pc = end_line(pc); + } pp_free(t1); break; @@ -751,7 +762,7 @@ cleanup: t1 = pp_pop(); fprintf(prof_fp, "%s", t1->pp_str); if ((flags & IN_FOR_HEADER) == 0) - end_line(pc); + pc = end_line(pc); pp_free(t1); break; @@ -878,10 +889,13 @@ cleanup: indent(pc->stmt_start->exec_count); if (pc->opcode == Op_K_case) { t1 = pp_pop(); - fprintf(prof_fp, "%s %s:\n", op2str(pc->opcode), t1->pp_str); + fprintf(prof_fp, "%s %s:", op2str(pc->opcode), t1->pp_str); + pc = end_line(pc); pp_free(t1); - } else - fprintf(prof_fp, "%s:\n", op2str(pc->opcode)); + } else { + fprintf(prof_fp, "%s:", op2str(pc->opcode)); + pc = end_line(pc); + } indent_in(); pprint(pc->stmt_start->nexti, pc->stmt_end->nexti, NO_PPRINT_FLAGS); indent_out(); @@ -996,17 +1010,21 @@ cleanup: /* end_line --- end pretty print line with new line or on-line comment */ -void +INSTRUCTION * end_line(INSTRUCTION *ip) { + INSTRUCTION *ret = ip; + if (ip->nexti->opcode == Op_comment && ip->nexti->memory->comment_type == EOL_COMMENT) { fprintf(prof_fp, "\t"); print_comment(ip->nexti, -1); - ip = ip->nexti->nexti; + ret = ip->nexti; } else fprintf(prof_fp, "\n"); + + return ret; } /* pp_string_fp --- printy print a string to the fp */ @@ -1736,7 +1754,12 @@ pp_func(INSTRUCTION *pc, void *data ATTRIBUTE_UNUSED) if (j < pcount - 1) fprintf(prof_fp, ", "); } - fprintf(prof_fp, ")\n"); + if (fp->opcode == Op_comment + && fp->memory->comment_type == EOL_COMMENT) { + fprintf(prof_fp, ")"); + fp = end_line(fp); + } else + fprintf(prof_fp, ")\n"); if (do_profile) indent(0); fprintf(prof_fp, "{\n"); -- cgit v1.2.3 From fe455fb8d3372fb0ced65b10216c3c30971acb81 Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sat, 5 Mar 2016 21:52:37 +0200 Subject: Improve end of line comments in if/else. --- profile.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index f55ad287..fd7e52f5 100644 --- a/profile.c +++ b/profile.c @@ -911,7 +911,7 @@ cleanup: ip1 = pc->branch_if; if (ip1->exec_count > 0) fprintf(prof_fp, " # %ld", ip1->exec_count); - end_line(pc); + ip1 = end_line(ip1); indent_in(); pprint(ip1->nexti, pc->branch_else, NO_PPRINT_FLAGS); indent_out(); @@ -947,7 +947,9 @@ cleanup: && pc->branch_end == pc->nexti->nexti->branch_else->lasti) { pprint(pc->nexti, pc->branch_end, IN_ELSE_IF); } else { - fprintf(prof_fp, "{\n"); + fprintf(prof_fp, "{"); + end_line(pc); + skip_comment = true; indent_in(); pprint(pc->nexti, pc->branch_end, NO_PPRINT_FLAGS); indent_out(); -- cgit v1.2.3 From b040b8f577a203efbd39be90d1554267c5bf15ba Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Thu, 17 Mar 2016 21:54:01 +0200 Subject: More improvements in comment printing. --- profile.c | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index fd7e52f5..531c6e47 100644 --- a/profile.c +++ b/profile.c @@ -785,28 +785,33 @@ cleanup: fprintf(prof_fp, "%s (", op2str(pc->opcode)); pprint(pc->nexti, ip1->while_body, NO_PPRINT_FLAGS); t1 = pp_pop(); - fprintf(prof_fp, "%s) {\n", t1->pp_str); + fprintf(prof_fp, "%s) {", t1->pp_str); pp_free(t1); + ip1->while_body = end_line(ip1->while_body); indent_in(); pprint(ip1->while_body->nexti, pc->target_break, NO_PPRINT_FLAGS); indent_out(); indent(SPACEOVER); - fprintf(prof_fp, "}\n"); - pc = pc->target_break; + fprintf(prof_fp, "}"); + pc = end_line(pc->target_break); break; case Op_K_do: ip1 = pc + 1; indent(pc->nexti->exec_count); - fprintf(prof_fp, "%s {\n", op2str(pc->opcode)); + fprintf(prof_fp, "%s {", op2str(pc->opcode)); + end_line(pc->nexti); + skip_comment = true; indent_in(); pprint(pc->nexti->nexti, ip1->doloop_cond, NO_PPRINT_FLAGS); indent_out(); pprint(ip1->doloop_cond, pc->target_break, NO_PPRINT_FLAGS); indent(SPACEOVER); t1 = pp_pop(); - fprintf(prof_fp, "} %s (%s)\n", op2str(Op_K_while), t1->pp_str); + fprintf(prof_fp, "} %s (%s)", op2str(Op_K_while), t1->pp_str); pp_free(t1); + end_line(pc->target_break); + skip_comment = true; pc = pc->target_break; break; @@ -836,12 +841,16 @@ cleanup: pprint(pc->target_continue, pc->target_break, IN_FOR_HEADER); } - fprintf(prof_fp, ") {\n"); + fprintf(prof_fp, ") {"); + end_line(ip1->forloop_body); + skip_comment = true; indent_in(); pprint(ip1->forloop_body->nexti, pc->target_continue, NO_PPRINT_FLAGS); indent_out(); indent(SPACEOVER); - fprintf(prof_fp, "}\n"); + fprintf(prof_fp, "}"); + end_line(pc->target_break); + skip_comment = true; pc = pc->target_break; break; @@ -859,14 +868,18 @@ cleanup: else item = m->vname; indent(ip1->forloop_body->exec_count); - fprintf(prof_fp, "%s (%s%s%s) {\n", op2str(Op_K_arrayfor), + fprintf(prof_fp, "%s (%s%s%s) {", op2str(Op_K_arrayfor), item, op2str(Op_in_array), array); + end_line(ip1->forloop_body); + skip_comment = true; indent_in(); pp_free(t1); pprint(ip1->forloop_body->nexti, pc->target_break, NO_PPRINT_FLAGS); indent_out(); indent(SPACEOVER); - fprintf(prof_fp, "}\n"); + fprintf(prof_fp, "}"); + end_line(pc->target_break); + skip_comment = true; pc = pc->target_break; } break; @@ -918,7 +931,10 @@ cleanup: pc = pc->branch_else; if (pc->nexti->opcode == Op_no_op) { /* no following else */ indent(SPACEOVER); - fprintf(prof_fp, "}\n"); + fprintf(prof_fp, "}"); +// pc = end_line(pc->nexti); + end_line(pc->nexti); + skip_comment = true; } /* * See next case; turn off the flag so that the @@ -954,8 +970,15 @@ cleanup: pprint(pc->nexti, pc->branch_end, NO_PPRINT_FLAGS); indent_out(); indent(SPACEOVER); - fprintf(prof_fp, "}\n"); + fprintf(prof_fp, "}"); + end_line(pc->branch_end); + skip_comment = true; } + /* + * Don't do end_line() here, we get multiple blank lines after + * the final else in a chain of else-ifs since they all point + * to the same branch_end. + */ pc = pc->branch_end; break; -- cgit v1.2.3 From 2fcbc7891d4440a30ff000ee39b0662f155a64ab Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Sat, 19 Mar 2016 22:01:49 +0200 Subject: More code improvements. Add ChangeLog. Update test. --- profile.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index 531c6e47..b6797192 100644 --- a/profile.c +++ b/profile.c @@ -932,9 +932,10 @@ cleanup: if (pc->nexti->opcode == Op_no_op) { /* no following else */ indent(SPACEOVER); fprintf(prof_fp, "}"); -// pc = end_line(pc->nexti); - end_line(pc->nexti); - skip_comment = true; + if (pc->nexti->nexti->opcode != Op_comment) + fprintf(prof_fp, "\n"); + /* else + It will be printed at the top. */ } /* * See next case; turn off the flag so that the -- cgit v1.2.3 From 3e92d06bebd53eba773749351641e3eaed7c117f Mon Sep 17 00:00:00 2001 From: "Arnold D. Robbins" Date: Mon, 21 Mar 2016 05:36:33 +0200 Subject: Improve pretty-printing comment after if statement. --- profile.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'profile.c') diff --git a/profile.c b/profile.c index b6797192..3eca03bd 100644 --- a/profile.c +++ b/profile.c @@ -932,7 +932,8 @@ cleanup: if (pc->nexti->opcode == Op_no_op) { /* no following else */ indent(SPACEOVER); fprintf(prof_fp, "}"); - if (pc->nexti->nexti->opcode != Op_comment) + if (pc->nexti->nexti->opcode != Op_comment + || pc->nexti->nexti->memory->comment_type == FULL_COMMENT) fprintf(prof_fp, "\n"); /* else It will be printed at the top. */ -- cgit v1.2.3