blob: 823c914203b941796fd9d9d45ee87e3ca8c01c60 [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;
17use Cwd;
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 Kremenek9a3c7da2008-09-04 00:02:34 +000035 my ($Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_;
Ted Kremenek991c54b2008-08-08 20:46:42 +000036 my $Dir = "$HtmlDir/crashes";
37 mkpath $Dir;
38 my ($PPH, $PPFile) = tempfile("clang_crash_XXXXXX",
39 SUFFIX => GetPPExt($Lang),
40 DIR => $Dir);
41
Ted Kremenek2ec5cd52008-08-25 20:44:31 +000042 system $CC, @$Args, "-E", "-o", $PPFile;
Ted Kremenek991c54b2008-08-08 20:46:42 +000043 close ($PPH);
44 open (OUT, ">", "$PPFile.info") or die "Cannot open $PPFile.info\n";
Ted Kremenek5d31f832008-08-18 18:38:29 +000045 print OUT "$file\n";
46 print OUT "$ErrorType\n";
Ted Kremenek2dd7ad12008-08-18 20:55:25 +000047 print OUT "@$Args\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +000048 close OUT;
Ted Kremenek01479d02008-09-04 00:41:45 +000049 `uname -a >> $PPFile.info 2>&1`;
50 `$CC -v >> $PPFile.info 2>&1`;
Ted Kremenek9a3c7da2008-09-04 00:02:34 +000051 system 'mv',$ofile,"$PPFile.output";
Ted Kremenek991c54b2008-08-08 20:46:42 +000052}
Ted Kremenekb0982882008-03-25 22:35:32 +000053
Ted Kremenekfbeeca82008-07-19 06:11:04 +000054##----------------------------------------------------------------------------##
55# Running the analyzer.
56##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +000057
Ted Kremenekfbeeca82008-07-19 06:11:04 +000058sub Analyze {
59 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +000060
Ted Kremenekfbeeca82008-07-19 06:11:04 +000061 # Skip anything related to C++.
62 return if ($Lang =~ /c[+][+]/);
Ted Kremenek5d31f832008-08-18 18:38:29 +000063
Ted Kremenekfbeeca82008-07-19 06:11:04 +000064 my $RunAnalyzer = 0;
65 my $Cmd;
66 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +000067 my @CmdArgsSansAnalyses;
Ted Kremenek61cd9882008-05-24 15:58:54 +000068
Ted Kremenekfbeeca82008-07-19 06:11:04 +000069 if ($Lang =~ /header/) {
70 exit 0 if (!defined ($Output));
71 $Cmd = 'cp';
72 push @CmdArgs,$file;
73 # Remove the PCH extension.
74 $Output =~ s/[.]gch$//;
75 push @CmdArgs,$Output;
Ted Kremenek991c54b2008-08-08 20:46:42 +000076 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000077 }
78 else {
79 $Cmd = $Clang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000080 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
81 push @CmdArgs,@$Args;
Ted Kremenek991c54b2008-08-08 20:46:42 +000082 @CmdArgsSansAnalyses = @CmdArgs;
83 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenekfbeeca82008-07-19 06:11:04 +000084 $RunAnalyzer = 1;
85 }
86
87 my @PrintArgs;
88 my $dir;
89
90 if ($Verbose) {
91 $dir = getcwd();
92 print STDERR "\n[LOCATION]: $dir\n";
93 push @PrintArgs,"'$Cmd'";
94 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
95 }
96
97 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +000098 # We MUST print to stderr. Some clients use the stdout output of
99 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000100 print STDERR join(' ',@PrintArgs);
101 print STDERR "\n";
102 }
103 elsif ($Verbose == 2) {
104 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
105 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000106
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000107 if ($RunAnalyzer and defined($HtmlDir)) {
108 push @CmdArgs,'-o';
109 push @CmdArgs,$HtmlDir;
110 }
Ted Kremenek948e06b2008-08-27 22:30:34 +0000111
112 if (defined $ENV{'CCC_UBI'}) {
113 push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
114 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000115
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000116 # Capture the STDERR of clang and send it to a temporary file.
117 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
118 # We save the output file in the 'crashes' directory if clang encounters
119 # any problems with the file.
120 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
121 my $pid = fork();
122 if ($pid == 0) {
123 open(STDOUT,">&", \*STDERR);
124 open(STDERR,">&", $ofh);
125 exec $Cmd, @CmdArgs;
126 }
127 close ($ofh);
128 wait;
129 my $Result = $?;
130
131 # Did the command die because of a signal?
132 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
133 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
134 "Crash", $ofile);
135 }
136 elsif ($Result) {
137 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
138 "Parser Rejects", $ofile);
139 }
140
141 `rm -f $ofile`;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000142}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000143
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000144##----------------------------------------------------------------------------##
145# Lookup tables.
146##----------------------------------------------------------------------------##
147
148my %CompileOptionMap = (
149 '-nostdinc' => 0,
150 '-fobjc-gc-only' => 0,
151 '-fobjc-gc' => 0,
152 '-include' => 1,
153 '-idirafter' => 1,
154 '-iprefix' => 1,
155 '-iquote' => 1,
156 '-isystem' => 1,
157 '-iwithprefix' => 1,
158 '-iwithprefixbefore' => 1
159);
160
161my %LinkerOptionMap = (
162 '-framework' => 1
163);
164
165my %CompilerLinkerOptionMap = (
166 '-isysroot' => 1,
167 '-arch' => 1,
168 '-v' => 0
169);
170
171my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000172 '-MT' => 1, # Ignore these preprocessor options.
173 '-MF' => 1,
174
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000175 '-fsyntax-only' => 0,
176 '-save-temps' => 0,
177 '-install_name' => 1,
178 '-exported_symbols_list' => 1,
179 '-current_version' => 1,
180 '-compatibility_version' => 1,
181 '-init' => 1,
182 '-e' => 1,
183 '-seg1addr' => 1,
184 '-bundle_loader' => 1,
185 '-multiply_defined' => 1,
186 '-sectorder' => 3,
187 '--param' => 1,
188 '-u' => 1
189);
190
191my %LangMap = (
192 'c' => 'c',
193 'cpp' => 'c++',
194 'cc' => 'c++',
195 'i' => 'c-cpp-output',
196 'm' => 'objective-c',
197 'mi' => 'objective-c-cpp-output'
198);
199
200##----------------------------------------------------------------------------##
201# Main Logic.
202##----------------------------------------------------------------------------##
203
204my $Action = 'link';
205my @CompileOpts;
206my @LinkOpts;
207my @Files;
208my $Lang;
209my $Output;
210
211# Forward arguments to gcc.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000212my $Status = system($CC,@ARGV);
Ted Kremenekcb344d02008-08-28 01:18:44 +0000213if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000214
215# Get the analysis options.
216my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
217if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
218
219# Determine the level of verbosity.
220my $Verbose = 0;
221if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
222if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
223
224# Determine what clang executable to use.
225my $Clang = $ENV{'CLANG'};
226if (!defined $Clang) { $Clang = 'clang'; }
227
228# Get the HTML output directory.
229my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
230
231
232# Process the arguments.
233foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
234 my $Arg = $ARGV[$i];
235
236 # Modes ccc-analyzer supports
237 if ($Arg eq '-E') { $Action = 'preprocess'; }
238 elsif ($Arg eq '-c') { $Action = 'compile'; }
239 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenekb0982882008-03-25 22:35:32 +0000240
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000241 # Options with possible arguments that should pass through to compiler.
242 if (defined $CompileOptionMap{$Arg}) {
243 my $Cnt = $CompileOptionMap{$Arg};
244 push @CompileOpts,$Arg;
245 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
246 next;
247 }
248
249 # Options with possible arguments that should pass through to linker.
250 if (defined $LinkerOptionMap{$Arg}) {
251 my $Cnt = $LinkerOptionMap{$Arg};
252 push @LinkOpts,$Arg;
253 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
254 next;
255 }
256
257 # Options with possible arguments that should pass through to both compiler
258 # and the linker.
259 if (defined $CompilerLinkerOptionMap{$Arg}) {
260 my $Cnt = $CompilerLinkerOptionMap{$Arg};
261 push @CompileOpts,$Arg;
262 push @LinkOpts,$Arg;
263 while ($Cnt > 0) {
264 ++$i; --$Cnt;
265 push @CompileOpts, $ARGV[$i];
266 push @LinkOpts, $ARGV[$i];
267 }
268 next;
269 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000270
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000271 # Ignored options.
272 if (defined $IgnoredOptionMap{$Arg}) {
273 my $Cnt = $IgnoredOptionMap{$Arg};
274 while ($Cnt > 0) {
275 ++$i; --$Cnt;
276 }
277 next;
278 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000279
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000280 # Compile mode flags.
281 if ($Arg =~ /^-[D,I,U](.*)$/) {
282 my $Tmp = $Arg;
283 if ($1 eq '') {
284 # FIXME: Check if we are going off the end.
285 ++$i;
286 $Tmp = $Arg . $ARGV[$i];
287 }
288 push @CompileOpts,$Tmp;
289 next;
290 }
291
292 # Language.
293 if ($Arg eq '-x') {
294 $Lang = $ARGV[$i+1];
295 ++$i; next;
296 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000297
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000298 # Output file.
299 if ($Arg eq '-o') {
300 ++$i;
301 $Output = $ARGV[$i];
302 next;
303 }
304
305 # Get the link mode.
306 if ($Arg =~ /^-[l,L,O]/) {
307 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
308 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
309 else { push @LinkOpts,$Arg; }
310 next;
311 }
312
313 if ($Arg =~ /^-std=/) {
314 push @CompileOpts,$Arg;
315 next;
316 }
317
318# if ($Arg =~ /^-f/) {
319# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
320# push @CompileOpts,$Arg;
321# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
322# }
323
324 # Get the compiler/link mode.
325 if ($Arg =~ /^-F(.+)$/) {
326 my $Tmp = $Arg;
327 if ($1 eq '') {
328 # FIXME: Check if we are going off the end.
329 ++$i;
330 $Tmp = $Arg . $ARGV[$i];
331 }
332 push @CompileOpts,$Tmp;
333 push @LinkOpts,$Tmp;
334 next;
335 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000336
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000337 # Input files.
338 if ($Arg eq '-filelist') {
339 # FIXME: Make sure we aren't walking off the end.
340 open(IN, $ARGV[$i+1]);
341 while (<IN>) { s/\015?\012//; push @Files,$_; }
342 close(IN);
343 ++$i; next;
344 }
345
346 if (!($Arg =~ /^-/)) {
347 push @Files,$Arg; next;
348 }
349}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000350
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000351if ($Action eq 'compile' or $Action eq 'link') {
352 foreach my $file (@Files) {
353 # Determine the language for the file.
354 my $FileLang = $Lang;
355
356 if (!defined($FileLang)) {
357 # Infer the language from the extension.
358 if ($file =~ /[.]([^.]+)$/) {
359 $FileLang = $LangMap{$1};
360 }
361 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000362
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000363 next if (!defined $FileLang);
364
365 my @AnalyzeArgs;
366
367 if ($FileLang ne 'unknown') {
368 push @AnalyzeArgs,'-x';
369 push @AnalyzeArgs,$FileLang;
370 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000371
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000372 push @AnalyzeArgs,@CompileOpts;
373 push @AnalyzeArgs,$file;
374
375 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
376 $Verbose, $HtmlDir, $file, $Analyses);
377 }
378}
Ted Kremenekb0982882008-03-25 22:35:32 +0000379
Ted Kremenek948e06b2008-08-27 22:30:34 +0000380exit($Status >> 8);
381
382