xref: /llvm-project/clang/utils/analyzer/reducer.pl (revision a54f160b3a98b91cd241a555d904a6b6453affc4)
1#!/usr/bin/env perl
2use strict;
3use warnings;
4use File::Temp qw/ tempdir /;
5my $prog = "reducer";
6
7die "$prog <code file> <error string> [optional command]\n" if ($#ARGV < 0);
8my $file = shift @ARGV;
9die "$prog: [error] cannot read file $file\n" if (! -r $file);
10
11my $magic = shift @ARGV;
12die "$prog: [error] no error string specified\n" if (! defined $magic);
13
14# Create a backup of the file.
15my $dir = tempdir( CLEANUP => 1 );
16print "$prog: created temporary directory '$dir'\n";
17my $srcFile = "$dir/$file";
18`cp $file $srcFile`;
19
20# Create the script.
21my $scriptFile = "$dir/script";
22open(OUT, ">$scriptFile") or die "$prog: cannot create '$scriptFile'\n";
23my $reduceOut = "$dir/reduceOut";
24
25my $command;
26if (scalar(@ARGV) > 0) { $command = \@ARGV; }
27else {
28  my $compiler = "clang";
29  $command = [$compiler, "-fsyntax-only", "-Wfatal-errors", "-Wno-deprecated-declarations", "-Wimplicit-function-declaration"];
30}
31push @$command, $srcFile;
32my $commandStr = "@$command";
33
34print OUT <<ENDTEXT;
35#!/usr/bin/env perl
36use strict;
37use warnings;
38my \$BAD = 1;
39my \$GOOD = 0;
40`rm -f $reduceOut`;
41my \$command = "$commandStr > $reduceOut 2>&1";
42system(\$command);
43open(IN, "$reduceOut") or exit(\$BAD);
44my \$found = 0;
45while(<IN>) {
46  if (/$magic/) { exit \$GOOD; }
47}
48exit \$BAD;
49ENDTEXT
50close(OUT);
51`chmod +x $scriptFile`;
52
53print "$prog: starting reduction\n";
54sub multidelta($) {
55    my ($level) = @_;
56    system("multidelta -level=$level $scriptFile $srcFile");
57}
58
59for (my $i = 1 ; $i <= 5; $i++) {
60  foreach my $level (0,0,1,1,2,2,10) {
61    multidelta($level);
62  }
63}
64
65# Copy the final file.
66`cp $srcFile $file.reduced`;
67print "$prog: generated '$file.reduced";
68