blob: 33888649c2baf87a8329a70e0a1ad83307e391ed [file] [log] [blame]
Ted Kremenek2c67d1b2008-07-19 06:11:04 +00001#!/usr/bin/env perl
Ted Kremenek18e72bb2008-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 Kremenek2c67d1b2008-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 Kremenek18e72bb2008-03-25 22:35:32 +000012#
13##===----------------------------------------------------------------------===##
14
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000015use strict;
16use warnings;
Ted Kremenekc4f22f62008-09-21 19:56:14 +000017use Cwd qw/ getcwd abs_path /;
Ted Kremenek61c656b2008-08-08 20:46:42 +000018use File::Temp qw/ tempfile /;
19use File::Path qw / mkpath /;
Ted Kremeneka9c88eb2008-08-25 20:44:31 +000020
21my $CC = $ENV{'CCC_CC'};
22if (!defined $CC) { $CC = "gcc"; }
Ted Kremenek61c656b2008-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 Kremenekcd7c9202008-08-18 18:38:29 +000034sub ProcessClangFailure {
Ted Kremenekb78ff2b2008-09-25 00:51:44 +000035 my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
Ted Kremenek61c656b2008-08-08 20:46:42 +000036 my $Dir = "$HtmlDir/crashes";
37 mkpath $Dir;
Ted Kremenekb78ff2b2008-09-25 00:51:44 +000038
39 # Generate the preprocessed file with cc (i.e., gcc).
Ted Kremenek61c656b2008-08-08 20:46:42 +000040 my ($PPH, $PPFile) = tempfile("clang_crash_XXXXXX",
41 SUFFIX => GetPPExt($Lang),
42 DIR => $Dir);
43
Ted Kremeneka9c88eb2008-08-25 20:44:31 +000044 system $CC, @$Args, "-E", "-o", $PPFile;
Ted Kremenek61c656b2008-08-08 20:46:42 +000045 close ($PPH);
Ted Kremenekb78ff2b2008-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 Kremenek5ea73062008-09-25 00:25:16 +000053 open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n";
Ted Kremenek5e3d2d12008-09-21 18:04:49 +000054 print OUT abs_path($file), "\n";
Ted Kremenekcd7c9202008-08-18 18:38:29 +000055 print OUT "$ErrorType\n";
Ted Kremenekd367e822008-08-18 20:55:25 +000056 print OUT "@$Args\n";
Ted Kremenek61c656b2008-08-08 20:46:42 +000057 close OUT;
Ted Kremenek5ea73062008-09-25 00:25:16 +000058 `uname -a >> $PPFile.info.txt 2>&1`;
59 `$CC -v >> $PPFile.info.txt 2>&1`;
Ted Kremenek4622abf2008-09-12 22:49:36 +000060 system 'mv',$ofile,"$PPFile.stderr.txt";
Ted Kremenek61c656b2008-08-08 20:46:42 +000061}
Ted Kremenek18e72bb2008-03-25 22:35:32 +000062
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000063##----------------------------------------------------------------------------##
64# Running the analyzer.
65##----------------------------------------------------------------------------##
Ted Kremenek18e72bb2008-03-25 22:35:32 +000066
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000067sub Analyze {
68 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeon877866b2008-04-04 11:02:21 +000069
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000070 # Skip anything related to C++.
71 return if ($Lang =~ /c[+][+]/);
Ted Kremenekcd7c9202008-08-18 18:38:29 +000072
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000073 my $RunAnalyzer = 0;
74 my $Cmd;
75 my @CmdArgs;
Ted Kremenek61c656b2008-08-08 20:46:42 +000076 my @CmdArgsSansAnalyses;
Ted Kremenek5696dfe2008-05-24 15:58:54 +000077
Ted Kremenek2c67d1b2008-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 Kremenek61c656b2008-08-08 20:46:42 +000085 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000086 }
87 else {
88 $Cmd = $Clang;
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000089 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
90 push @CmdArgs,@$Args;
Ted Kremenek61c656b2008-08-08 20:46:42 +000091 @CmdArgsSansAnalyses = @CmdArgs;
92 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenek2c67d1b2008-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 Kremenek5696dfe2008-05-24 15:58:54 +0000107 # We MUST print to stderr. Some clients use the stdout output of
108 # gcc for various purposes.
Ted Kremenek2c67d1b2008-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 Kremenek5696dfe2008-05-24 15:58:54 +0000115
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000116 if ($RunAnalyzer and defined($HtmlDir)) {
117 push @CmdArgs,'-o';
118 push @CmdArgs,$HtmlDir;
119 }
Ted Kremenekd973f662008-08-27 22:30:34 +0000120
121 if (defined $ENV{'CCC_UBI'}) {
122 push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
123 }
Ted Kremenek61c656b2008-08-08 20:46:42 +0000124
Ted Kremenek0a662f92008-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 Kremenek243f6b62008-09-11 23:05:26 +0000129 pipe (FROM_CHILD, TO_PARENT);
Ted Kremenek0a662f92008-09-04 00:02:34 +0000130 my $pid = fork();
131 if ($pid == 0) {
Ted Kremenek243f6b62008-09-11 23:05:26 +0000132 close FROM_CHILD;
133 open(STDOUT,">&", \*TO_PARENT);
134 open(STDERR,">&", \*TO_PARENT);
Ted Kremenek0a662f92008-09-04 00:02:34 +0000135 exec $Cmd, @CmdArgs;
136 }
Ted Kremenek243f6b62008-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 Kremenek0a662f92008-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 Kremenekb78ff2b2008-09-25 00:51:44 +0000151 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek0a662f92008-09-04 00:02:34 +0000152 "Crash", $ofile);
153 }
154 elsif ($Result) {
Ted Kremenekb78ff2b2008-09-25 00:51:44 +0000155 ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
Ted Kremenek0a662f92008-09-04 00:02:34 +0000156 "Parser Rejects", $ofile);
157 }
158
159 `rm -f $ofile`;
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000160}
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000161
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000162##----------------------------------------------------------------------------##
163# Lookup tables.
164##----------------------------------------------------------------------------##
165
166my %CompileOptionMap = (
167 '-nostdinc' => 0,
168 '-fobjc-gc-only' => 0,
169 '-fobjc-gc' => 0,
170 '-include' => 1,
171 '-idirafter' => 1,
172 '-iprefix' => 1,
173 '-iquote' => 1,
174 '-isystem' => 1,
175 '-iwithprefix' => 1,
176 '-iwithprefixbefore' => 1
177);
178
179my %LinkerOptionMap = (
180 '-framework' => 1
181);
182
183my %CompilerLinkerOptionMap = (
184 '-isysroot' => 1,
185 '-arch' => 1,
186 '-v' => 0
187);
188
189my %IgnoredOptionMap = (
Ted Kremenek22128c62008-07-24 03:52:21 +0000190 '-MT' => 1, # Ignore these preprocessor options.
191 '-MF' => 1,
192
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000193 '-fsyntax-only' => 0,
194 '-save-temps' => 0,
195 '-install_name' => 1,
196 '-exported_symbols_list' => 1,
197 '-current_version' => 1,
198 '-compatibility_version' => 1,
199 '-init' => 1,
200 '-e' => 1,
201 '-seg1addr' => 1,
202 '-bundle_loader' => 1,
203 '-multiply_defined' => 1,
204 '-sectorder' => 3,
205 '--param' => 1,
206 '-u' => 1
207);
208
209my %LangMap = (
210 'c' => 'c',
211 'cpp' => 'c++',
212 'cc' => 'c++',
213 'i' => 'c-cpp-output',
214 'm' => 'objective-c',
215 'mi' => 'objective-c-cpp-output'
216);
217
Ted Kremenek6bedfef2008-09-29 16:15:20 +0000218my %UniqueOptions = (
219 '-isysroot' => 0
220);
221
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000222##----------------------------------------------------------------------------##
223# Main Logic.
224##----------------------------------------------------------------------------##
225
226my $Action = 'link';
227my @CompileOpts;
228my @LinkOpts;
229my @Files;
230my $Lang;
231my $Output;
Ted Kremenek6bedfef2008-09-29 16:15:20 +0000232my %Uniqued;
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000233
234# Forward arguments to gcc.
Ted Kremenek91ec36e2008-08-21 21:47:09 +0000235my $Status = system($CC,@ARGV);
Ted Kremenek07404d22008-08-28 01:18:44 +0000236if ($Status) { exit($Status >> 8); }
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000237
238# Get the analysis options.
239my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
240if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
241
242# Determine the level of verbosity.
243my $Verbose = 0;
244if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
245if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
246
247# Determine what clang executable to use.
248my $Clang = $ENV{'CLANG'};
249if (!defined $Clang) { $Clang = 'clang'; }
250
251# Get the HTML output directory.
252my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
253
Ted Kremenek06b9cad2008-09-25 20:17:57 +0000254my %ArchsSeen;
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000255
256# Process the arguments.
257foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
258 my $Arg = $ARGV[$i];
259
260 # Modes ccc-analyzer supports
261 if ($Arg eq '-E') { $Action = 'preprocess'; }
262 elsif ($Arg eq '-c') { $Action = 'compile'; }
263 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek06b9cad2008-09-25 20:17:57 +0000264
265 # Specially handle duplicate cases of -arch
266 if ($Arg eq "-arch") {
267 my $arch = $ARGV[$i+1];
268 $ArchsSeen{$arch} = 1;
269 ++$i;
270 next;
271 }
272
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000273 # Options with possible arguments that should pass through to compiler.
274 if (defined $CompileOptionMap{$Arg}) {
275 my $Cnt = $CompileOptionMap{$Arg};
276 push @CompileOpts,$Arg;
277 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
278 next;
279 }
280
281 # Options with possible arguments that should pass through to linker.
282 if (defined $LinkerOptionMap{$Arg}) {
283 my $Cnt = $LinkerOptionMap{$Arg};
284 push @LinkOpts,$Arg;
285 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
286 next;
287 }
288
289 # Options with possible arguments that should pass through to both compiler
290 # and the linker.
291 if (defined $CompilerLinkerOptionMap{$Arg}) {
292 my $Cnt = $CompilerLinkerOptionMap{$Arg};
293 push @CompileOpts,$Arg;
294 push @LinkOpts,$Arg;
Ted Kremenek6bedfef2008-09-29 16:15:20 +0000295
296 # Check if this is an option that should have a unique value, and if so
297 # determine if the value was checked before.
298 if ($UniqueOptions{$Arg}) {
299 if (defined $Uniqued{$Arg}) {
300 $i += $Cnt;
301 next;
302 }
303 $Uniqued{$Arg} = 1;
304 }
305
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000306 while ($Cnt > 0) {
307 ++$i; --$Cnt;
308 push @CompileOpts, $ARGV[$i];
309 push @LinkOpts, $ARGV[$i];
310 }
311 next;
312 }
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000313
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000314 # Ignored options.
315 if (defined $IgnoredOptionMap{$Arg}) {
316 my $Cnt = $IgnoredOptionMap{$Arg};
317 while ($Cnt > 0) {
318 ++$i; --$Cnt;
319 }
320 next;
321 }
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000322
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000323 # Compile mode flags.
324 if ($Arg =~ /^-[D,I,U](.*)$/) {
325 my $Tmp = $Arg;
326 if ($1 eq '') {
327 # FIXME: Check if we are going off the end.
328 ++$i;
329 $Tmp = $Arg . $ARGV[$i];
330 }
331 push @CompileOpts,$Tmp;
332 next;
333 }
334
335 # Language.
336 if ($Arg eq '-x') {
337 $Lang = $ARGV[$i+1];
338 ++$i; next;
339 }
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000340
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000341 # Output file.
342 if ($Arg eq '-o') {
343 ++$i;
344 $Output = $ARGV[$i];
345 next;
346 }
347
348 # Get the link mode.
349 if ($Arg =~ /^-[l,L,O]/) {
350 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
351 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
352 else { push @LinkOpts,$Arg; }
353 next;
354 }
355
356 if ($Arg =~ /^-std=/) {
357 push @CompileOpts,$Arg;
358 next;
359 }
360
361# if ($Arg =~ /^-f/) {
362# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
363# push @CompileOpts,$Arg;
364# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
365# }
366
367 # Get the compiler/link mode.
368 if ($Arg =~ /^-F(.+)$/) {
369 my $Tmp = $Arg;
370 if ($1 eq '') {
371 # FIXME: Check if we are going off the end.
372 ++$i;
373 $Tmp = $Arg . $ARGV[$i];
374 }
375 push @CompileOpts,$Tmp;
376 push @LinkOpts,$Tmp;
377 next;
378 }
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000379
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000380 # Input files.
381 if ($Arg eq '-filelist') {
382 # FIXME: Make sure we aren't walking off the end.
383 open(IN, $ARGV[$i+1]);
384 while (<IN>) { s/\015?\012//; push @Files,$_; }
385 close(IN);
386 ++$i; next;
387 }
388
389 if (!($Arg =~ /^-/)) {
390 push @Files,$Arg; next;
391 }
392}
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000393
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000394if ($Action eq 'compile' or $Action eq 'link') {
395 foreach my $file (@Files) {
396 # Determine the language for the file.
397 my $FileLang = $Lang;
398
399 if (!defined($FileLang)) {
400 # Infer the language from the extension.
401 if ($file =~ /[.]([^.]+)$/) {
402 $FileLang = $LangMap{$1};
403 }
404 }
Ted Kremenek5979b082008-05-14 20:10:33 +0000405
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000406 next if (!defined $FileLang);
407
408 my @AnalyzeArgs;
409
410 if ($FileLang ne 'unknown') {
411 push @AnalyzeArgs,'-x';
412 push @AnalyzeArgs,$FileLang;
413 }
Ted Kremenek18e72bb2008-03-25 22:35:32 +0000414
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000415 push @AnalyzeArgs,@CompileOpts;
416 push @AnalyzeArgs,$file;
417
Ted Kremenek06b9cad2008-09-25 20:17:57 +0000418 my @Archs = keys %ArchsSeen;
419 if (scalar @Archs) {
420 foreach my $arch (@Archs) {
421 my @NewArgs;
422 push @NewArgs, '-arch';
423 push @NewArgs, $arch;
424 push @NewArgs, @AnalyzeArgs;
425 Analyze($Clang, \@NewArgs, $FileLang, $Output,
426 $Verbose, $HtmlDir, $file, $Analyses);
427 }
428 }
429 else {
430 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
431 $Verbose, $HtmlDir, $file, $Analyses);
432 }
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000433 }
434}
Ted Kremenek18e72bb2008-03-25 22:35:32 +0000435
Ted Kremenekd973f662008-08-27 22:30:34 +0000436exit($Status >> 8);
437