| 1 | #!/usr/bin/env perl | ||
| 2 | # bin/dev/perl_types_refactor_names_parser.pl | ||
| 3 | # | ||
| 4 | # Parser to generate a mapping of old compound type names (suffix-first underscore) | ||
| 5 | # to new compound type names (prefix-first underscore) by comparing the baseline | ||
| 6 | # (pre-refactor) and current code in the git repository. | ||
| 7 | # | ||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | our $VERSION = 0.107_000; | ||
| 11 | |||
| 12 | use Getopt::Long; | ||
| 13 | use File::Find; | ||
| 14 | require Term::ReadLine; | ||
| 15 | |||
| 16 | sub usage { | ||
| 17 | die <<"USAGE"; | ||
| 18 | Usage: $0 [--baseline <commit>] [--interactive] [--help] | ||
| 19 | |||
| 20 | Options: | ||
| 21 | --baseline <commit> Git commit or ref representing the pre-refactor baseline | ||
| 22 | (default: 9fb763300058926db240b94179772753f642cd2a) | ||
| 23 | --interactive Prompt interactively for unchanged hot names (default: no prompts) | ||
| 24 | --help Show this help and exit | ||
| 25 | |||
| 26 | This script compares the baseline commit to the current working tree and | ||
| 27 | extracts all compound data type names, generating a mapping from old to new identifiers. | ||
| 28 | Outputs a Perl data structure. | ||
| 29 | USAGE | ||
| 30 | } | ||
| 31 | |||
| 32 | # default to non-interactive mode | ||
| 33 | my $interactive_UI = 0; | ||
| 34 | |||
| 35 | # when running in interactive mode, use Term::ReadLine to prompt for UNCHANGED hot names | ||
| 36 | my $term = Term::ReadLine->new('perl_types_refactor_names_parser', \*STDIN, \*STDOUT); | ||
| 37 | |||
| 38 | my %refactor_names_map; # must declare outside of any control structures | ||
| 39 | my $baseline = '9fb763300058926db240b94179772753f642cd2a'; | ||
| 40 | GetOptions( | ||
| 41 | 'baseline=s' => \$baseline, | ||
| 42 | 'interactive!' => \$interactive_UI, | ||
| 43 | 'help|h' => sub { usage() }, | ||
| 44 | ) or usage(); | ||
| 45 | |||
| 46 | use File::Path qw(make_path remove_tree); | ||
| 47 | use Data::Dumper; | ||
| 48 | $Data::Dumper::Sortkeys = 1; # sort Dumper() output, especially for final %refactor_names_map output | ||
| 49 | $Data::Dumper::Terse = 1; # don't output $VAR1 names, to avoid problems when re-loading later | ||
| 50 | |||
| 51 | # Flush per-hunk records into flat pairs, handling CREATED/DELETED. | ||
| 52 | sub _flush_records { | ||
| 53 | my ($records_ref, $pairs_ref, $suffix, $diff_path) = @_; | ||
| 54 | for my $rec (@$records_ref) { | ||
| 55 | my $old_names = $rec->{old} || []; | ||
| 56 | my $new_names = $rec->{new} || []; | ||
| 57 | # Deleted: minus-only | ||
| 58 | if (@$old_names && !@$new_names) { | ||
| 59 | for my $o (@$old_names) { | ||
| 60 | print STDERR "DELETED hot name '$o' under suffix '$suffix' in diff '$diff_path':\n"; | ||
| 61 | print STDERR " OLD LINE: $rec->{old_line}"; | ||
| 62 | print STDERR " SKIPPING UNSAFELY\n\n"; | ||
| 63 | } | ||
| 64 | next; | ||
| 65 | } | ||
| 66 | # Created: plus-only | ||
| 67 | if (@$new_names && !@$old_names) { | ||
| 68 | for my $n (@$new_names) { | ||
| 69 | print STDERR "CREATED hot name '$n' under suffix '$suffix' in diff '$diff_path':\n"; | ||
| 70 | print STDERR " NEW LINE: $rec->{new_line}"; | ||
| 71 | print STDERR " SKIPPING UNSAFELY\n\n"; | ||
| 72 | } | ||
| 73 | next; | ||
| 74 | } | ||
| 75 | # One-to-one mapping: strict only for C++ (.cpp/.h) suffixes | ||
| 76 | if (@$old_names == @$new_names) { | ||
| 77 | if ($suffix eq 'cpp' or $suffix eq 'h') { | ||
| 78 | # strict context check: remove diff markers and hot names | ||
| 79 | my ($ol, $nl) = ($rec->{old_line}, $rec->{new_line}); | ||
| 80 | $ol =~ s/^[-+]//; $nl =~ s/^[-+]//; | ||
| 81 | chomp $ol; chomp $nl; | ||
| 82 | for my $i (0 .. $#$old_names) { | ||
| 83 | my $o_pat = quotemeta $old_names->[$i]; | ||
| 84 | my $n_pat = quotemeta $new_names->[$i]; | ||
| 85 | $ol =~ s/\b$o_pat\b//g; | ||
| 86 | $nl =~ s/\b$n_pat\b//g; | ||
| 87 | } | ||
| 88 | $ol =~ s/\s+/ /g; $ol =~ s/^\s+|\s+$//g; | ||
| 89 | $nl =~ s/\s+/ /g; $nl =~ s/^\s+|\s+$//g; | ||
| 90 | if ($ol ne $nl) { | ||
| 91 | # contexts differ: record deletion+creation, skip pairing | ||
| 92 | for my $o (@$old_names) { | ||
| 93 | print STDERR "DELETED hot name '$o' under suffix '$suffix' in diff '$diff_path':\n"; | ||
| 94 | print STDERR " OLD LINE: $rec->{old_line}"; | ||
| 95 | print STDERR " SKIPPING UNSAFELY\n\n"; | ||
| 96 | } | ||
| 97 | for my $n (@$new_names) { | ||
| 98 | print STDERR "CREATED hot name '$n' under suffix '$suffix' in diff '$diff_path':\n"; | ||
| 99 | print STDERR " NEW LINE: $rec->{new_line}"; | ||
| 100 | print STDERR " SKIPPING UNSAFELY\n\n"; | ||
| 101 | } | ||
| 102 | next; | ||
| 103 | } | ||
| 104 | # contexts match: emit rename pairs | ||
| 105 | for my $i (0 .. $#$old_names) { | ||
| 106 | push @$pairs_ref, { | ||
| 107 | old => $old_names->[$i], | ||
| 108 | new => $new_names->[$i], | ||
| 109 | old_line => $rec->{old_line}, | ||
| 110 | new_line => $rec->{new_line}, | ||
| 111 | diff_file => $rec->{diff_file}, | ||
| 112 | suffix => $rec->{suffix}, | ||
| 113 | }; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | # for non-C++ files, accept any equal-length mapping | ||
| 118 | for my $i (0 .. $#$old_names) { | ||
| 119 | push @$pairs_ref, { | ||
| 120 | old => $old_names->[$i], | ||
| 121 | new => $new_names->[$i], | ||
| 122 | old_line => $rec->{old_line}, | ||
| 123 | new_line => $rec->{new_line}, | ||
| 124 | diff_file => $rec->{diff_file}, | ||
| 125 | suffix => $rec->{suffix}, | ||
| 126 | }; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | warn "SKIPPING MISMATCHED LINE for kind=$rec->{kind}: " | ||
| 132 | . scalar(@$old_names) . " old vs " . scalar(@$new_names) . " new\n" | ||
| 133 | . " OLD LINE: " . ($rec->{old_line}//'') | ||
| 134 | . " NEW LINE: " . ($rec->{new_line}//'') . "\n"; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | @$records_ref = (); | ||
| 138 | } | ||
| 139 | |||
| 140 | # recursively find all files under lib/Perl/Structure | ||
| 141 | my @files_unsorted; | ||
| 142 | find( | ||
| 143 | sub { push @files_unsorted, $File::Find::name if -f and $File::Find::name =~ m{^lib/Perl/Structure/} }, | ||
| 144 | 'lib/Perl/Structure' | ||
| 145 | ); | ||
| 146 | |||
| 147 | my @files = sort @files_unsorted; | ||
| 148 | # global accumulator of all hot-name pairs across files for conflict origin lookup | ||
| 149 | my @all_pairs; | ||
| 150 | #print 'have @files = ', Dumper(\@files), "\n"; | ||
| 151 | |||
| 152 | my $outroot = "bin/dev/perl_types_refactor_names/$baseline"; | ||
| 153 | # remove any existing baseline snapshot to avoid stale or partial data | ||
| 154 | if (-d $outroot) { | ||
| 155 | remove_tree($outroot) or die "Cannot remove old snapshot directory '$outroot': $!"; | ||
| 156 | } | ||
| 157 | |||
| 158 | # hot strings | ||
| 159 | my @hot_strings = qw( | ||
| 160 | AV | ||
| 161 | HV | ||
| 162 | RV | ||
| 163 | av | ||
| 164 | hv | ||
| 165 | rv | ||
| 166 | Sv | ||
| 167 | arrayref | ||
| 168 | array | ||
| 169 | hashref | ||
| 170 | hash | ||
| 171 | unordered_map | ||
| 172 | umap | ||
| 173 | vector | ||
| 174 | ); | ||
| 175 | my $hot_strings_joined = join('|', @hot_strings); | ||
| 176 | my $hot_strings_re = qr/(?:$hot_strings_joined)/; | ||
| 177 | #print Dumper($hot_strings_re); | ||
| 178 | |||
| 179 | # these hot names are symmetric and thus should not change | ||
| 180 | my @symmetric_hot_names = qw( | ||
| 181 | input_avref_avref | ||
| 182 | input_hvref_hvref | ||
| 183 | output_avref_avref | ||
| 184 | output_hvref_hvref | ||
| 185 | input_vector_vector | ||
| 186 | output_vector_vector | ||
| 187 | ); | ||
| 188 | |||
| 189 | # false-positive matches to ignore (non-hot names), | ||
| 190 | # including 1-D names which do not require any reversals or double-colons | ||
| 191 | my @not_hot_names = qw( | ||
| 192 | std::vector | ||
| 193 | std::unordered_map | ||
| 194 | SvRV | ||
| 195 | newSVrv | ||
| 196 | newAV | ||
| 197 | newHV | ||
| 198 | newRV_inc | ||
| 199 | newRV_noinc | ||
| 200 | AVRVHE | ||
| 201 | arrays | ||
| 202 | hashes | ||
| 203 | new_array | ||
| 204 | new_hash | ||
| 205 | temp_array | ||
| 206 | temp_av | ||
| 207 | input_av | ||
| 208 | input_hv | ||
| 209 | input_avref | ||
| 210 | input_hvref | ||
| 211 | input_umap | ||
| 212 | input_vector | ||
| 213 | output_av | ||
| 214 | output_hv | ||
| 215 | output_avref | ||
| 216 | output_hvref | ||
| 217 | output_umap | ||
| 218 | output_vector | ||
| 219 | av_len | ||
| 220 | av_fetch | ||
| 221 | av_push | ||
| 222 | hv_fetch | ||
| 223 | hv_iterinit | ||
| 224 | hv_iternext | ||
| 225 | hv_iterval | ||
| 226 | hv_iterkeysv | ||
| 227 | hv_store | ||
| 228 | hashentry_CHECK | ||
| 229 | hashentry_CHECKTRACE | ||
| 230 | have | ||
| 231 | reserve | ||
| 232 | ); | ||
| 233 | |||
| 234 | # hard-coded hot names to override parser misses (suffix => { old => new, ... }) | ||
| 235 | my %hot_names_hard_coded = ( | ||
| 236 | 'cpp' => { | ||
| 237 | 'XS_unpack_integer_hashref_arrayref' => 'XS_unpack_arrayref_hashref_integer', | ||
| 238 | 'XS_unpack_number_hashref_arrayref' => 'XS_unpack_arrayref_hashref_number', | ||
| 239 | 'XS_unpack_string_hashref_arrayref' => 'XS_unpack_arrayref_hashref_string', | ||
| 240 | }, | ||
| 241 | 'h' => { | ||
| 242 | 'XS_unpack_integer_hashref_arrayref' => 'XS_unpack_arrayref_hashref_integer', | ||
| 243 | 'XS_unpack_number_hashref_arrayref' => 'XS_unpack_arrayref_hashref_number', | ||
| 244 | 'XS_unpack_string_hashref_arrayref' => 'XS_unpack_arrayref_hashref_string', | ||
| 245 | }, | ||
| 246 | ); | ||
| 247 | |||
| 248 | # hot-name typos: exact minus diff-line (no trailing newline) => [[typo, correction], [typo, correction], ...] | ||
| 249 | my %hot_name_typos = ( | ||
| 250 | # DEV NOTE: must use q!...! as quotations due to both double & single quotes present in CHECKTRACE strings | ||
| 251 | |||
| 252 | # these XS_unpack lines in the file `lib/Perl/Structure/Array/SubTypes2D.h` | ||
| 253 | # originally had `input_avref_avref` instead of `input_avref_hvref` | ||
| 254 | '-integer_hashref_arrayref XS_unpack_integer_hashref_arrayref(SV* input_avref_avref);' => | ||
| 255 | [['input_avref_avref', 'input_avref_hvref']], | ||
| 256 | '-number_hashref_arrayref XS_unpack_number_hashref_arrayref(SV* input_avref_avref);' => | ||
| 257 | [['input_avref_avref', 'input_avref_hvref']], | ||
| 258 | '-string_hashref_arrayref XS_unpack_string_hashref_arrayref(SV* input_avref_avref);' => | ||
| 259 | [['input_avref_avref', 'input_avref_hvref']], | ||
| 260 | |||
| 261 | # these number type-checking lines in the file `lib/Perl/Structure/Hash/SubTypes2D.cpp` | ||
| 262 | # originally had `EIVAVRVHE00` instead of `ENVAVRVHE00` | ||
| 263 | '- if (possible_number_arrayref_hashentry == NULL) { croak("\nERROR EIVAVRVHE00, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber_arrayref_hashentry value expected but undefined/null value found,\ncroaking"); }' => | ||
| 264 | [['EIVAVRVHE00', 'EHVRVAVRVNVHE00']], | ||
| 265 | q!- if (possible_number_arrayref_hashentry == NULL) { croak("\nERROR EIVAVRVHE00, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nnumber_arrayref_hashentry value expected but undefined/null value found,\nin variable '%s' from subroutine '%s',\ncroaking", variable_name, subroutine_name); }! => | ||
| 266 | [['EIVAVRVHE00', 'EHVRVAVRVNVHE00']], | ||
| 267 | |||
| 268 | # these number type-checking lines in the file `lib/Perl/Structure/Hash/SubTypes2D.cpp` | ||
| 269 | # originally had `EPVAVRVHE00` instead of `ENVAVRVHE00` | ||
| 270 | '- if (possible_string_arrayref_hashentry == NULL) { croak("\nERROR EIVAVRVHE00, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nstring_arrayref_hashentry value expected but undefined/null value found,\ncroaking"); }' => | ||
| 271 | [['EIVAVRVHE00', 'EHVRVAVRVPVHE00']], | ||
| 272 | q!- if (possible_string_arrayref_hashentry == NULL) { croak("\nERROR EIVAVRVHE00, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES:\nstring_arrayref_hashentry value expected but undefined/null value found,\nin variable '%s' from subroutine '%s',\ncroaking", variable_name, subroutine_name); }! => | ||
| 273 | [['EIVAVRVHE00', 'EHVRVAVRVPVHE00']], | ||
| 274 | |||
| 275 | # these XS_pack lines in the file `lib/Perl/Structure/Hash/SubTypes2D.h` | ||
| 276 | # originally had `input_vector_unordered_map` instead of `input_unordered_map_unordered_map` | ||
| 277 | '-void XS_pack_integer_hashref_hashref(SV* output_hvref_hvref, integer_hashref_hashref input_vector_unordered_map);' => | ||
| 278 | [['input_vector_unordered_map', 'input_umap_umap']], | ||
| 279 | '-void XS_pack_number_hashref_hashref(SV* output_hvref_hvref, number_hashref_hashref input_vector_unordered_map);' => | ||
| 280 | [['input_vector_unordered_map', 'input_umap_umap']], | ||
| 281 | '-void XS_pack_string_hashref_hashref(SV* output_hvref_hvref, string_hashref_hashref input_vector_unordered_map);' => | ||
| 282 | [['input_vector_unordered_map', 'input_umap_umap']], | ||
| 283 | |||
| 284 | # these integer type-checking lines in the file `lib/Perl/Structure/Array/SubTypes1D.cpp` | ||
| 285 | # originally had `*integer_hashref*` instead of `*integer_arrayref*` | ||
| 286 | '-// integer_CHECKTRACE(*input_av_element, (char*)((string)"*input_av_element at index " + to_string(i)).c_str(), "XS_unpack_integer_hashref()");' => | ||
| 287 | [['XS_unpack_integer_hashref', 'XS_unpack_arrayref_integer']], | ||
| 288 | '-// integer_CHECKTRACE(*input_av_element, (char*)((string)"*input_av_element at index " + to_string(i)).c_str(), "integer_hashref_to_string()");' => | ||
| 289 | [['integer_hashref_to_string', 'arrayref_integer_to_string']], | ||
| 290 | |||
| 291 | # this comment line in the file `lib/Perl/Structure/Hash/SubTypes2D.pm` | ||
| 292 | # originall had `*_arrayref_hashref_typetest*` instead of `TYPE_arrayref_hashref_typetestX` | ||
| 293 | '-# for *_CHECK*() used in *_arrayref_hashref_typetest*()' => | ||
| 294 | [['_arrayref_hashref_typetest', 'hashref_arrayref_TYPE_typetestX']], | ||
| 295 | |||
| 296 | # this comment in the file `Perl/Structure/Array/SubTypes1D.pm` | ||
| 297 | # originally had `*_arrayref_typetest1` instead of `TYPE_arrayref_typetest1`; | ||
| 298 | # DEV NOTE: there is a trailing space on the old line, must include below to match correctly! | ||
| 299 | '-use Perl::Type::Integer; # for integer_CHECKTRACE(), used in *_arrayref_typetest1() ' => | ||
| 300 | [['_arrayref_typetest1', 'arrayref_TYPE_typetest1']], | ||
| 301 | |||
| 302 | # this comment line in the file `lib/Perl/Structure/Array/SubTypes2D.pm` | ||
| 303 | # originally had `*_arrayref_arrayref` and `*_hashref_arrayref` | ||
| 304 | # instead of `TYPE_arrayref_arrayref` & `TYPE_hashref_arrayref` | ||
| 305 | '-# START HERE: implement integer_arrayref_arrayref_CHECK*() & integer_arrayref_arrayref_to_string() & integer_arrayref_arrayref_typetest*() as in Hash/SubTypes.pm, then *_arrayref_arrayref, then *_hashref_arrayref' => | ||
| 306 | [['_arrayref_arrayref', 'arrayref_arrayref_TYPE'], | ||
| 307 | ['_hashref_arrayref', 'arrayref_hashref_TYPE']], | ||
| 308 | |||
| 309 | # this comment in the file `Perl/Structure/Hash/SubTypes1D.pm` | ||
| 310 | # originally had `*_hashref_typetest1` instead of `TYPE_hashref_typetest1`; | ||
| 311 | '-use Perl::Type::Integer; # for integer_CHECKTRACE(), used in *_hashref_typetest1()' => | ||
| 312 | [['_hashref_typetest1', 'hashref_TYPE_typetest1']], | ||
| 313 | ); # end %hot_name_typos | ||
| 314 | |||
| 315 | for my $file (@files) { | ||
| 316 | # determine file suffix | ||
| 317 | my ($suffix) = $file =~ /\.([^\.]+)$/; # must have parentheses around $suffix to avoid wrong values | ||
| 318 | |||
| 319 | # determine output path under baseline snapshot | ||
| 320 | my $baseline_path = "$outroot/$file"; | ||
| 321 | my ($baseline_dir) = $baseline_path =~ m{^(.+)/[^/]+$}; | ||
| 322 | make_path($baseline_dir); | ||
| 323 | |||
| 324 | # save baseline version | ||
| 325 | open my $baseline_filehandle_in, '-|', "git show $baseline:$file" or next; | ||
| 326 | open my $baseline_filehandle_out, '>', $baseline_path or die $!; | ||
| 327 | print $baseline_filehandle_out $_ while <$baseline_filehandle_in>; | ||
| 328 | close $baseline_filehandle_out; | ||
| 329 | close $baseline_filehandle_in; | ||
| 330 | |||
| 331 | # generate unified diff between baseline and current file | ||
| 332 | my $diff_path = "$baseline_path.diff"; | ||
| 333 | open my $diff_filehandle_out, '>', $diff_path | ||
| 334 | or die "Cannot open '$diff_path' for writing: $!"; | ||
| 335 | # run diff as external command, capturing its output | ||
| 336 | open my $diff_filehandle_in, '-|', 'diff', '-u', $baseline_path, $file | ||
| 337 | or warn "Failed to exec diff on '$baseline_path' vs '$file': $!" and next; | ||
| 338 | while (my $line = <$diff_filehandle_in>) { | ||
| 339 | print $diff_filehandle_out $line; | ||
| 340 | } | ||
| 341 | close $diff_filehandle_in; | ||
| 342 | close $diff_filehandle_out; | ||
| 343 | # parse diff for hot names into per-line records | ||
| 344 | open my $diff_path_filehandle_in, '<', $diff_path or next; | ||
| 345 | my @records; | ||
| 346 | my @pairs; | ||
| 347 | while (my $line = <$diff_path_filehandle_in>) { | ||
| 348 | # hunk boundary: flush previous hunk's records | ||
| 349 | if ($line =~ /^@@/) { | ||
| 350 | _flush_records(\@records, \@pairs, $suffix, $diff_path); | ||
| 351 | next; | ||
| 352 | } | ||
| 353 | # only consider removed ('-') or added ('+') lines | ||
| 354 | my $sign = substr($line, 0, 1); | ||
| 355 | next unless $sign eq '-' or $sign eq '+'; | ||
| 356 | my $content = substr($line, 1); | ||
| 357 | # DO NOT skip commented code changes; there are still lots of hot strings inside comments! | ||
| 358 | # next if $content =~ /^\s*#/; | ||
| 359 | # find all hot names (full user-defined identifiers) and select those containing any hot substring | ||
| 360 | my @found_hot_names; | ||
| 361 | while ($content =~ /\b([A-Za-z_:][A-Za-z0-9_:]*)\b/g) { | ||
| 362 | my $possible_hot_name = $1; | ||
| 363 | # a hot name must contain a hot string plus other characters, | ||
| 364 | # but not be in @not_hot_names or @symmetric_hot_names | ||
| 365 | next unless $possible_hot_name =~ /$hot_strings_re/; # require one hot string to match | ||
| 366 | next if grep { $_ eq $possible_hot_name } @hot_strings; # require more chars than just hot string | ||
| 367 | next if grep { $_ eq $possible_hot_name } @symmetric_hot_names; # skip symmetric hot names | ||
| 368 | next if grep { $_ eq $possible_hot_name } @not_hot_names; # disallow forbidden non-hot names | ||
| 369 | push @found_hot_names, $possible_hot_name; | ||
| 370 | } | ||
| 371 | next unless @found_hot_names; | ||
| 372 | # determine change kind to group records by type | ||
| 373 | my $kind = do { | ||
| 374 | if ($content =~ /^\s*typedef\b/) { 'typedef' } | ||
| 375 | elsif ($content =~ /\bXS_unpack_/) { 'xs_unpack' } | ||
| 376 | elsif ($content =~ /\bXS_pack_/) { 'xs_pack' } | ||
| 377 | elsif ($content =~ /_to_string\b/) { 'to_string' } | ||
| 378 | elsif ($content =~ /_typetest/) { 'typetest' } | ||
| 379 | elsif ($content =~ /_CHECK(?:TRACE)?\b/) { 'check' } | ||
| 380 | else { 'other' } | ||
| 381 | }; | ||
| 382 | # debug: show extracted hot names and classification | ||
| 383 | print STDERR "DEBUG: diff line ($sign) kind='$kind' content: $content"; | ||
| 384 | print STDERR "DEBUG: extracted hot names: (" . join(", ", @found_hot_names) . ")\n"; | ||
| 385 | # group minus/plus lines with FIFO pairing inside each hunk | ||
| 386 | if ($sign eq '-') { | ||
| 387 | # record deletion-only entry | ||
| 388 | push @records, { | ||
| 389 | kind => $kind, | ||
| 390 | old => [@found_hot_names], | ||
| 391 | old_line => $line, | ||
| 392 | diff_file => $diff_path, | ||
| 393 | suffix => $suffix, | ||
| 394 | }; | ||
| 395 | } else { | ||
| 396 | # FIFO pairing: match to first unmatched deletion of same kind | ||
| 397 | if (my ($record) = grep { $_->{kind} eq $kind && !exists $_->{new} } @records) { | ||
| 398 | $record->{new} = [@found_hot_names]; | ||
| 399 | $record->{new_line} = $line; | ||
| 400 | } else { | ||
| 401 | # record creation-only entry | ||
| 402 | push @records, { | ||
| 403 | kind => $kind, | ||
| 404 | old => [], | ||
| 405 | old_line => undef, | ||
| 406 | new => [@found_hot_names], | ||
| 407 | new_line => $line, | ||
| 408 | diff_file => $diff_path, | ||
| 409 | suffix => $suffix, | ||
| 410 | }; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | } | ||
| 414 | close $diff_path_filehandle_in; | ||
| 415 | |||
| 416 | # flush any remaining records from last hunk | ||
| 417 | _flush_records(\@records, \@pairs, $suffix, $diff_path); | ||
| 418 | |||
| 419 | # collect this file's valid pairs for cross-file conflict tracking | ||
| 420 | push @all_pairs, @pairs; | ||
| 421 | |||
| 422 | # align old/new pairs and populate refactor_names_map | ||
| 423 | ENTRY: for my $entry (@pairs) { | ||
| 424 | # TYPO: skip known one-off hot-name corrections | ||
| 425 | (my $line = $entry->{old_line}) =~ s/\n\z//; | ||
| 426 | if (my $pairs = $hot_name_typos{$line}) { | ||
| 427 | for my $pair (@$pairs) { | ||
| 428 | my ($typo, $correct) = @$pair; | ||
| 429 | if ($entry->{old} eq $typo && $entry->{new} eq $correct) { | ||
| 430 | print STDERR "TYPO detected hot name '$typo' under suffix '$suffix' in diff '$entry->{diff_file}':\n"; | ||
| 431 | print STDERR " OLD LINE: $entry->{old_line}"; | ||
| 432 | print STDERR " NEW LINE: $entry->{new_line}"; | ||
| 433 | print STDERR " HOT NAME: $typo\n"; | ||
| 434 | print STDERR " SKIPPING SAFELY\n\n"; | ||
| 435 | next ENTRY; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | } | ||
| 439 | next unless defined $entry->{old}; | ||
| 440 | my $old_name = $entry->{old}; | ||
| 441 | my $new_name = $entry->{new}; | ||
| 442 | # first encounter, hot name mapping does not yet exist | ||
| 443 | if (! exists $refactor_names_map{$suffix}{$old_name}) { | ||
| 444 | # new hot name mapping found, store it | ||
| 445 | if ($old_name ne $new_name) { | ||
| 446 | print STDERR "Matched hot name '$old_name' under suffix '$suffix' in diff '$diff_path':\n"; | ||
| 447 | print STDERR " OLD LINE: $entry->{old_line}"; | ||
| 448 | print STDERR " NEW LINE: $entry->{new_line}"; | ||
| 449 | print STDERR " OLD NAME: $old_name\n"; | ||
| 450 | print STDERR " NEW NAME: $new_name\n\n"; | ||
| 451 | $refactor_names_map{$suffix}{$old_name} = $new_name; | ||
| 452 | } | ||
| 453 | # unchanged hot name found, prompt or skip | ||
| 454 | else { | ||
| 455 | print STDERR "UNCHANGED hot name '$old_name' under suffix '$suffix' in diff '$diff_path':\n"; | ||
| 456 | print STDERR " OLD LINE: $entry->{old_line}"; | ||
| 457 | print STDERR " NEW LINE: $entry->{new_line}"; | ||
| 458 | print STDERR " OLD NAME: $old_name\n"; | ||
| 459 | |||
| 460 | # if interactive UI, prompt for a new hot name and store it | ||
| 461 | if ($interactive_UI) { | ||
| 462 | # use Term::ReadLine to prompt with default | ||
| 463 | my $prompt = " NEW NAME? "; | ||
| 464 | $new_name = $term->readline($prompt, $old_name); | ||
| 465 | print STDERR "\n"; | ||
| 466 | $refactor_names_map{$suffix}{$old_name} = $new_name; | ||
| 467 | } | ||
| 468 | # non-interactive UI, no way to know what this hot name should map to, no choice but to skip it | ||
| 469 | else { | ||
| 470 | print STDERR " NEW NAME UNKNOWN, SKIPPING UNSAFELY\n\n"; | ||
| 471 | } | ||
| 472 | } | ||
| 473 | } | ||
| 474 | # subsequent encounters, hot name mapping already exists | ||
| 475 | else { | ||
| 476 | my $new_existing = $refactor_names_map{$suffix}{$old_name}; | ||
| 477 | # retrieve the first new_line associated with the existing mapping | ||
| 478 | # only consider pairs with both old and new defined to avoid warnings | ||
| 479 | my ($orig_pair) = grep { | ||
| 480 | defined($_->{old}) && defined($_->{new}) | ||
| 481 | && $_->{suffix} eq $suffix | ||
| 482 | && $_->{old} eq $old_name | ||
| 483 | && $_->{new} eq $new_existing | ||
| 484 | } @all_pairs; | ||
| 485 | my $new_existing_line = $orig_pair ? $orig_pair->{new_line} : ''; | ||
| 486 | chomp $new_existing_line; | ||
| 487 | # capture original old line for conflict comparison | ||
| 488 | my $old_existing_line = $orig_pair ? $orig_pair->{old_line} : ''; | ||
| 489 | chomp $old_existing_line; | ||
| 490 | # record diff file paths for original and current mappings | ||
| 491 | my $old_diff_path = $orig_pair && $orig_pair->{diff_file} ? $orig_pair->{diff_file} : ''; | ||
| 492 | my $new_diff_path = $entry->{diff_file}; | ||
| 493 | |||
| 494 | # detect hot names that should be changed but aren't yet; | ||
| 495 | # already have this hot name mapping, can safely skip with no action required | ||
| 496 | if ($new_name eq $old_name) { | ||
| 497 | print STDERR "CHANGEABLE hot name '$old_name' under suffix '$suffix' in diff '$diff_path':\n"; | ||
| 498 | print STDERR " OLD LINE: $entry->{old_line}"; | ||
| 499 | print STDERR " NEW LINE1: $new_existing_line\n"; | ||
| 500 | print STDERR " NEW LINE2: $entry->{new_line}"; | ||
| 501 | print STDERR " OLD NAME: $old_name\n"; | ||
| 502 | print STDERR " NEW NAME1: $new_existing\n"; | ||
| 503 | print STDERR " NEW NAME2: $new_name\n"; | ||
| 504 | print STDERR " SKIPPING SAFELY\n\n"; | ||
| 505 | } | ||
| 506 | # detect conflicts that have one old hot name with multiple new hot names | ||
| 507 | elsif ($refactor_names_map{$suffix}{$old_name} ne $new_name) { | ||
| 508 | print STDERR "CONFLICTED hot name '$old_name' under suffix '$suffix':\n"; | ||
| 509 | print STDERR " OLD DIFF: $old_diff_path\n"; | ||
| 510 | print STDERR " NEW DIFF: $new_diff_path\n"; | ||
| 511 | print STDERR " OLD LINE1: $old_existing_line\n"; | ||
| 512 | print STDERR " NEW LINE1: $new_existing_line\n"; | ||
| 513 | print STDERR " OLD LINE2: $entry->{old_line}"; | ||
| 514 | print STDERR " NEW LINE2: $entry->{new_line}"; | ||
| 515 | print STDERR " OLD NAME: $old_name\n"; | ||
| 516 | print STDERR " NEW NAME1: $new_existing\n"; | ||
| 517 | print STDERR " NEW NAME2: $new_name\n"; | ||
| 518 | # if non-interactive UI, default to keeping existing (first) new name | ||
| 519 | # by simply not updating %refactor_names_map | ||
| 520 | if (not $interactive_UI) { | ||
| 521 | print STDERR " NEW NAME: $new_existing [ KEEPING EXISTING NEW NAME1]\n"; | ||
| 522 | print STDERR " NEW NAME CONFLICT, SKIPPING UNSAFELY\n\n"; | ||
| 523 | } | ||
| 524 | # if interactive UI, prompt for which of the two hot names to use | ||
| 525 | else { | ||
| 526 | print STDERR " NEW NAME? "; | ||
| 527 | chomp(my $chosen = <STDIN>); | ||
| 528 | print STDERR "\n"; | ||
| 529 | $refactor_names_map{$suffix}{$old_name} = $chosen; | ||
| 530 | } | ||
| 531 | } | ||
| 532 | # exact repeat of existing hot name mapping, can safely skip with no action required | ||
| 533 | else { | ||
| 534 | print STDERR "Re-matched hot name '$old_name' under suffix '$suffix' in diff '$diff_path':\n"; | ||
| 535 | print STDERR " OLD LINE: $entry->{old_line}"; | ||
| 536 | print STDERR " NEW LINE: $entry->{new_line}"; | ||
| 537 | print STDERR " OLD NAME: $old_name\n"; | ||
| 538 | print STDERR " NEW NAME: $new_name\n"; | ||
| 539 | print STDERR " SKIPPING SAFELY\n\n"; | ||
| 540 | } | ||
| 541 | } | ||
| 542 | } | ||
| 543 | } | ||
| 544 | |||
| 545 | # merge in hard-coded hot-name mappings after parsing all diffs | ||
| 546 | for my $suffix (keys %hot_names_hard_coded) { | ||
| 547 | for my $old_name (keys %{ $hot_names_hard_coded{$suffix} }) { | ||
| 548 | my $new_name = $hot_names_hard_coded{$suffix}{$old_name}; | ||
| 549 | if (exists $refactor_names_map{$suffix}{$old_name}) { | ||
| 550 | print STDERR "HARD-CODED CLOBBER hot name '$old_name' under suffix '$suffix':\n"; | ||
| 551 | print STDERR " OLD NAME: $old_name\n"; | ||
| 552 | print STDERR " NEW NAME1: $refactor_names_map{$suffix}{$old_name} (existing)\n"; | ||
| 553 | print STDERR " NEW NAME2: $new_name (hard-coded)\n"; | ||
| 554 | } | ||
| 555 | $refactor_names_map{$suffix}{$old_name} = $new_name; | ||
| 556 | } | ||
| 557 | } | ||
| 558 | |||
| 559 | # dump to Perl module | ||
| 560 | my $mapfile = 'bin/dev/perl_types_refactor_names_map.pm'; | ||
| 561 | open my $map_filehandle_out, '>', $mapfile or die $!; | ||
| 562 | print $map_filehandle_out 'package Perl::Types::RefactorNamesMap;', "\n", | ||
| 563 | 'use strict; use warnings;', "\n", | ||
| 564 | 'our $refactor_names_map = '; | ||
| 565 | print $map_filehandle_out Dumper(\%refactor_names_map); | ||
| 566 | print $map_filehandle_out ";\n1;\n"; | ||
| 567 | close $map_filehandle_out; | ||
| 568 | |||
| 569 | exit 0; |