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