blob: 68df56a38eae79da77708071e098f8c8d4d4949b [file] [log] [blame]
Ted Kremenekfbeeca82008-07-19 06:11:04 +00001#!/usr/bin/env perl
Ted Kremenekb0982882008-03-25 22:35:32 +00002#
3# The LLVM Compiler Infrastructure
4#
5# This file is distributed under the University of Illinois Open Source
6# License. See LICENSE.TXT for details.
7#
8##===----------------------------------------------------------------------===##
9#
Ted Kremenekfbeeca82008-07-19 06:11:04 +000010# A script designed to interpose between the build system and gcc. It invokes
11# both gcc and the static analyzer.
Ted Kremenekb0982882008-03-25 22:35:32 +000012#
13##===----------------------------------------------------------------------===##
14
Ted Kremenekfbeeca82008-07-19 06:11:04 +000015use strict;
16use warnings;
Ted Kremenekb315a392008-09-21 19:56:14 +000017use Cwd qw/ getcwd abs_path /;
Ted Kremenek991c54b2008-08-08 20:46:42 +000018use File::Temp qw/ tempfile /;
19use File::Path qw / mkpath /;
Ted Kremenekddf32da2009-01-21 00:42:24 +000020use File::Basename;
Ted Kremenek2ec5cd52008-08-25 20:44:31 +000021
22my $CC = $ENV{'CCC_CC'};
23if (!defined $CC) { $CC = "gcc"; }
Ted Kremenekddf32da2009-01-21 00:42:24 +000024my $CleanupFile;
25my $ResultFile;
26
27# Remove any stale files at exit.
28END {
29 if (defined $CleanupFile && -z $CleanupFile) {
30 `rm -f $CleanupFile`;
31 }
32}
33
Ted Kremenek991c54b2008-08-08 20:46:42 +000034##----------------------------------------------------------------------------##
35# Process Clang Crashes.
36##----------------------------------------------------------------------------##
37
38sub GetPPExt {
39 my $Lang = shift;
40 if ($Lang =~ /objective-c/) { return ".mi"; }
41 return ".i";
42}
43
Ted Kremenek5d31f832008-08-18 18:38:29 +000044sub ProcessClangFailure {
Ted Kremenekc3998fa2008-09-25 00:51:44 +000045 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
Ted Kremenek991c54b2008-08-08 20:46:42 +000046 my $Dir = "$HtmlDir/crashes";
47 mkpath $Dir;
Ted Kremenekc3998fa2008-09-25 00:51:44 +000048
49 # Generate the preprocessed file with cc (i.e., gcc).
Ted Kremenek991c54b2008-08-08 20:46:42 +000050 my ($PPH, $PPFile) = tempfile("clang_crash_XXXXXX",
51 SUFFIX => GetPPExt($Lang),
52 DIR => $Dir);
53
Ted Kremenek2ec5cd52008-08-25 20:44:31 +000054 system $CC, @$Args, "-E", "-o", $PPFile;
Ted Kremenek991c54b2008-08-08 20:46:42 +000055 close ($PPH);
Ted Kremenekc3998fa2008-09-25 00:51:44 +000056
57 # Generate the preprocessed file with clang.
58 my $PPFile_Clang = $PPFile;
59 $PPFile_Clang =~ s/[.](.+)$/.clang.$1/;
60 system $Clang, @$Args, "-E", "-o", "$PPFile_Clang";
61
62 # Create the info file.
Ted Kremenek82a12532008-09-25 00:25:16 +000063 open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
Ted Kremenek5f2825f2008-09-21 18:04:49 +000064 print OUT abs_path($file), "\n";
Ted Kremenek5d31f832008-08-18 18:38:29 +000065 print OUT "$ErrorType\n";
Ted Kremenek2dd7ad12008-08-18 20:55:25 +000066 print OUT "@$Args\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +000067 close OUT;
Ted Kremenek82a12532008-09-25 00:25:16 +000068 `uname -a >> $PPFile.info.txt 2>&1`;
69 `$CC -v >> $PPFile.info.txt 2>&1`;
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +000070 system 'mv',$ofile,"$PPFile.stderr.txt";
Ted Kremenek991c54b2008-08-08 20:46:42 +000071}
Ted Kremenekb0982882008-03-25 22:35:32 +000072
Ted Kremenekfbeeca82008-07-19 06:11:04 +000073##----------------------------------------------------------------------------##
74# Running the analyzer.
75##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +000076
Ted Kremenekfbeeca82008-07-19 06:11:04 +000077sub Analyze {
78 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +000079
Ted Kremenekfbeeca82008-07-19 06:11:04 +000080 # Skip anything related to C++.
81 return if ($Lang =~ /c[+][+]/);
Ted Kremenek5d31f832008-08-18 18:38:29 +000082
Ted Kremenekfbeeca82008-07-19 06:11:04 +000083 my $RunAnalyzer = 0;
84 my $Cmd;
85 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +000086 my @CmdArgsSansAnalyses;
Ted Kremenek61cd9882008-05-24 15:58:54 +000087
Ted Kremenekfbeeca82008-07-19 06:11:04 +000088 if ($Lang =~ /header/) {
89 exit 0 if (!defined ($Output));
90 $Cmd = 'cp';
91 push @CmdArgs,$file;
92 # Remove the PCH extension.
93 $Output =~ s/[.]gch$//;
94 push @CmdArgs,$Output;
Ted Kremenek991c54b2008-08-08 20:46:42 +000095 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000096 }
97 else {
98 $Cmd = $Clang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000099 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
100 push @CmdArgs,@$Args;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000101 @CmdArgsSansAnalyses = @CmdArgs;
Daniel Dunbard4270232009-01-20 23:17:32 +0000102 push @CmdArgs,'--analyze';
Ted Kremenek991c54b2008-08-08 20:46:42 +0000103 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000104 $RunAnalyzer = 1;
105 }
106
107 my @PrintArgs;
108 my $dir;
109
110 if ($Verbose) {
111 $dir = getcwd();
112 print STDERR "\n[LOCATION]: $dir\n";
113 push @PrintArgs,"'$Cmd'";
114 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
115 }
116
117 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +0000118 # We MUST print to stderr. Some clients use the stdout output of
119 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000120 print STDERR join(' ',@PrintArgs);
121 print STDERR "\n";
122 }
123 elsif ($Verbose == 2) {
124 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
125 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000126
Ted Kremenekddf32da2009-01-21 00:42:24 +0000127 if ($RunAnalyzer) {
128 if (defined $ResultFile) {
129 push @CmdArgs,'-o';
130 push @CmdArgs, $ResultFile;
131 }
132 elsif (defined $HtmlDir) {
133 push @CmdArgs,'-o';
134 push @CmdArgs, $HtmlDir;
135 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000136 }
Ted Kremenek948e06b2008-08-27 22:30:34 +0000137
138 if (defined $ENV{'CCC_UBI'}) {
139 push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
140 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000141
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000142 # Capture the STDERR of clang and send it to a temporary file.
143 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
144 # We save the output file in the 'crashes' directory if clang encounters
145 # any problems with the file.
Ted Kremenek13462682008-09-11 23:05:26 +0000146 pipe (FROM_CHILD, TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000147 my $pid = fork();
148 if ($pid == 0) {
Ted Kremenek13462682008-09-11 23:05:26 +0000149 close FROM_CHILD;
150 open(STDOUT,">&", \*TO_PARENT);
151 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000152 exec $Cmd, @CmdArgs;
153 }
Ted Kremenek13462682008-09-11 23:05:26 +0000154
155 close TO_PARENT;
156 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
157
158 while (<FROM_CHILD>) {
159 print $ofh $_;
160 print STDERR $_;
161 }
162
163 waitpid($pid,0);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000164 my $Result = $?;
165
166 # Did the command die because of a signal?
167 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
Ted Kremenekc3998fa2008-09-25 00:51:44 +0000168 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000169 "Crash", $ofile);
170 }
171 elsif ($Result) {
Ted Kremenekc3998fa2008-09-25 00:51:44 +0000172 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000173 "Parser Rejects", $ofile);
174 }
175
176 `rm -f $ofile`;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000177}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000178
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000179##----------------------------------------------------------------------------##
180# Lookup tables.
181##----------------------------------------------------------------------------##
182
183my %CompileOptionMap = (
184 '-nostdinc' => 0,
Anders Carlsson06c58b12008-12-19 20:56:23 +0000185 '-fblocks' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000186 '-fobjc-gc-only' => 0,
187 '-fobjc-gc' => 0,
188 '-include' => 1,
189 '-idirafter' => 1,
190 '-iprefix' => 1,
191 '-iquote' => 1,
192 '-isystem' => 1,
193 '-iwithprefix' => 1,
194 '-iwithprefixbefore' => 1
195);
196
197my %LinkerOptionMap = (
198 '-framework' => 1
199);
200
201my %CompilerLinkerOptionMap = (
202 '-isysroot' => 1,
203 '-arch' => 1,
Ted Kremeneke4f69522008-09-29 22:45:28 +0000204 '-v' => 0,
Ted Kremenekb10362a2008-09-30 23:40:25 +0000205 '-fpascal-strings' => 0,
206 '-mmacosx-version-min' => 0 # This is really a 1 argument, but always has '='
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000207);
208
209my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000210 '-MT' => 1, # Ignore these preprocessor options.
211 '-MF' => 1,
212
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000213 '-fsyntax-only' => 0,
214 '-save-temps' => 0,
215 '-install_name' => 1,
216 '-exported_symbols_list' => 1,
217 '-current_version' => 1,
218 '-compatibility_version' => 1,
219 '-init' => 1,
220 '-e' => 1,
221 '-seg1addr' => 1,
222 '-bundle_loader' => 1,
223 '-multiply_defined' => 1,
224 '-sectorder' => 3,
225 '--param' => 1,
226 '-u' => 1
227);
228
229my %LangMap = (
230 'c' => 'c',
231 'cpp' => 'c++',
232 'cc' => 'c++',
233 'i' => 'c-cpp-output',
234 'm' => 'objective-c',
235 'mi' => 'objective-c-cpp-output'
236);
237
Ted Kremeneka30730e2008-09-29 16:15:20 +0000238my %UniqueOptions = (
239 '-isysroot' => 0
240);
241
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000242##----------------------------------------------------------------------------##
243# Main Logic.
244##----------------------------------------------------------------------------##
245
246my $Action = 'link';
247my @CompileOpts;
248my @LinkOpts;
249my @Files;
250my $Lang;
251my $Output;
Ted Kremeneka30730e2008-09-29 16:15:20 +0000252my %Uniqued;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000253
254# Forward arguments to gcc.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000255my $Status = system($CC,@ARGV);
Ted Kremenekcb344d02008-08-28 01:18:44 +0000256if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000257
258# Get the analysis options.
259my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
260if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
261
Zhongxing Xu07c37672008-10-27 14:26:32 +0000262# Get the store model.
263my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
264
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000265# Get the output format.
266my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
267
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000268# Determine the level of verbosity.
269my $Verbose = 0;
270if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
271if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
272
273# Determine what clang executable to use.
274my $Clang = $ENV{'CLANG'};
275if (!defined $Clang) { $Clang = 'clang'; }
276
277# Get the HTML output directory.
278my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
279
Ted Kremenek27783eb2008-09-25 20:17:57 +0000280my %ArchsSeen;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000281
282# Process the arguments.
283foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000284 my $Arg = $ARGV[$i];
285 my ($ArgKey) = split /=/,$Arg,2;
286
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000287 # Modes ccc-analyzer supports
288 if ($Arg eq '-E') { $Action = 'preprocess'; }
289 elsif ($Arg eq '-c') { $Action = 'compile'; }
290 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek27783eb2008-09-25 20:17:57 +0000291
292 # Specially handle duplicate cases of -arch
293 if ($Arg eq "-arch") {
294 my $arch = $ARGV[$i+1];
295 $ArchsSeen{$arch} = 1;
296 ++$i;
297 next;
298 }
299
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000300 # Options with possible arguments that should pass through to compiler.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000301 if (defined $CompileOptionMap{$ArgKey}) {
302 my $Cnt = $CompileOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000303 push @CompileOpts,$Arg;
304 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
305 next;
306 }
307
308 # Options with possible arguments that should pass through to linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000309 if (defined $LinkerOptionMap{$ArgKey}) {
310 my $Cnt = $LinkerOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000311 push @LinkOpts,$Arg;
312 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
313 next;
314 }
315
316 # Options with possible arguments that should pass through to both compiler
317 # and the linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000318 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
319 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000320
Ted Kremeneka30730e2008-09-29 16:15:20 +0000321 # Check if this is an option that should have a unique value, and if so
322 # determine if the value was checked before.
323 if ($UniqueOptions{$Arg}) {
324 if (defined $Uniqued{$Arg}) {
325 $i += $Cnt;
326 next;
327 }
328 $Uniqued{$Arg} = 1;
329 }
330
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000331 push @CompileOpts,$Arg;
332 push @LinkOpts,$Arg;
333
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000334 while ($Cnt > 0) {
335 ++$i; --$Cnt;
336 push @CompileOpts, $ARGV[$i];
337 push @LinkOpts, $ARGV[$i];
338 }
339 next;
340 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000341
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000342 # Ignored options.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000343 if (defined $IgnoredOptionMap{$ArgKey}) {
344 my $Cnt = $IgnoredOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000345 while ($Cnt > 0) {
346 ++$i; --$Cnt;
347 }
348 next;
349 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000350
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000351 # Compile mode flags.
352 if ($Arg =~ /^-[D,I,U](.*)$/) {
353 my $Tmp = $Arg;
354 if ($1 eq '') {
355 # FIXME: Check if we are going off the end.
356 ++$i;
357 $Tmp = $Arg . $ARGV[$i];
358 }
359 push @CompileOpts,$Tmp;
360 next;
361 }
362
363 # Language.
364 if ($Arg eq '-x') {
365 $Lang = $ARGV[$i+1];
366 ++$i; next;
367 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000368
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000369 # Output file.
370 if ($Arg eq '-o') {
371 ++$i;
372 $Output = $ARGV[$i];
373 next;
374 }
375
376 # Get the link mode.
377 if ($Arg =~ /^-[l,L,O]/) {
378 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
379 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
380 else { push @LinkOpts,$Arg; }
381 next;
382 }
383
384 if ($Arg =~ /^-std=/) {
385 push @CompileOpts,$Arg;
386 next;
387 }
388
389# if ($Arg =~ /^-f/) {
390# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
391# push @CompileOpts,$Arg;
392# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
393# }
394
395 # Get the compiler/link mode.
396 if ($Arg =~ /^-F(.+)$/) {
397 my $Tmp = $Arg;
398 if ($1 eq '') {
399 # FIXME: Check if we are going off the end.
400 ++$i;
401 $Tmp = $Arg . $ARGV[$i];
402 }
403 push @CompileOpts,$Tmp;
404 push @LinkOpts,$Tmp;
405 next;
406 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000407
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000408 # Input files.
409 if ($Arg eq '-filelist') {
410 # FIXME: Make sure we aren't walking off the end.
411 open(IN, $ARGV[$i+1]);
412 while (<IN>) { s/\015?\012//; push @Files,$_; }
413 close(IN);
414 ++$i; next;
415 }
416
417 if (!($Arg =~ /^-/)) {
418 push @Files,$Arg; next;
419 }
420}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000421
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000422if ($Action eq 'compile' or $Action eq 'link') {
423 foreach my $file (@Files) {
424 # Determine the language for the file.
425 my $FileLang = $Lang;
426
427 if (!defined($FileLang)) {
428 # Infer the language from the extension.
429 if ($file =~ /[.]([^.]+)$/) {
430 $FileLang = $LangMap{$1};
431 }
432 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000433
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000434 next if (!defined $FileLang);
435
436 my @AnalyzeArgs;
437
438 if ($FileLang ne 'unknown') {
439 push @AnalyzeArgs,'-x';
440 push @AnalyzeArgs,$FileLang;
441 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000442
Zhongxing Xu07c37672008-10-27 14:26:32 +0000443 if (defined $StoreModel) {
444 push @AnalyzeArgs, $StoreModel;
445 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000446
447 if (defined $OutputFormat) {
448 push @AnalyzeArgs, "-analyzer-output-" . $OutputFormat;
Ted Kremenekddf32da2009-01-21 00:42:24 +0000449 if ($OutputFormat eq "plist") {
450 # Change "Output" to be a file.
451 my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist",
452 DIR => $HtmlDir);
453 $ResultFile = $f;
454 $CleanupFile = $f;
455 }
456
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000457 }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000458
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000459 push @AnalyzeArgs,@CompileOpts;
460 push @AnalyzeArgs,$file;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000461
Ted Kremenek27783eb2008-09-25 20:17:57 +0000462 my @Archs = keys %ArchsSeen;
463 if (scalar @Archs) {
464 foreach my $arch (@Archs) {
465 my @NewArgs;
466 push @NewArgs, '-arch';
467 push @NewArgs, $arch;
468 push @NewArgs, @AnalyzeArgs;
469 Analyze($Clang, \@NewArgs, $FileLang, $Output,
470 $Verbose, $HtmlDir, $file, $Analyses);
471 }
472 }
473 else {
474 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
475 $Verbose, $HtmlDir, $file, $Analyses);
476 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000477 }
478}
Ted Kremenekb0982882008-03-25 22:35:32 +0000479
Ted Kremenek948e06b2008-08-27 22:30:34 +0000480exit($Status >> 8);
481