From: Nicholas Nethercote Date: Tue, 28 Mar 2023 06:22:42 +0000 (+1100) Subject: cg_annotate.in: fix a small bug in the printing of past-the-end lines. X-Git-Tag: VALGRIND_3_21_0~90 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6436be0a3ff05e72c097dab960347726c32c8890;p=thirdparty%2Fvalgrind.git cg_annotate.in: fix a small bug in the printing of past-the-end lines. --- diff --git a/cachegrind/cg_annotate.in b/cachegrind/cg_annotate.in index 240e069ccb..2ebd93911b 100755 --- a/cachegrind/cg_annotate.in +++ b/cachegrind/cg_annotate.in @@ -686,18 +686,17 @@ def print_annotated_src_file( pairs.append((lo, hi)) i += 1 - # Annotate chosen lines, tracking total annotated counts. - line_num = 0 - if pairs: + def print_lines(pairs: list[tuple[int, int]]) -> None: + line_num = 0 while pairs: + src_line = "" (lo, hi) = pairs.pop(0) while line_num < lo - 1: - tmp = src_file.readline() + src_line = src_file.readline() line_num += 1 - if not tmp: - break # EOF + if not src_line: + return # EOF - src_line = "" # Print line number, unless start of file. if lo != 1: print("-- line", lo, "-" * 40) @@ -706,7 +705,7 @@ def print_annotated_src_file( src_line = src_file.readline() line_num += 1 if not src_line: - break + return # EOF if line_nums and line_num == line_nums[0]: printer.print_cc(dict_line_cc[line_num], src_line[:-1]) annotated_ccs.line_nums_known_cc += dict_line_cc[line_num] @@ -714,14 +713,16 @@ def print_annotated_src_file( else: printer.print_missing_cc(src_line[:-1]) - # Print line number, unless EOF. - if src_line: - print("-- line", hi, "-" * 40) - else: - break + # Print line number. + print("-- line", hi, "-" * 40) + + # Annotate chosen lines, tracking total annotated counts. + if pairs: + print_lines(pairs) # If there was info on lines past the end of the file, warn. if line_nums: + print() for line_num in line_nums: printer.print_cc(dict_line_cc[line_num], f"") annotated_ccs.line_nums_known_cc += dict_line_cc[line_num] diff --git a/cachegrind/tests/ann2.post.exp b/cachegrind/tests/ann2.post.exp index 315e13474d..0e85dcb556 100644 --- a/cachegrind/tests/ann2.post.exp +++ b/cachegrind/tests/ann2.post.exp @@ -126,7 +126,7 @@ A SomeCount VeryLongEventName . . . two . . . three -- line 3 ---------------------------------------- --- line 18 ---------------------------------------- + 300 (0.3%) 100 (0.1%) 0 300 (0.3%) 100 (0.1%) 0 200 (0.2%) 0 -1,000 (n/a)