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