blob: c39e4179514e6338e743abfcf6e5513ddf3aac79 [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 {
Anna Zaks91342942011-09-09 18:43:53 +000058 if (defined $ResultFile && -z $ResultFile) {
59 `rm -f $ResultFile`;
60 }
61 if (defined $CleanupFile) {
Ted Kremenekddf32da2009-01-21 00:42:24 +000062 `rm -f $CleanupFile`;
63 }
64}
65
Ted Kremenek991c54b2008-08-08 20:46:42 +000066##----------------------------------------------------------------------------##
67# Process Clang Crashes.
68##----------------------------------------------------------------------------##
69
70sub GetPPExt {
71 my $Lang = shift;
Ted Kremenek79433b52009-12-16 18:32:41 +000072 if ($Lang =~ /objective-c\+\+/) { return ".mii" };
Ted Kremenek991c54b2008-08-08 20:46:42 +000073 if ($Lang =~ /objective-c/) { return ".mi"; }
Ted Kremenek51b939f2009-12-16 05:02:47 +000074 if ($Lang =~ /c\+\+/) { return ".ii"; }
Ted Kremenek991c54b2008-08-08 20:46:42 +000075 return ".i";
76}
77
Ted Kremeneka4d8cde2009-04-28 17:37:44 +000078# Set this to 1 if we want to include 'parser rejects' files.
79my $IncludeParserRejects = 0;
Ted Kremenek5daa3be2009-01-27 01:19:08 +000080my $ParserRejects = "Parser Rejects";
Ted Kremeneka4d8cde2009-04-28 17:37:44 +000081
Ted Kremenek938eef12009-02-17 23:31:05 +000082my $AttributeIgnored = "Attribute Ignored";
Ted Kremenek5daa3be2009-01-27 01:19:08 +000083
Ted Kremenek5d31f832008-08-18 18:38:29 +000084sub ProcessClangFailure {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +000085 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
Ted Kremenek938eef12009-02-17 23:31:05 +000086 my $Dir = "$HtmlDir/failures";
Ted Kremenek991c54b2008-08-08 20:46:42 +000087 mkpath $Dir;
Ted Kremenek5daa3be2009-01-27 01:19:08 +000088
89 my $prefix = "clang_crash";
Ted Kremenek938eef12009-02-17 23:31:05 +000090 if ($ErrorType eq $ParserRejects) {
91 $prefix = "clang_parser_rejects";
92 }
93 elsif ($ErrorType eq $AttributeIgnored) {
94 $prefix = "clang_attribute_ignored";
95 }
Ted Kremenekc3998fa2008-09-25 00:51:44 +000096
Ted Kremenek3386c8a2009-07-28 00:14:21 +000097 # Generate the preprocessed file with Clang.
Ted Kremenek5daa3be2009-01-27 01:19:08 +000098 my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX",
99 SUFFIX => GetPPExt($Lang),
100 DIR => $Dir);
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000101 system $Clang, @$Args, "-E", "-o", $PPFile;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000102 close ($PPH);
Ted Kremenekc3998fa2008-09-25 00:51:44 +0000103
104 # Create the info file.
Ted Kremenek82a12532008-09-25 00:25:16 +0000105 open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
Ted Kremenek5f2825f2008-09-21 18:04:49 +0000106 print OUT abs_path($file), "\n";
Ted Kremenek5d31f832008-08-18 18:38:29 +0000107 print OUT "$ErrorType\n";
Ted Kremenek2dd7ad12008-08-18 20:55:25 +0000108 print OUT "@$Args\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +0000109 close OUT;
Ted Kremenek82a12532008-09-25 00:25:16 +0000110 `uname -a >> $PPFile.info.txt 2>&1`;
Ted Kremenek51365b52009-12-15 02:35:54 +0000111 `$Compiler -v >> $PPFile.info.txt 2>&1`;
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +0000112 system 'mv',$ofile,"$PPFile.stderr.txt";
Ted Kremenek938eef12009-02-17 23:31:05 +0000113 return (basename $PPFile);
Ted Kremenek991c54b2008-08-08 20:46:42 +0000114}
Ted Kremenekb0982882008-03-25 22:35:32 +0000115
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000116##----------------------------------------------------------------------------##
117# Running the analyzer.
118##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +0000119
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000120sub GetCCArgs {
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000121 my $mode = shift;
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000122 my $Args = shift;
123
124 pipe (FROM_CHILD, TO_PARENT);
125 my $pid = fork();
126 if ($pid == 0) {
127 close FROM_CHILD;
128 open(STDOUT,">&", \*TO_PARENT);
129 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000130 exec $Clang, "-###", $mode, @$Args;
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000131 }
132 close(TO_PARENT);
133 my $line;
134 while (<FROM_CHILD>) {
Ted Kremenek32317b22009-12-11 23:12:52 +0000135 next if (!/-cc1/);
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000136 $line = $_;
137 }
138
139 waitpid($pid,0);
140 close(FROM_CHILD);
141
Ted Kremenek32317b22009-12-11 23:12:52 +0000142 die "could not find clang line\n" if (!defined $line);
Ted Kremenek1df35e32009-05-11 23:29:51 +0000143 # Strip the newline and initial whitspace
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000144 chomp $line;
Ted Kremenek1df35e32009-05-11 23:29:51 +0000145 $line =~ s/^\s+//;
Ted Kremenek2aefcb22010-04-06 19:41:24 +0000146 my @items = quotewords('\s+', 0, $line);
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000147 my $cmd = shift @items;
Ted Kremenek32317b22009-12-11 23:12:52 +0000148 die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/));
Ted Kremenekfd9df0e2009-05-09 19:19:28 +0000149 return \@items;
150}
151
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000152sub Analyze {
Ted Kremenek27871ea2011-03-10 21:10:08 +0000153 my ($Clang, $OriginalArgs, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000154 $file) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +0000155
Ted Kremenek27871ea2011-03-10 21:10:08 +0000156 my @Args = @$OriginalArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000157 my $Cmd;
158 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000159 my @CmdArgsSansAnalyses;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000160
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000161 if ($Lang =~ /header/) {
162 exit 0 if (!defined ($Output));
163 $Cmd = 'cp';
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000164 push @CmdArgs, $file;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000165 # Remove the PCH extension.
166 $Output =~ s/[.]gch$//;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000167 push @CmdArgs, $Output;
168 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000169 }
170 else {
Ted Kremenek2a3a8b92009-12-11 22:44:53 +0000171 $Cmd = $Clang;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000172 if ($Lang eq "objective-c" || $Lang eq "objective-c++") {
Ted Kremenek27871ea2011-03-10 21:10:08 +0000173 push @Args,'-DIBOutlet=__attribute__((iboutlet))';
174 push @Args,'-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection)))';
175 push @Args,'-DIBAction=void)__attribute__((ibaction)';
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000176 }
177
178 # Create arguments for doing regular parsing.
Ted Kremenek27871ea2011-03-10 21:10:08 +0000179 my $SyntaxArgs = GetCCArgs("-fsyntax-only", \@Args);
180 @CmdArgsSansAnalyses = @$SyntaxArgs;
181
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000182 # Create arguments for doing static analysis.
183 if (defined $ResultFile) {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000184 push @Args, '-o', $ResultFile;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000185 }
186 elsif (defined $HtmlDir) {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000187 push @Args, '-o', $HtmlDir;
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000188 }
Ted Kremenekb26fbc12011-03-21 20:12:21 +0000189 if ($Verbose) {
190 push @Args, "-Xclang", "-analyzer-display-progress";
191 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000192
193 foreach my $arg (@$AnalyzeArgs) {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000194 push @Args, "-Xclang", $arg;
Ted Kremenek8382cf52009-11-13 18:46:29 +0000195 }
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000196
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000197 # Display Ubiviz graph?
198 if (defined $ENV{'CCC_UBI'}) {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000199 push @Args, "-Xclang", "-analyzer-viz-egraph-ubigraph";
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000200 }
201
Ted Kremenek27871ea2011-03-10 21:10:08 +0000202 my $AnalysisArgs = GetCCArgs("--analyze", \@Args);
203 @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 Kremenek27871ea2011-03-10 21:10:08 +0000220 print STDERR join(' ', @PrintArgs);
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000221 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++',
Anna Zaks71f11d62011-08-31 23:53:24 +0000371 'ii' => 'c++',
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000372 'i' => 'c-cpp-output',
373 'm' => 'objective-c',
Anna Zaks71f11d62011-08-31 23:53:24 +0000374 'mi' => 'objective-c-cpp-output',
375 'mm' => 'objective-c++'
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000376);
377
Ted Kremeneka30730e2008-09-29 16:15:20 +0000378my %UniqueOptions = (
379 '-isysroot' => 0
380);
381
Ted Kremenekf6f8d5a2010-03-08 19:06:44 +0000382##----------------------------------------------------------------------------##
383# Languages accepted.
384##----------------------------------------------------------------------------##
385
Ted Kremenekefaf1912009-05-11 21:08:34 +0000386my %LangsAccepted = (
387 "objective-c" => 1,
Ted Kremenek0a208182011-08-11 22:47:20 +0000388 "c" => 1,
389 "c++" => 1,
390 "objective-c++" => 1
Ted Kremenekefaf1912009-05-11 21:08:34 +0000391);
392
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000393##----------------------------------------------------------------------------##
394# Main Logic.
395##----------------------------------------------------------------------------##
396
397my $Action = 'link';
398my @CompileOpts;
399my @LinkOpts;
400my @Files;
401my $Lang;
402my $Output;
Ted Kremeneka30730e2008-09-29 16:15:20 +0000403my %Uniqued;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000404
405# Forward arguments to gcc.
Ted Kremenek51365b52009-12-15 02:35:54 +0000406my $Status = system($Compiler,@ARGV);
Tom Carea716f632010-09-29 23:48:31 +0000407if (defined $ENV{'CCC_ANALYZER_LOG'}) {
408 print "$Compiler @ARGV\n";
409}
Ted Kremenekcb344d02008-08-28 01:18:44 +0000410if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000411
412# Get the analysis options.
413my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000414
Zhongxing Xu07c37672008-10-27 14:26:32 +0000415# Get the store model.
416my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000417
418# Get the constraints engine.
419my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
Zhongxing Xu07c37672008-10-27 14:26:32 +0000420
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000421# Get the output format.
422my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
Ted Kremenek3891a152009-02-17 05:01:10 +0000423if (!defined $OutputFormat) { $OutputFormat = "html"; }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000424
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000425# Determine the level of verbosity.
426my $Verbose = 0;
427if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
428if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
429
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000430# Get the HTML output directory.
431my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
432
Ted Kremenek84591322009-02-24 22:07:12 +0000433my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
Ted Kremenek27783eb2008-09-25 20:17:57 +0000434my %ArchsSeen;
Ted Kremenek84591322009-02-24 22:07:12 +0000435my $HadArch = 0;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000436
437# Process the arguments.
438foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000439 my $Arg = $ARGV[$i];
440 my ($ArgKey) = split /=/,$Arg,2;
441
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000442 # Modes ccc-analyzer supports
Ted Kremenekd732a7b2009-08-04 00:57:12 +0000443 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000444 elsif ($Arg eq '-c') { $Action = 'compile'; }
445 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek27783eb2008-09-25 20:17:57 +0000446
447 # Specially handle duplicate cases of -arch
448 if ($Arg eq "-arch") {
449 my $arch = $ARGV[$i+1];
Ted Kremenek84591322009-02-24 22:07:12 +0000450 # We don't want to process 'ppc' because of Clang's lack of support
451 # for Altivec (also some #defines won't likely be defined correctly, etc.)
452 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
453 $HadArch = 1;
Ted Kremenek27783eb2008-09-25 20:17:57 +0000454 ++$i;
455 next;
456 }
457
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000458 # Options with possible arguments that should pass through to compiler.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000459 if (defined $CompileOptionMap{$ArgKey}) {
460 my $Cnt = $CompileOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000461 push @CompileOpts,$Arg;
462 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
463 next;
464 }
465
466 # Options with possible arguments that should pass through to linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000467 if (defined $LinkerOptionMap{$ArgKey}) {
468 my $Cnt = $LinkerOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000469 push @LinkOpts,$Arg;
470 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
471 next;
472 }
473
474 # Options with possible arguments that should pass through to both compiler
475 # and the linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000476 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
477 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000478
Ted Kremeneka30730e2008-09-29 16:15:20 +0000479 # Check if this is an option that should have a unique value, and if so
480 # determine if the value was checked before.
481 if ($UniqueOptions{$Arg}) {
482 if (defined $Uniqued{$Arg}) {
483 $i += $Cnt;
484 next;
485 }
486 $Uniqued{$Arg} = 1;
487 }
488
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000489 push @CompileOpts,$Arg;
490 push @LinkOpts,$Arg;
491
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000492 while ($Cnt > 0) {
493 ++$i; --$Cnt;
494 push @CompileOpts, $ARGV[$i];
495 push @LinkOpts, $ARGV[$i];
496 }
497 next;
498 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000499
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000500 # Ignored options.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000501 if (defined $IgnoredOptionMap{$ArgKey}) {
502 my $Cnt = $IgnoredOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000503 while ($Cnt > 0) {
504 ++$i; --$Cnt;
505 }
506 next;
507 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000508
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000509 # Compile mode flags.
510 if ($Arg =~ /^-[D,I,U](.*)$/) {
511 my $Tmp = $Arg;
512 if ($1 eq '') {
513 # FIXME: Check if we are going off the end.
514 ++$i;
515 $Tmp = $Arg . $ARGV[$i];
516 }
517 push @CompileOpts,$Tmp;
518 next;
519 }
520
521 # Language.
522 if ($Arg eq '-x') {
523 $Lang = $ARGV[$i+1];
524 ++$i; next;
525 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000526
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000527 # Output file.
528 if ($Arg eq '-o') {
529 ++$i;
530 $Output = $ARGV[$i];
531 next;
532 }
533
534 # Get the link mode.
535 if ($Arg =~ /^-[l,L,O]/) {
536 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
537 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
538 else { push @LinkOpts,$Arg; }
539 next;
540 }
541
542 if ($Arg =~ /^-std=/) {
543 push @CompileOpts,$Arg;
544 next;
545 }
546
547# if ($Arg =~ /^-f/) {
548# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
549# push @CompileOpts,$Arg;
550# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
551# }
552
553 # Get the compiler/link mode.
554 if ($Arg =~ /^-F(.+)$/) {
555 my $Tmp = $Arg;
556 if ($1 eq '') {
557 # FIXME: Check if we are going off the end.
558 ++$i;
559 $Tmp = $Arg . $ARGV[$i];
560 }
561 push @CompileOpts,$Tmp;
562 push @LinkOpts,$Tmp;
563 next;
564 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000565
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000566 # Input files.
567 if ($Arg eq '-filelist') {
568 # FIXME: Make sure we aren't walking off the end.
569 open(IN, $ARGV[$i+1]);
570 while (<IN>) { s/\015?\012//; push @Files,$_; }
571 close(IN);
Ted Kremenek08959372009-08-14 18:20:50 +0000572 ++$i;
573 next;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000574 }
575
Ted Kremenek08959372009-08-14 18:20:50 +0000576 # Handle -Wno-. We don't care about extra warnings, but
577 # we should suppress ones that we don't want to see.
578 if ($Arg =~ /^-Wno-/) {
579 push @CompileOpts, $Arg;
580 next;
581 }
582
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000583 if (!($Arg =~ /^-/)) {
Ted Kremenek08959372009-08-14 18:20:50 +0000584 push @Files, $Arg;
585 next;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000586 }
587}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000588
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000589if ($Action eq 'compile' or $Action eq 'link') {
Ted Kremenek84591322009-02-24 22:07:12 +0000590 my @Archs = keys %ArchsSeen;
591 # Skip the file if we don't support the architectures specified.
Ted Kremenek0e0eb8b2009-02-25 00:10:37 +0000592 exit 0 if ($HadArch && scalar(@Archs) == 0);
Ted Kremenek84591322009-02-24 22:07:12 +0000593
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000594 foreach my $file (@Files) {
595 # Determine the language for the file.
596 my $FileLang = $Lang;
597
598 if (!defined($FileLang)) {
599 # Infer the language from the extension.
600 if ($file =~ /[.]([^.]+)$/) {
601 $FileLang = $LangMap{$1};
602 }
603 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000604
Ted Kremenek1d81ed92010-02-12 00:10:34 +0000605 # FileLang still not defined? Skip the file.
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000606 next if (!defined $FileLang);
Ted Kremenek1d81ed92010-02-12 00:10:34 +0000607
608 # Language not accepted?
Ted Kremenekefaf1912009-05-11 21:08:34 +0000609 next if (!defined $LangsAccepted{$FileLang});
Ted Kremenek1d81ed92010-02-12 00:10:34 +0000610
Ted Kremenek9a9e0d42009-05-15 04:20:31 +0000611 my @CmdArgs;
612 my @AnalyzeArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000613
614 if ($FileLang ne 'unknown') {
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000615 push @CmdArgs, '-x', $FileLang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000616 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000617
Zhongxing Xu07c37672008-10-27 14:26:32 +0000618 if (defined $StoreModel) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000619 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
Zhongxing Xu07c37672008-10-27 14:26:32 +0000620 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000621
622 if (defined $ConstraintsModel) {
623 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
624 }
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000625
Anna Zaks71f11d62011-08-31 23:53:24 +0000626 if (defined $Analyses) {
627 push @AnalyzeArgs, split '\s+', $Analyses;
628 }
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000629
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000630 if (defined $OutputFormat) {
Ted Kremenekbe1fe1e2009-02-17 04:27:41 +0000631 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
Ted Kremenek7753b352009-07-27 22:10:34 +0000632 if ($OutputFormat =~ /plist/) {
Ted Kremenekddf32da2009-01-21 00:42:24 +0000633 # Change "Output" to be a file.
634 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
635 DIR => $HtmlDir);
636 $ResultFile = $f;
Anna Zaks91342942011-09-09 18:43:53 +0000637 # If the HtmlDir is not set, we sould clean up the plist files.
638 if (!defined $HtmlDir || -z $HtmlDir) {
639 $CleanupFile = $f;
640 }
Ted Kremenekddf32da2009-01-21 00:42:24 +0000641 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000642 }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000643
Ted Kremenek7fe679f2011-02-17 02:28:30 +0000644 push @CmdArgs, @CompileOpts;
645 push @CmdArgs, $file;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000646
Ted Kremenek27783eb2008-09-25 20:17:57 +0000647 if (scalar @Archs) {
648 foreach my $arch (@Archs) {
649 my @NewArgs;
Ted Kremenek5e48bcf2011-03-16 21:10:42 +0000650 push @NewArgs, '-arch', $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