blob: 14cbf54df10da22dfdb3ecfb2b029ca31944942a [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 Kremenek2ec5cd52008-08-25 20:44:31 +000020
21my $CC = $ENV{'CCC_CC'};
22if (!defined $CC) { $CC = "gcc"; }
Ted Kremenek991c54b2008-08-08 20:46:42 +000023
24##----------------------------------------------------------------------------##
25# Process Clang Crashes.
26##----------------------------------------------------------------------------##
27
28sub GetPPExt {
29 my $Lang = shift;
30 if ($Lang =~ /objective-c/) { return ".mi"; }
31 return ".i";
32}
33
Ted Kremenek5d31f832008-08-18 18:38:29 +000034sub ProcessClangFailure {
Ted Kremenekc3998fa2008-09-25 00:51:44 +000035 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
Ted Kremenek991c54b2008-08-08 20:46:42 +000036 my $Dir = "$HtmlDir/crashes";
37 mkpath $Dir;
Ted Kremenekc3998fa2008-09-25 00:51:44 +000038
39 # Generate the preprocessed file with cc (i.e., gcc).
Ted Kremenek991c54b2008-08-08 20:46:42 +000040 my ($PPH, $PPFile) = tempfile("clang_crash_XXXXXX",
41 SUFFIX => GetPPExt($Lang),
42 DIR => $Dir);
43
Ted Kremenek2ec5cd52008-08-25 20:44:31 +000044 system $CC, @$Args, "-E", "-o", $PPFile;
Ted Kremenek991c54b2008-08-08 20:46:42 +000045 close ($PPH);
Ted Kremenekc3998fa2008-09-25 00:51:44 +000046
47 # Generate the preprocessed file with clang.
48 my $PPFile_Clang = $PPFile;
49 $PPFile_Clang =~ s/[.](.+)$/.clang.$1/;
50 system $Clang, @$Args, "-E", "-o", "$PPFile_Clang";
51
52 # Create the info file.
Ted Kremenek82a12532008-09-25 00:25:16 +000053 open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
Ted Kremenek5f2825f2008-09-21 18:04:49 +000054 print OUT abs_path($file), "\n";
Ted Kremenek5d31f832008-08-18 18:38:29 +000055 print OUT "$ErrorType\n";
Ted Kremenek2dd7ad12008-08-18 20:55:25 +000056 print OUT "@$Args\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +000057 close OUT;
Ted Kremenek82a12532008-09-25 00:25:16 +000058 `uname -a >> $PPFile.info.txt 2>&1`;
59 `$CC -v >> $PPFile.info.txt 2>&1`;
Ted Kremenek9f9b1fd2008-09-12 22:49:36 +000060 system 'mv',$ofile,"$PPFile.stderr.txt";
Ted Kremenek991c54b2008-08-08 20:46:42 +000061}
Ted Kremenekb0982882008-03-25 22:35:32 +000062
Ted Kremenekfbeeca82008-07-19 06:11:04 +000063##----------------------------------------------------------------------------##
64# Running the analyzer.
65##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +000066
Ted Kremenekfbeeca82008-07-19 06:11:04 +000067sub Analyze {
68 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +000069
Ted Kremenekfbeeca82008-07-19 06:11:04 +000070 # Skip anything related to C++.
71 return if ($Lang =~ /c[+][+]/);
Ted Kremenek5d31f832008-08-18 18:38:29 +000072
Ted Kremenekfbeeca82008-07-19 06:11:04 +000073 my $RunAnalyzer = 0;
74 my $Cmd;
75 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +000076 my @CmdArgsSansAnalyses;
Ted Kremenek61cd9882008-05-24 15:58:54 +000077
Ted Kremenekfbeeca82008-07-19 06:11:04 +000078 if ($Lang =~ /header/) {
79 exit 0 if (!defined ($Output));
80 $Cmd = 'cp';
81 push @CmdArgs,$file;
82 # Remove the PCH extension.
83 $Output =~ s/[.]gch$//;
84 push @CmdArgs,$Output;
Ted Kremenek991c54b2008-08-08 20:46:42 +000085 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000086 }
87 else {
88 $Cmd = $Clang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000089 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
90 push @CmdArgs,@$Args;
Ted Kremenek991c54b2008-08-08 20:46:42 +000091 @CmdArgsSansAnalyses = @CmdArgs;
92 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenekfbeeca82008-07-19 06:11:04 +000093 $RunAnalyzer = 1;
94 }
95
96 my @PrintArgs;
97 my $dir;
98
99 if ($Verbose) {
100 $dir = getcwd();
101 print STDERR "\n[LOCATION]: $dir\n";
102 push @PrintArgs,"'$Cmd'";
103 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
104 }
105
106 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +0000107 # We MUST print to stderr. Some clients use the stdout output of
108 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000109 print STDERR join(' ',@PrintArgs);
110 print STDERR "\n";
111 }
112 elsif ($Verbose == 2) {
113 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
114 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000115
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000116 if ($RunAnalyzer and defined($HtmlDir)) {
117 push @CmdArgs,'-o';
118 push @CmdArgs,$HtmlDir;
119 }
Ted Kremenek948e06b2008-08-27 22:30:34 +0000120
121 if (defined $ENV{'CCC_UBI'}) {
122 push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
123 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000124
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000125 # Capture the STDERR of clang and send it to a temporary file.
126 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
127 # We save the output file in the 'crashes' directory if clang encounters
128 # any problems with the file.
Ted Kremenek13462682008-09-11 23:05:26 +0000129 pipe (FROM_CHILD, TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000130 my $pid = fork();
131 if ($pid == 0) {
Ted Kremenek13462682008-09-11 23:05:26 +0000132 close FROM_CHILD;
133 open(STDOUT,">&", \*TO_PARENT);
134 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000135 exec $Cmd, @CmdArgs;
136 }
Ted Kremenek13462682008-09-11 23:05:26 +0000137
138 close TO_PARENT;
139 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
140
141 while (<FROM_CHILD>) {
142 print $ofh $_;
143 print STDERR $_;
144 }
145
146 waitpid($pid,0);
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000147 my $Result = $?;
148
149 # Did the command die because of a signal?
150 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
Ted Kremenekc3998fa2008-09-25 00:51:44 +0000151 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000152 "Crash", $ofile);
153 }
154 elsif ($Result) {
Ted Kremenekc3998fa2008-09-25 00:51:44 +0000155 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000156 "Parser Rejects", $ofile);
157 }
158
159 `rm -f $ofile`;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000160}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000161
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000162##----------------------------------------------------------------------------##
163# Lookup tables.
164##----------------------------------------------------------------------------##
165
166my %CompileOptionMap = (
167 '-nostdinc' => 0,
Anders Carlsson06c58b12008-12-19 20:56:23 +0000168 '-fblocks' => 0,
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000169 '-fobjc-gc-only' => 0,
170 '-fobjc-gc' => 0,
171 '-include' => 1,
172 '-idirafter' => 1,
173 '-iprefix' => 1,
174 '-iquote' => 1,
175 '-isystem' => 1,
176 '-iwithprefix' => 1,
177 '-iwithprefixbefore' => 1
178);
179
180my %LinkerOptionMap = (
181 '-framework' => 1
182);
183
184my %CompilerLinkerOptionMap = (
185 '-isysroot' => 1,
186 '-arch' => 1,
Ted Kremeneke4f69522008-09-29 22:45:28 +0000187 '-v' => 0,
Ted Kremenekb10362a2008-09-30 23:40:25 +0000188 '-fpascal-strings' => 0,
189 '-mmacosx-version-min' => 0 # This is really a 1 argument, but always has '='
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000190);
191
192my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000193 '-MT' => 1, # Ignore these preprocessor options.
194 '-MF' => 1,
195
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000196 '-fsyntax-only' => 0,
197 '-save-temps' => 0,
198 '-install_name' => 1,
199 '-exported_symbols_list' => 1,
200 '-current_version' => 1,
201 '-compatibility_version' => 1,
202 '-init' => 1,
203 '-e' => 1,
204 '-seg1addr' => 1,
205 '-bundle_loader' => 1,
206 '-multiply_defined' => 1,
207 '-sectorder' => 3,
208 '--param' => 1,
209 '-u' => 1
210);
211
212my %LangMap = (
213 'c' => 'c',
214 'cpp' => 'c++',
215 'cc' => 'c++',
216 'i' => 'c-cpp-output',
217 'm' => 'objective-c',
218 'mi' => 'objective-c-cpp-output'
219);
220
Ted Kremeneka30730e2008-09-29 16:15:20 +0000221my %UniqueOptions = (
222 '-isysroot' => 0
223);
224
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000225##----------------------------------------------------------------------------##
226# Main Logic.
227##----------------------------------------------------------------------------##
228
229my $Action = 'link';
230my @CompileOpts;
231my @LinkOpts;
232my @Files;
233my $Lang;
234my $Output;
Ted Kremeneka30730e2008-09-29 16:15:20 +0000235my %Uniqued;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000236
237# Forward arguments to gcc.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000238my $Status = system($CC,@ARGV);
Ted Kremenekcb344d02008-08-28 01:18:44 +0000239if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000240
241# Get the analysis options.
242my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
243if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
244
Zhongxing Xu07c37672008-10-27 14:26:32 +0000245# Get the store model.
246my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'};
247
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000248# Get the output format.
249my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'};
250
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000251# Determine the level of verbosity.
252my $Verbose = 0;
253if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
254if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
255
256# Determine what clang executable to use.
257my $Clang = $ENV{'CLANG'};
258if (!defined $Clang) { $Clang = 'clang'; }
259
260# Get the HTML output directory.
261my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
262
Ted Kremenek27783eb2008-09-25 20:17:57 +0000263my %ArchsSeen;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000264
265# Process the arguments.
266foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000267 my $Arg = $ARGV[$i];
268 my ($ArgKey) = split /=/,$Arg,2;
269
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000270 # Modes ccc-analyzer supports
271 if ($Arg eq '-E') { $Action = 'preprocess'; }
272 elsif ($Arg eq '-c') { $Action = 'compile'; }
273 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek27783eb2008-09-25 20:17:57 +0000274
275 # Specially handle duplicate cases of -arch
276 if ($Arg eq "-arch") {
277 my $arch = $ARGV[$i+1];
278 $ArchsSeen{$arch} = 1;
279 ++$i;
280 next;
281 }
282
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000283 # Options with possible arguments that should pass through to compiler.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000284 if (defined $CompileOptionMap{$ArgKey}) {
285 my $Cnt = $CompileOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000286 push @CompileOpts,$Arg;
287 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
288 next;
289 }
290
291 # Options with possible arguments that should pass through to linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000292 if (defined $LinkerOptionMap{$ArgKey}) {
293 my $Cnt = $LinkerOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000294 push @LinkOpts,$Arg;
295 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
296 next;
297 }
298
299 # Options with possible arguments that should pass through to both compiler
300 # and the linker.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000301 if (defined $CompilerLinkerOptionMap{$ArgKey}) {
302 my $Cnt = $CompilerLinkerOptionMap{$ArgKey};
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000303
Ted Kremeneka30730e2008-09-29 16:15:20 +0000304 # Check if this is an option that should have a unique value, and if so
305 # determine if the value was checked before.
306 if ($UniqueOptions{$Arg}) {
307 if (defined $Uniqued{$Arg}) {
308 $i += $Cnt;
309 next;
310 }
311 $Uniqued{$Arg} = 1;
312 }
313
Ted Kremenek47fc25f2008-09-29 23:06:09 +0000314 push @CompileOpts,$Arg;
315 push @LinkOpts,$Arg;
316
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000317 while ($Cnt > 0) {
318 ++$i; --$Cnt;
319 push @CompileOpts, $ARGV[$i];
320 push @LinkOpts, $ARGV[$i];
321 }
322 next;
323 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000324
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000325 # Ignored options.
Ted Kremenek89c4fcf2008-10-19 06:42:38 +0000326 if (defined $IgnoredOptionMap{$ArgKey}) {
327 my $Cnt = $IgnoredOptionMap{$ArgKey};
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000328 while ($Cnt > 0) {
329 ++$i; --$Cnt;
330 }
331 next;
332 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000333
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000334 # Compile mode flags.
335 if ($Arg =~ /^-[D,I,U](.*)$/) {
336 my $Tmp = $Arg;
337 if ($1 eq '') {
338 # FIXME: Check if we are going off the end.
339 ++$i;
340 $Tmp = $Arg . $ARGV[$i];
341 }
342 push @CompileOpts,$Tmp;
343 next;
344 }
345
346 # Language.
347 if ($Arg eq '-x') {
348 $Lang = $ARGV[$i+1];
349 ++$i; next;
350 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000351
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000352 # Output file.
353 if ($Arg eq '-o') {
354 ++$i;
355 $Output = $ARGV[$i];
356 next;
357 }
358
359 # Get the link mode.
360 if ($Arg =~ /^-[l,L,O]/) {
361 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
362 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
363 else { push @LinkOpts,$Arg; }
364 next;
365 }
366
367 if ($Arg =~ /^-std=/) {
368 push @CompileOpts,$Arg;
369 next;
370 }
371
372# if ($Arg =~ /^-f/) {
373# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
374# push @CompileOpts,$Arg;
375# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
376# }
377
378 # Get the compiler/link mode.
379 if ($Arg =~ /^-F(.+)$/) {
380 my $Tmp = $Arg;
381 if ($1 eq '') {
382 # FIXME: Check if we are going off the end.
383 ++$i;
384 $Tmp = $Arg . $ARGV[$i];
385 }
386 push @CompileOpts,$Tmp;
387 push @LinkOpts,$Tmp;
388 next;
389 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000390
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000391 # Input files.
392 if ($Arg eq '-filelist') {
393 # FIXME: Make sure we aren't walking off the end.
394 open(IN, $ARGV[$i+1]);
395 while (<IN>) { s/\015?\012//; push @Files,$_; }
396 close(IN);
397 ++$i; next;
398 }
399
400 if (!($Arg =~ /^-/)) {
401 push @Files,$Arg; next;
402 }
403}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000404
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000405if ($Action eq 'compile' or $Action eq 'link') {
406 foreach my $file (@Files) {
407 # Determine the language for the file.
408 my $FileLang = $Lang;
409
410 if (!defined($FileLang)) {
411 # Infer the language from the extension.
412 if ($file =~ /[.]([^.]+)$/) {
413 $FileLang = $LangMap{$1};
414 }
415 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000416
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000417 next if (!defined $FileLang);
418
419 my @AnalyzeArgs;
420
421 if ($FileLang ne 'unknown') {
422 push @AnalyzeArgs,'-x';
423 push @AnalyzeArgs,$FileLang;
424 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000425
Zhongxing Xu07c37672008-10-27 14:26:32 +0000426 if (defined $StoreModel) {
427 push @AnalyzeArgs, $StoreModel;
428 }
Ted Kremenekdb4f5f22008-11-04 00:02:53 +0000429
430 if (defined $OutputFormat) {
431 push @AnalyzeArgs, "-analyzer-output-" . $OutputFormat;
432 }
Zhongxing Xu07c37672008-10-27 14:26:32 +0000433
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000434 push @AnalyzeArgs,@CompileOpts;
435 push @AnalyzeArgs,$file;
Zhongxing Xu07c37672008-10-27 14:26:32 +0000436
Ted Kremenek27783eb2008-09-25 20:17:57 +0000437 my @Archs = keys %ArchsSeen;
438 if (scalar @Archs) {
439 foreach my $arch (@Archs) {
440 my @NewArgs;
441 push @NewArgs, '-arch';
442 push @NewArgs, $arch;
443 push @NewArgs, @AnalyzeArgs;
444 Analyze($Clang, \@NewArgs, $FileLang, $Output,
445 $Verbose, $HtmlDir, $file, $Analyses);
446 }
447 }
448 else {
449 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
450 $Verbose, $HtmlDir, $file, $Analyses);
451 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000452 }
453}
Ted Kremenekb0982882008-03-25 22:35:32 +0000454
Ted Kremenek948e06b2008-08-27 22:30:34 +0000455exit($Status >> 8);
456