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