| 1 | # | ||
| 2 | # $Id: Assert.pm,v 0.1 2001/03/31 10:04:36 ram Exp $ | ||
| 3 | # | ||
| 4 | # Copyright (c) 2000-2001, Christophe Dehaudt & Raphael Manfredi | ||
| 5 | # | ||
| 6 | # You may redistribute only under the terms of the Artistic License, | ||
| 7 | # as specified in the README file that comes with the distribution. | ||
| 8 | # | ||
| 9 | # HISTORY | ||
| 10 | # $Log: Assert.pm,v $ | ||
| 11 | # Revision 0.1 2001/03/31 10:04:36 ram | ||
| 12 | # Baseline for first Alpha release. | ||
| 13 | # | ||
| 14 | # $EndLog$ | ||
| 15 | # | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | |||
| 19 | package Carp::Datum::Assert; | ||
| 20 | |||
| 21 | require Exporter; | ||
| 22 | |||
| 23 | use vars qw(@ISA @EXPORT_OK); | ||
| 24 | @ISA = qw(Exporter); | ||
| 25 | |||
| 26 | @EXPORT_OK = qw(assert_expr stack_dump); | ||
| 27 | |||
| 28 | use Log::Agent; | ||
| 29 | |||
| 30 | # | ||
| 31 | # assert_expr | ||
| 32 | # | ||
| 33 | # Fetch corresponding assert expression by going back to the file where | ||
| 34 | # the failure occurred, and parsing it. This is very rough. | ||
| 35 | # | ||
| 36 | # Arguments: | ||
| 37 | # offset amount of frames to skip | ||
| 38 | # | ||
| 39 | # Returns assertion expression if found, undef otherwise. | ||
| 40 | # | ||
| 41 | sub assert_expr { | ||
| 42 | my ($offset) = @_; | ||
| 43 | my ($package, $file, $line) = caller($offset); | ||
| 44 | |||
| 45 | local *FILE; | ||
| 46 | unless (open(FILE, $file)) { | ||
| 47 | logerr "can't open $file to get assert expression: $!"; | ||
| 48 | return; | ||
| 49 | } | ||
| 50 | |||
| 51 | local $_; | ||
| 52 | my $count = $line; | ||
| 53 | |||
| 54 | while (<FILE>) { | ||
| 55 | last unless --$count > 0; | ||
| 56 | } | ||
| 57 | |||
| 58 | if ($count) { | ||
| 59 | logwarn "reached EOF in $file whilst looking for line #$line"; | ||
| 60 | close FILE; | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | unless (s/^\s*(?:DASSERT|DREQUIRE|DENSURE|VERIFY)\b//) { | ||
| 65 | chomp; | ||
| 66 | logwarn "expected assertion at line #$line in $file, got: $_"; | ||
| 67 | close FILE; | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | |||
| 71 | # | ||
| 72 | # Ok, we found something... now perform heuristic parsing... | ||
| 73 | # | ||
| 74 | |||
| 75 | my $expr = $_; | ||
| 76 | |||
| 77 | $expr =~ s/^\s+//; | ||
| 78 | $expr =~ s/\s+$//; | ||
| 79 | $expr =~ s/^\(\s*//; | ||
| 80 | |||
| 81 | if ($expr =~ s/(?:\)\s*)?;\s*$//) { | ||
| 82 | # | ||
| 83 | # Expression seems to be all on one line, like in: | ||
| 84 | # | ||
| 85 | # DASSERT($a == $b, "a equals b"); | ||
| 86 | # | ||
| 87 | # We're only interested in the "$a == $b" part though. | ||
| 88 | # | ||
| 89 | |||
| 90 | $expr =~ s/^\s*(.*?)\s*,\s*['"].*/$1/; #' for emacs coloring | ||
| 91 | close FILE; | ||
| 92 | return $expr; | ||
| 93 | } | ||
| 94 | |||
| 95 | # | ||
| 96 | # Expression is not contained on one line. Advance in the file until | ||
| 97 | # we see a ";" ending a line. Limit to the next 10 lines, or something | ||
| 98 | # is probably wrong. | ||
| 99 | # | ||
| 100 | |||
| 101 | my $limit = 10; | ||
| 102 | while ($limit-- > 0) { | ||
| 103 | $_ = <FILE>; | ||
| 104 | unless (defined $_) { | ||
| 105 | logwarn "reached EOF in $file whilst building ". | ||
| 106 | "assert text from line #$line"; | ||
| 107 | close FILE; | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | chomp; | ||
| 111 | s/^\s+//; | ||
| 112 | $expr .= " " . $_; | ||
| 113 | last if /;\s*$/; | ||
| 114 | } | ||
| 115 | close FILE; | ||
| 116 | |||
| 117 | logwarn "assertion in $file, line #$line too long, cutting parsing" | ||
| 118 | if $limit < 0; | ||
| 119 | |||
| 120 | # | ||
| 121 | # Got something? Same processing as above. | ||
| 122 | # | ||
| 123 | |||
| 124 | if ($expr =~ s/(?:\)\s*)?;\s*$//) { | ||
| 125 | $expr =~ s/^\s*(.*?)\s*,\s*['"].*/$1/; #' for emacs coloring | ||
| 126 | return $expr; | ||
| 127 | } | ||
| 128 | |||
| 129 | logwarn "can't compute assertion text at $file, line #$line, guessing..."; | ||
| 130 | |||
| 131 | # | ||
| 132 | # Limit to first 60 chars, then mark end with ... | ||
| 133 | # | ||
| 134 | |||
| 135 | $expr =~ s/^(.*?),\s*['"].*/$1/; #' for emacs coloring | ||
| 136 | $expr = substr($expr, 0, 60); | ||
| 137 | $expr .= "..."; | ||
| 138 | |||
| 139 | return $expr; | ||
| 140 | } | ||
| 141 | |||
| 142 | # | ||
| 143 | # stack_dump | ||
| 144 | # | ||
| 145 | # Dump the stack, discarding the first $offset frames. | ||
| 146 | # | ||
| 147 | sub stack_dump { | ||
| 148 | my ($offset) = @_; | ||
| 149 | |||
| 150 | # | ||
| 151 | # Let Carp do the hard work. | ||
| 152 | # | ||
| 153 | |||
| 154 | require Carp; | ||
| 155 | local $Carp::CarpLevel = 0; | ||
| 156 | |||
| 157 | my $message = Carp::longmess("dump"); | ||
| 158 | my @stack = split(/\n/, $message); | ||
| 159 | splice(@stack, 0, $offset + 1); # Also skip initial "dump error" line | ||
| 160 | |||
| 161 | foreach my $l (@stack) { $l =~ s/^\s+// } | ||
| 162 | |||
| 163 | return \@stack; | ||
| 164 | } | ||
| 165 | |||
| 166 | 1; | ||
| 167 | |||
| 168 | =head1 NAME | ||
| 169 | |||
| 170 | Carp::Datum::Assert - Assertion expression extractor | ||
| 171 | |||
| 172 | =head1 SYNOPSIS | ||
| 173 | |||
| 174 | # Not meant to be used by user code | ||
| 175 | |||
| 176 | =head1 DESCRIPTION | ||
| 177 | |||
| 178 | This module is used internally by C<Carp::Datum> to extract the expression | ||
| 179 | text of a failed assertion, directly from the file. | ||
| 180 | |||
| 181 | This extraction is done lexically, and the general guidelines, which | ||
| 182 | are documented in L<Carp::Datum::Strip>, apply here too. | ||
| 183 | |||
| 184 | =head1 AUTHORS | ||
| 185 | |||
| 186 | Christophe Dehaudt and Raphael Manfredi are the original authors. | ||
| 187 | |||
| 188 | Send bug reports, hints, tips, suggestions to Dave Hoover at <squirrel@cpan.org>. | ||
| 189 | |||
| 190 | =head1 SEE ALSO | ||
| 191 | |||
| 192 | Carp::Datum(3), Carp::Datum::Strip(3). | ||
| 193 | |||
| 194 | =cut | ||
| 195 |