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