| 1 | package Parse::Diagnostics; | ||||||||||
| 2 | 2 | require Exporter; | |||||||||
| 3 | 2 | @ISA = qw(Exporter); | |||||||||
| 4 | 2 | @EXPORT_OK = qw/parse_diagnostics parse_diagnostics_pp parse_diagnostics_xs/; | |||||||||
| 5 | 2 | %EXPORT_TAGS = ( | |||||||||
| 6 | all => \@EXPORT_OK, | ||||||||||
| 7 | ); | ||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use C::Tokenize '$string_re'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | our $VERSION = '0.03'; | |||||||||
| 14 | |||||||||||
| 15 | 2 | our $message_re = qr | |||||||||
| 16 | / | ||||||||||
| 17 | ( | ||||||||||
| 18 | "([^"]|\\")+" | ||||||||||
| 19 | | | ||||||||||
| 20 | '([^']|\\')+' | ||||||||||
| 21 | ) | ||||||||||
| 22 | (?:,.*?)? | ||||||||||
| 23 | /x; | ||||||||||
| 24 | |||||||||||
| 25 | 2 | our $diagnostics_re = qr | |||||||||
| 26 | / | ||||||||||
| 27 | ( | ||||||||||
| 28 | croak | ||||||||||
| 29 | | | ||||||||||
| 30 | carp | ||||||||||
| 31 | | | ||||||||||
| 32 | die | ||||||||||
| 33 | | | ||||||||||
| 34 | warn | ||||||||||
| 35 | | | ||||||||||
| 36 | confess | ||||||||||
| 37 | | | ||||||||||
| 38 | cluck | ||||||||||
| 39 | ) | ||||||||||
| 40 | \s* | ||||||||||
| 41 | ( | ||||||||||
| 42 | \( | ||||||||||
| 43 | $message_re | ||||||||||
| 44 | \) | ||||||||||
| 45 | | | ||||||||||
| 46 | $message_re | ||||||||||
| 47 | ) | ||||||||||
| 48 | /x; | ||||||||||
| 49 | |||||||||||
| 50 | 2 | our $xs_diagnostics_re = qr | |||||||||
| 51 | / | ||||||||||
| 52 | ( | ||||||||||
| 53 | croak | ||||||||||
| 54 | | | ||||||||||
| 55 | warn | ||||||||||
| 56 | | | ||||||||||
| 57 | vwarn | ||||||||||
| 58 | | | ||||||||||
| 59 | vcroak | ||||||||||
| 60 | | | ||||||||||
| 61 | die | ||||||||||
| 62 | | | ||||||||||
| 63 | croak_sv | ||||||||||
| 64 | | | ||||||||||
| 65 | die_sv | ||||||||||
| 66 | ) | ||||||||||
| 67 | \s* | ||||||||||
| 68 | \( | ||||||||||
| 69 | \s* | ||||||||||
| 70 | ($string_re) | ||||||||||
| 71 | /x; | ||||||||||
| 72 | |||||||||||
| 73 | 2 | our $c_diagnostics_re = qr | |||||||||
| 74 | / | ||||||||||
| 75 | (v?fprintf) | ||||||||||
| 76 | \s*\(\s* | ||||||||||
| 77 | stderr | ||||||||||
| 78 | \s*,\s* | ||||||||||
| 79 | ($string_re) | ||||||||||
| 80 | /x; | ||||||||||
| 81 | |||||||||||
| 82 | # Match "$regex" to "$contents" globally, and record the lines of each | ||||||||||
| 83 | # match. | ||||||||||
| 84 | |||||||||||
| 85 | sub regex_lines | ||||||||||
| 86 | 2 | ▶ | { | ||||||||
|
Subroutine
regex_lines: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 87 | 2 | my ($contents, $regex) = @_; | |||||||||
| 88 | # Copy the contents, then delete chunks off the front of it as we | ||||||||||
| 89 | # find diagnostics, so we can keep track of the line numbers. | ||||||||||
| 90 | 2 | my $copycontents = $contents; | |||||||||
| 91 | 2 | my @diagnostics; | |||||||||
| 92 | 2 | my $line = 1; | |||||||||
| 93 | 2 | ▶ | while ($copycontents =~ s/^(.*?)$regex//s) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 3 | my $leading = $1; | |||||||||
| 95 | 3 | my $type = $2; | |||||||||
| 96 | 3 | my $message = $3; | |||||||||
| 97 | # Count the lines in $leading. | ||||||||||
| 98 | 3 | my $lines = ($leading =~ tr/\n//); | |||||||||
| 99 | 3 | push @diagnostics, { | |||||||||
| 100 | type => $type, | ||||||||||
| 101 | message => $message, | ||||||||||
| 102 | line => $line + $lines, | ||||||||||
| 103 | }; | ||||||||||
| 104 | # print "$message ", $line + $lines, "\n"; | ||||||||||
| 105 | # Add the lines in $lines and whatever lines may be in | ||||||||||
| 106 | # $message to the current line. | ||||||||||
| 107 | 3 | $line += $lines + ($message =~ tr/\n//); | |||||||||
| 108 | } | ||||||||||
| 109 | 2 | return \@diagnostics; | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | sub parse_diagnostics_pp | ||||||||||
| 113 | 2 | ▶ | { | ||||||||
|
Subroutine
parse_diagnostics_pp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 114 | 2 | my ($contents, %options) = @_; | |||||||||
| 115 | 2 | return regex_lines ($contents, $diagnostics_re); | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | sub parse_diagnostics_xs | ||||||||||
| 119 | 0 | ▶ | { | ||||||||
|
Subroutine
parse_diagnostics_xs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 120 | 0 | my ($contents, %options) = @_; | |||||||||
| 121 | 0 | my @diagnostics; | |||||||||
| 122 | 0 | my $xs = regex_lines ($contents, $xs_diagnostics_re); | |||||||||
| 123 | 0 | push @diagnostics, @$xs; | |||||||||
| 124 | 0 | my $c = regex_lines ($contents, $c_diagnostics_re); | |||||||||
| 125 | 0 | push @diagnostics, @$c; | |||||||||
| 126 | 0 | return \@diagnostics; | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | sub parse_diagnostics | ||||||||||
| 130 | 1 | ▶ | { | ||||||||
|
Subroutine
parse_diagnostics: called CC=5 SCAR=17.3
Pod
documented
| |||||||||||
| 131 | 1 | my ($file, %options) = @_; | |||||||||
| 132 | 1 | my $contents = path ($file)->slurp (); | |||||||||
| 133 | 1 | my $diagnostics; | |||||||||
| 134 | 1 | ▶ | if ($file =~ /\.(c|xs)$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | $diagnostics = parse_diagnostics_xs ($contents, %options); | |||||||||
| 136 | } | ||||||||||
| 137 | else { | ||||||||||
| 138 | 1 | $diagnostics = parse_diagnostics_pp ($contents, %options); | |||||||||
| 139 | } | ||||||||||
| 140 | # Get user-defined diagnostics | ||||||||||
| 141 | 1 | ▶ | if ($options{user_re}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | 0 | my $udiagnostics = regex_lines ($contents, $options{user_re}); | |||||||||
| 143 | 0 | push @$diagnostics, @$udiagnostics; | |||||||||
| 144 | } | ||||||||||
| 145 | # Hashmap of duplicates | ||||||||||
| 146 | 1 | my %dl; | |||||||||
| 147 | my @diagnostics; | ||||||||||
| 148 | # Eliminate duplicate diagnostics | ||||||||||
| 149 | 1 | for my $d (@$diagnostics) { | |||||||||
| 150 | 1 | my $key = $d->{message} . "-" . $d->{line}; | |||||||||
| 151 | 1 | ▶ | if (! $dl{$key}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 1 | push @diagnostics, $d; | |||||||||
| 153 | 1 | $dl{$key} = 1; | |||||||||
| 154 | } | ||||||||||
| 155 | } | ||||||||||
| 156 | # Sort by line | ||||||||||
| 157 | 0 | @diagnostics = sort {$a->{line} <=> $b->{line}} @diagnostics; | |||||||||
| 158 | 1 | return \@diagnostics; | |||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | 2 | 1; | |||||||||