blob: 4e3f6dd3544f11ae157d57be1f4314a3ab0d342c [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 Kremenek5de40922009-02-27 06:17:38 +0000187 if (!($file =~/conftest/)) {
188 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
189 $ParserRejects, $ofile);
190 }
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000191 }
Ted Kremenek938eef12009-02-17 23:31:05 +0000192 else {
193 # Check if there were any unhandled attributes.
194 if (open(CHILD, $ofile)) {
195 my %attributes_not_handled;
196 my $ppfile;
197 while (<CHILD>) {
198 next if (! /warning: '([^\']+)' attribute ignored/);
199
200 # Have we already spotted this unhandled attribute?
201 next if (defined $attributes_not_handled{$1});
202 $attributes_not_handled{$1} = 1;
203
Ted Kremenek7e5bd6f2009-02-21 04:46:20 +0000204 # Get the name of the attribute file.
205 my $dir = "$HtmlDir/failures";
206 my $afile = "$dir/attribute_ignored_$1.txt";
207
208 # Only create another preprocessed file if the attribute file
209 # doesn't exist yet.
210 next if (-e $afile);
211
Ted Kremenek938eef12009-02-17 23:31:05 +0000212 # Add this file to the list of files that contained this attribute.
213 # Generate a preprocessed file if we haven't already.
214 if (!(defined $ppfile)) {
215 $ppfile = ProcessClangFailure($Clang, $Lang, $file,
216 \@CmdArgsSansAnalyses,
217 $HtmlDir, $AttributeIgnored, $ofile);
218 }
219
Ted Kremenek938eef12009-02-17 23:31:05 +0000220 mkpath $dir;
Ted Kremenek7e5bd6f2009-02-21 04:46:20 +0000221 open(AFILE, ">$afile");
Ted Kremenek938eef12009-02-17 23:31:05 +0000222 print AFILE "$ppfile\n";
223 close(AFILE);
224 }
225 close CHILD;
226 }
227 }
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000228
229 `rm -f $ofile`;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000230}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000231
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000232##----------------------------------------------------------------------------##
233# Lookup tables.
234##----------------------------------------------------------------------------##
235
236my %CompileOptionMap = (
237 '-nostdinc' => 0,
Anders Carlsson06c58b12008-12-19 20:56:23 +0000238 '-fblocks' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000239 '-fobjc-gc-only' => 0,
Ted Kremenek6c4312d2009-02-26 23:09:43 +0000240 '-fobjc-gc' => 0,
241 '-ffreestanding' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000242 '-include' => 1,
243 '-idirafter' => 1,
244 '-iprefix' => 1,
245 '-iquote' => 1,
246 '-isystem' => 1,
247 '-iwithprefix' => 1,
248 '-iwithprefixbefore' => 1
249);
250
251my %LinkerOptionMap = (
252 '-framework' => 1
253);
254
255my %CompilerLinkerOptionMap = (
256 '-isysroot' => 1,
257 '-arch' => 1,
Ted Kremeneke4f69522008-09-29 22:45:28 +0000258 '-v' => 0,
Ted Kremenekb10362a2008-09-30 23:40:25 +0000259 '-fpascal-strings' => 0,
Daniel Dunbar8d33cd72009-04-10 19:52:24 +0000260 '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '='
261 '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '='
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000262);
263
264my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000265 '-MT' => 1, # Ignore these preprocessor options.
266 '-MF' => 1,
267
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000268 '-fsyntax-only' => 0,
269 '-save-temps' => 0,
270 '-install_name' => 1,
271 '-exported_symbols_list' => 1,
272 '-current_version' => 1,
273 '-compatibility_version' => 1,
274 '-init' => 1,
275 '-e' => 1,
276 '-seg1addr' => 1,
277 '-bundle_loader' => 1,
278 '-multiply_defined' => 1,
279 '-sectorder' => 3,
280 '--param' => 1,
281 '-u' => 1
282);
283
284my %LangMap = (
285 'c' => 'c',
286 'cpp' => 'c++',
287 'cc' => 'c++',
288 'i' => 'c-cpp-output',
289 'm' => 'objective-c',
290 'mi' => 'objective-c-cpp-output'
291);
292
Ted Kremeneka30730e2008-09-29 16:15:20 +0000293my %UniqueOptions = (
294 '-isysroot' => 0
295);
296
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000297##----------------------------------------------------------------------------##
298# Main Logic.
299##----------------------------------------------------------------------------##
300
301my $Action = 'link';
302my @CompileOpts;
303my @LinkOpts;
304my @Files;
305my $Lang;
306my $Output;
Ted Kremeneka30730e2008-09-29 16:15:20 +0000307my %Uniqued;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000308
309# Forward arguments to gcc.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000310my $Status = system($CC,@ARGV);
Ted Kremenekcb344d02008-08-28 01:18:44 +0000311if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000312
313# Get the analysis options.
314my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
315if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
316
Zhongxing Xu07c37672008-10-27 14:26:32 +0000317# Get the store model.
318my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000319if (!defined $StoreModel) { $StoreModel = "basic"; }
320
321# Get the constraints engine.
322my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
Ted Kremenek9f4ecb32009-02-20 21:49:22 +0000323if (!defined $ConstraintsModel) { $ConstraintsModel = "range"; }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000324
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000325# Get the output format.
326my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
Ted Kremenek3891a152009-02-17 05:01:10 +0000327if (!defined $OutputFormat) { $OutputFormat = "html"; }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000328
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000329# Determine the level of verbosity.
330my $Verbose = 0;
331if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
332if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
333
334# Determine what clang executable to use.
335my $Clang = $ENV{'CLANG'};
Ted Kremenek318e6a62009-03-24 04:29:13 +0000336if (!defined $Clang) { $Clang = 'clang-cc'; }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000337
338# Get the HTML output directory.
339my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
340
Ted Kremenek84591322009-02-24 22:07:12 +0000341my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000342my %ArchsSeen;
Ted Kremenek84591322009-02-24 22:07:12 +0000343my $HadArch = 0;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000344
345# Process the arguments.
346foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000347 my $Arg = $ARGV[$i];
348 my ($ArgKey) = split /=/,$Arg,2;
349
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000350 # Modes ccc-analyzer supports
351 if ($Arg eq '-E') { $Action = 'preprocess'; }
352 elsif ($Arg eq '-c') { $Action = 'compile'; }
353 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek27783eb2008-09-25 20:17:57 +0000354
355 # Specially handle duplicate cases of -arch
356 if ($Arg eq "-arch") {
357 my $arch = $ARGV[$i+1];
Ted Kremenek84591322009-02-24 22:07:12 +0000358 # We don't want to process 'ppc' because of Clang's lack of support
359 # for Altivec (also some #defines won't likely be defined correctly, etc.)
360 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
361 $HadArch = 1;
Ted Kremenek27783eb2008-09-25 20:17:57 +0000362 ++$i;
363 next;
364 }
365
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000366 # Options with possible arguments that should pass through to compiler.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000367 if (defined $CompileOptionMap{$ArgKey}) {
368 my $Cnt = $CompileOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000369 push @CompileOpts,$Arg;
370 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
371 next;
372 }
373
374 # Options with possible arguments that should pass through to linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000375 if (defined $LinkerOptionMap{$ArgKey}) {
376 my $Cnt = $LinkerOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000377 push @LinkOpts,$Arg;
378 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
379 next;
380 }
381
382 # Options with possible arguments that should pass through to both compiler
383 # and the linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000384 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
385 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000386
Ted Kremeneka30730e2008-09-29 16:15:20 +0000387 # Check if this is an option that should have a unique value, and if so
388 # determine if the value was checked before.
389 if ($UniqueOptions{$Arg}) {
390 if (defined $Uniqued{$Arg}) {
391 $i += $Cnt;
392 next;
393 }
394 $Uniqued{$Arg} = 1;
395 }
396
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000397 push @CompileOpts,$Arg;
398 push @LinkOpts,$Arg;
399
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000400 while ($Cnt > 0) {
401 ++$i; --$Cnt;
402 push @CompileOpts, $ARGV[$i];
403 push @LinkOpts, $ARGV[$i];
404 }
405 next;
406 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000407
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000408 # Ignored options.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000409 if (defined $IgnoredOptionMap{$ArgKey}) {
410 my $Cnt = $IgnoredOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000411 while ($Cnt > 0) {
412 ++$i; --$Cnt;
413 }
414 next;
415 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000416
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000417 # Compile mode flags.
418 if ($Arg =~ /^-[D,I,U](.*)$/) {
419 my $Tmp = $Arg;
420 if ($1 eq '') {
421 # FIXME: Check if we are going off the end.
422 ++$i;
423 $Tmp = $Arg . $ARGV[$i];
424 }
425 push @CompileOpts,$Tmp;
426 next;
427 }
428
429 # Language.
430 if ($Arg eq '-x') {
431 $Lang = $ARGV[$i+1];
432 ++$i; next;
433 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000434
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000435 # Output file.
436 if ($Arg eq '-o') {
437 ++$i;
438 $Output = $ARGV[$i];
439 next;
440 }
441
442 # Get the link mode.
443 if ($Arg =~ /^-[l,L,O]/) {
444 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
445 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
446 else { push @LinkOpts,$Arg; }
447 next;
448 }
449
450 if ($Arg =~ /^-std=/) {
451 push @CompileOpts,$Arg;
452 next;
453 }
454
455# if ($Arg =~ /^-f/) {
456# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
457# push @CompileOpts,$Arg;
458# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
459# }
460
461 # Get the compiler/link mode.
462 if ($Arg =~ /^-F(.+)$/) {
463 my $Tmp = $Arg;
464 if ($1 eq '') {
465 # FIXME: Check if we are going off the end.
466 ++$i;
467 $Tmp = $Arg . $ARGV[$i];
468 }
469 push @CompileOpts,$Tmp;
470 push @LinkOpts,$Tmp;
471 next;
472 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000473
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000474 # Input files.
475 if ($Arg eq '-filelist') {
476 # FIXME: Make sure we aren't walking off the end.
477 open(IN, $ARGV[$i+1]);
478 while (<IN>) { s/\015?\012//; push @Files,$_; }
479 close(IN);
480 ++$i; next;
481 }
482
483 if (!($Arg =~ /^-/)) {
484 push @Files,$Arg; next;
485 }
486}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000487
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000488if ($Action eq 'compile' or $Action eq 'link') {
Ted Kremenek84591322009-02-24 22:07:12 +0000489 my @Archs = keys %ArchsSeen;
490 # Skip the file if we don't support the architectures specified.
Ted Kremenek0e0eb8b2009-02-25 00:10:37 +0000491 exit 0 if ($HadArch && scalar(@Archs) == 0);
Ted Kremenek84591322009-02-24 22:07:12 +0000492
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000493 foreach my $file (@Files) {
494 # Determine the language for the file.
495 my $FileLang = $Lang;
496
497 if (!defined($FileLang)) {
498 # Infer the language from the extension.
499 if ($file =~ /[.]([^.]+)$/) {
500 $FileLang = $LangMap{$1};
501 }
502 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000503
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000504 next if (!defined $FileLang);
505
506 my @AnalyzeArgs;
507
508 if ($FileLang ne 'unknown') {
509 push @AnalyzeArgs,'-x';
510 push @AnalyzeArgs,$FileLang;
511 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000512
Zhongxing Xu07c37672008-10-27 14:26:32 +0000513 if (defined $StoreModel) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000514 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
Zhongxing Xu07c37672008-10-27 14:26:32 +0000515 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000516
517 if (defined $ConstraintsModel) {
518 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
519 }
520
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000521 if (defined $OutputFormat) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000522 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000523 if ($OutputFormat eq "plist") {
524 # Change "Output" to be a file.
525 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
526 DIR => $HtmlDir);
527 $ResultFile = $f;
528 $CleanupFile = $f;
529 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000530 }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000531
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000532 push @AnalyzeArgs,@CompileOpts;
533 push @AnalyzeArgs,$file;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000534
Ted Kremenek27783eb2008-09-25 20:17:57 +0000535 if (scalar @Archs) {
536 foreach my $arch (@Archs) {
537 my @NewArgs;
538 push @NewArgs, '-arch';
539 push @NewArgs, $arch;
540 push @NewArgs, @AnalyzeArgs;
541 Analyze($Clang, \@NewArgs, $FileLang, $Output,
542 $Verbose, $HtmlDir, $file, $Analyses);
543 }
544 }
545 else {
546 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
547 $Verbose, $HtmlDir, $file, $Analyses);
548 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000549 }
550}
Ted Kremenekb0982882008-03-25 22:35:32 +0000551
Ted Kremenek948e06b2008-08-27 22:30:34 +0000552exit($Status >> 8);
553