blob: 8e1f26ba7ff41da043ae5839b9396d5bef3cc24c [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 Kremeneka4d8cde2009-04-28 17:37:44 +000044# Set this to 1 if we want to include 'parser rejects' files.
45my $IncludeParserRejects = 0;
Ted Kremenek5daa3be2009-01-27 01:19:08 +000046my $ParserRejects = "Parser Rejects";
Ted Kremeneka4d8cde2009-04-28 17:37:44 +000047
Ted Kremenek938eef12009-02-17 23:31:05 +000048my $AttributeIgnored = "Attribute Ignored";
Ted Kremenek5daa3be2009-01-27 01:19:08 +000049
Ted Kremenek5d31f832008-08-18 18:38:29 +000050sub ProcessClangFailure {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000051 my ($ClangCC, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
Ted Kremenek938eef12009-02-17 23:31:05 +000052 my $Dir = "$HtmlDir/failures";
Ted Kremenek991c54b2008-08-08 20:46:42 +000053 mkpath $Dir;
Ted Kremenek5daa3be2009-01-27 01:19:08 +000054
55 my $prefix = "clang_crash";
Ted Kremenek938eef12009-02-17 23:31:05 +000056 if ($ErrorType eq $ParserRejects) {
57 $prefix = "clang_parser_rejects";
58 }
59 elsif ($ErrorType eq $AttributeIgnored) {
60 $prefix = "clang_attribute_ignored";
61 }
Ted Kremenekc3998fa2008-09-25 00:51:44 +000062
63 # Generate the preprocessed file with cc (i.e., gcc).
Ted Kremenek5daa3be2009-01-27 01:19:08 +000064 my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX",
65 SUFFIX => GetPPExt($Lang),
66 DIR => $Dir);
Ted Kremenek991c54b2008-08-08 20:46:42 +000067
Ted Kremenek2ec5cd52008-08-25 20:44:31 +000068 system $CC, @$Args, "-E", "-o", $PPFile;
Ted Kremenek991c54b2008-08-08 20:46:42 +000069 close ($PPH);
Ted Kremenekc3998fa2008-09-25 00:51:44 +000070
71 # Generate the preprocessed file with clang.
72 my $PPFile_Clang = $PPFile;
73 $PPFile_Clang =~ s/[.](.+)$/.clang.$1/;
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000074 system $ClangCC, @$Args, "-E", "-o", "$PPFile_Clang";
Ted Kremenekc3998fa2008-09-25 00:51:44 +000075
76 # Create the info file.
Ted Kremenek82a12532008-09-25 00:25:16 +000077 open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
Ted Kremenek5f2825f2008-09-21 18:04:49 +000078 print OUT abs_path($file), "\n";
Ted Kremenek5d31f832008-08-18 18:38:29 +000079 print OUT "$ErrorType\n";
Ted Kremenek2dd7ad12008-08-18 20:55:25 +000080 print OUT "@$Args\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +000081 close OUT;
Ted Kremenek82a12532008-09-25 00:25:16 +000082 `uname -a >> $PPFile.info.txt 2>&1`;
83 `$CC -v >> $PPFile.info.txt 2>&1`;
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +000084 system 'mv',$ofile,"$PPFile.stderr.txt";
Ted Kremenek938eef12009-02-17 23:31:05 +000085 return (basename $PPFile);
Ted Kremenek991c54b2008-08-08 20:46:42 +000086}
Ted Kremenekb0982882008-03-25 22:35:32 +000087
Ted Kremenekfbeeca82008-07-19 06:11:04 +000088##----------------------------------------------------------------------------##
89# Running the analyzer.
90##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +000091
Ted Kremenekfd9df0e2009-05-09 19:19:28 +000092# Determine what clang executable to use.
93my $Clang = $ENV{'CLANG'};
94if (!defined $Clang) { $Clang = 'clang'; }
95
96sub GetCCArgs {
97 my $Args = shift;
98
99 pipe (FROM_CHILD, TO_PARENT);
100 my $pid = fork();
101 if ($pid == 0) {
102 close FROM_CHILD;
103 open(STDOUT,">&", \*TO_PARENT);
104 open(STDERR,">&", \*TO_PARENT);
105 exec $Clang, "-###", "-fsyntax-only", @$Args;
106 }
107 close(TO_PARENT);
108 my $line;
109 while (<FROM_CHILD>) {
110 next if (!/clang-cc/);
111 $line = $_;
112 }
113
114 waitpid($pid,0);
115 close(FROM_CHILD);
116
117 die "could not find clang-cc line\n" if (!defined $line);
118 my @items = ($line =~ /(".*?"|\S+)/g);
119
120 for (my $i = 0; $i < scalar(@items); ++$i) {
121 my $x = $items[$i];
122 $x =~ s/^"//;
123 $x =~ s/"$//;
124 $items[$i] = $x;
125 }
126
127 my $cmd = shift @items;
128 die "cannot find 'clang-cc' in 'clang' command\n" if (!($cmd =~ /clang-cc/));
129 return \@items;
130}
131
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000132sub Analyze {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000133 my ($ClangCC, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Ted Kremenekefaf1912009-05-11 21:08:34 +0000134
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000135 $Args = GetCCArgs($Args);
Seo Sanghyeond3894652008-04-04 11:02:21 +0000136
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000137 # Skip anything related to C++.
138 return if ($Lang =~ /c[+][+]/);
Ted Kremenek5d31f832008-08-18 18:38:29 +0000139
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000140 my $RunAnalyzer = 0;
141 my $Cmd;
142 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000143 my @CmdArgsSansAnalyses;
Ted Kremenek61cd9882008-05-24 15:58:54 +0000144
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000145 if ($Lang =~ /header/) {
146 exit 0 if (!defined ($Output));
147 $Cmd = 'cp';
148 push @CmdArgs,$file;
149 # Remove the PCH extension.
150 $Output =~ s/[.]gch$//;
151 push @CmdArgs,$Output;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000152 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000153 }
154 else {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000155 $Cmd = $ClangCC;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000156 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
157 push @CmdArgs,@$Args;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000158 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenek938eef12009-02-17 23:31:05 +0000159 push @CmdArgs,'-analyze';
160 push @CmdArgs,"-analyzer-display-progress";
Ted Kremeneka2bdaf52009-02-26 17:36:31 +0000161 push @CmdArgs,"-analyzer-eagerly-assume";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000162 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000163 $RunAnalyzer = 1;
164 }
165
166 my @PrintArgs;
167 my $dir;
168
169 if ($Verbose) {
170 $dir = getcwd();
171 print STDERR "\n[LOCATION]: $dir\n";
172 push @PrintArgs,"'$Cmd'";
173 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
174 }
175
176 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +0000177 # We MUST print to stderr. Some clients use the stdout output of
178 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000179 print STDERR join(' ',@PrintArgs);
180 print STDERR "\n";
181 }
182 elsif ($Verbose == 2) {
183 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
184 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000185
Ted Kremenekddf32da2009-01-21 00:42:24 +0000186 if ($RunAnalyzer) {
187 if (defined $ResultFile) {
188 push @CmdArgs,'-o';
189 push @CmdArgs, $ResultFile;
190 }
191 elsif (defined $HtmlDir) {
192 push @CmdArgs,'-o';
193 push @CmdArgs, $HtmlDir;
194 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000195 }
Ted Kremenek948e06b2008-08-27 22:30:34 +0000196
197 if (defined $ENV{'CCC_UBI'}) {
198 push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
199 }
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000200
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000201 # Capture the STDERR of clang and send it to a temporary file.
202 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
203 # We save the output file in the 'crashes' directory if clang encounters
204 # any problems with the file.
Ted Kremenek13462682008-09-11 23:05:26 +0000205 pipe (FROM_CHILD, TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000206 my $pid = fork();
207 if ($pid == 0) {
Ted Kremenek13462682008-09-11 23:05:26 +0000208 close FROM_CHILD;
209 open(STDOUT,">&", \*TO_PARENT);
210 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000211 exec $Cmd, @CmdArgs;
212 }
Ted Kremenek13462682008-09-11 23:05:26 +0000213
214 close TO_PARENT;
215 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
216
217 while (<FROM_CHILD>) {
218 print $ofh $_;
219 print STDERR $_;
220 }
221
222 waitpid($pid,0);
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000223 close(FROM_CHILD);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000224 my $Result = $?;
225
226 # Did the command die because of a signal?
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000227 if ($Result & 127 and $Cmd eq $ClangCC and defined $HtmlDir) {
228 ProcessClangFailure($ClangCC, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000229 "Crash", $ofile);
230 }
231 elsif ($Result) {
Ted Kremeneka4d8cde2009-04-28 17:37:44 +0000232 if ($IncludeParserRejects && !($file =~/conftest/)) {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000233 ProcessClangFailure($ClangCC, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek5de40922009-02-27 06:17:38 +0000234 $ParserRejects, $ofile);
235 }
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000236 }
Ted Kremenek938eef12009-02-17 23:31:05 +0000237 else {
238 # Check if there were any unhandled attributes.
239 if (open(CHILD, $ofile)) {
240 my %attributes_not_handled;
Ted Kremenek32e94642009-04-22 13:42:27 +0000241
242 # Don't flag warnings about the following attributes that we
243 # know are currently not supported by Clang.
244 $attributes_not_handled{"cdecl"} = 1;
245
Ted Kremenek938eef12009-02-17 23:31:05 +0000246 my $ppfile;
247 while (<CHILD>) {
248 next if (! /warning: '([^\']+)' attribute ignored/);
249
250 # Have we already spotted this unhandled attribute?
251 next if (defined $attributes_not_handled{$1});
252 $attributes_not_handled{$1} = 1;
253
Ted Kremenek7e5bd6f2009-02-21 04:46:20 +0000254 # Get the name of the attribute file.
255 my $dir = "$HtmlDir/failures";
256 my $afile = "$dir/attribute_ignored_$1.txt";
257
258 # Only create another preprocessed file if the attribute file
259 # doesn't exist yet.
260 next if (-e $afile);
261
Ted Kremenek938eef12009-02-17 23:31:05 +0000262 # Add this file to the list of files that contained this attribute.
263 # Generate a preprocessed file if we haven't already.
264 if (!(defined $ppfile)) {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000265 $ppfile = ProcessClangFailure($ClangCC, $Lang, $file,
Ted Kremenek938eef12009-02-17 23:31:05 +0000266 \@CmdArgsSansAnalyses,
267 $HtmlDir, $AttributeIgnored, $ofile);
268 }
269
Ted Kremenek938eef12009-02-17 23:31:05 +0000270 mkpath $dir;
Ted Kremenek7e5bd6f2009-02-21 04:46:20 +0000271 open(AFILE, ">$afile");
Ted Kremenek938eef12009-02-17 23:31:05 +0000272 print AFILE "$ppfile\n";
273 close(AFILE);
274 }
275 close CHILD;
276 }
277 }
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000278
279 `rm -f $ofile`;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000280}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000281
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000282##----------------------------------------------------------------------------##
283# Lookup tables.
284##----------------------------------------------------------------------------##
285
286my %CompileOptionMap = (
287 '-nostdinc' => 0,
Anders Carlsson06c58b12008-12-19 20:56:23 +0000288 '-fblocks' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000289 '-fobjc-gc-only' => 0,
Ted Kremenek6c4312d2009-02-26 23:09:43 +0000290 '-fobjc-gc' => 0,
291 '-ffreestanding' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000292 '-include' => 1,
293 '-idirafter' => 1,
294 '-iprefix' => 1,
295 '-iquote' => 1,
296 '-isystem' => 1,
297 '-iwithprefix' => 1,
298 '-iwithprefixbefore' => 1
299);
300
301my %LinkerOptionMap = (
302 '-framework' => 1
303);
304
305my %CompilerLinkerOptionMap = (
306 '-isysroot' => 1,
307 '-arch' => 1,
Ted Kremeneke4f69522008-09-29 22:45:28 +0000308 '-v' => 0,
Ted Kremenekb10362a2008-09-30 23:40:25 +0000309 '-fpascal-strings' => 0,
Daniel Dunbar8d33cd72009-04-10 19:52:24 +0000310 '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '='
311 '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '='
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000312);
313
314my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000315 '-MT' => 1, # Ignore these preprocessor options.
316 '-MF' => 1,
317
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000318 '-fsyntax-only' => 0,
319 '-save-temps' => 0,
320 '-install_name' => 1,
321 '-exported_symbols_list' => 1,
322 '-current_version' => 1,
323 '-compatibility_version' => 1,
324 '-init' => 1,
325 '-e' => 1,
326 '-seg1addr' => 1,
327 '-bundle_loader' => 1,
328 '-multiply_defined' => 1,
329 '-sectorder' => 3,
330 '--param' => 1,
331 '-u' => 1
332);
333
334my %LangMap = (
335 'c' => 'c',
336 'cpp' => 'c++',
337 'cc' => 'c++',
338 'i' => 'c-cpp-output',
339 'm' => 'objective-c',
340 'mi' => 'objective-c-cpp-output'
341);
342
Ted Kremeneka30730e2008-09-29 16:15:20 +0000343my %UniqueOptions = (
344 '-isysroot' => 0
345);
346
Ted Kremenekefaf1912009-05-11 21:08:34 +0000347my %LangsAccepted = (
348 "objective-c" => 1,
349 "c" => 1
350);
351
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000352##----------------------------------------------------------------------------##
353# Main Logic.
354##----------------------------------------------------------------------------##
355
356my $Action = 'link';
357my @CompileOpts;
358my @LinkOpts;
359my @Files;
360my $Lang;
361my $Output;
Ted Kremeneka30730e2008-09-29 16:15:20 +0000362my %Uniqued;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000363
364# Forward arguments to gcc.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000365my $Status = system($CC,@ARGV);
Ted Kremenekcb344d02008-08-28 01:18:44 +0000366if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000367
368# Get the analysis options.
369my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
370if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
371
Zhongxing Xu07c37672008-10-27 14:26:32 +0000372# Get the store model.
373my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000374if (!defined $StoreModel) { $StoreModel = "basic"; }
375
376# Get the constraints engine.
377my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
Ted Kremenek9f4ecb32009-02-20 21:49:22 +0000378if (!defined $ConstraintsModel) { $ConstraintsModel = "range"; }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000379
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000380# Get the output format.
381my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
Ted Kremenek3891a152009-02-17 05:01:10 +0000382if (!defined $OutputFormat) { $OutputFormat = "html"; }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000383
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000384# Determine the level of verbosity.
385my $Verbose = 0;
386if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
387if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
388
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000389# Determine what clang-cc executable to use.
390my $ClangCC = $ENV{'CLANG_CC'};
391if (!defined $ClangCC) { $ClangCC = 'clang-cc'; }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000392
393# Get the HTML output directory.
394my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
395
Ted Kremenek84591322009-02-24 22:07:12 +0000396my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000397my %ArchsSeen;
Ted Kremenek84591322009-02-24 22:07:12 +0000398my $HadArch = 0;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000399
400# Process the arguments.
401foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000402 my $Arg = $ARGV[$i];
403 my ($ArgKey) = split /=/,$Arg,2;
404
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000405 # Modes ccc-analyzer supports
406 if ($Arg eq '-E') { $Action = 'preprocess'; }
407 elsif ($Arg eq '-c') { $Action = 'compile'; }
408 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek27783eb2008-09-25 20:17:57 +0000409
410 # Specially handle duplicate cases of -arch
411 if ($Arg eq "-arch") {
412 my $arch = $ARGV[$i+1];
Ted Kremenek84591322009-02-24 22:07:12 +0000413 # We don't want to process 'ppc' because of Clang's lack of support
414 # for Altivec (also some #defines won't likely be defined correctly, etc.)
415 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
416 $HadArch = 1;
Ted Kremenek27783eb2008-09-25 20:17:57 +0000417 ++$i;
418 next;
419 }
420
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000421 # Options with possible arguments that should pass through to compiler.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000422 if (defined $CompileOptionMap{$ArgKey}) {
423 my $Cnt = $CompileOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000424 push @CompileOpts,$Arg;
425 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
426 next;
427 }
428
429 # Options with possible arguments that should pass through to linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000430 if (defined $LinkerOptionMap{$ArgKey}) {
431 my $Cnt = $LinkerOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000432 push @LinkOpts,$Arg;
433 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
434 next;
435 }
436
437 # Options with possible arguments that should pass through to both compiler
438 # and the linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000439 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
440 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000441
Ted Kremeneka30730e2008-09-29 16:15:20 +0000442 # Check if this is an option that should have a unique value, and if so
443 # determine if the value was checked before.
444 if ($UniqueOptions{$Arg}) {
445 if (defined $Uniqued{$Arg}) {
446 $i += $Cnt;
447 next;
448 }
449 $Uniqued{$Arg} = 1;
450 }
451
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000452 push @CompileOpts,$Arg;
453 push @LinkOpts,$Arg;
454
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000455 while ($Cnt > 0) {
456 ++$i; --$Cnt;
457 push @CompileOpts, $ARGV[$i];
458 push @LinkOpts, $ARGV[$i];
459 }
460 next;
461 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000462
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000463 # Ignored options.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000464 if (defined $IgnoredOptionMap{$ArgKey}) {
465 my $Cnt = $IgnoredOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000466 while ($Cnt > 0) {
467 ++$i; --$Cnt;
468 }
469 next;
470 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000471
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000472 # Compile mode flags.
473 if ($Arg =~ /^-[D,I,U](.*)$/) {
474 my $Tmp = $Arg;
475 if ($1 eq '') {
476 # FIXME: Check if we are going off the end.
477 ++$i;
478 $Tmp = $Arg . $ARGV[$i];
479 }
480 push @CompileOpts,$Tmp;
481 next;
482 }
483
484 # Language.
485 if ($Arg eq '-x') {
486 $Lang = $ARGV[$i+1];
487 ++$i; next;
488 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000489
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000490 # Output file.
491 if ($Arg eq '-o') {
492 ++$i;
493 $Output = $ARGV[$i];
494 next;
495 }
496
497 # Get the link mode.
498 if ($Arg =~ /^-[l,L,O]/) {
499 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
500 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
501 else { push @LinkOpts,$Arg; }
502 next;
503 }
504
505 if ($Arg =~ /^-std=/) {
506 push @CompileOpts,$Arg;
507 next;
508 }
509
510# if ($Arg =~ /^-f/) {
511# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
512# push @CompileOpts,$Arg;
513# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
514# }
515
516 # Get the compiler/link mode.
517 if ($Arg =~ /^-F(.+)$/) {
518 my $Tmp = $Arg;
519 if ($1 eq '') {
520 # FIXME: Check if we are going off the end.
521 ++$i;
522 $Tmp = $Arg . $ARGV[$i];
523 }
524 push @CompileOpts,$Tmp;
525 push @LinkOpts,$Tmp;
526 next;
527 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000528
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000529 # Input files.
530 if ($Arg eq '-filelist') {
531 # FIXME: Make sure we aren't walking off the end.
532 open(IN, $ARGV[$i+1]);
533 while (<IN>) { s/\015?\012//; push @Files,$_; }
534 close(IN);
535 ++$i; next;
536 }
537
538 if (!($Arg =~ /^-/)) {
539 push @Files,$Arg; next;
540 }
541}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000542
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000543if ($Action eq 'compile' or $Action eq 'link') {
Ted Kremenek84591322009-02-24 22:07:12 +0000544 my @Archs = keys %ArchsSeen;
545 # Skip the file if we don't support the architectures specified.
Ted Kremenek0e0eb8b2009-02-25 00:10:37 +0000546 exit 0 if ($HadArch && scalar(@Archs) == 0);
Ted Kremenek84591322009-02-24 22:07:12 +0000547
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000548 foreach my $file (@Files) {
549 # Determine the language for the file.
550 my $FileLang = $Lang;
551
552 if (!defined($FileLang)) {
553 # Infer the language from the extension.
554 if ($file =~ /[.]([^.]+)$/) {
555 $FileLang = $LangMap{$1};
556 }
557 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000558
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000559 next if (!defined $FileLang);
Ted Kremenekefaf1912009-05-11 21:08:34 +0000560 next if (!defined $LangsAccepted{$FileLang});
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000561
562 my @AnalyzeArgs;
563
564 if ($FileLang ne 'unknown') {
565 push @AnalyzeArgs,'-x';
566 push @AnalyzeArgs,$FileLang;
567 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000568
Zhongxing Xu07c37672008-10-27 14:26:32 +0000569 if (defined $StoreModel) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000570 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
Zhongxing Xu07c37672008-10-27 14:26:32 +0000571 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000572
573 if (defined $ConstraintsModel) {
574 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
575 }
576
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000577 if (defined $OutputFormat) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000578 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000579 if ($OutputFormat eq "plist") {
580 # Change "Output" to be a file.
581 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
582 DIR => $HtmlDir);
583 $ResultFile = $f;
584 $CleanupFile = $f;
585 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000586 }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000587
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000588 push @AnalyzeArgs,@CompileOpts;
589 push @AnalyzeArgs,$file;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000590
Ted Kremenek27783eb2008-09-25 20:17:57 +0000591 if (scalar @Archs) {
592 foreach my $arch (@Archs) {
593 my @NewArgs;
594 push @NewArgs, '-arch';
595 push @NewArgs, $arch;
596 push @NewArgs, @AnalyzeArgs;
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000597 Analyze($ClangCC, \@NewArgs, $FileLang, $Output,
Ted Kremenek27783eb2008-09-25 20:17:57 +0000598 $Verbose, $HtmlDir, $file, $Analyses);
599 }
600 }
601 else {
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000602 Analyze($ClangCC, \@AnalyzeArgs, $FileLang, $Output,
Ted Kremenek27783eb2008-09-25 20:17:57 +0000603 $Verbose, $HtmlDir, $file, $Analyses);
604 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000605 }
606}
Ted Kremenekb0982882008-03-25 22:35:32 +0000607
Ted Kremenek948e06b2008-08-27 22:30:34 +0000608exit($Status >> 8);
609