From dab7abbbee09bda73cb8295d581e289370e57959 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 22 Jun 2010 03:40:04 +0000 Subject: [PATCH] Allow negative numbers in the cachegrind.out.* file. Also protect against division-by-zero. Both are required for cg_diff (not yet committed) to work properly. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@11184 --- cachegrind/cg_annotate.in | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cachegrind/cg_annotate.in b/cachegrind/cg_annotate.in index 9d27fd5bea..e76c94a452 100644 --- a/cachegrind/cg_annotate.in +++ b/cachegrind/cg_annotate.in @@ -168,6 +168,12 @@ END # Used in various places of output. my $fancy = '-' x 80 . "\n"; +sub safe_div($$) +{ + my ($x, $y) = @_; + return ($y == 0 ? 0 : $x / $y); +} + #----------------------------------------------------------------------------- # Argument and option handling #----------------------------------------------------------------------------- @@ -385,7 +391,7 @@ sub read_input_file() # Read body of input file. while () { s/#.*$//; # remove comments - if (s/^(\d+)\s+//) { + if (s/^(-?\d+)\s+//) { my $lineNum = $1; my $CC = line_to_CC($_); defined($currFuncCC) || die; @@ -494,7 +500,7 @@ sub mycmp ($$) $x = -1 unless defined $x; $y = -1 unless defined $y; - my $cmp = $y <=> $x; # reverse sort + my $cmp = abs($y) <=> abs($x); # reverse sort of absolute size if (0 != $cmp) { return $cmp; } @@ -505,7 +511,7 @@ sub mycmp ($$) sub commify ($) { my ($val) = @_; - 1 while ($val =~ s/^(\d+)(\d{3})/$1,$2/); + 1 while ($val =~ s/^(-?\d+)(\d{3})/$1,$2/); return $val; } @@ -614,7 +620,8 @@ sub print_summary_and_fn_totals () # Stop when we've reached all the thresholds my $reached_all_thresholds = 1; foreach my $i (0 .. scalar @thresholds - 1) { - my $prop = $curr_totals[$i] * 100 / $summary_CC->[$sort_order[$i]]; + my $prop = safe_div(abs($curr_totals[$i] * 100), + abs($summary_CC->[$sort_order[$i]])); $reached_all_thresholds &&= ($prop >= $thresholds[$i]); } last if $reached_all_thresholds; @@ -874,7 +881,8 @@ sub annotate_ann_files($) foreach (my $i = 0; $i < @$summary_CC; $i++) { $percent_printed_CC->[$i] = sprintf("%.0f", - $printed_totals_CC->[$i] / $summary_CC->[$i] * 100); + safe_div(abs($printed_totals_CC->[$i]), + abs($summary_CC->[$i] * 100))); } my $pp_CC_col_widths = compute_CC_col_widths($percent_printed_CC); print($fancy); -- 2.47.2