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 | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 187 | ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir, |
Ted Kremenek | 5daa3be | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 188 | $ParserRejects, $ofile); |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 189 | } |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 190 | else { |
| 191 | # Check if there were any unhandled attributes. |
| 192 | if (open(CHILD, $ofile)) { |
| 193 | my %attributes_not_handled; |
| 194 | my $ppfile; |
| 195 | while (<CHILD>) { |
| 196 | next if (! /warning: '([^\']+)' attribute ignored/); |
| 197 | |
| 198 | # Have we already spotted this unhandled attribute? |
| 199 | next if (defined $attributes_not_handled{$1}); |
| 200 | $attributes_not_handled{$1} = 1; |
| 201 | |
Ted Kremenek | 7e5bd6f | 2009-02-21 04:46:20 +0000 | [diff] [blame] | 202 | # Get the name of the attribute file. |
| 203 | my $dir = "$HtmlDir/failures"; |
| 204 | my $afile = "$dir/attribute_ignored_$1.txt"; |
| 205 | |
| 206 | # Only create another preprocessed file if the attribute file |
| 207 | # doesn't exist yet. |
| 208 | next if (-e $afile); |
| 209 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 210 | # Add this file to the list of files that contained this attribute. |
| 211 | # Generate a preprocessed file if we haven't already. |
| 212 | if (!(defined $ppfile)) { |
| 213 | $ppfile = ProcessClangFailure($Clang, $Lang, $file, |
| 214 | \@CmdArgsSansAnalyses, |
| 215 | $HtmlDir, $AttributeIgnored, $ofile); |
| 216 | } |
| 217 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 218 | mkpath $dir; |
Ted Kremenek | 7e5bd6f | 2009-02-21 04:46:20 +0000 | [diff] [blame] | 219 | open(AFILE, ">$afile"); |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 220 | print AFILE "$ppfile\n"; |
| 221 | close(AFILE); |
| 222 | } |
| 223 | close CHILD; |
| 224 | } |
| 225 | } |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 226 | |
| 227 | `rm -f $ofile`; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 228 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 229 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 230 | ##----------------------------------------------------------------------------## |
| 231 | # Lookup tables. |
| 232 | ##----------------------------------------------------------------------------## |
| 233 | |
| 234 | my %CompileOptionMap = ( |
| 235 | '-nostdinc' => 0, |
Anders Carlsson | 06c58b1 | 2008-12-19 20:56:23 +0000 | [diff] [blame] | 236 | '-fblocks' => 0, |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 237 | '-fobjc-gc-only' => 0, |
| 238 | '-fobjc-gc' => 0, |
| 239 | '-include' => 1, |
| 240 | '-idirafter' => 1, |
| 241 | '-iprefix' => 1, |
| 242 | '-iquote' => 1, |
| 243 | '-isystem' => 1, |
| 244 | '-iwithprefix' => 1, |
| 245 | '-iwithprefixbefore' => 1 |
| 246 | ); |
| 247 | |
| 248 | my %LinkerOptionMap = ( |
| 249 | '-framework' => 1 |
| 250 | ); |
| 251 | |
| 252 | my %CompilerLinkerOptionMap = ( |
| 253 | '-isysroot' => 1, |
| 254 | '-arch' => 1, |
Ted Kremenek | e4f6952 | 2008-09-29 22:45:28 +0000 | [diff] [blame] | 255 | '-v' => 0, |
Ted Kremenek | b10362a | 2008-09-30 23:40:25 +0000 | [diff] [blame] | 256 | '-fpascal-strings' => 0, |
| 257 | '-mmacosx-version-min' => 0 # This is really a 1 argument, but always has '=' |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 258 | ); |
| 259 | |
| 260 | my %IgnoredOptionMap = ( |
Ted Kremenek | 9402609 | 2008-07-24 03:52:21 +0000 | [diff] [blame] | 261 | '-MT' => 1, # Ignore these preprocessor options. |
| 262 | '-MF' => 1, |
| 263 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 264 | '-fsyntax-only' => 0, |
| 265 | '-save-temps' => 0, |
| 266 | '-install_name' => 1, |
| 267 | '-exported_symbols_list' => 1, |
| 268 | '-current_version' => 1, |
| 269 | '-compatibility_version' => 1, |
| 270 | '-init' => 1, |
| 271 | '-e' => 1, |
| 272 | '-seg1addr' => 1, |
| 273 | '-bundle_loader' => 1, |
| 274 | '-multiply_defined' => 1, |
| 275 | '-sectorder' => 3, |
| 276 | '--param' => 1, |
| 277 | '-u' => 1 |
| 278 | ); |
| 279 | |
| 280 | my %LangMap = ( |
| 281 | 'c' => 'c', |
| 282 | 'cpp' => 'c++', |
| 283 | 'cc' => 'c++', |
| 284 | 'i' => 'c-cpp-output', |
| 285 | 'm' => 'objective-c', |
| 286 | 'mi' => 'objective-c-cpp-output' |
| 287 | ); |
| 288 | |
Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 289 | my %UniqueOptions = ( |
| 290 | '-isysroot' => 0 |
| 291 | ); |
| 292 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 293 | ##----------------------------------------------------------------------------## |
| 294 | # Main Logic. |
| 295 | ##----------------------------------------------------------------------------## |
| 296 | |
| 297 | my $Action = 'link'; |
| 298 | my @CompileOpts; |
| 299 | my @LinkOpts; |
| 300 | my @Files; |
| 301 | my $Lang; |
| 302 | my $Output; |
Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 303 | my %Uniqued; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 304 | |
| 305 | # Forward arguments to gcc. |
Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 306 | my $Status = system($CC,@ARGV); |
Ted Kremenek | cb344d0 | 2008-08-28 01:18:44 +0000 | [diff] [blame] | 307 | if ($Status) { exit($Status >> 8); } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 308 | |
| 309 | # Get the analysis options. |
| 310 | my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'}; |
| 311 | if (!defined($Analyses)) { $Analyses = '-checker-cfref'; } |
| 312 | |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 313 | # Get the store model. |
| 314 | my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'}; |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 315 | if (!defined $StoreModel) { $StoreModel = "basic"; } |
| 316 | |
| 317 | # Get the constraints engine. |
| 318 | my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'}; |
Ted Kremenek | 9f4ecb3 | 2009-02-20 21:49:22 +0000 | [diff] [blame] | 319 | if (!defined $ConstraintsModel) { $ConstraintsModel = "range"; } |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 321 | # Get the output format. |
| 322 | my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'}; |
Ted Kremenek | 3891a15 | 2009-02-17 05:01:10 +0000 | [diff] [blame] | 323 | if (!defined $OutputFormat) { $OutputFormat = "html"; } |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 324 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 325 | # Determine the level of verbosity. |
| 326 | my $Verbose = 0; |
| 327 | if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; } |
| 328 | if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; } |
| 329 | |
| 330 | # Determine what clang executable to use. |
| 331 | my $Clang = $ENV{'CLANG'}; |
| 332 | if (!defined $Clang) { $Clang = 'clang'; } |
| 333 | |
| 334 | # Get the HTML output directory. |
| 335 | my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'}; |
| 336 | |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 337 | my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1); |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 338 | my %ArchsSeen; |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 339 | my $HadArch = 0; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 340 | |
| 341 | # Process the arguments. |
| 342 | foreach (my $i = 0; $i < scalar(@ARGV); ++$i) { |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 343 | my $Arg = $ARGV[$i]; |
| 344 | my ($ArgKey) = split /=/,$Arg,2; |
| 345 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 346 | # Modes ccc-analyzer supports |
| 347 | if ($Arg eq '-E') { $Action = 'preprocess'; } |
| 348 | elsif ($Arg eq '-c') { $Action = 'compile'; } |
| 349 | elsif ($Arg =~ /^-print-prog-name/) { exit 0; } |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 350 | |
| 351 | # Specially handle duplicate cases of -arch |
| 352 | if ($Arg eq "-arch") { |
| 353 | my $arch = $ARGV[$i+1]; |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 354 | # We don't want to process 'ppc' because of Clang's lack of support |
| 355 | # for Altivec (also some #defines won't likely be defined correctly, etc.) |
| 356 | if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; } |
| 357 | $HadArch = 1; |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 358 | ++$i; |
| 359 | next; |
| 360 | } |
| 361 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 362 | # Options with possible arguments that should pass through to compiler. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 363 | if (defined $CompileOptionMap{$ArgKey}) { |
| 364 | my $Cnt = $CompileOptionMap{$ArgKey}; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 365 | push @CompileOpts,$Arg; |
| 366 | while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; } |
| 367 | next; |
| 368 | } |
| 369 | |
| 370 | # Options with possible arguments that should pass through to linker. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 371 | if (defined $LinkerOptionMap{$ArgKey}) { |
| 372 | my $Cnt = $LinkerOptionMap{$ArgKey}; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 373 | push @LinkOpts,$Arg; |
| 374 | while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; } |
| 375 | next; |
| 376 | } |
| 377 | |
| 378 | # Options with possible arguments that should pass through to both compiler |
| 379 | # and the linker. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 380 | if (defined $CompilerLinkerOptionMap{$ArgKey}) { |
| 381 | my $Cnt = $CompilerLinkerOptionMap{$ArgKey}; |
Ted Kremenek | 47fc25f | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 382 | |
Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 383 | # Check if this is an option that should have a unique value, and if so |
| 384 | # determine if the value was checked before. |
| 385 | if ($UniqueOptions{$Arg}) { |
| 386 | if (defined $Uniqued{$Arg}) { |
| 387 | $i += $Cnt; |
| 388 | next; |
| 389 | } |
| 390 | $Uniqued{$Arg} = 1; |
| 391 | } |
| 392 | |
Ted Kremenek | 47fc25f | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 393 | push @CompileOpts,$Arg; |
| 394 | push @LinkOpts,$Arg; |
| 395 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 396 | while ($Cnt > 0) { |
| 397 | ++$i; --$Cnt; |
| 398 | push @CompileOpts, $ARGV[$i]; |
| 399 | push @LinkOpts, $ARGV[$i]; |
| 400 | } |
| 401 | next; |
| 402 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 403 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 404 | # Ignored options. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 405 | if (defined $IgnoredOptionMap{$ArgKey}) { |
| 406 | my $Cnt = $IgnoredOptionMap{$ArgKey}; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 407 | while ($Cnt > 0) { |
| 408 | ++$i; --$Cnt; |
| 409 | } |
| 410 | next; |
| 411 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 412 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 413 | # Compile mode flags. |
| 414 | if ($Arg =~ /^-[D,I,U](.*)$/) { |
| 415 | my $Tmp = $Arg; |
| 416 | if ($1 eq '') { |
| 417 | # FIXME: Check if we are going off the end. |
| 418 | ++$i; |
| 419 | $Tmp = $Arg . $ARGV[$i]; |
| 420 | } |
| 421 | push @CompileOpts,$Tmp; |
| 422 | next; |
| 423 | } |
| 424 | |
| 425 | # Language. |
| 426 | if ($Arg eq '-x') { |
| 427 | $Lang = $ARGV[$i+1]; |
| 428 | ++$i; next; |
| 429 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 430 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 431 | # Output file. |
| 432 | if ($Arg eq '-o') { |
| 433 | ++$i; |
| 434 | $Output = $ARGV[$i]; |
| 435 | next; |
| 436 | } |
| 437 | |
| 438 | # Get the link mode. |
| 439 | if ($Arg =~ /^-[l,L,O]/) { |
| 440 | if ($Arg eq '-O') { push @LinkOpts,'-O1'; } |
| 441 | elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; } |
| 442 | else { push @LinkOpts,$Arg; } |
| 443 | next; |
| 444 | } |
| 445 | |
| 446 | if ($Arg =~ /^-std=/) { |
| 447 | push @CompileOpts,$Arg; |
| 448 | next; |
| 449 | } |
| 450 | |
| 451 | # if ($Arg =~ /^-f/) { |
| 452 | # # FIXME: Not sure if the remaining -fxxxx options have no arguments. |
| 453 | # push @CompileOpts,$Arg; |
| 454 | # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts. |
| 455 | # } |
| 456 | |
| 457 | # Get the compiler/link mode. |
| 458 | if ($Arg =~ /^-F(.+)$/) { |
| 459 | my $Tmp = $Arg; |
| 460 | if ($1 eq '') { |
| 461 | # FIXME: Check if we are going off the end. |
| 462 | ++$i; |
| 463 | $Tmp = $Arg . $ARGV[$i]; |
| 464 | } |
| 465 | push @CompileOpts,$Tmp; |
| 466 | push @LinkOpts,$Tmp; |
| 467 | next; |
| 468 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 469 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 470 | # Input files. |
| 471 | if ($Arg eq '-filelist') { |
| 472 | # FIXME: Make sure we aren't walking off the end. |
| 473 | open(IN, $ARGV[$i+1]); |
| 474 | while (<IN>) { s/\015?\012//; push @Files,$_; } |
| 475 | close(IN); |
| 476 | ++$i; next; |
| 477 | } |
| 478 | |
| 479 | if (!($Arg =~ /^-/)) { |
| 480 | push @Files,$Arg; next; |
| 481 | } |
| 482 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 484 | if ($Action eq 'compile' or $Action eq 'link') { |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 485 | my @Archs = keys %ArchsSeen; |
| 486 | # Skip the file if we don't support the architectures specified. |
Ted Kremenek | 0e0eb8b | 2009-02-25 00:10:37 +0000 | [diff] [blame] | 487 | exit 0 if ($HadArch && scalar(@Archs) == 0); |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 488 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 489 | foreach my $file (@Files) { |
| 490 | # Determine the language for the file. |
| 491 | my $FileLang = $Lang; |
| 492 | |
| 493 | if (!defined($FileLang)) { |
| 494 | # Infer the language from the extension. |
| 495 | if ($file =~ /[.]([^.]+)$/) { |
| 496 | $FileLang = $LangMap{$1}; |
| 497 | } |
| 498 | } |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 499 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 500 | next if (!defined $FileLang); |
| 501 | |
| 502 | my @AnalyzeArgs; |
| 503 | |
| 504 | if ($FileLang ne 'unknown') { |
| 505 | push @AnalyzeArgs,'-x'; |
| 506 | push @AnalyzeArgs,$FileLang; |
| 507 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 508 | |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 509 | if (defined $StoreModel) { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 510 | push @AnalyzeArgs, "-analyzer-store=$StoreModel"; |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 511 | } |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 512 | |
| 513 | if (defined $ConstraintsModel) { |
| 514 | push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel"; |
| 515 | } |
| 516 | |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 517 | if (defined $OutputFormat) { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 518 | push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 519 | if ($OutputFormat eq "plist") { |
| 520 | # Change "Output" to be a file. |
| 521 | my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist", |
| 522 | DIR => $HtmlDir); |
| 523 | $ResultFile = $f; |
| 524 | $CleanupFile = $f; |
| 525 | } |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 526 | } |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 527 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 528 | push @AnalyzeArgs,@CompileOpts; |
| 529 | push @AnalyzeArgs,$file; |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 530 | |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 531 | if (scalar @Archs) { |
| 532 | foreach my $arch (@Archs) { |
| 533 | my @NewArgs; |
| 534 | push @NewArgs, '-arch'; |
| 535 | push @NewArgs, $arch; |
| 536 | push @NewArgs, @AnalyzeArgs; |
| 537 | Analyze($Clang, \@NewArgs, $FileLang, $Output, |
| 538 | $Verbose, $HtmlDir, $file, $Analyses); |
| 539 | } |
| 540 | } |
| 541 | else { |
| 542 | Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output, |
| 543 | $Verbose, $HtmlDir, $file, $Analyses); |
| 544 | } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 547 | |
Ted Kremenek | 948e06b | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 548 | exit($Status >> 8); |
| 549 | |