blob: 7793a8db49b1e48fadd7c5d608eeb87e11d4b689 [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 Kremenek27871ea2011-03-10 21:10:08 +0000150 my ($Clang, $OriginalArgs, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000151 $file) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +0000152
Ted Kremenek27871ea2011-03-10 21:10:08 +0000153 my @Args = @$OriginalArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000154 my $Cmd;
155 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000156 my @CmdArgsSansAnalyses;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000157
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000158 if ($Lang =~ /header/) {
159 exit 0 if (!defined ($Output));
160 $Cmd = 'cp';
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000161 push @CmdArgs, $file;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000162 # Remove the PCH extension.
163 $Output =~ s/[.]gch$//;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000164 push @CmdArgs, $Output;
165 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000166 }
167 else {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000168 $Cmd = $Clang;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000169 if ($Lang eq "objective-c" || $Lang eq "objective-c++") {
Ted Kremenek27871ea2011-03-10 21:10:08 +0000170 push @Args,'-DIBOutlet=__attribute__((iboutlet))';
171 push @Args,'-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection)))';
172 push @Args,'-DIBAction=void)__attribute__((ibaction)';
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000173 }
174
175 # Create arguments for doing regular parsing.
Ted Kremenek27871ea2011-03-10 21:10:08 +0000176 my $SyntaxArgs = GetCCArgs("-fsyntax-only", \@Args);
177 @CmdArgsSansAnalyses = @$SyntaxArgs;
178
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000179 # Create arguments for doing static analysis.
180 if (defined $ResultFile) {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000181 push @Args, '-o', $ResultFile;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000182 }
183 elsif (defined $HtmlDir) {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000184 push @Args, '-o', $HtmlDir;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000185 }
Ted Kremenekb26fbc12011-03-21 20:12:21 +0000186 if ($Verbose) {
187 push @Args, "-Xclang", "-analyzer-display-progress";
188 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000189
190 foreach my $arg (@$AnalyzeArgs) {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000191 push @Args, "-Xclang", $arg;
Ted Kremenek8382cf52009-11-13 18:46:29 +0000192 }
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000193
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000194 # Display Ubiviz graph?
195 if (defined $ENV{'CCC_UBI'}) {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000196 push @Args, "-Xclang", "-analyzer-viz-egraph-ubigraph";
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000197 }
198
Ted Kremenek27871ea2011-03-10 21:10:08 +0000199 my $AnalysisArgs = GetCCArgs("--analyze", \@Args);
200 @CmdArgs = @$AnalysisArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000201 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000202
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000203 my @PrintArgs;
204 my $dir;
Ted Kremenek63fe5ec2009-08-02 05:42:46 +0000205
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000206 if ($Verbose) {
207 $dir = getcwd();
208 print STDERR "\n[LOCATION]: $dir\n";
209 push @PrintArgs,"'$Cmd'";
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000210 foreach my $arg (@CmdArgs) {
211 push @PrintArgs,"\'$arg\'";
212 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000213 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000214 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +0000215 # We MUST print to stderr. Some clients use the stdout output of
216 # gcc for various purposes.
Ted Kremenek27871ea2011-03-10 21:10:08 +0000217 print STDERR join(' ', @PrintArgs);
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000218 print STDERR "\n";
219 }
220 elsif ($Verbose == 2) {
221 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
222 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000223
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000224 # Capture the STDERR of clang and send it to a temporary file.
225 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
226 # We save the output file in the 'crashes' directory if clang encounters
227 # any problems with the file.
Ted Kremenek13462682008-09-11 23:05:26 +0000228 pipe (FROM_CHILD, TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000229 my $pid = fork();
230 if ($pid == 0) {
Ted Kremenek13462682008-09-11 23:05:26 +0000231 close FROM_CHILD;
232 open(STDOUT,">&", \*TO_PARENT);
233 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000234 exec $Cmd, @CmdArgs;
235 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000236
Ted Kremenek13462682008-09-11 23:05:26 +0000237 close TO_PARENT;
238 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
239
240 while (<FROM_CHILD>) {
241 print $ofh $_;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000242 print STDERR $_;
Ted Kremenek13462682008-09-11 23:05:26 +0000243 }
244
245 waitpid($pid,0);
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000246 close(FROM_CHILD);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000247 my $Result = $?;
248
249 # Did the command die because of a signal?
Ted Kremeneke600bed2009-07-30 23:55:19 +0000250 if ($ReportFailures) {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000251 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
252 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
Ted Kremeneke600bed2009-07-30 23:55:19 +0000253 $HtmlDir, "Crash", $ofile);
Ted Kremenek5de40922009-02-27 06:17:38 +0000254 }
Ted Kremeneke600bed2009-07-30 23:55:19 +0000255 elsif ($Result) {
256 if ($IncludeParserRejects && !($file =~/conftest/)) {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000257 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
Ted Kremeneke600bed2009-07-30 23:55:19 +0000258 $HtmlDir, $ParserRejects, $ofile);
Ted Kremenek938eef12009-02-17 23:31:05 +0000259 }
Ted Kremeneke600bed2009-07-30 23:55:19 +0000260 }
261 else {
262 # Check if there were any unhandled attributes.
263 if (open(CHILD, $ofile)) {
264 my %attributes_not_handled;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000265
Ted Kremeneke600bed2009-07-30 23:55:19 +0000266 # Don't flag warnings about the following attributes that we
267 # know are currently not supported by Clang.
268 $attributes_not_handled{"cdecl"} = 1;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000269
Ted Kremeneke600bed2009-07-30 23:55:19 +0000270 my $ppfile;
271 while (<CHILD>) {
272 next if (! /warning: '([^\']+)' attribute ignored/);
273
274 # Have we already spotted this unhandled attribute?
275 next if (defined $attributes_not_handled{$1});
276 $attributes_not_handled{$1} = 1;
277
278 # Get the name of the attribute file.
279 my $dir = "$HtmlDir/failures";
280 my $afile = "$dir/attribute_ignored_$1.txt";
281
282 # Only create another preprocessed file if the attribute file
283 # doesn't exist yet.
284 next if (-e $afile);
285
286 # Add this file to the list of files that contained this attribute.
287 # Generate a preprocessed file if we haven't already.
288 if (!(defined $ppfile)) {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000289 $ppfile = ProcessClangFailure($Clang, $Lang, $file,
Ted Kremeneke600bed2009-07-30 23:55:19 +0000290 \@CmdArgsSansAnalyses,
291 $HtmlDir, $AttributeIgnored, $ofile);
292 }
293
294 mkpath $dir;
295 open(AFILE, ">$afile");
296 print AFILE "$ppfile\n";
297 close(AFILE);
298 }
299 close CHILD;
300 }
Ted Kremenek938eef12009-02-17 23:31:05 +0000301 }
302 }
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000303
Ted Kremeneke3bf8a82009-08-04 00:55:59 +0000304 unlink($ofile);
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000305}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000306
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000307##----------------------------------------------------------------------------##
308# Lookup tables.
309##----------------------------------------------------------------------------##
310
311my %CompileOptionMap = (
312 '-nostdinc' => 0,
Anders Carlsson06c58b12008-12-19 20:56:23 +0000313 '-fblocks' => 0,
Shantonu Send9d97262010-07-03 03:08:23 +0000314 '-fno-builtin' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000315 '-fobjc-gc-only' => 0,
Ted Kremenek6c4312d2009-02-26 23:09:43 +0000316 '-fobjc-gc' => 0,
317 '-ffreestanding' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000318 '-include' => 1,
319 '-idirafter' => 1,
Ted Kremenek55d7d362010-06-08 18:27:55 +0000320 '-imacros' => 1,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000321 '-iprefix' => 1,
322 '-iquote' => 1,
323 '-isystem' => 1,
324 '-iwithprefix' => 1,
325 '-iwithprefixbefore' => 1
326);
327
328my %LinkerOptionMap = (
329 '-framework' => 1
330);
331
332my %CompilerLinkerOptionMap = (
333 '-isysroot' => 1,
334 '-arch' => 1,
Charles Davisa1359ba2010-03-02 15:26:41 +0000335 '-m32' => 0,
336 '-m64' => 0,
Ted Kremeneke4f69522008-09-29 22:45:28 +0000337 '-v' => 0,
Ted Kremenekb10362a2008-09-30 23:40:25 +0000338 '-fpascal-strings' => 0,
Daniel Dunbar8d33cd72009-04-10 19:52:24 +0000339 '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '='
340 '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '='
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000341);
342
343my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000344 '-MT' => 1, # Ignore these preprocessor options.
345 '-MF' => 1,
346
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000347 '-fsyntax-only' => 0,
348 '-save-temps' => 0,
349 '-install_name' => 1,
350 '-exported_symbols_list' => 1,
351 '-current_version' => 1,
352 '-compatibility_version' => 1,
353 '-init' => 1,
354 '-e' => 1,
355 '-seg1addr' => 1,
356 '-bundle_loader' => 1,
357 '-multiply_defined' => 1,
358 '-sectorder' => 3,
359 '--param' => 1,
360 '-u' => 1
361);
362
363my %LangMap = (
364 'c' => 'c',
Shantonu Send9d97262010-07-03 03:08:23 +0000365 'cp' => 'c++',
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000366 'cpp' => 'c++',
367 'cc' => 'c++',
Anna Zaks71f11d62011-08-31 23:53:24 +0000368 'ii' => 'c++',
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000369 'i' => 'c-cpp-output',
370 'm' => 'objective-c',
Anna Zaks71f11d62011-08-31 23:53:24 +0000371 'mi' => 'objective-c-cpp-output',
372 'mm' => 'objective-c++'
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000373);
374
Ted Kremeneka30730e2008-09-29 16:15:20 +0000375my %UniqueOptions = (
376 '-isysroot' => 0
377);
378
Ted Kremenekf6f8d5a2010-03-08 19:06:44 +0000379##----------------------------------------------------------------------------##
380# Languages accepted.
381##----------------------------------------------------------------------------##
382
Ted Kremenekefaf1912009-05-11 21:08:34 +0000383my %LangsAccepted = (
384 "objective-c" => 1,
Ted Kremenek0a208182011-08-11 22:47:20 +0000385 "c" => 1,
386 "c++" => 1,
387 "objective-c++" => 1
Ted Kremenekefaf1912009-05-11 21:08:34 +0000388);
389
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000390##----------------------------------------------------------------------------##
391# Main Logic.
392##----------------------------------------------------------------------------##
393
394my $Action = 'link';
395my @CompileOpts;
396my @LinkOpts;
397my @Files;
398my $Lang;
399my $Output;
Ted Kremeneka30730e2008-09-29 16:15:20 +0000400my %Uniqued;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000401
402# Forward arguments to gcc.
Ted Kremenek51365b52009-12-15 02:35:54 +0000403my $Status = system($Compiler,@ARGV);
Tom Carea716f632010-09-29 23:48:31 +0000404if (defined $ENV{'CCC_ANALYZER_LOG'}) {
405 print "$Compiler @ARGV\n";
406}
Ted Kremenekcb344d02008-08-28 01:18:44 +0000407if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000408
409# Get the analysis options.
410my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000411
Zhongxing Xu07c37672008-10-27 14:26:32 +0000412# Get the store model.
413my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000414
415# Get the constraints engine.
416my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
Zhongxing Xu07c37672008-10-27 14:26:32 +0000417
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000418# Get the output format.
419my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
Ted Kremenek3891a152009-02-17 05:01:10 +0000420if (!defined $OutputFormat) { $OutputFormat = "html"; }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000421
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000422# Determine the level of verbosity.
423my $Verbose = 0;
424if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
425if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
426
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000427# Get the HTML output directory.
428my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
429
Ted Kremenek84591322009-02-24 22:07:12 +0000430my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000431my %ArchsSeen;
Ted Kremenek84591322009-02-24 22:07:12 +0000432my $HadArch = 0;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000433
434# Process the arguments.
435foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000436 my $Arg = $ARGV[$i];
437 my ($ArgKey) = split /=/,$Arg,2;
438
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000439 # Modes ccc-analyzer supports
Ted Kremenekd732a7b2009-08-04 00:57:12 +0000440 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000441 elsif ($Arg eq '-c') { $Action = 'compile'; }
442 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek27783eb2008-09-25 20:17:57 +0000443
444 # Specially handle duplicate cases of -arch
445 if ($Arg eq "-arch") {
446 my $arch = $ARGV[$i+1];
Ted Kremenek84591322009-02-24 22:07:12 +0000447 # We don't want to process 'ppc' because of Clang's lack of support
448 # for Altivec (also some #defines won't likely be defined correctly, etc.)
449 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
450 $HadArch = 1;
Ted Kremenek27783eb2008-09-25 20:17:57 +0000451 ++$i;
452 next;
453 }
454
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000455 # Options with possible arguments that should pass through to compiler.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000456 if (defined $CompileOptionMap{$ArgKey}) {
457 my $Cnt = $CompileOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000458 push @CompileOpts,$Arg;
459 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
460 next;
461 }
462
463 # Options with possible arguments that should pass through to linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000464 if (defined $LinkerOptionMap{$ArgKey}) {
465 my $Cnt = $LinkerOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000466 push @LinkOpts,$Arg;
467 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
468 next;
469 }
470
471 # Options with possible arguments that should pass through to both compiler
472 # and the linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000473 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
474 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000475
Ted Kremeneka30730e2008-09-29 16:15:20 +0000476 # Check if this is an option that should have a unique value, and if so
477 # determine if the value was checked before.
478 if ($UniqueOptions{$Arg}) {
479 if (defined $Uniqued{$Arg}) {
480 $i += $Cnt;
481 next;
482 }
483 $Uniqued{$Arg} = 1;
484 }
485
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000486 push @CompileOpts,$Arg;
487 push @LinkOpts,$Arg;
488
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000489 while ($Cnt > 0) {
490 ++$i; --$Cnt;
491 push @CompileOpts, $ARGV[$i];
492 push @LinkOpts, $ARGV[$i];
493 }
494 next;
495 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000496
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000497 # Ignored options.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000498 if (defined $IgnoredOptionMap{$ArgKey}) {
499 my $Cnt = $IgnoredOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000500 while ($Cnt > 0) {
501 ++$i; --$Cnt;
502 }
503 next;
504 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000505
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000506 # Compile mode flags.
507 if ($Arg =~ /^-[D,I,U](.*)$/) {
508 my $Tmp = $Arg;
509 if ($1 eq '') {
510 # FIXME: Check if we are going off the end.
511 ++$i;
512 $Tmp = $Arg . $ARGV[$i];
513 }
514 push @CompileOpts,$Tmp;
515 next;
516 }
517
518 # Language.
519 if ($Arg eq '-x') {
520 $Lang = $ARGV[$i+1];
521 ++$i; next;
522 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000523
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000524 # Output file.
525 if ($Arg eq '-o') {
526 ++$i;
527 $Output = $ARGV[$i];
528 next;
529 }
530
531 # Get the link mode.
532 if ($Arg =~ /^-[l,L,O]/) {
533 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
534 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
535 else { push @LinkOpts,$Arg; }
536 next;
537 }
538
539 if ($Arg =~ /^-std=/) {
540 push @CompileOpts,$Arg;
541 next;
542 }
543
544# if ($Arg =~ /^-f/) {
545# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
546# push @CompileOpts,$Arg;
547# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
548# }
549
550 # Get the compiler/link mode.
551 if ($Arg =~ /^-F(.+)$/) {
552 my $Tmp = $Arg;
553 if ($1 eq '') {
554 # FIXME: Check if we are going off the end.
555 ++$i;
556 $Tmp = $Arg . $ARGV[$i];
557 }
558 push @CompileOpts,$Tmp;
559 push @LinkOpts,$Tmp;
560 next;
561 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000562
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000563 # Input files.
564 if ($Arg eq '-filelist') {
565 # FIXME: Make sure we aren't walking off the end.
566 open(IN, $ARGV[$i+1]);
567 while (<IN>) { s/\015?\012//; push @Files,$_; }
568 close(IN);
Ted Kremenek08959372009-08-14 18:20:50 +0000569 ++$i;
570 next;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000571 }
572
Ted Kremenek08959372009-08-14 18:20:50 +0000573 # Handle -Wno-. We don't care about extra warnings, but
574 # we should suppress ones that we don't want to see.
575 if ($Arg =~ /^-Wno-/) {
576 push @CompileOpts, $Arg;
577 next;
578 }
579
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000580 if (!($Arg =~ /^-/)) {
Ted Kremenek08959372009-08-14 18:20:50 +0000581 push @Files, $Arg;
582 next;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000583 }
584}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000585
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000586if ($Action eq 'compile' or $Action eq 'link') {
Ted Kremenek84591322009-02-24 22:07:12 +0000587 my @Archs = keys %ArchsSeen;
588 # Skip the file if we don't support the architectures specified.
Ted Kremenek0e0eb8b2009-02-25 00:10:37 +0000589 exit 0 if ($HadArch && scalar(@Archs) == 0);
Ted Kremenek84591322009-02-24 22:07:12 +0000590
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000591 foreach my $file (@Files) {
592 # Determine the language for the file.
593 my $FileLang = $Lang;
594
595 if (!defined($FileLang)) {
596 # Infer the language from the extension.
597 if ($file =~ /[.]([^.]+)$/) {
598 $FileLang = $LangMap{$1};
599 }
600 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000601
Ted Kremenek1d81ed92010-02-12 00:10:34 +0000602 # FileLang still not defined? Skip the file.
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000603 next if (!defined $FileLang);
Ted Kremenek1d81ed92010-02-12 00:10:34 +0000604
605 # Language not accepted?
Ted Kremenekefaf1912009-05-11 21:08:34 +0000606 next if (!defined $LangsAccepted{$FileLang});
Ted Kremenek1d81ed92010-02-12 00:10:34 +0000607
Ted Kremenek9a9e0d42009-05-15 04:20:31 +0000608 my @CmdArgs;
609 my @AnalyzeArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000610
611 if ($FileLang ne 'unknown') {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000612 push @CmdArgs, '-x', $FileLang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000613 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000614
Zhongxing Xu07c37672008-10-27 14:26:32 +0000615 if (defined $StoreModel) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000616 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
Zhongxing Xu07c37672008-10-27 14:26:32 +0000617 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000618
619 if (defined $ConstraintsModel) {
620 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
621 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000622
Anna Zaks71f11d62011-08-31 23:53:24 +0000623 if (defined $Analyses) {
624 push @AnalyzeArgs, split '\s+', $Analyses;
625 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000626
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000627 if (defined $OutputFormat) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000628 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
Ted Kremenek7753b352009-07-27 22:10:34 +0000629 if ($OutputFormat =~ /plist/) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000630 # Change "Output" to be a file.
631 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
632 DIR => $HtmlDir);
633 $ResultFile = $f;
634 $CleanupFile = $f;
635 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000636 }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000637
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000638 push @CmdArgs, @CompileOpts;
639 push @CmdArgs, $file;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000640
Ted Kremenek27783eb2008-09-25 20:17:57 +0000641 if (scalar @Archs) {
642 foreach my $arch (@Archs) {
643 my @NewArgs;
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000644 push @NewArgs, '-arch', $arch;
Ted Kremenek9a9e0d42009-05-15 04:20:31 +0000645 push @NewArgs, @CmdArgs;
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000646 Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output,
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000647 $Verbose, $HtmlDir, $file);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000648 }
649 }
650 else {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000651 Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output,
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000652 $Verbose, $HtmlDir, $file);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000653 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000654 }
655}
Ted Kremenekb0982882008-03-25 22:35:32 +0000656
Ted Kremenek948e06b2008-08-27 22:30:34 +0000657exit($Status >> 8);
658