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