blob: 84d25ab6348a1a298d59eaedce9532aa39d96440 [file] [log] [blame]
Ted Kremenekf7ffd662008-07-19 06:11:04 +00001#!/usr/bin/env perl
Ted Kremenek5efdf842008-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 Kremenekf7ffd662008-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 Kremenek5efdf842008-03-25 22:35:32 +000012#
13##===----------------------------------------------------------------------===##
14
Ted Kremenekf7ffd662008-07-19 06:11:04 +000015use strict;
16use warnings;
Ted Kremenekf65a0c62009-12-15 02:35:54 +000017use FindBin;
Ted Kremenek23432d42008-09-21 19:56:14 +000018use Cwd qw/ getcwd abs_path /;
Ted Kremenek994c8e32008-08-08 20:46:42 +000019use File::Temp qw/ tempfile /;
20use File::Path qw / mkpath /;
Ted Kremenek13747162009-01-21 00:42:24 +000021use File::Basename;
Ted Kremenek25421bb2009-05-11 23:29:51 +000022use Text::ParseWords;
Ted Kremenek6aa1ad02008-08-25 20:44:31 +000023
Ted Kremenekf65a0c62009-12-15 02:35:54 +000024##===----------------------------------------------------------------------===##
25# Compiler command setup.
26##===----------------------------------------------------------------------===##
27
28my $Compiler;
29my $Clang;
Anna Zaks1d391522012-01-06 01:54:05 +000030my $DefaultCCompiler;
31my $DefaultCXXCompiler;
32
33if (`uname -a` =~ m/Darwin/) {
34 $DefaultCCompiler = 'clang';
35 $DefaultCXXCompiler = 'clang++';
36} else {
37 $DefaultCCompiler = 'gcc';
38 $DefaultCXXCompiler = 'g++';
39}
Ted Kremenekf65a0c62009-12-15 02:35:54 +000040
41if ($FindBin::Script =~ /c\+\+-analyzer/) {
42 $Compiler = $ENV{'CCC_CXX'};
Anna Zaks1d391522012-01-06 01:54:05 +000043 if (!defined $Compiler) { $Compiler = $DefaultCXXCompiler; }
Ted Kremenekf65a0c62009-12-15 02:35:54 +000044
45 $Clang = $ENV{'CLANG_CXX'};
46 if (!defined $Clang) { $Clang = 'clang++'; }
47}
48else {
49 $Compiler = $ENV{'CCC_CC'};
Anna Zaks1d391522012-01-06 01:54:05 +000050 if (!defined $Compiler) { $Compiler = $DefaultCCompiler; }
Ted Kremenekf65a0c62009-12-15 02:35:54 +000051
52 $Clang = $ENV{'CLANG'};
53 if (!defined $Clang) { $Clang = 'clang'; }
54}
55
56##===----------------------------------------------------------------------===##
57# Cleanup.
58##===----------------------------------------------------------------------===##
Ted Kremenekbfe393f2009-07-30 23:55:19 +000059
60my $ReportFailures = $ENV{'CCC_REPORT_FAILURES'};
61if (!defined $ReportFailures) { $ReportFailures = 1; }
62
Ted Kremenek13747162009-01-21 00:42:24 +000063my $CleanupFile;
64my $ResultFile;
65
66# Remove any stale files at exit.
67END {
Anna Zaks45ce1bf2011-09-09 18:43:53 +000068 if (defined $ResultFile && -z $ResultFile) {
69 `rm -f $ResultFile`;
70 }
71 if (defined $CleanupFile) {
Ted Kremenek13747162009-01-21 00:42:24 +000072 `rm -f $CleanupFile`;
73 }
74}
75
Ted Kremenek994c8e32008-08-08 20:46:42 +000076##----------------------------------------------------------------------------##
77# Process Clang Crashes.
78##----------------------------------------------------------------------------##
79
80sub GetPPExt {
81 my $Lang = shift;
Ted Kremenek22a8a4b2009-12-16 18:32:41 +000082 if ($Lang =~ /objective-c\+\+/) { return ".mii" };
Ted Kremenek994c8e32008-08-08 20:46:42 +000083 if ($Lang =~ /objective-c/) { return ".mi"; }
Ted Kremenekdca68162009-12-16 05:02:47 +000084 if ($Lang =~ /c\+\+/) { return ".ii"; }
Ted Kremenek994c8e32008-08-08 20:46:42 +000085 return ".i";
86}
87
Ted Kremenekce6b8652009-04-28 17:37:44 +000088# Set this to 1 if we want to include 'parser rejects' files.
89my $IncludeParserRejects = 0;
Ted Kremenek725fb432009-01-27 01:19:08 +000090my $ParserRejects = "Parser Rejects";
Ted Kremenek13ed6f12009-02-17 23:31:05 +000091my $AttributeIgnored = "Attribute Ignored";
Anna Zaks9fed0842011-11-07 22:38:10 +000092my $OtherError = "Other Error";
Ted Kremenek725fb432009-01-27 01:19:08 +000093
Ted Kremenek5abf5462008-08-18 18:38:29 +000094sub ProcessClangFailure {
Ted Kremenek5c512e62009-12-11 22:44:53 +000095 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
Ted Kremenek13ed6f12009-02-17 23:31:05 +000096 my $Dir = "$HtmlDir/failures";
Ted Kremenek994c8e32008-08-08 20:46:42 +000097 mkpath $Dir;
Ted Kremenek725fb432009-01-27 01:19:08 +000098
99 my $prefix = "clang_crash";
Ted Kremenek13ed6f12009-02-17 23:31:05 +0000100 if ($ErrorType eq $ParserRejects) {
101 $prefix = "clang_parser_rejects";
102 }
103 elsif ($ErrorType eq $AttributeIgnored) {
104 $prefix = "clang_attribute_ignored";
105 }
Anna Zaks9fed0842011-11-07 22:38:10 +0000106 elsif ($ErrorType eq $OtherError) {
107 $prefix = "clang_other_error";
108 }
Ted Kremenek72e4b0b2008-09-25 00:51:44 +0000109
Ted Kremenek0799d4f2009-07-28 00:14:21 +0000110 # Generate the preprocessed file with Clang.
Ted Kremenek725fb432009-01-27 01:19:08 +0000111 my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX",
112 SUFFIX => GetPPExt($Lang),
113 DIR => $Dir);
Ted Kremenek5c512e62009-12-11 22:44:53 +0000114 system $Clang, @$Args, "-E", "-o", $PPFile;
Ted Kremenek994c8e32008-08-08 20:46:42 +0000115 close ($PPH);
Ted Kremenek72e4b0b2008-09-25 00:51:44 +0000116
117 # Create the info file.
Ted Kremenek1ad3b3d2008-09-25 00:25:16 +0000118 open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
Ted Kremenek32c11812008-09-21 18:04:49 +0000119 print OUT abs_path($file), "\n";
Ted Kremenek5abf5462008-08-18 18:38:29 +0000120 print OUT "$ErrorType\n";
Ted Kremenekb3c98d32008-08-18 20:55:25 +0000121 print OUT "@$Args\n";
Ted Kremenek994c8e32008-08-08 20:46:42 +0000122 close OUT;
Ted Kremenek1ad3b3d2008-09-25 00:25:16 +0000123 `uname -a >> $PPFile.info.txt 2>&1`;
Ted Kremenekf65a0c62009-12-15 02:35:54 +0000124 `$Compiler -v >> $PPFile.info.txt 2>&1`;
Ted Kremenek907921f2008-09-12 22:49:36 +0000125 system 'mv',$ofile,"$PPFile.stderr.txt";
Ted Kremenek13ed6f12009-02-17 23:31:05 +0000126 return (basename $PPFile);
Ted Kremenek994c8e32008-08-08 20:46:42 +0000127}
Ted Kremenek5efdf842008-03-25 22:35:32 +0000128
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000129##----------------------------------------------------------------------------##
130# Running the analyzer.
131##----------------------------------------------------------------------------##
Ted Kremenek5efdf842008-03-25 22:35:32 +0000132
Ted Kremenek1f991f02009-05-09 19:19:28 +0000133sub GetCCArgs {
Ted Kremenek42ec9142011-02-17 02:28:30 +0000134 my $mode = shift;
Ted Kremenek1f991f02009-05-09 19:19:28 +0000135 my $Args = shift;
136
137 pipe (FROM_CHILD, TO_PARENT);
138 my $pid = fork();
139 if ($pid == 0) {
140 close FROM_CHILD;
141 open(STDOUT,">&", \*TO_PARENT);
142 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek42ec9142011-02-17 02:28:30 +0000143 exec $Clang, "-###", $mode, @$Args;
Ted Kremenek1f991f02009-05-09 19:19:28 +0000144 }
145 close(TO_PARENT);
146 my $line;
147 while (<FROM_CHILD>) {
Ted Kremenekf92b4462009-12-11 23:12:52 +0000148 next if (!/-cc1/);
Ted Kremenek1f991f02009-05-09 19:19:28 +0000149 $line = $_;
150 }
151
152 waitpid($pid,0);
153 close(FROM_CHILD);
154
Ted Kremenekf92b4462009-12-11 23:12:52 +0000155 die "could not find clang line\n" if (!defined $line);
Ted Kremenek25421bb2009-05-11 23:29:51 +0000156 # Strip the newline and initial whitspace
Ted Kremenek42ec9142011-02-17 02:28:30 +0000157 chomp $line;
Ted Kremenek25421bb2009-05-11 23:29:51 +0000158 $line =~ s/^\s+//;
Ted Kremenek7deb7052010-04-06 19:41:24 +0000159 my @items = quotewords('\s+', 0, $line);
Ted Kremenek1f991f02009-05-09 19:19:28 +0000160 my $cmd = shift @items;
Ted Kremenekf92b4462009-12-11 23:12:52 +0000161 die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/));
Ted Kremenek1f991f02009-05-09 19:19:28 +0000162 return \@items;
163}
164
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000165sub Analyze {
Ted Kremenek339f7c32011-03-10 21:10:08 +0000166 my ($Clang, $OriginalArgs, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir,
Ted Kremenek42ec9142011-02-17 02:28:30 +0000167 $file) = @_;
Seo Sanghyeonb7bf0f32008-04-04 11:02:21 +0000168
Ted Kremenek339f7c32011-03-10 21:10:08 +0000169 my @Args = @$OriginalArgs;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000170 my $Cmd;
171 my @CmdArgs;
Ted Kremenek994c8e32008-08-08 20:46:42 +0000172 my @CmdArgsSansAnalyses;
Ted Kremenek42ec9142011-02-17 02:28:30 +0000173
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000174 if ($Lang =~ /header/) {
175 exit 0 if (!defined ($Output));
176 $Cmd = 'cp';
Ted Kremenek42ec9142011-02-17 02:28:30 +0000177 push @CmdArgs, $file;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000178 # Remove the PCH extension.
179 $Output =~ s/[.]gch$//;
Ted Kremenek42ec9142011-02-17 02:28:30 +0000180 push @CmdArgs, $Output;
181 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000182 }
183 else {
Ted Kremenek5c512e62009-12-11 22:44:53 +0000184 $Cmd = $Clang;
Ted Kremenek42ec9142011-02-17 02:28:30 +0000185 if ($Lang eq "objective-c" || $Lang eq "objective-c++") {
Ted Kremenek339f7c32011-03-10 21:10:08 +0000186 push @Args,'-DIBOutlet=__attribute__((iboutlet))';
187 push @Args,'-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection)))';
188 push @Args,'-DIBAction=void)__attribute__((ibaction)';
Ted Kremenek42ec9142011-02-17 02:28:30 +0000189 }
190
191 # Create arguments for doing regular parsing.
Ted Kremenek339f7c32011-03-10 21:10:08 +0000192 my $SyntaxArgs = GetCCArgs("-fsyntax-only", \@Args);
193 @CmdArgsSansAnalyses = @$SyntaxArgs;
194
Ted Kremenek42ec9142011-02-17 02:28:30 +0000195 # Create arguments for doing static analysis.
196 if (defined $ResultFile) {
Ted Kremenekd4bcb4f2011-03-16 21:10:42 +0000197 push @Args, '-o', $ResultFile;
Ted Kremenek42ec9142011-02-17 02:28:30 +0000198 }
199 elsif (defined $HtmlDir) {
Ted Kremenekd4bcb4f2011-03-16 21:10:42 +0000200 push @Args, '-o', $HtmlDir;
Ted Kremenek42ec9142011-02-17 02:28:30 +0000201 }
Ted Kremenek9acb3462011-03-21 20:12:21 +0000202 if ($Verbose) {
203 push @Args, "-Xclang", "-analyzer-display-progress";
204 }
Ted Kremenek42ec9142011-02-17 02:28:30 +0000205
206 foreach my $arg (@$AnalyzeArgs) {
Ted Kremenekd4bcb4f2011-03-16 21:10:42 +0000207 push @Args, "-Xclang", $arg;
Ted Kremenek4ef13f82009-11-13 18:46:29 +0000208 }
Ted Kremenekd4bcb4f2011-03-16 21:10:42 +0000209
Ted Kremenek42ec9142011-02-17 02:28:30 +0000210 # Display Ubiviz graph?
211 if (defined $ENV{'CCC_UBI'}) {
Ted Kremenekd4bcb4f2011-03-16 21:10:42 +0000212 push @Args, "-Xclang", "-analyzer-viz-egraph-ubigraph";
Ted Kremenek42ec9142011-02-17 02:28:30 +0000213 }
214
Ted Kremenek339f7c32011-03-10 21:10:08 +0000215 my $AnalysisArgs = GetCCArgs("--analyze", \@Args);
216 @CmdArgs = @$AnalysisArgs;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000217 }
Ted Kremenek42ec9142011-02-17 02:28:30 +0000218
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000219 my @PrintArgs;
220 my $dir;
Ted Kremenek7ac29bb2009-08-02 05:42:46 +0000221
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000222 if ($Verbose) {
223 $dir = getcwd();
224 print STDERR "\n[LOCATION]: $dir\n";
225 push @PrintArgs,"'$Cmd'";
Ted Kremenek42ec9142011-02-17 02:28:30 +0000226 foreach my $arg (@CmdArgs) {
227 push @PrintArgs,"\'$arg\'";
228 }
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000229 }
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000230 if ($Verbose == 1) {
Ted Kremenekf18f4602008-05-24 15:58:54 +0000231 # We MUST print to stderr. Some clients use the stdout output of
232 # gcc for various purposes.
Ted Kremenek339f7c32011-03-10 21:10:08 +0000233 print STDERR join(' ', @PrintArgs);
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000234 print STDERR "\n";
235 }
236 elsif ($Verbose == 2) {
237 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
238 }
Ted Kremenek42ec9142011-02-17 02:28:30 +0000239
Ted Kremenek370de842008-09-04 00:02:34 +0000240 # Capture the STDERR of clang and send it to a temporary file.
241 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
242 # We save the output file in the 'crashes' directory if clang encounters
243 # any problems with the file.
Ted Kremenek4ab81cb2008-09-11 23:05:26 +0000244 pipe (FROM_CHILD, TO_PARENT);
Ted Kremenek370de842008-09-04 00:02:34 +0000245 my $pid = fork();
246 if ($pid == 0) {
Ted Kremenek4ab81cb2008-09-11 23:05:26 +0000247 close FROM_CHILD;
248 open(STDOUT,">&", \*TO_PARENT);
249 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek370de842008-09-04 00:02:34 +0000250 exec $Cmd, @CmdArgs;
251 }
Ted Kremenek42ec9142011-02-17 02:28:30 +0000252
Ted Kremenek4ab81cb2008-09-11 23:05:26 +0000253 close TO_PARENT;
254 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
255
256 while (<FROM_CHILD>) {
257 print $ofh $_;
Ted Kremenek42ec9142011-02-17 02:28:30 +0000258 print STDERR $_;
Ted Kremenek4ab81cb2008-09-11 23:05:26 +0000259 }
260
261 waitpid($pid,0);
Ted Kremenek1f991f02009-05-09 19:19:28 +0000262 close(FROM_CHILD);
Ted Kremenek370de842008-09-04 00:02:34 +0000263 my $Result = $?;
264
265 # Did the command die because of a signal?
Ted Kremenekbfe393f2009-07-30 23:55:19 +0000266 if ($ReportFailures) {
Ted Kremenek5c512e62009-12-11 22:44:53 +0000267 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
268 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
Ted Kremenekbfe393f2009-07-30 23:55:19 +0000269 $HtmlDir, "Crash", $ofile);
Ted Kremenek078b8872009-02-27 06:17:38 +0000270 }
Ted Kremenekbfe393f2009-07-30 23:55:19 +0000271 elsif ($Result) {
272 if ($IncludeParserRejects && !($file =~/conftest/)) {
Ted Kremenek5c512e62009-12-11 22:44:53 +0000273 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
Ted Kremenekbfe393f2009-07-30 23:55:19 +0000274 $HtmlDir, $ParserRejects, $ofile);
Anna Zaks9fed0842011-11-07 22:38:10 +0000275 } else {
276 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses,
277 $HtmlDir, $OtherError, $ofile);
Ted Kremenek13ed6f12009-02-17 23:31:05 +0000278 }
Ted Kremenekbfe393f2009-07-30 23:55:19 +0000279 }
280 else {
281 # Check if there were any unhandled attributes.
282 if (open(CHILD, $ofile)) {
283 my %attributes_not_handled;
Ted Kremenek42ec9142011-02-17 02:28:30 +0000284
Ted Kremenekbfe393f2009-07-30 23:55:19 +0000285 # Don't flag warnings about the following attributes that we
286 # know are currently not supported by Clang.
287 $attributes_not_handled{"cdecl"} = 1;
Ted Kremenek42ec9142011-02-17 02:28:30 +0000288
Ted Kremenekbfe393f2009-07-30 23:55:19 +0000289 my $ppfile;
290 while (<CHILD>) {
291 next if (! /warning: '([^\']+)' attribute ignored/);
292
293 # Have we already spotted this unhandled attribute?
294 next if (defined $attributes_not_handled{$1});
295 $attributes_not_handled{$1} = 1;
296
297 # Get the name of the attribute file.
298 my $dir = "$HtmlDir/failures";
299 my $afile = "$dir/attribute_ignored_$1.txt";
300
301 # Only create another preprocessed file if the attribute file
302 # doesn't exist yet.
303 next if (-e $afile);
304
305 # Add this file to the list of files that contained this attribute.
306 # Generate a preprocessed file if we haven't already.
307 if (!(defined $ppfile)) {
Ted Kremenek5c512e62009-12-11 22:44:53 +0000308 $ppfile = ProcessClangFailure($Clang, $Lang, $file,
Ted Kremenekbfe393f2009-07-30 23:55:19 +0000309 \@CmdArgsSansAnalyses,
310 $HtmlDir, $AttributeIgnored, $ofile);
311 }
312
313 mkpath $dir;
314 open(AFILE, ">$afile");
315 print AFILE "$ppfile\n";
316 close(AFILE);
317 }
318 close CHILD;
319 }
Ted Kremenek13ed6f12009-02-17 23:31:05 +0000320 }
321 }
Ted Kremenek370de842008-09-04 00:02:34 +0000322
Ted Kremenek90fc8a42009-08-04 00:55:59 +0000323 unlink($ofile);
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000324}
Ted Kremenekf18f4602008-05-24 15:58:54 +0000325
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000326##----------------------------------------------------------------------------##
327# Lookup tables.
328##----------------------------------------------------------------------------##
329
330my %CompileOptionMap = (
331 '-nostdinc' => 0,
Anders Carlsson09113472008-12-19 20:56:23 +0000332 '-fblocks' => 0,
Shantonu Sendf44f742010-07-03 03:08:23 +0000333 '-fno-builtin' => 0,
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000334 '-fobjc-gc-only' => 0,
Ted Kremenek1b44ba42009-02-26 23:09:43 +0000335 '-fobjc-gc' => 0,
336 '-ffreestanding' => 0,
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000337 '-include' => 1,
338 '-idirafter' => 1,
Ted Kremeneke869a182010-06-08 18:27:55 +0000339 '-imacros' => 1,
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000340 '-iprefix' => 1,
341 '-iquote' => 1,
342 '-isystem' => 1,
343 '-iwithprefix' => 1,
344 '-iwithprefixbefore' => 1
345);
346
347my %LinkerOptionMap = (
Ted Kremenek415287d2012-03-06 20:06:12 +0000348 '-framework' => 1,
349 '-fobjc-link-runtime' => 0
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000350);
351
352my %CompilerLinkerOptionMap = (
Ted Kremenek49db0522012-01-26 23:30:13 +0000353 '-fobjc-arc' => 0,
354 '-fobjc-abi-version' => 0, # This is really a 1 argument, but always has '='
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000355 '-isysroot' => 1,
356 '-arch' => 1,
Charles Davisdde71b92010-03-02 15:26:41 +0000357 '-m32' => 0,
358 '-m64' => 0,
Ted Kremenek6b2e07a2008-09-29 22:45:28 +0000359 '-v' => 0,
Ted Kremeneke8450fe2008-09-30 23:40:25 +0000360 '-fpascal-strings' => 0,
Daniel Dunbar497ff132009-04-10 19:52:24 +0000361 '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '='
362 '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '='
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000363);
364
365my %IgnoredOptionMap = (
Ted Kremenek4a154b22008-07-24 03:52:21 +0000366 '-MT' => 1, # Ignore these preprocessor options.
367 '-MF' => 1,
368
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000369 '-fsyntax-only' => 0,
370 '-save-temps' => 0,
371 '-install_name' => 1,
372 '-exported_symbols_list' => 1,
373 '-current_version' => 1,
374 '-compatibility_version' => 1,
375 '-init' => 1,
376 '-e' => 1,
377 '-seg1addr' => 1,
378 '-bundle_loader' => 1,
379 '-multiply_defined' => 1,
380 '-sectorder' => 3,
381 '--param' => 1,
Anna Zaks3a7f73d2012-01-06 01:54:02 +0000382 '-u' => 1,
383 '--serialize-diagnostics' => 1
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000384);
385
386my %LangMap = (
387 'c' => 'c',
Shantonu Sendf44f742010-07-03 03:08:23 +0000388 'cp' => 'c++',
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000389 'cpp' => 'c++',
Anna Zaksd6827412012-04-14 16:30:00 +0000390 'cxx' => 'c++',
391 'txx' => 'c++',
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000392 'cc' => 'c++',
Anna Zaks5efad632011-08-31 23:53:24 +0000393 'ii' => 'c++',
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000394 'i' => 'c-cpp-output',
395 'm' => 'objective-c',
Anna Zaks5efad632011-08-31 23:53:24 +0000396 'mi' => 'objective-c-cpp-output',
397 'mm' => 'objective-c++'
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000398);
399
Ted Kremenek8b89a652008-09-29 16:15:20 +0000400my %UniqueOptions = (
401 '-isysroot' => 0
402);
403
Ted Kremenekadccbca2010-03-08 19:06:44 +0000404##----------------------------------------------------------------------------##
405# Languages accepted.
406##----------------------------------------------------------------------------##
407
Ted Kremenekdc99ec42009-05-11 21:08:34 +0000408my %LangsAccepted = (
409 "objective-c" => 1,
Ted Kremenekd33c4d32011-08-11 22:47:20 +0000410 "c" => 1,
411 "c++" => 1,
412 "objective-c++" => 1
Ted Kremenekdc99ec42009-05-11 21:08:34 +0000413);
414
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000415##----------------------------------------------------------------------------##
416# Main Logic.
417##----------------------------------------------------------------------------##
418
419my $Action = 'link';
420my @CompileOpts;
421my @LinkOpts;
422my @Files;
423my $Lang;
424my $Output;
Ted Kremenek8b89a652008-09-29 16:15:20 +0000425my %Uniqued;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000426
427# Forward arguments to gcc.
Ted Kremenekf65a0c62009-12-15 02:35:54 +0000428my $Status = system($Compiler,@ARGV);
Tom Carea5f13c862010-09-29 23:48:31 +0000429if (defined $ENV{'CCC_ANALYZER_LOG'}) {
430 print "$Compiler @ARGV\n";
431}
Ted Kremenek1a422782008-08-28 01:18:44 +0000432if ($Status) { exit($Status >> 8); }
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000433
434# Get the analysis options.
435my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000436
Anna Zaks268f1542012-05-25 01:13:50 +0000437# Get the plugins to load.
438my $Plugins = $ENV{'CCC_ANALYZER_PLUGINS'};
439
Zhongxing Xuad4c3de2008-10-27 14:26:32 +0000440# Get the store model.
441my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
Ted Kremenekb5351812009-02-17 04:27:41 +0000442
443# Get the constraints engine.
444my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'};
Zhongxing Xuad4c3de2008-10-27 14:26:32 +0000445
Ted Kremenek90230552008-11-04 00:02:53 +0000446# Get the output format.
447my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
Ted Kremenekd3d16aa2009-02-17 05:01:10 +0000448if (!defined $OutputFormat) { $OutputFormat = "html"; }
Ted Kremenek90230552008-11-04 00:02:53 +0000449
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000450# Determine the level of verbosity.
451my $Verbose = 0;
452if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
453if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
454
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000455# Get the HTML output directory.
456my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
457
Ted Kremenek9b15eff2009-02-24 22:07:12 +0000458my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1);
Ted Kremenek15146a52008-09-25 20:17:57 +0000459my %ArchsSeen;
Ted Kremenek9b15eff2009-02-24 22:07:12 +0000460my $HadArch = 0;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000461
462# Process the arguments.
463foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
Ted Kremenekad4a57d2008-10-19 06:42:38 +0000464 my $Arg = $ARGV[$i];
465 my ($ArgKey) = split /=/,$Arg,2;
466
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000467 # Modes ccc-analyzer supports
Ted Kremenekba8d7fc2009-08-04 00:57:12 +0000468 if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; }
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000469 elsif ($Arg eq '-c') { $Action = 'compile'; }
470 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek15146a52008-09-25 20:17:57 +0000471
472 # Specially handle duplicate cases of -arch
473 if ($Arg eq "-arch") {
474 my $arch = $ARGV[$i+1];
Ted Kremenek9b15eff2009-02-24 22:07:12 +0000475 # We don't want to process 'ppc' because of Clang's lack of support
476 # for Altivec (also some #defines won't likely be defined correctly, etc.)
477 if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; }
478 $HadArch = 1;
Ted Kremenek15146a52008-09-25 20:17:57 +0000479 ++$i;
480 next;
481 }
482
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000483 # Options with possible arguments that should pass through to compiler.
Ted Kremenekad4a57d2008-10-19 06:42:38 +0000484 if (defined $CompileOptionMap{$ArgKey}) {
485 my $Cnt = $CompileOptionMap{$ArgKey};
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000486 push @CompileOpts,$Arg;
487 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
488 next;
489 }
490
491 # Options with possible arguments that should pass through to linker.
Ted Kremenekad4a57d2008-10-19 06:42:38 +0000492 if (defined $LinkerOptionMap{$ArgKey}) {
493 my $Cnt = $LinkerOptionMap{$ArgKey};
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000494 push @LinkOpts,$Arg;
495 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
496 next;
497 }
498
499 # Options with possible arguments that should pass through to both compiler
500 # and the linker.
Ted Kremenekad4a57d2008-10-19 06:42:38 +0000501 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
502 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
Ted Kremenek887c49d2008-09-29 23:06:09 +0000503
Ted Kremenek8b89a652008-09-29 16:15:20 +0000504 # Check if this is an option that should have a unique value, and if so
505 # determine if the value was checked before.
506 if ($UniqueOptions{$Arg}) {
507 if (defined $Uniqued{$Arg}) {
508 $i += $Cnt;
509 next;
510 }
511 $Uniqued{$Arg} = 1;
512 }
513
Ted Kremenek887c49d2008-09-29 23:06:09 +0000514 push @CompileOpts,$Arg;
515 push @LinkOpts,$Arg;
516
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000517 while ($Cnt > 0) {
518 ++$i; --$Cnt;
519 push @CompileOpts, $ARGV[$i];
520 push @LinkOpts, $ARGV[$i];
521 }
522 next;
523 }
Ted Kremenekf18f4602008-05-24 15:58:54 +0000524
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000525 # Ignored options.
Ted Kremenekad4a57d2008-10-19 06:42:38 +0000526 if (defined $IgnoredOptionMap{$ArgKey}) {
527 my $Cnt = $IgnoredOptionMap{$ArgKey};
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000528 while ($Cnt > 0) {
529 ++$i; --$Cnt;
530 }
531 next;
532 }
Ted Kremenekf18f4602008-05-24 15:58:54 +0000533
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000534 # Compile mode flags.
535 if ($Arg =~ /^-[D,I,U](.*)$/) {
536 my $Tmp = $Arg;
537 if ($1 eq '') {
538 # FIXME: Check if we are going off the end.
539 ++$i;
540 $Tmp = $Arg . $ARGV[$i];
541 }
542 push @CompileOpts,$Tmp;
543 next;
544 }
545
546 # Language.
547 if ($Arg eq '-x') {
548 $Lang = $ARGV[$i+1];
549 ++$i; next;
550 }
Ted Kremenekf18f4602008-05-24 15:58:54 +0000551
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000552 # Output file.
553 if ($Arg eq '-o') {
554 ++$i;
555 $Output = $ARGV[$i];
556 next;
557 }
558
559 # Get the link mode.
560 if ($Arg =~ /^-[l,L,O]/) {
561 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
562 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
563 else { push @LinkOpts,$Arg; }
564 next;
565 }
566
567 if ($Arg =~ /^-std=/) {
568 push @CompileOpts,$Arg;
569 next;
570 }
571
572# if ($Arg =~ /^-f/) {
573# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
574# push @CompileOpts,$Arg;
575# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
576# }
577
578 # Get the compiler/link mode.
579 if ($Arg =~ /^-F(.+)$/) {
580 my $Tmp = $Arg;
581 if ($1 eq '') {
582 # FIXME: Check if we are going off the end.
583 ++$i;
584 $Tmp = $Arg . $ARGV[$i];
585 }
586 push @CompileOpts,$Tmp;
587 push @LinkOpts,$Tmp;
588 next;
589 }
Ted Kremenekf18f4602008-05-24 15:58:54 +0000590
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000591 # Input files.
592 if ($Arg eq '-filelist') {
593 # FIXME: Make sure we aren't walking off the end.
594 open(IN, $ARGV[$i+1]);
595 while (<IN>) { s/\015?\012//; push @Files,$_; }
596 close(IN);
Ted Kremenekd0d72562009-08-14 18:20:50 +0000597 ++$i;
598 next;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000599 }
600
Ted Kremenekd0d72562009-08-14 18:20:50 +0000601 # Handle -Wno-. We don't care about extra warnings, but
602 # we should suppress ones that we don't want to see.
603 if ($Arg =~ /^-Wno-/) {
604 push @CompileOpts, $Arg;
605 next;
606 }
607
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000608 if (!($Arg =~ /^-/)) {
Ted Kremenekd0d72562009-08-14 18:20:50 +0000609 push @Files, $Arg;
610 next;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000611 }
612}
Ted Kremenekf18f4602008-05-24 15:58:54 +0000613
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000614if ($Action eq 'compile' or $Action eq 'link') {
Ted Kremenek9b15eff2009-02-24 22:07:12 +0000615 my @Archs = keys %ArchsSeen;
616 # Skip the file if we don't support the architectures specified.
Ted Kremenek86cb75a2009-02-25 00:10:37 +0000617 exit 0 if ($HadArch && scalar(@Archs) == 0);
Ted Kremenek9b15eff2009-02-24 22:07:12 +0000618
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000619 foreach my $file (@Files) {
620 # Determine the language for the file.
621 my $FileLang = $Lang;
622
623 if (!defined($FileLang)) {
624 # Infer the language from the extension.
625 if ($file =~ /[.]([^.]+)$/) {
626 $FileLang = $LangMap{$1};
627 }
628 }
Ted Kremeneke3fc13a2008-05-14 20:10:33 +0000629
Ted Kremenek14015de2010-02-12 00:10:34 +0000630 # FileLang still not defined? Skip the file.
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000631 next if (!defined $FileLang);
Ted Kremenek14015de2010-02-12 00:10:34 +0000632
633 # Language not accepted?
Ted Kremenekdc99ec42009-05-11 21:08:34 +0000634 next if (!defined $LangsAccepted{$FileLang});
Ted Kremenek14015de2010-02-12 00:10:34 +0000635
Ted Kremenek46727df2009-05-15 04:20:31 +0000636 my @CmdArgs;
637 my @AnalyzeArgs;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000638
639 if ($FileLang ne 'unknown') {
Ted Kremenekd4bcb4f2011-03-16 21:10:42 +0000640 push @CmdArgs, '-x', $FileLang;
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000641 }
Ted Kremenek5efdf842008-03-25 22:35:32 +0000642
Zhongxing Xuad4c3de2008-10-27 14:26:32 +0000643 if (defined $StoreModel) {
Ted Kremenekb5351812009-02-17 04:27:41 +0000644 push @AnalyzeArgs, "-analyzer-store=$StoreModel";
Zhongxing Xuad4c3de2008-10-27 14:26:32 +0000645 }
Ted Kremenekb5351812009-02-17 04:27:41 +0000646
647 if (defined $ConstraintsModel) {
648 push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel";
649 }
Ted Kremenek42ec9142011-02-17 02:28:30 +0000650
Anna Zaks5efad632011-08-31 23:53:24 +0000651 if (defined $Analyses) {
652 push @AnalyzeArgs, split '\s+', $Analyses;
653 }
Ted Kremenekb5351812009-02-17 04:27:41 +0000654
Anna Zaks268f1542012-05-25 01:13:50 +0000655 if (defined $Plugins) {
656 push @AnalyzeArgs, split '\s+', $Plugins;
657 }
658
Ted Kremenek90230552008-11-04 00:02:53 +0000659 if (defined $OutputFormat) {
Ted Kremenekb5351812009-02-17 04:27:41 +0000660 push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat;
Ted Kremenek5cc54862009-07-27 22:10:34 +0000661 if ($OutputFormat =~ /plist/) {
Ted Kremenek13747162009-01-21 00:42:24 +0000662 # Change "Output" to be a file.
663 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
664 DIR => $HtmlDir);
665 $ResultFile = $f;
Anna Zaks45ce1bf2011-09-09 18:43:53 +0000666 # If the HtmlDir is not set, we sould clean up the plist files.
667 if (!defined $HtmlDir || -z $HtmlDir) {
668 $CleanupFile = $f;
669 }
Ted Kremenek13747162009-01-21 00:42:24 +0000670 }
Ted Kremenek90230552008-11-04 00:02:53 +0000671 }
Zhongxing Xuad4c3de2008-10-27 14:26:32 +0000672
Ted Kremenek42ec9142011-02-17 02:28:30 +0000673 push @CmdArgs, @CompileOpts;
674 push @CmdArgs, $file;
Zhongxing Xuad4c3de2008-10-27 14:26:32 +0000675
Ted Kremenek15146a52008-09-25 20:17:57 +0000676 if (scalar @Archs) {
677 foreach my $arch (@Archs) {
678 my @NewArgs;
Ted Kremenekd4bcb4f2011-03-16 21:10:42 +0000679 push @NewArgs, '-arch', $arch;
Ted Kremenek46727df2009-05-15 04:20:31 +0000680 push @NewArgs, @CmdArgs;
Ted Kremenek5c512e62009-12-11 22:44:53 +0000681 Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output,
Ted Kremenek42ec9142011-02-17 02:28:30 +0000682 $Verbose, $HtmlDir, $file);
Ted Kremenek15146a52008-09-25 20:17:57 +0000683 }
684 }
685 else {
Ted Kremenek5c512e62009-12-11 22:44:53 +0000686 Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output,
Ted Kremenek42ec9142011-02-17 02:28:30 +0000687 $Verbose, $HtmlDir, $file);
Ted Kremenek15146a52008-09-25 20:17:57 +0000688 }
Ted Kremenekf7ffd662008-07-19 06:11:04 +0000689 }
690}
Ted Kremenek5efdf842008-03-25 22:35:32 +0000691
Ted Kremenek7b628062008-08-27 22:30:34 +0000692exit($Status >> 8);
693