blob: 5601387d794c4f204ecbb54ce6c8dc23c99ce3a4 [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 Kremenek51365b52009-12-15 02:35:54 +000017use FindBin;
Ted Kremenekb315a392008-09-21 19:56:14 +000018use Cwd qw/ getcwd abs_path /;
Ted Kremenek991c54b2008-08-08 20:46:42 +000019use File::Temp qw/ tempfile /;
20use File::Path qw / mkpath /;
Ted Kremenekddf32da2009-01-21 00:42:24 +000021use File::Basename;
Ted Kremenek1df35e32009-05-11 23:29:51 +000022use Text::ParseWords;
Ted Kremenek2ec5cd52008-08-25 20:44:31 +000023
Ted Kremenek51365b52009-12-15 02:35:54 +000024##===----------------------------------------------------------------------===##
25# Compiler command setup.
26##===----------------------------------------------------------------------===##
27
28my $Compiler;
29my $Clang;
30
31if ($FindBin::Script =~ /c\+\+-analyzer/) {
32 $Compiler = $ENV{'CCC_CXX'};
33 if (!defined $Compiler) { $Compiler = "g++"; }
34
35 $Clang = $ENV{'CLANG_CXX'};
36 if (!defined $Clang) { $Clang = 'clang++'; }
37}
38else {
39 $Compiler = $ENV{'CCC_CC'};
40 if (!defined $Compiler) { $Compiler = "gcc"; }
41
42 $Clang = $ENV{'CLANG'};
43 if (!defined $Clang) { $Clang = 'clang'; }
44}
45
46##===----------------------------------------------------------------------===##
47# Cleanup.
48##===----------------------------------------------------------------------===##
Ted Kremeneke600bed2009-07-30 23:55:19 +000049
50my $ReportFailures = $ENV{'CCC_REPORT_FAILURES'};
51if (!defined $ReportFailures) { $ReportFailures = 1; }
52
Ted Kremenekddf32da2009-01-21 00:42:24 +000053my $CleanupFile;
54my $ResultFile;
55
56# Remove any stale files at exit.
57END {
58 if (defined $CleanupFile && -z $CleanupFile) {
59 `rm -f $CleanupFile`;
60 }
61}
62
Ted Kremenek991c54b2008-08-08 20:46:42 +000063##----------------------------------------------------------------------------##
64# Process Clang Crashes.
65##----------------------------------------------------------------------------##
66
67sub GetPPExt {
68 my $Lang = shift;
Ted Kremenek79433b52009-12-16 18:32:41 +000069 if ($Lang =~ /objective-c\+\+/) { return ".mii" };
Ted Kremenek991c54b2008-08-08 20:46:42 +000070 if ($Lang =~ /objective-c/) { return ".mi"; }
Ted Kremenek51b939f2009-12-16 05:02:47 +000071 if ($Lang =~ /c\+\+/) { return ".ii"; }
Ted Kremenek991c54b2008-08-08 20:46:42 +000072 return ".i";
73}
74
Ted Kremeneka4d8cde2009-04-28 17:37:44 +000075# Set this to 1 if we want to include 'parser rejects' files.
76my $IncludeParserRejects = 0;
Ted Kremenek5daa3be2009-01-27 01:19:08 +000077my $ParserRejects = "Parser Rejects";
Ted Kremeneka4d8cde2009-04-28 17:37:44 +000078
Ted Kremenek938eef12009-02-17 23:31:05 +000079my $AttributeIgnored = "Attribute Ignored";
Ted Kremenek5daa3be2009-01-27 01:19:08 +000080
Ted Kremenek5d31f832008-08-18 18:38:29 +000081sub ProcessClangFailure {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +000082 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
Ted Kremenek938eef12009-02-17 23:31:05 +000083 my $Dir = "$HtmlDir/failures";
Ted Kremenek991c54b2008-08-08 20:46:42 +000084 mkpath $Dir;
Ted Kremenek5daa3be2009-01-27 01:19:08 +000085
86 my $prefix = "clang_crash";
Ted Kremenek938eef12009-02-17 23:31:05 +000087 if ($ErrorType eq $ParserRejects) {
88 $prefix = "clang_parser_rejects";
89 }
90 elsif ($ErrorType eq $AttributeIgnored) {
91 $prefix = "clang_attribute_ignored";
92 }
Ted Kremenekc3998fa2008-09-25 00:51:44 +000093
Ted Kremenek3386c8a2009-07-28 00:14:21 +000094 # Generate the preprocessed file with Clang.
Ted Kremenek5daa3be2009-01-27 01:19:08 +000095 my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX",
96 SUFFIX => GetPPExt($Lang),
97 DIR => $Dir);
Ted Kremenek2a3a8b92009-12-11 22:44:53 +000098 system $Clang, @$Args, "-E", "-o", $PPFile;
Ted Kremenek991c54b2008-08-08 20:46:42 +000099 close ($PPH);
Ted Kremenekc3998fa2008-09-25 00:51:44 +0000100
101 # Create the info file.
Ted Kremenek82a12532008-09-25 00:25:16 +0000102 open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
Ted Kremenek5f2825f2008-09-21 18:04:49 +0000103 print OUT abs_path($file), "\n";
Ted Kremenek5d31f832008-08-18 18:38:29 +0000104 print OUT "$ErrorType\n";
Ted Kremenek2dd7ad12008-08-18 20:55:25 +0000105 print OUT "@$Args\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000106 close OUT;
Ted Kremenek82a12532008-09-25 00:25:16 +0000107 `uname -a >> $PPFile.info.txt 2>&1`;
Ted Kremenek51365b52009-12-15 02:35:54 +0000108 `$Compiler -v >> $PPFile.info.txt 2>&1`;
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000109 system 'mv',$ofile,"$PPFile.stderr.txt";
Ted Kremenek938eef12009-02-17 23:31:05 +0000110 return (basename $PPFile);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000111}
Ted Kremenekb0982882008-03-25 22:35:32 +0000112
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000113##----------------------------------------------------------------------------##
114# Running the analyzer.
115##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +0000116
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000117sub GetCCArgs {
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000118 my $mode = shift;
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000119 my $Args = shift;
120
121 pipe (FROM_CHILD, TO_PARENT);
122 my $pid = fork();
123 if ($pid == 0) {
124 close FROM_CHILD;
125 open(STDOUT,">&", \*TO_PARENT);
126 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000127 exec $Clang, "-###", $mode, @$Args;
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000128 }
129 close(TO_PARENT);
130 my $line;
131 while (<FROM_CHILD>) {
Ted Kremenek32317b22009-12-11 23:12:52 +0000132 next if (!/-cc1/);
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000133 $line = $_;
134 }
135
136 waitpid($pid,0);
137 close(FROM_CHILD);
138
Ted Kremenek32317b22009-12-11 23:12:52 +0000139 die "could not find clang line\n" if (!defined $line);
Ted Kremenek1df35e32009-05-11 23:29:51 +0000140 # Strip the newline and initial whitspace
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000141 chomp $line;
Ted Kremenek1df35e32009-05-11 23:29:51 +0000142 $line =~ s/^\s+//;
Ted Kremenek2aefcb22010-04-06 19:41:24 +0000143 my @items = quotewords('\s+', 0, $line);
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000144 my $cmd = shift @items;
Ted Kremenek32317b22009-12-11 23:12:52 +0000145 die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/));
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000146 return \@items;
147}
148
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000149sub Analyze {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000150 my ($Clang, $Args, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000151 $file) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +0000152
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000153 my $Cmd;
154 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000155 my @CmdArgsSansAnalyses;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000156
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000157 if ($Lang =~ /header/) {
158 exit 0 if (!defined ($Output));
159 $Cmd = 'cp';
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000160 push @CmdArgs, $file;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000161 # Remove the PCH extension.
162 $Output =~ s/[.]gch$//;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000163 push @CmdArgs, $Output;
164 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000165 }
166 else {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000167 $Cmd = $Clang;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000168 if ($Lang eq "objective-c" || $Lang eq "objective-c++") {
169 push @$Args,'-DIBOutlet=__attribute__((iboutlet))';
170 push @$Args,'-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection)))';
171 push @$Args,'-DIBAction=void)__attribute__((ibaction)';
172 }
173
174 # Create arguments for doing regular parsing.
175 my $SyntaxArgs = GetCCArgs("-fsyntax-only", $Args);
176 @CmdArgsSansAnalyses = @CmdArgs;
177 push @CmdArgsSansAnalyses, @$SyntaxArgs;
Ted Kremenek8382cf52009-11-13 18:46:29 +0000178
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000179 # Create arguments for doing static analysis.
180 if (defined $ResultFile) {
181 push @$Args,'-o';
182 push @$Args, $ResultFile;
183 }
184 elsif (defined $HtmlDir) {
185 push @$Args,'-o';
186 push @$Args, $HtmlDir;
187 }
188 push @$Args,"-Xclang";
189 push @$Args,"-analyzer-display-progress";
190
191 foreach my $arg (@$AnalyzeArgs) {
192 push @$Args, "-Xclang";
193 push @$Args, $arg;
Ted Kremenek8382cf52009-11-13 18:46:29 +0000194 }
195
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000196 # Display Ubiviz graph?
197 if (defined $ENV{'CCC_UBI'}) {
198 push @$Args, "-Xclang";
199 push @$Args,"-analyzer-viz-egraph-ubigraph";
200 }
201
202 my $AnalysisArgs = GetCCArgs("--analyze", $Args);
203 push @CmdArgs, @$AnalysisArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000204 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000205
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000206 my @PrintArgs;
207 my $dir;
Ted Kremenek63fe5ec2009-08-02 05:42:46 +0000208
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000209 if ($Verbose) {
210 $dir = getcwd();
211 print STDERR "\n[LOCATION]: $dir\n";
212 push @PrintArgs,"'$Cmd'";
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000213 foreach my $arg (@CmdArgs) {
214 push @PrintArgs,"\'$arg\'";
215 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000216 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000217 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +0000218 # We MUST print to stderr. Some clients use the stdout output of
219 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000220 print STDERR join(' ',@PrintArgs);
221 print STDERR "\n";
222 }
223 elsif ($Verbose == 2) {
224 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
225 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000226
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000227 # Capture the STDERR of clang and send it to a temporary file.
228 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
229 # We save the output file in the 'crashes' directory if clang encounters
230 # any problems with the file.
Ted Kremenek13462682008-09-11 23:05:26 +0000231 pipe (FROM_CHILD, TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000232 my $pid = fork();
233 if ($pid == 0) {
Ted Kremenek13462682008-09-11 23:05:26 +0000234 close FROM_CHILD;
235 open(STDOUT,">&", \*TO_PARENT);
236 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000237 exec $Cmd, @CmdArgs;
238 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000239
Ted Kremenek13462682008-09-11 23:05:26 +0000240 close TO_PARENT;
241 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
242
243 while (<FROM_CHILD>) {
244 print $ofh $_;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000245 print STDERR $_;
Ted Kremenek13462682008-09-11 23:05:26 +0000246 }
247
248 waitpid($pid,0);
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000249 close(FROM_CHILD);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000250 my $Result = $?;
251
252 # Did the command die because of a signal?
Ted Kremeneke600bed2009-07-30 23:55:19 +0000253 if ($ReportFailures) {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000254 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
255 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
Ted Kremeneke600bed2009-07-30 23:55:19 +0000256 $HtmlDir, "Crash", $ofile);
Ted Kremenek5de40922009-02-27 06:17:38 +0000257 }
Ted Kremeneke600bed2009-07-30 23:55:19 +0000258 elsif ($Result) {
259 if ($IncludeParserRejects && !($file =~/conftest/)) {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000260 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
Ted Kremeneke600bed2009-07-30 23:55:19 +0000261 $HtmlDir, $ParserRejects, $ofile);
Ted Kremenek938eef12009-02-17 23:31:05 +0000262 }
Ted Kremeneke600bed2009-07-30 23:55:19 +0000263 }
264 else {
265 # Check if there were any unhandled attributes.
266 if (open(CHILD, $ofile)) {
267 my %attributes_not_handled;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000268
Ted Kremeneke600bed2009-07-30 23:55:19 +0000269 # Don't flag warnings about the following attributes that we
270 # know are currently not supported by Clang.
271 $attributes_not_handled{"cdecl"} = 1;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000272
Ted Kremeneke600bed2009-07-30 23:55:19 +0000273 my $ppfile;
274 while (<CHILD>) {
275 next if (! /warning: '([^\']+)' attribute ignored/);
276
277 # Have we already spotted this unhandled attribute?
278 next if (defined $attributes_not_handled{$1});
279 $attributes_not_handled{$1} = 1;
280
281 # Get the name of the attribute file.
282 my $dir = "$HtmlDir/failures";
283 my $afile = "$dir/attribute_ignored_$1.txt";
284
285 # Only create another preprocessed file if the attribute file
286 # doesn't exist yet.
287 next if (-e $afile);
288
289 # Add this file to the list of files that contained this attribute.
290 # Generate a preprocessed file if we haven't already.
291 if (!(defined $ppfile)) {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000292 $ppfile = ProcessClangFailure($Clang, $Lang, $file,
Ted Kremeneke600bed2009-07-30 23:55:19 +0000293 \@CmdArgsSansAnalyses,
294 $HtmlDir, $AttributeIgnored, $ofile);
295 }
296
297 mkpath $dir;
298 open(AFILE, ">$afile");
299 print AFILE "$ppfile\n";
300 close(AFILE);
301 }
302 close CHILD;
303 }
Ted Kremenek938eef12009-02-17 23:31:05 +0000304 }
305 }
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000306
Ted Kremeneke3bf8a82009-08-04 00:55:59 +0000307 unlink($ofile);
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000308}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000309
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000310##----------------------------------------------------------------------------##
311# Lookup tables.
312##----------------------------------------------------------------------------##
313
314my %CompileOptionMap = (
315 '-nostdinc' => 0,
Anders Carlsson06c58b12008-12-19 20:56:23 +0000316 '-fblocks' => 0,
Shantonu Send9d97262010-07-03 03:08:23 +0000317 '-fno-builtin' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000318 '-fobjc-gc-only' => 0,
Ted Kremenek6c4312d2009-02-26 23:09:43 +0000319 '-fobjc-gc' => 0,
320 '-ffreestanding' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000321 '-include' => 1,
322 '-idirafter' => 1,
Ted Kremenek55d7d362010-06-08 18:27:55 +0000323 '-imacros' => 1,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000324 '-iprefix' => 1,
325 '-iquote' => 1,
326 '-isystem' => 1,
327 '-iwithprefix' => 1,
328 '-iwithprefixbefore' => 1
329);
330
331my %LinkerOptionMap = (
332 '-framework' => 1
333);
334
335my %CompilerLinkerOptionMap = (
336 '-isysroot' => 1,
337 '-arch' => 1,
Charles Davisa1359ba2010-03-02 15:26:41 +0000338 '-m32' => 0,
339 '-m64' => 0,
Ted Kremeneke4f69522008-09-29 22:45:28 +0000340 '-v' => 0,
Ted Kremenekb10362a2008-09-30 23:40:25 +0000341 '-fpascal-strings' => 0,
Daniel Dunbar8d33cd72009-04-10 19:52:24 +0000342 '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '='
343 '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '='
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000344);
345
346my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000347 '-MT' => 1, # Ignore these preprocessor options.
348 '-MF' => 1,
349
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000350 '-fsyntax-only' => 0,
351 '-save-temps' => 0,
352 '-install_name' => 1,
353 '-exported_symbols_list' => 1,
354 '-current_version' => 1,
355 '-compatibility_version' => 1,
356 '-init' => 1,
357 '-e' => 1,
358 '-seg1addr' => 1,
359 '-bundle_loader' => 1,
360 '-multiply_defined' => 1,
361 '-sectorder' => 3,
362 '--param' => 1,
363 '-u' => 1
364);
365
366my %LangMap = (
367 'c' => 'c',
Shantonu Send9d97262010-07-03 03:08:23 +0000368 'cp' => 'c++',
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000369 'cpp' => 'c++',
370 'cc' => 'c++',
371 'i' => 'c-cpp-output',
372 'm' => 'objective-c',
373 'mi' => 'objective-c-cpp-output'
374);
375
Ted Kremeneka30730e2008-09-29 16:15:20 +0000376my %UniqueOptions = (
377 '-isysroot' => 0
378);
379
Ted Kremenekf6f8d5a2010-03-08 19:06:44 +0000380##----------------------------------------------------------------------------##
381# Languages accepted.
382##----------------------------------------------------------------------------##
383
Ted Kremenekefaf1912009-05-11 21:08:34 +0000384my %LangsAccepted = (
385 "objective-c" => 1,
Ted Kremenekf6f8d5a2010-03-08 19:06:44 +0000386 "c" => 1
Ted Kremenekefaf1912009-05-11 21:08:34 +0000387);
388
Ted Kremenekf6f8d5a2010-03-08 19:06:44 +0000389if (defined $ENV{'CCC_ANALYZER_CPLUSPLUS'}) {
390 $LangsAccepted{"c++"} = 1;
391 $LangsAccepted{"objective-c++"} = 1;
392}
393
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000394##----------------------------------------------------------------------------##
395# Main Logic.
396##----------------------------------------------------------------------------##
397
398my $Action = 'link';
399my @CompileOpts;
400my @LinkOpts;
401my @Files;
402my $Lang;
403my $Output;
Ted Kremeneka30730e2008-09-29 16:15:20 +0000404my %Uniqued;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000405
406# Forward arguments to gcc.
Ted Kremenek51365b52009-12-15 02:35:54 +0000407my $Status = system($Compiler,@ARGV);
Tom Carea716f632010-09-29 23:48:31 +0000408if (defined $ENV{'CCC_ANALYZER_LOG'}) {
409 print "$Compiler @ARGV\n";
410}
Ted Kremenekcb344d02008-08-28 01:18:44 +0000411if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000412
413# Get the analysis options.
414my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000415
Zhongxing Xu07c37672008-10-27 14:26:32 +0000416# Get the store model.
417my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000418
419# Get the constraints engine.
420my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
Zhongxing Xu07c37672008-10-27 14:26:32 +0000421
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000422# Get the output format.
423my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
Ted Kremenek3891a152009-02-17 05:01:10 +0000424if (!defined $OutputFormat) { $OutputFormat = "html"; }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000425
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000426# Determine the level of verbosity.
427my $Verbose = 0;
428if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
429if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
430
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000431# Get the HTML output directory.
432my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
433
Ted Kremenek84591322009-02-24 22:07:12 +0000434my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000435my %ArchsSeen;
Ted Kremenek84591322009-02-24 22:07:12 +0000436my $HadArch = 0;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000437
438# Process the arguments.
439foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000440 my $Arg = $ARGV[$i];
441 my ($ArgKey) = split /=/,$Arg,2;
442
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000443 # Modes ccc-analyzer supports
Ted Kremenekd732a7b2009-08-04 00:57:12 +0000444 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000445 elsif ($Arg eq '-c') { $Action = 'compile'; }
446 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek27783eb2008-09-25 20:17:57 +0000447
448 # Specially handle duplicate cases of -arch
449 if ($Arg eq "-arch") {
450 my $arch = $ARGV[$i+1];
Ted Kremenek84591322009-02-24 22:07:12 +0000451 # We don't want to process 'ppc' because of Clang's lack of support
452 # for Altivec (also some #defines won't likely be defined correctly, etc.)
453 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
454 $HadArch = 1;
Ted Kremenek27783eb2008-09-25 20:17:57 +0000455 ++$i;
456 next;
457 }
458
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000459 # Options with possible arguments that should pass through to compiler.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000460 if (defined $CompileOptionMap{$ArgKey}) {
461 my $Cnt = $CompileOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000462 push @CompileOpts,$Arg;
463 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
464 next;
465 }
466
467 # Options with possible arguments that should pass through to linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000468 if (defined $LinkerOptionMap{$ArgKey}) {
469 my $Cnt = $LinkerOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000470 push @LinkOpts,$Arg;
471 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
472 next;
473 }
474
475 # Options with possible arguments that should pass through to both compiler
476 # and the linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000477 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
478 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000479
Ted Kremeneka30730e2008-09-29 16:15:20 +0000480 # Check if this is an option that should have a unique value, and if so
481 # determine if the value was checked before.
482 if ($UniqueOptions{$Arg}) {
483 if (defined $Uniqued{$Arg}) {
484 $i += $Cnt;
485 next;
486 }
487 $Uniqued{$Arg} = 1;
488 }
489
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000490 push @CompileOpts,$Arg;
491 push @LinkOpts,$Arg;
492
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000493 while ($Cnt > 0) {
494 ++$i; --$Cnt;
495 push @CompileOpts, $ARGV[$i];
496 push @LinkOpts, $ARGV[$i];
497 }
498 next;
499 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000500
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000501 # Ignored options.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000502 if (defined $IgnoredOptionMap{$ArgKey}) {
503 my $Cnt = $IgnoredOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000504 while ($Cnt > 0) {
505 ++$i; --$Cnt;
506 }
507 next;
508 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000509
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000510 # Compile mode flags.
511 if ($Arg =~ /^-[D,I,U](.*)$/) {
512 my $Tmp = $Arg;
513 if ($1 eq '') {
514 # FIXME: Check if we are going off the end.
515 ++$i;
516 $Tmp = $Arg . $ARGV[$i];
517 }
518 push @CompileOpts,$Tmp;
519 next;
520 }
521
522 # Language.
523 if ($Arg eq '-x') {
524 $Lang = $ARGV[$i+1];
525 ++$i; next;
526 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000527
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000528 # Output file.
529 if ($Arg eq '-o') {
530 ++$i;
531 $Output = $ARGV[$i];
532 next;
533 }
534
535 # Get the link mode.
536 if ($Arg =~ /^-[l,L,O]/) {
537 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
538 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
539 else { push @LinkOpts,$Arg; }
540 next;
541 }
542
543 if ($Arg =~ /^-std=/) {
544 push @CompileOpts,$Arg;
545 next;
546 }
547
548# if ($Arg =~ /^-f/) {
549# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
550# push @CompileOpts,$Arg;
551# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
552# }
553
554 # Get the compiler/link mode.
555 if ($Arg =~ /^-F(.+)$/) {
556 my $Tmp = $Arg;
557 if ($1 eq '') {
558 # FIXME: Check if we are going off the end.
559 ++$i;
560 $Tmp = $Arg . $ARGV[$i];
561 }
562 push @CompileOpts,$Tmp;
563 push @LinkOpts,$Tmp;
564 next;
565 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000566
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000567 # Input files.
568 if ($Arg eq '-filelist') {
569 # FIXME: Make sure we aren't walking off the end.
570 open(IN, $ARGV[$i+1]);
571 while (<IN>) { s/\015?\012//; push @Files,$_; }
572 close(IN);
Ted Kremenek08959372009-08-14 18:20:50 +0000573 ++$i;
574 next;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000575 }
576
Ted Kremenek08959372009-08-14 18:20:50 +0000577 # Handle -Wno-. We don't care about extra warnings, but
578 # we should suppress ones that we don't want to see.
579 if ($Arg =~ /^-Wno-/) {
580 push @CompileOpts, $Arg;
581 next;
582 }
583
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000584 if (!($Arg =~ /^-/)) {
Ted Kremenek08959372009-08-14 18:20:50 +0000585 push @Files, $Arg;
586 next;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000587 }
588}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000589
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000590if ($Action eq 'compile' or $Action eq 'link') {
Ted Kremenek84591322009-02-24 22:07:12 +0000591 my @Archs = keys %ArchsSeen;
592 # Skip the file if we don't support the architectures specified.
Ted Kremenek0e0eb8b2009-02-25 00:10:37 +0000593 exit 0 if ($HadArch && scalar(@Archs) == 0);
Ted Kremenek84591322009-02-24 22:07:12 +0000594
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000595 foreach my $file (@Files) {
596 # Determine the language for the file.
597 my $FileLang = $Lang;
598
599 if (!defined($FileLang)) {
600 # Infer the language from the extension.
601 if ($file =~ /[.]([^.]+)$/) {
602 $FileLang = $LangMap{$1};
603 }
604 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000605
Ted Kremenek1d81ed92010-02-12 00:10:34 +0000606 # FileLang still not defined? Skip the file.
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000607 next if (!defined $FileLang);
Ted Kremenek1d81ed92010-02-12 00:10:34 +0000608
609 # Language not accepted?
Ted Kremenekefaf1912009-05-11 21:08:34 +0000610 next if (!defined $LangsAccepted{$FileLang});
Ted Kremenek1d81ed92010-02-12 00:10:34 +0000611
Ted Kremenek9a9e0d42009-05-15 04:20:31 +0000612 my @CmdArgs;
613 my @AnalyzeArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000614
615 if ($FileLang ne 'unknown') {
Ted Kremenek9a9e0d42009-05-15 04:20:31 +0000616 push @CmdArgs,'-x';
617 push @CmdArgs,$FileLang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000618 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000619
Zhongxing Xu07c37672008-10-27 14:26:32 +0000620 if (defined $StoreModel) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000621 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
Zhongxing Xu07c37672008-10-27 14:26:32 +0000622 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000623
624 if (defined $ConstraintsModel) {
625 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
626 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000627
628# if (defined $Analyses) {
629# push @AnalyzeArgs, split '\s+', $Analyses;
630# }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000631
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000632 if (defined $OutputFormat) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000633 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
Ted Kremenek7753b352009-07-27 22:10:34 +0000634 if ($OutputFormat =~ /plist/) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000635 # Change "Output" to be a file.
636 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
637 DIR => $HtmlDir);
638 $ResultFile = $f;
639 $CleanupFile = $f;
640 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000641 }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000642
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000643 push @CmdArgs, @CompileOpts;
644 push @CmdArgs, $file;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000645
Ted Kremenek27783eb2008-09-25 20:17:57 +0000646 if (scalar @Archs) {
647 foreach my $arch (@Archs) {
648 my @NewArgs;
649 push @NewArgs, '-arch';
650 push @NewArgs, $arch;
Ted Kremenek9a9e0d42009-05-15 04:20:31 +0000651 push @NewArgs, @CmdArgs;
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000652 Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output,
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000653 $Verbose, $HtmlDir, $file);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000654 }
655 }
656 else {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000657 Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output,
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000658 $Verbose, $HtmlDir, $file);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000659 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000660 }
661}
Ted Kremenekb0982882008-03-25 22:35:32 +0000662
Ted Kremenek948e06b2008-08-27 22:30:34 +0000663exit($Status >> 8);
664