Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 2 | # |
| 3 | # The LLVM Compiler Infrastructure |
| 4 | # |
| 5 | # This file is distributed under the University of Illinois Open Source |
| 6 | # License. See LICENSE.TXT for details. |
| 7 | # |
| 8 | ##===----------------------------------------------------------------------===## |
| 9 | # |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 10 | # A script designed to interpose between the build system and gcc. It invokes |
| 11 | # both gcc and the static analyzer. |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 12 | # |
| 13 | ##===----------------------------------------------------------------------===## |
| 14 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 15 | use strict; |
| 16 | use warnings; |
Ted Kremenek | b315a39 | 2008-09-21 19:56:14 +0000 | [diff] [blame] | 17 | use Cwd qw/ getcwd abs_path /; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 18 | use File::Temp qw/ tempfile /; |
| 19 | use File::Path qw / mkpath /; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 20 | use File::Basename; |
Ted Kremenek | 2ec5cd5 | 2008-08-25 20:44:31 +0000 | [diff] [blame] | 21 | |
| 22 | my $CC = $ENV{'CCC_CC'}; |
| 23 | if (!defined $CC) { $CC = "gcc"; } |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 24 | my $CleanupFile; |
| 25 | my $ResultFile; |
| 26 | |
| 27 | # Remove any stale files at exit. |
| 28 | END { |
| 29 | if (defined $CleanupFile && -z $CleanupFile) { |
| 30 | `rm -f $CleanupFile`; |
| 31 | } |
| 32 | } |
| 33 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 34 | ##----------------------------------------------------------------------------## |
| 35 | # Process Clang Crashes. |
| 36 | ##----------------------------------------------------------------------------## |
| 37 | |
| 38 | sub GetPPExt { |
| 39 | my $Lang = shift; |
| 40 | if ($Lang =~ /objective-c/) { return ".mi"; } |
| 41 | return ".i"; |
| 42 | } |
| 43 | |
Ted Kremenek | a4d8cde | 2009-04-28 17:37:44 +0000 | [diff] [blame] | 44 | # Set this to 1 if we want to include 'parser rejects' files. |
| 45 | my $IncludeParserRejects = 0; |
Ted Kremenek | 5daa3be | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 46 | my $ParserRejects = "Parser Rejects"; |
Ted Kremenek | a4d8cde | 2009-04-28 17:37:44 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 48 | my $AttributeIgnored = "Attribute Ignored"; |
Ted Kremenek | 5daa3be | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 50 | sub ProcessClangFailure { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 51 | my ($ClangCC, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 52 | my $Dir = "$HtmlDir/failures"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 53 | mkpath $Dir; |
Ted Kremenek | 5daa3be | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 54 | |
| 55 | my $prefix = "clang_crash"; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 56 | if ($ErrorType eq $ParserRejects) { |
| 57 | $prefix = "clang_parser_rejects"; |
| 58 | } |
| 59 | elsif ($ErrorType eq $AttributeIgnored) { |
| 60 | $prefix = "clang_attribute_ignored"; |
| 61 | } |
Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 62 | |
| 63 | # Generate the preprocessed file with cc (i.e., gcc). |
Ted Kremenek | 5daa3be | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 64 | my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX", |
| 65 | SUFFIX => GetPPExt($Lang), |
| 66 | DIR => $Dir); |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | 2ec5cd5 | 2008-08-25 20:44:31 +0000 | [diff] [blame] | 68 | system $CC, @$Args, "-E", "-o", $PPFile; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 69 | close ($PPH); |
Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 70 | |
| 71 | # Generate the preprocessed file with clang. |
| 72 | my $PPFile_Clang = $PPFile; |
| 73 | $PPFile_Clang =~ s/[.](.+)$/.clang.$1/; |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 74 | system $ClangCC, @$Args, "-E", "-o", "$PPFile_Clang"; |
Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 75 | |
| 76 | # Create the info file. |
Ted Kremenek | 82a1253 | 2008-09-25 00:25:16 +0000 | [diff] [blame] | 77 | open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n"; |
Ted Kremenek | 5f2825f | 2008-09-21 18:04:49 +0000 | [diff] [blame] | 78 | print OUT abs_path($file), "\n"; |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 79 | print OUT "$ErrorType\n"; |
Ted Kremenek | 2dd7ad1 | 2008-08-18 20:55:25 +0000 | [diff] [blame] | 80 | print OUT "@$Args\n"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 81 | close OUT; |
Ted Kremenek | 82a1253 | 2008-09-25 00:25:16 +0000 | [diff] [blame] | 82 | `uname -a >> $PPFile.info.txt 2>&1`; |
| 83 | `$CC -v >> $PPFile.info.txt 2>&1`; |
Ted Kremenek | 9f9b1fd | 2008-09-12 22:49:36 +0000 | [diff] [blame] | 84 | system 'mv',$ofile,"$PPFile.stderr.txt"; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 85 | return (basename $PPFile); |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 86 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 88 | ##----------------------------------------------------------------------------## |
| 89 | # Running the analyzer. |
| 90 | ##----------------------------------------------------------------------------## |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 92 | # Determine what clang executable to use. |
| 93 | my $Clang = $ENV{'CLANG'}; |
| 94 | if (!defined $Clang) { $Clang = 'clang'; } |
| 95 | |
| 96 | sub GetCCArgs { |
| 97 | my $Args = shift; |
| 98 | |
| 99 | pipe (FROM_CHILD, TO_PARENT); |
| 100 | my $pid = fork(); |
| 101 | if ($pid == 0) { |
| 102 | close FROM_CHILD; |
| 103 | open(STDOUT,">&", \*TO_PARENT); |
| 104 | open(STDERR,">&", \*TO_PARENT); |
| 105 | exec $Clang, "-###", "-fsyntax-only", @$Args; |
| 106 | } |
| 107 | close(TO_PARENT); |
| 108 | my $line; |
| 109 | while (<FROM_CHILD>) { |
| 110 | next if (!/clang-cc/); |
| 111 | $line = $_; |
| 112 | } |
| 113 | |
| 114 | waitpid($pid,0); |
| 115 | close(FROM_CHILD); |
| 116 | |
| 117 | die "could not find clang-cc line\n" if (!defined $line); |
| 118 | my @items = ($line =~ /(".*?"|\S+)/g); |
| 119 | |
| 120 | for (my $i = 0; $i < scalar(@items); ++$i) { |
| 121 | my $x = $items[$i]; |
| 122 | $x =~ s/^"//; |
| 123 | $x =~ s/"$//; |
| 124 | $items[$i] = $x; |
| 125 | } |
| 126 | |
| 127 | my $cmd = shift @items; |
| 128 | die "cannot find 'clang-cc' in 'clang' command\n" if (!($cmd =~ /clang-cc/)); |
| 129 | return \@items; |
| 130 | } |
| 131 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 132 | sub Analyze { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 133 | my ($ClangCC, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_; |
Ted Kremenek | efaf191 | 2009-05-11 21:08:34 +0000 | [diff] [blame^] | 134 | |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 135 | $Args = GetCCArgs($Args); |
Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 137 | # Skip anything related to C++. |
| 138 | return if ($Lang =~ /c[+][+]/); |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 140 | my $RunAnalyzer = 0; |
| 141 | my $Cmd; |
| 142 | my @CmdArgs; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 143 | my @CmdArgsSansAnalyses; |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 145 | if ($Lang =~ /header/) { |
| 146 | exit 0 if (!defined ($Output)); |
| 147 | $Cmd = 'cp'; |
| 148 | push @CmdArgs,$file; |
| 149 | # Remove the PCH extension. |
| 150 | $Output =~ s/[.]gch$//; |
| 151 | push @CmdArgs,$Output; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 152 | @CmdArgsSansAnalyses = @CmdArgs; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 153 | } |
| 154 | else { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 155 | $Cmd = $ClangCC; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 156 | push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))'; |
| 157 | push @CmdArgs,@$Args; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 158 | @CmdArgsSansAnalyses = @CmdArgs; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 159 | push @CmdArgs,'-analyze'; |
| 160 | push @CmdArgs,"-analyzer-display-progress"; |
Ted Kremenek | a2bdaf5 | 2009-02-26 17:36:31 +0000 | [diff] [blame] | 161 | push @CmdArgs,"-analyzer-eagerly-assume"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 162 | push @CmdArgs,(split /\s/,$Analyses); |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 163 | $RunAnalyzer = 1; |
| 164 | } |
| 165 | |
| 166 | my @PrintArgs; |
| 167 | my $dir; |
| 168 | |
| 169 | if ($Verbose) { |
| 170 | $dir = getcwd(); |
| 171 | print STDERR "\n[LOCATION]: $dir\n"; |
| 172 | push @PrintArgs,"'$Cmd'"; |
| 173 | foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; } |
| 174 | } |
| 175 | |
| 176 | if ($Verbose == 1) { |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 177 | # We MUST print to stderr. Some clients use the stdout output of |
| 178 | # gcc for various purposes. |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 179 | print STDERR join(' ',@PrintArgs); |
| 180 | print STDERR "\n"; |
| 181 | } |
| 182 | elsif ($Verbose == 2) { |
| 183 | print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n"; |
| 184 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 186 | if ($RunAnalyzer) { |
| 187 | if (defined $ResultFile) { |
| 188 | push @CmdArgs,'-o'; |
| 189 | push @CmdArgs, $ResultFile; |
| 190 | } |
| 191 | elsif (defined $HtmlDir) { |
| 192 | push @CmdArgs,'-o'; |
| 193 | push @CmdArgs, $HtmlDir; |
| 194 | } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 195 | } |
Ted Kremenek | 948e06b | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 196 | |
| 197 | if (defined $ENV{'CCC_UBI'}) { |
| 198 | push @CmdArgs,"--analyzer-viz-egraph-ubigraph"; |
| 199 | } |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 201 | # Capture the STDERR of clang and send it to a temporary file. |
| 202 | # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR. |
| 203 | # We save the output file in the 'crashes' directory if clang encounters |
| 204 | # any problems with the file. |
Ted Kremenek | 1346268 | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 205 | pipe (FROM_CHILD, TO_PARENT); |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 206 | my $pid = fork(); |
| 207 | if ($pid == 0) { |
Ted Kremenek | 1346268 | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 208 | close FROM_CHILD; |
| 209 | open(STDOUT,">&", \*TO_PARENT); |
| 210 | open(STDERR,">&", \*TO_PARENT); |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 211 | exec $Cmd, @CmdArgs; |
| 212 | } |
Ted Kremenek | 1346268 | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 213 | |
| 214 | close TO_PARENT; |
| 215 | my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir); |
| 216 | |
| 217 | while (<FROM_CHILD>) { |
| 218 | print $ofh $_; |
| 219 | print STDERR $_; |
| 220 | } |
| 221 | |
| 222 | waitpid($pid,0); |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 223 | close(FROM_CHILD); |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 224 | my $Result = $?; |
| 225 | |
| 226 | # Did the command die because of a signal? |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 227 | if ($Result & 127 and $Cmd eq $ClangCC and defined $HtmlDir) { |
| 228 | ProcessClangFailure($ClangCC, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir, |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 229 | "Crash", $ofile); |
| 230 | } |
| 231 | elsif ($Result) { |
Ted Kremenek | a4d8cde | 2009-04-28 17:37:44 +0000 | [diff] [blame] | 232 | if ($IncludeParserRejects && !($file =~/conftest/)) { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 233 | ProcessClangFailure($ClangCC, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir, |
Ted Kremenek | 5de4092 | 2009-02-27 06:17:38 +0000 | [diff] [blame] | 234 | $ParserRejects, $ofile); |
| 235 | } |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 236 | } |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 237 | else { |
| 238 | # Check if there were any unhandled attributes. |
| 239 | if (open(CHILD, $ofile)) { |
| 240 | my %attributes_not_handled; |
Ted Kremenek | 32e9464 | 2009-04-22 13:42:27 +0000 | [diff] [blame] | 241 | |
| 242 | # Don't flag warnings about the following attributes that we |
| 243 | # know are currently not supported by Clang. |
| 244 | $attributes_not_handled{"cdecl"} = 1; |
| 245 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 246 | my $ppfile; |
| 247 | while (<CHILD>) { |
| 248 | next if (! /warning: '([^\']+)' attribute ignored/); |
| 249 | |
| 250 | # Have we already spotted this unhandled attribute? |
| 251 | next if (defined $attributes_not_handled{$1}); |
| 252 | $attributes_not_handled{$1} = 1; |
| 253 | |
Ted Kremenek | 7e5bd6f | 2009-02-21 04:46:20 +0000 | [diff] [blame] | 254 | # Get the name of the attribute file. |
| 255 | my $dir = "$HtmlDir/failures"; |
| 256 | my $afile = "$dir/attribute_ignored_$1.txt"; |
| 257 | |
| 258 | # Only create another preprocessed file if the attribute file |
| 259 | # doesn't exist yet. |
| 260 | next if (-e $afile); |
| 261 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 262 | # Add this file to the list of files that contained this attribute. |
| 263 | # Generate a preprocessed file if we haven't already. |
| 264 | if (!(defined $ppfile)) { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 265 | $ppfile = ProcessClangFailure($ClangCC, $Lang, $file, |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 266 | \@CmdArgsSansAnalyses, |
| 267 | $HtmlDir, $AttributeIgnored, $ofile); |
| 268 | } |
| 269 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 270 | mkpath $dir; |
Ted Kremenek | 7e5bd6f | 2009-02-21 04:46:20 +0000 | [diff] [blame] | 271 | open(AFILE, ">$afile"); |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 272 | print AFILE "$ppfile\n"; |
| 273 | close(AFILE); |
| 274 | } |
| 275 | close CHILD; |
| 276 | } |
| 277 | } |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 278 | |
| 279 | `rm -f $ofile`; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 280 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 281 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 282 | ##----------------------------------------------------------------------------## |
| 283 | # Lookup tables. |
| 284 | ##----------------------------------------------------------------------------## |
| 285 | |
| 286 | my %CompileOptionMap = ( |
| 287 | '-nostdinc' => 0, |
Anders Carlsson | 06c58b1 | 2008-12-19 20:56:23 +0000 | [diff] [blame] | 288 | '-fblocks' => 0, |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 289 | '-fobjc-gc-only' => 0, |
Ted Kremenek | 6c4312d | 2009-02-26 23:09:43 +0000 | [diff] [blame] | 290 | '-fobjc-gc' => 0, |
| 291 | '-ffreestanding' => 0, |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 292 | '-include' => 1, |
| 293 | '-idirafter' => 1, |
| 294 | '-iprefix' => 1, |
| 295 | '-iquote' => 1, |
| 296 | '-isystem' => 1, |
| 297 | '-iwithprefix' => 1, |
| 298 | '-iwithprefixbefore' => 1 |
| 299 | ); |
| 300 | |
| 301 | my %LinkerOptionMap = ( |
| 302 | '-framework' => 1 |
| 303 | ); |
| 304 | |
| 305 | my %CompilerLinkerOptionMap = ( |
| 306 | '-isysroot' => 1, |
| 307 | '-arch' => 1, |
Ted Kremenek | e4f6952 | 2008-09-29 22:45:28 +0000 | [diff] [blame] | 308 | '-v' => 0, |
Ted Kremenek | b10362a | 2008-09-30 23:40:25 +0000 | [diff] [blame] | 309 | '-fpascal-strings' => 0, |
Daniel Dunbar | 8d33cd7 | 2009-04-10 19:52:24 +0000 | [diff] [blame] | 310 | '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '=' |
| 311 | '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '=' |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 312 | ); |
| 313 | |
| 314 | my %IgnoredOptionMap = ( |
Ted Kremenek | 9402609 | 2008-07-24 03:52:21 +0000 | [diff] [blame] | 315 | '-MT' => 1, # Ignore these preprocessor options. |
| 316 | '-MF' => 1, |
| 317 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 318 | '-fsyntax-only' => 0, |
| 319 | '-save-temps' => 0, |
| 320 | '-install_name' => 1, |
| 321 | '-exported_symbols_list' => 1, |
| 322 | '-current_version' => 1, |
| 323 | '-compatibility_version' => 1, |
| 324 | '-init' => 1, |
| 325 | '-e' => 1, |
| 326 | '-seg1addr' => 1, |
| 327 | '-bundle_loader' => 1, |
| 328 | '-multiply_defined' => 1, |
| 329 | '-sectorder' => 3, |
| 330 | '--param' => 1, |
| 331 | '-u' => 1 |
| 332 | ); |
| 333 | |
| 334 | my %LangMap = ( |
| 335 | 'c' => 'c', |
| 336 | 'cpp' => 'c++', |
| 337 | 'cc' => 'c++', |
| 338 | 'i' => 'c-cpp-output', |
| 339 | 'm' => 'objective-c', |
| 340 | 'mi' => 'objective-c-cpp-output' |
| 341 | ); |
| 342 | |
Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 343 | my %UniqueOptions = ( |
| 344 | '-isysroot' => 0 |
| 345 | ); |
| 346 | |
Ted Kremenek | efaf191 | 2009-05-11 21:08:34 +0000 | [diff] [blame^] | 347 | my %LangsAccepted = ( |
| 348 | "objective-c" => 1, |
| 349 | "c" => 1 |
| 350 | ); |
| 351 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 352 | ##----------------------------------------------------------------------------## |
| 353 | # Main Logic. |
| 354 | ##----------------------------------------------------------------------------## |
| 355 | |
| 356 | my $Action = 'link'; |
| 357 | my @CompileOpts; |
| 358 | my @LinkOpts; |
| 359 | my @Files; |
| 360 | my $Lang; |
| 361 | my $Output; |
Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 362 | my %Uniqued; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 363 | |
| 364 | # Forward arguments to gcc. |
Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 365 | my $Status = system($CC,@ARGV); |
Ted Kremenek | cb344d0 | 2008-08-28 01:18:44 +0000 | [diff] [blame] | 366 | if ($Status) { exit($Status >> 8); } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 367 | |
| 368 | # Get the analysis options. |
| 369 | my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'}; |
| 370 | if (!defined($Analyses)) { $Analyses = '-checker-cfref'; } |
| 371 | |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 372 | # Get the store model. |
| 373 | my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'}; |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 374 | if (!defined $StoreModel) { $StoreModel = "basic"; } |
| 375 | |
| 376 | # Get the constraints engine. |
| 377 | my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'}; |
Ted Kremenek | 9f4ecb3 | 2009-02-20 21:49:22 +0000 | [diff] [blame] | 378 | if (!defined $ConstraintsModel) { $ConstraintsModel = "range"; } |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 380 | # Get the output format. |
| 381 | my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'}; |
Ted Kremenek | 3891a15 | 2009-02-17 05:01:10 +0000 | [diff] [blame] | 382 | if (!defined $OutputFormat) { $OutputFormat = "html"; } |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 383 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 384 | # Determine the level of verbosity. |
| 385 | my $Verbose = 0; |
| 386 | if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; } |
| 387 | if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; } |
| 388 | |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 389 | # Determine what clang-cc executable to use. |
| 390 | my $ClangCC = $ENV{'CLANG_CC'}; |
| 391 | if (!defined $ClangCC) { $ClangCC = 'clang-cc'; } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 392 | |
| 393 | # Get the HTML output directory. |
| 394 | my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'}; |
| 395 | |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 396 | my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1); |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 397 | my %ArchsSeen; |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 398 | my $HadArch = 0; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 399 | |
| 400 | # Process the arguments. |
| 401 | foreach (my $i = 0; $i < scalar(@ARGV); ++$i) { |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 402 | my $Arg = $ARGV[$i]; |
| 403 | my ($ArgKey) = split /=/,$Arg,2; |
| 404 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 405 | # Modes ccc-analyzer supports |
| 406 | if ($Arg eq '-E') { $Action = 'preprocess'; } |
| 407 | elsif ($Arg eq '-c') { $Action = 'compile'; } |
| 408 | elsif ($Arg =~ /^-print-prog-name/) { exit 0; } |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 409 | |
| 410 | # Specially handle duplicate cases of -arch |
| 411 | if ($Arg eq "-arch") { |
| 412 | my $arch = $ARGV[$i+1]; |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 413 | # We don't want to process 'ppc' because of Clang's lack of support |
| 414 | # for Altivec (also some #defines won't likely be defined correctly, etc.) |
| 415 | if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; } |
| 416 | $HadArch = 1; |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 417 | ++$i; |
| 418 | next; |
| 419 | } |
| 420 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 421 | # Options with possible arguments that should pass through to compiler. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 422 | if (defined $CompileOptionMap{$ArgKey}) { |
| 423 | my $Cnt = $CompileOptionMap{$ArgKey}; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 424 | push @CompileOpts,$Arg; |
| 425 | while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; } |
| 426 | next; |
| 427 | } |
| 428 | |
| 429 | # Options with possible arguments that should pass through to linker. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 430 | if (defined $LinkerOptionMap{$ArgKey}) { |
| 431 | my $Cnt = $LinkerOptionMap{$ArgKey}; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 432 | push @LinkOpts,$Arg; |
| 433 | while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; } |
| 434 | next; |
| 435 | } |
| 436 | |
| 437 | # Options with possible arguments that should pass through to both compiler |
| 438 | # and the linker. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 439 | if (defined $CompilerLinkerOptionMap{$ArgKey}) { |
| 440 | my $Cnt = $CompilerLinkerOptionMap{$ArgKey}; |
Ted Kremenek | 47fc25f | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 442 | # Check if this is an option that should have a unique value, and if so |
| 443 | # determine if the value was checked before. |
| 444 | if ($UniqueOptions{$Arg}) { |
| 445 | if (defined $Uniqued{$Arg}) { |
| 446 | $i += $Cnt; |
| 447 | next; |
| 448 | } |
| 449 | $Uniqued{$Arg} = 1; |
| 450 | } |
| 451 | |
Ted Kremenek | 47fc25f | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 452 | push @CompileOpts,$Arg; |
| 453 | push @LinkOpts,$Arg; |
| 454 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 455 | while ($Cnt > 0) { |
| 456 | ++$i; --$Cnt; |
| 457 | push @CompileOpts, $ARGV[$i]; |
| 458 | push @LinkOpts, $ARGV[$i]; |
| 459 | } |
| 460 | next; |
| 461 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 462 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 463 | # Ignored options. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 464 | if (defined $IgnoredOptionMap{$ArgKey}) { |
| 465 | my $Cnt = $IgnoredOptionMap{$ArgKey}; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 466 | while ($Cnt > 0) { |
| 467 | ++$i; --$Cnt; |
| 468 | } |
| 469 | next; |
| 470 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 471 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 472 | # Compile mode flags. |
| 473 | if ($Arg =~ /^-[D,I,U](.*)$/) { |
| 474 | my $Tmp = $Arg; |
| 475 | if ($1 eq '') { |
| 476 | # FIXME: Check if we are going off the end. |
| 477 | ++$i; |
| 478 | $Tmp = $Arg . $ARGV[$i]; |
| 479 | } |
| 480 | push @CompileOpts,$Tmp; |
| 481 | next; |
| 482 | } |
| 483 | |
| 484 | # Language. |
| 485 | if ($Arg eq '-x') { |
| 486 | $Lang = $ARGV[$i+1]; |
| 487 | ++$i; next; |
| 488 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 489 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 490 | # Output file. |
| 491 | if ($Arg eq '-o') { |
| 492 | ++$i; |
| 493 | $Output = $ARGV[$i]; |
| 494 | next; |
| 495 | } |
| 496 | |
| 497 | # Get the link mode. |
| 498 | if ($Arg =~ /^-[l,L,O]/) { |
| 499 | if ($Arg eq '-O') { push @LinkOpts,'-O1'; } |
| 500 | elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; } |
| 501 | else { push @LinkOpts,$Arg; } |
| 502 | next; |
| 503 | } |
| 504 | |
| 505 | if ($Arg =~ /^-std=/) { |
| 506 | push @CompileOpts,$Arg; |
| 507 | next; |
| 508 | } |
| 509 | |
| 510 | # if ($Arg =~ /^-f/) { |
| 511 | # # FIXME: Not sure if the remaining -fxxxx options have no arguments. |
| 512 | # push @CompileOpts,$Arg; |
| 513 | # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts. |
| 514 | # } |
| 515 | |
| 516 | # Get the compiler/link mode. |
| 517 | if ($Arg =~ /^-F(.+)$/) { |
| 518 | my $Tmp = $Arg; |
| 519 | if ($1 eq '') { |
| 520 | # FIXME: Check if we are going off the end. |
| 521 | ++$i; |
| 522 | $Tmp = $Arg . $ARGV[$i]; |
| 523 | } |
| 524 | push @CompileOpts,$Tmp; |
| 525 | push @LinkOpts,$Tmp; |
| 526 | next; |
| 527 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 528 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 529 | # Input files. |
| 530 | if ($Arg eq '-filelist') { |
| 531 | # FIXME: Make sure we aren't walking off the end. |
| 532 | open(IN, $ARGV[$i+1]); |
| 533 | while (<IN>) { s/\015?\012//; push @Files,$_; } |
| 534 | close(IN); |
| 535 | ++$i; next; |
| 536 | } |
| 537 | |
| 538 | if (!($Arg =~ /^-/)) { |
| 539 | push @Files,$Arg; next; |
| 540 | } |
| 541 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 542 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 543 | if ($Action eq 'compile' or $Action eq 'link') { |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 544 | my @Archs = keys %ArchsSeen; |
| 545 | # Skip the file if we don't support the architectures specified. |
Ted Kremenek | 0e0eb8b | 2009-02-25 00:10:37 +0000 | [diff] [blame] | 546 | exit 0 if ($HadArch && scalar(@Archs) == 0); |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 547 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 548 | foreach my $file (@Files) { |
| 549 | # Determine the language for the file. |
| 550 | my $FileLang = $Lang; |
| 551 | |
| 552 | if (!defined($FileLang)) { |
| 553 | # Infer the language from the extension. |
| 554 | if ($file =~ /[.]([^.]+)$/) { |
| 555 | $FileLang = $LangMap{$1}; |
| 556 | } |
| 557 | } |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 558 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 559 | next if (!defined $FileLang); |
Ted Kremenek | efaf191 | 2009-05-11 21:08:34 +0000 | [diff] [blame^] | 560 | next if (!defined $LangsAccepted{$FileLang}); |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 561 | |
| 562 | my @AnalyzeArgs; |
| 563 | |
| 564 | if ($FileLang ne 'unknown') { |
| 565 | push @AnalyzeArgs,'-x'; |
| 566 | push @AnalyzeArgs,$FileLang; |
| 567 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 568 | |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 569 | if (defined $StoreModel) { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 570 | push @AnalyzeArgs, "-analyzer-store=$StoreModel"; |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 571 | } |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 572 | |
| 573 | if (defined $ConstraintsModel) { |
| 574 | push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel"; |
| 575 | } |
| 576 | |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 577 | if (defined $OutputFormat) { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 578 | push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 579 | if ($OutputFormat eq "plist") { |
| 580 | # Change "Output" to be a file. |
| 581 | my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist", |
| 582 | DIR => $HtmlDir); |
| 583 | $ResultFile = $f; |
| 584 | $CleanupFile = $f; |
| 585 | } |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 586 | } |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 587 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 588 | push @AnalyzeArgs,@CompileOpts; |
| 589 | push @AnalyzeArgs,$file; |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 590 | |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 591 | if (scalar @Archs) { |
| 592 | foreach my $arch (@Archs) { |
| 593 | my @NewArgs; |
| 594 | push @NewArgs, '-arch'; |
| 595 | push @NewArgs, $arch; |
| 596 | push @NewArgs, @AnalyzeArgs; |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 597 | Analyze($ClangCC, \@NewArgs, $FileLang, $Output, |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 598 | $Verbose, $HtmlDir, $file, $Analyses); |
| 599 | } |
| 600 | } |
| 601 | else { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 602 | Analyze($ClangCC, \@AnalyzeArgs, $FileLang, $Output, |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 603 | $Verbose, $HtmlDir, $file, $Analyses); |
| 604 | } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 607 | |
Ted Kremenek | 948e06b | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 608 | exit($Status >> 8); |
| 609 | |