blob: 8bb1d7587a0ae82a36d1290f771f390b3d360c67 [file] [log] [blame]
Ted Kremenekfbeeca82008-07-19 06:11:04 +00001#!/usr/bin/env perl
Ted Kremenekb0982882008-03-25 22:35:32 +00002#
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 Kremenekfbeeca82008-07-19 06:11:04 +000010# A script designed to interpose between the build system and gcc. It invokes
11# both gcc and the static analyzer.
Ted Kremenekb0982882008-03-25 22:35:32 +000012#
13##===----------------------------------------------------------------------===##
14
Ted Kremenekfbeeca82008-07-19 06:11:04 +000015use strict;
16use warnings;
Ted Kremenekb315a392008-09-21 19:56:14 +000017use Cwd qw/ getcwd abs_path /;
Ted Kremenek991c54b2008-08-08 20:46:42 +000018use File::Temp qw/ tempfile /;
19use File::Path qw / mkpath /;
Ted Kremenekddf32da2009-01-21 00:42:24 +000020use File::Basename;
Ted Kremenek2ec5cd52008-08-25 20:44:31 +000021
22my $CC = $ENV{'CCC_CC'};
23if (!defined $CC) { $CC = "gcc"; }
Ted Kremenekddf32da2009-01-21 00:42:24 +000024my $CleanupFile;
25my $ResultFile;
26
27# Remove any stale files at exit.
28END {
29 if (defined $CleanupFile && -z $CleanupFile) {
30 `rm -f $CleanupFile`;
31 }
32}
33
Ted Kremenek991c54b2008-08-08 20:46:42 +000034##----------------------------------------------------------------------------##
35# Process Clang Crashes.
36##----------------------------------------------------------------------------##
37
38sub GetPPExt {
39 my $Lang = shift;
40 if ($Lang =~ /objective-c/) { return ".mi"; }
41 return ".i";
42}
43
Ted Kremenek5daa3be2009-01-27 01:19:08 +000044my $ParserRejects = "Parser Rejects";
Ted Kremenek938eef12009-02-17 23:31:05 +000045my $AttributeIgnored = "Attribute Ignored";
Ted Kremenek5daa3be2009-01-27 01:19:08 +000046
Ted Kremenek5d31f832008-08-18 18:38:29 +000047sub ProcessClangFailure {
Ted Kremenekc3998fa2008-09-25 00:51:44 +000048 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
Ted Kremenek938eef12009-02-17 23:31:05 +000049 my $Dir = "$HtmlDir/failures";
Ted Kremenek991c54b2008-08-08 20:46:42 +000050 mkpath $Dir;
Ted Kremenek5daa3be2009-01-27 01:19:08 +000051
52 my $prefix = "clang_crash";
Ted Kremenek938eef12009-02-17 23:31:05 +000053 if ($ErrorType eq $ParserRejects) {
54 $prefix = "clang_parser_rejects";
55 }
56 elsif ($ErrorType eq $AttributeIgnored) {
57 $prefix = "clang_attribute_ignored";
58 }
Ted Kremenekc3998fa2008-09-25 00:51:44 +000059
60 # Generate the preprocessed file with cc (i.e., gcc).
Ted Kremenek5daa3be2009-01-27 01:19:08 +000061 my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX",
62 SUFFIX => GetPPExt($Lang),
63 DIR => $Dir);
Ted Kremenek991c54b2008-08-08 20:46:42 +000064
Ted Kremenek2ec5cd52008-08-25 20:44:31 +000065 system $CC, @$Args, "-E", "-o", $PPFile;
Ted Kremenek991c54b2008-08-08 20:46:42 +000066 close ($PPH);
Ted Kremenekc3998fa2008-09-25 00:51:44 +000067
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 Kremenek82a12532008-09-25 00:25:16 +000074 open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
Ted Kremenek5f2825f2008-09-21 18:04:49 +000075 print OUT abs_path($file), "\n";
Ted Kremenek5d31f832008-08-18 18:38:29 +000076 print OUT "$ErrorType\n";
Ted Kremenek2dd7ad12008-08-18 20:55:25 +000077 print OUT "@$Args\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +000078 close OUT;
Ted Kremenek82a12532008-09-25 00:25:16 +000079 `uname -a >> $PPFile.info.txt 2>&1`;
80 `$CC -v >> $PPFile.info.txt 2>&1`;
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +000081 system 'mv',$ofile,"$PPFile.stderr.txt";
Ted Kremenek938eef12009-02-17 23:31:05 +000082 return (basename $PPFile);
Ted Kremenek991c54b2008-08-08 20:46:42 +000083}
Ted Kremenekb0982882008-03-25 22:35:32 +000084
Ted Kremenekfbeeca82008-07-19 06:11:04 +000085##----------------------------------------------------------------------------##
86# Running the analyzer.
87##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +000088
Ted Kremenekfbeeca82008-07-19 06:11:04 +000089sub Analyze {
90 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +000091
Ted Kremenekfbeeca82008-07-19 06:11:04 +000092 # Skip anything related to C++.
93 return if ($Lang =~ /c[+][+]/);
Ted Kremenek5d31f832008-08-18 18:38:29 +000094
Ted Kremenekfbeeca82008-07-19 06:11:04 +000095 my $RunAnalyzer = 0;
96 my $Cmd;
97 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +000098 my @CmdArgsSansAnalyses;
Ted Kremenek61cd9882008-05-24 15:58:54 +000099
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000100 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 Kremenek991c54b2008-08-08 20:46:42 +0000107 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000108 }
109 else {
110 $Cmd = $Clang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000111 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
112 push @CmdArgs,@$Args;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000113 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenek938eef12009-02-17 23:31:05 +0000114 push @CmdArgs,'-analyze';
115 push @CmdArgs,"-analyzer-display-progress";
116 push @CmdArgs,"-disable-free";
Ted Kremeneka2bdaf52009-02-26 17:36:31 +0000117 push @CmdArgs,"-analyzer-eagerly-assume";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000118 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000119 $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 Kremenek61cd9882008-05-24 15:58:54 +0000133 # We MUST print to stderr. Some clients use the stdout output of
134 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000135 print STDERR join(' ',@PrintArgs);
136 print STDERR "\n";
137 }
138 elsif ($Verbose == 2) {
139 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
140 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000141
Ted Kremenekddf32da2009-01-21 00:42:24 +0000142 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 Kremenekfbeeca82008-07-19 06:11:04 +0000151 }
Ted Kremenek948e06b2008-08-27 22:30:34 +0000152
153 if (defined $ENV{'CCC_UBI'}) {
154 push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
155 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000156
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000157 # 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 Kremenek13462682008-09-11 23:05:26 +0000161 pipe (FROM_CHILD, TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000162 my $pid = fork();
163 if ($pid == 0) {
Ted Kremenek13462682008-09-11 23:05:26 +0000164 close FROM_CHILD;
165 open(STDOUT,">&", \*TO_PARENT);
166 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000167 exec $Cmd, @CmdArgs;
168 }
Ted Kremenek13462682008-09-11 23:05:26 +0000169
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 Kremenek9a3c7da2008-09-04 00:02:34 +0000179 my $Result = $?;
180
181 # Did the command die because of a signal?
182 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
Ted Kremenekc3998fa2008-09-25 00:51:44 +0000183 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000184 "Crash", $ofile);
185 }
186 elsif ($Result) {
Ted Kremenekc3998fa2008-09-25 00:51:44 +0000187 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek5daa3be2009-01-27 01:19:08 +0000188 $ParserRejects, $ofile);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000189 }
Ted Kremenek938eef12009-02-17 23:31:05 +0000190 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 Kremenek7e5bd6f2009-02-21 04:46:20 +0000202 # 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 Kremenek938eef12009-02-17 23:31:05 +0000210 # 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 Kremenek938eef12009-02-17 23:31:05 +0000218 mkpath $dir;
Ted Kremenek7e5bd6f2009-02-21 04:46:20 +0000219 open(AFILE, ">$afile");
Ted Kremenek938eef12009-02-17 23:31:05 +0000220 print AFILE "$ppfile\n";
221 close(AFILE);
222 }
223 close CHILD;
224 }
225 }
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000226
227 `rm -f $ofile`;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000228}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000229
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000230##----------------------------------------------------------------------------##
231# Lookup tables.
232##----------------------------------------------------------------------------##
233
234my %CompileOptionMap = (
235 '-nostdinc' => 0,
Anders Carlsson06c58b12008-12-19 20:56:23 +0000236 '-fblocks' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000237 '-fobjc-gc-only' => 0,
Ted Kremenek6c4312d2009-02-26 23:09:43 +0000238 '-fobjc-gc' => 0,
239 '-ffreestanding' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000240 '-include' => 1,
241 '-idirafter' => 1,
242 '-iprefix' => 1,
243 '-iquote' => 1,
244 '-isystem' => 1,
245 '-iwithprefix' => 1,
246 '-iwithprefixbefore' => 1
247);
248
249my %LinkerOptionMap = (
250 '-framework' => 1
251);
252
253my %CompilerLinkerOptionMap = (
254 '-isysroot' => 1,
255 '-arch' => 1,
Ted Kremeneke4f69522008-09-29 22:45:28 +0000256 '-v' => 0,
Ted Kremenekb10362a2008-09-30 23:40:25 +0000257 '-fpascal-strings' => 0,
258 '-mmacosx-version-min' => 0 # This is really a 1 argument, but always has '='
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000259);
260
261my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000262 '-MT' => 1, # Ignore these preprocessor options.
263 '-MF' => 1,
264
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000265 '-fsyntax-only' => 0,
266 '-save-temps' => 0,
267 '-install_name' => 1,
268 '-exported_symbols_list' => 1,
269 '-current_version' => 1,
270 '-compatibility_version' => 1,
271 '-init' => 1,
272 '-e' => 1,
273 '-seg1addr' => 1,
274 '-bundle_loader' => 1,
275 '-multiply_defined' => 1,
276 '-sectorder' => 3,
277 '--param' => 1,
278 '-u' => 1
279);
280
281my %LangMap = (
282 'c' => 'c',
283 'cpp' => 'c++',
284 'cc' => 'c++',
285 'i' => 'c-cpp-output',
286 'm' => 'objective-c',
287 'mi' => 'objective-c-cpp-output'
288);
289
Ted Kremeneka30730e2008-09-29 16:15:20 +0000290my %UniqueOptions = (
291 '-isysroot' => 0
292);
293
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000294##----------------------------------------------------------------------------##
295# Main Logic.
296##----------------------------------------------------------------------------##
297
298my $Action = 'link';
299my @CompileOpts;
300my @LinkOpts;
301my @Files;
302my $Lang;
303my $Output;
Ted Kremeneka30730e2008-09-29 16:15:20 +0000304my %Uniqued;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000305
306# Forward arguments to gcc.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000307my $Status = system($CC,@ARGV);
Ted Kremenekcb344d02008-08-28 01:18:44 +0000308if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000309
310# Get the analysis options.
311my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
312if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
313
Zhongxing Xu07c37672008-10-27 14:26:32 +0000314# Get the store model.
315my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000316if (!defined $StoreModel) { $StoreModel = "basic"; }
317
318# Get the constraints engine.
319my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
Ted Kremenek9f4ecb32009-02-20 21:49:22 +0000320if (!defined $ConstraintsModel) { $ConstraintsModel = "range"; }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000321
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000322# Get the output format.
323my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
Ted Kremenek3891a152009-02-17 05:01:10 +0000324if (!defined $OutputFormat) { $OutputFormat = "html"; }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000325
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000326# Determine the level of verbosity.
327my $Verbose = 0;
328if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
329if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
330
331# Determine what clang executable to use.
332my $Clang = $ENV{'CLANG'};
333if (!defined $Clang) { $Clang = 'clang'; }
334
335# Get the HTML output directory.
336my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
337
Ted Kremenek84591322009-02-24 22:07:12 +0000338my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000339my %ArchsSeen;
Ted Kremenek84591322009-02-24 22:07:12 +0000340my $HadArch = 0;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000341
342# Process the arguments.
343foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000344 my $Arg = $ARGV[$i];
345 my ($ArgKey) = split /=/,$Arg,2;
346
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000347 # Modes ccc-analyzer supports
348 if ($Arg eq '-E') { $Action = 'preprocess'; }
349 elsif ($Arg eq '-c') { $Action = 'compile'; }
350 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek27783eb2008-09-25 20:17:57 +0000351
352 # Specially handle duplicate cases of -arch
353 if ($Arg eq "-arch") {
354 my $arch = $ARGV[$i+1];
Ted Kremenek84591322009-02-24 22:07:12 +0000355 # We don't want to process 'ppc' because of Clang's lack of support
356 # for Altivec (also some #defines won't likely be defined correctly, etc.)
357 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
358 $HadArch = 1;
Ted Kremenek27783eb2008-09-25 20:17:57 +0000359 ++$i;
360 next;
361 }
362
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000363 # Options with possible arguments that should pass through to compiler.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000364 if (defined $CompileOptionMap{$ArgKey}) {
365 my $Cnt = $CompileOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000366 push @CompileOpts,$Arg;
367 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
368 next;
369 }
370
371 # Options with possible arguments that should pass through to linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000372 if (defined $LinkerOptionMap{$ArgKey}) {
373 my $Cnt = $LinkerOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000374 push @LinkOpts,$Arg;
375 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
376 next;
377 }
378
379 # Options with possible arguments that should pass through to both compiler
380 # and the linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000381 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
382 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000383
Ted Kremeneka30730e2008-09-29 16:15:20 +0000384 # Check if this is an option that should have a unique value, and if so
385 # determine if the value was checked before.
386 if ($UniqueOptions{$Arg}) {
387 if (defined $Uniqued{$Arg}) {
388 $i += $Cnt;
389 next;
390 }
391 $Uniqued{$Arg} = 1;
392 }
393
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000394 push @CompileOpts,$Arg;
395 push @LinkOpts,$Arg;
396
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000397 while ($Cnt > 0) {
398 ++$i; --$Cnt;
399 push @CompileOpts, $ARGV[$i];
400 push @LinkOpts, $ARGV[$i];
401 }
402 next;
403 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000404
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000405 # Ignored options.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000406 if (defined $IgnoredOptionMap{$ArgKey}) {
407 my $Cnt = $IgnoredOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000408 while ($Cnt > 0) {
409 ++$i; --$Cnt;
410 }
411 next;
412 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000413
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000414 # Compile mode flags.
415 if ($Arg =~ /^-[D,I,U](.*)$/) {
416 my $Tmp = $Arg;
417 if ($1 eq '') {
418 # FIXME: Check if we are going off the end.
419 ++$i;
420 $Tmp = $Arg . $ARGV[$i];
421 }
422 push @CompileOpts,$Tmp;
423 next;
424 }
425
426 # Language.
427 if ($Arg eq '-x') {
428 $Lang = $ARGV[$i+1];
429 ++$i; next;
430 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000431
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000432 # Output file.
433 if ($Arg eq '-o') {
434 ++$i;
435 $Output = $ARGV[$i];
436 next;
437 }
438
439 # Get the link mode.
440 if ($Arg =~ /^-[l,L,O]/) {
441 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
442 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
443 else { push @LinkOpts,$Arg; }
444 next;
445 }
446
447 if ($Arg =~ /^-std=/) {
448 push @CompileOpts,$Arg;
449 next;
450 }
451
452# if ($Arg =~ /^-f/) {
453# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
454# push @CompileOpts,$Arg;
455# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
456# }
457
458 # Get the compiler/link mode.
459 if ($Arg =~ /^-F(.+)$/) {
460 my $Tmp = $Arg;
461 if ($1 eq '') {
462 # FIXME: Check if we are going off the end.
463 ++$i;
464 $Tmp = $Arg . $ARGV[$i];
465 }
466 push @CompileOpts,$Tmp;
467 push @LinkOpts,$Tmp;
468 next;
469 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000470
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000471 # Input files.
472 if ($Arg eq '-filelist') {
473 # FIXME: Make sure we aren't walking off the end.
474 open(IN, $ARGV[$i+1]);
475 while (<IN>) { s/\015?\012//; push @Files,$_; }
476 close(IN);
477 ++$i; next;
478 }
479
480 if (!($Arg =~ /^-/)) {
481 push @Files,$Arg; next;
482 }
483}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000484
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000485if ($Action eq 'compile' or $Action eq 'link') {
Ted Kremenek84591322009-02-24 22:07:12 +0000486 my @Archs = keys %ArchsSeen;
487 # Skip the file if we don't support the architectures specified.
Ted Kremenek0e0eb8b2009-02-25 00:10:37 +0000488 exit 0 if ($HadArch && scalar(@Archs) == 0);
Ted Kremenek84591322009-02-24 22:07:12 +0000489
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000490 foreach my $file (@Files) {
491 # Determine the language for the file.
492 my $FileLang = $Lang;
493
494 if (!defined($FileLang)) {
495 # Infer the language from the extension.
496 if ($file =~ /[.]([^.]+)$/) {
497 $FileLang = $LangMap{$1};
498 }
499 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000500
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000501 next if (!defined $FileLang);
502
503 my @AnalyzeArgs;
504
505 if ($FileLang ne 'unknown') {
506 push @AnalyzeArgs,'-x';
507 push @AnalyzeArgs,$FileLang;
508 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000509
Zhongxing Xu07c37672008-10-27 14:26:32 +0000510 if (defined $StoreModel) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000511 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
Zhongxing Xu07c37672008-10-27 14:26:32 +0000512 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000513
514 if (defined $ConstraintsModel) {
515 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
516 }
517
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000518 if (defined $OutputFormat) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000519 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000520 if ($OutputFormat eq "plist") {
521 # Change "Output" to be a file.
522 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
523 DIR => $HtmlDir);
524 $ResultFile = $f;
525 $CleanupFile = $f;
526 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000527 }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000528
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000529 push @AnalyzeArgs,@CompileOpts;
530 push @AnalyzeArgs,$file;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000531
Ted Kremenek27783eb2008-09-25 20:17:57 +0000532 if (scalar @Archs) {
533 foreach my $arch (@Archs) {
534 my @NewArgs;
535 push @NewArgs, '-arch';
536 push @NewArgs, $arch;
537 push @NewArgs, @AnalyzeArgs;
538 Analyze($Clang, \@NewArgs, $FileLang, $Output,
539 $Verbose, $HtmlDir, $file, $Analyses);
540 }
541 }
542 else {
543 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
544 $Verbose, $HtmlDir, $file, $Analyses);
545 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000546 }
547}
Ted Kremenekb0982882008-03-25 22:35:32 +0000548
Ted Kremenek948e06b2008-08-27 22:30:34 +0000549exit($Status >> 8);
550