blob: 323d529fa8e7e2791fa34da78fdb876aad5f4f35 [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 Kremenek9a3c7da2008-09-04 00:02:34 +000049 system 'mv',$ofile,"$PPFile.output";
Ted Kremenek991c54b2008-08-08 20:46:42 +000050}
Ted Kremenekb0982882008-03-25 22:35:32 +000051
Ted Kremenekfbeeca82008-07-19 06:11:04 +000052##----------------------------------------------------------------------------##
53# Running the analyzer.
54##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +000055
Ted Kremenekfbeeca82008-07-19 06:11:04 +000056sub Analyze {
57 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +000058
Ted Kremenekfbeeca82008-07-19 06:11:04 +000059 # Skip anything related to C++.
60 return if ($Lang =~ /c[+][+]/);
Ted Kremenek5d31f832008-08-18 18:38:29 +000061
Ted Kremenekfbeeca82008-07-19 06:11:04 +000062 my $RunAnalyzer = 0;
63 my $Cmd;
64 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +000065 my @CmdArgsSansAnalyses;
Ted Kremenek61cd9882008-05-24 15:58:54 +000066
Ted Kremenekfbeeca82008-07-19 06:11:04 +000067 if ($Lang =~ /header/) {
68 exit 0 if (!defined ($Output));
69 $Cmd = 'cp';
70 push @CmdArgs,$file;
71 # Remove the PCH extension.
72 $Output =~ s/[.]gch$//;
73 push @CmdArgs,$Output;
Ted Kremenek991c54b2008-08-08 20:46:42 +000074 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000075 }
76 else {
77 $Cmd = $Clang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000078 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
79 push @CmdArgs,@$Args;
Ted Kremenek991c54b2008-08-08 20:46:42 +000080 @CmdArgsSansAnalyses = @CmdArgs;
81 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenekfbeeca82008-07-19 06:11:04 +000082 $RunAnalyzer = 1;
83 }
84
85 my @PrintArgs;
86 my $dir;
87
88 if ($Verbose) {
89 $dir = getcwd();
90 print STDERR "\n[LOCATION]: $dir\n";
91 push @PrintArgs,"'$Cmd'";
92 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
93 }
94
95 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +000096 # We MUST print to stderr. Some clients use the stdout output of
97 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +000098 print STDERR join(' ',@PrintArgs);
99 print STDERR "\n";
100 }
101 elsif ($Verbose == 2) {
102 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
103 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000104
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000105 if ($RunAnalyzer and defined($HtmlDir)) {
106 push @CmdArgs,'-o';
107 push @CmdArgs,$HtmlDir;
108 }
Ted Kremenek948e06b2008-08-27 22:30:34 +0000109
110 if (defined $ENV{'CCC_UBI'}) {
111 push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
112 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000113
Ted Kremenek9a3c7da2008-09-04 00:02:34 +0000114 # Capture the STDERR of clang and send it to a temporary file.
115 # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR.
116 # We save the output file in the 'crashes' directory if clang encounters
117 # any problems with the file.
118 my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir);
119 my $pid = fork();
120 if ($pid == 0) {
121 open(STDOUT,">&", \*STDERR);
122 open(STDERR,">&", $ofh);
123 exec $Cmd, @CmdArgs;
124 }
125 close ($ofh);
126 wait;
127 my $Result = $?;
128
129 # Did the command die because of a signal?
130 if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) {
131 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
132 "Crash", $ofile);
133 }
134 elsif ($Result) {
135 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
136 "Parser Rejects", $ofile);
137 }
138
139 `rm -f $ofile`;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000140}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000141
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000142##----------------------------------------------------------------------------##
143# Lookup tables.
144##----------------------------------------------------------------------------##
145
146my %CompileOptionMap = (
147 '-nostdinc' => 0,
148 '-fobjc-gc-only' => 0,
149 '-fobjc-gc' => 0,
150 '-include' => 1,
151 '-idirafter' => 1,
152 '-iprefix' => 1,
153 '-iquote' => 1,
154 '-isystem' => 1,
155 '-iwithprefix' => 1,
156 '-iwithprefixbefore' => 1
157);
158
159my %LinkerOptionMap = (
160 '-framework' => 1
161);
162
163my %CompilerLinkerOptionMap = (
164 '-isysroot' => 1,
165 '-arch' => 1,
166 '-v' => 0
167);
168
169my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000170 '-MT' => 1, # Ignore these preprocessor options.
171 '-MF' => 1,
172
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000173 '-fsyntax-only' => 0,
174 '-save-temps' => 0,
175 '-install_name' => 1,
176 '-exported_symbols_list' => 1,
177 '-current_version' => 1,
178 '-compatibility_version' => 1,
179 '-init' => 1,
180 '-e' => 1,
181 '-seg1addr' => 1,
182 '-bundle_loader' => 1,
183 '-multiply_defined' => 1,
184 '-sectorder' => 3,
185 '--param' => 1,
186 '-u' => 1
187);
188
189my %LangMap = (
190 'c' => 'c',
191 'cpp' => 'c++',
192 'cc' => 'c++',
193 'i' => 'c-cpp-output',
194 'm' => 'objective-c',
195 'mi' => 'objective-c-cpp-output'
196);
197
198##----------------------------------------------------------------------------##
199# Main Logic.
200##----------------------------------------------------------------------------##
201
202my $Action = 'link';
203my @CompileOpts;
204my @LinkOpts;
205my @Files;
206my $Lang;
207my $Output;
208
209# Forward arguments to gcc.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000210my $Status = system($CC,@ARGV);
Ted Kremenekcb344d02008-08-28 01:18:44 +0000211if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000212
213# Get the analysis options.
214my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
215if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
216
217# Determine the level of verbosity.
218my $Verbose = 0;
219if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
220if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
221
222# Determine what clang executable to use.
223my $Clang = $ENV{'CLANG'};
224if (!defined $Clang) { $Clang = 'clang'; }
225
226# Get the HTML output directory.
227my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
228
229
230# Process the arguments.
231foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
232 my $Arg = $ARGV[$i];
233
234 # Modes ccc-analyzer supports
235 if ($Arg eq '-E') { $Action = 'preprocess'; }
236 elsif ($Arg eq '-c') { $Action = 'compile'; }
237 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenekb0982882008-03-25 22:35:32 +0000238
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000239 # Options with possible arguments that should pass through to compiler.
240 if (defined $CompileOptionMap{$Arg}) {
241 my $Cnt = $CompileOptionMap{$Arg};
242 push @CompileOpts,$Arg;
243 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
244 next;
245 }
246
247 # Options with possible arguments that should pass through to linker.
248 if (defined $LinkerOptionMap{$Arg}) {
249 my $Cnt = $LinkerOptionMap{$Arg};
250 push @LinkOpts,$Arg;
251 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
252 next;
253 }
254
255 # Options with possible arguments that should pass through to both compiler
256 # and the linker.
257 if (defined $CompilerLinkerOptionMap{$Arg}) {
258 my $Cnt = $CompilerLinkerOptionMap{$Arg};
259 push @CompileOpts,$Arg;
260 push @LinkOpts,$Arg;
261 while ($Cnt > 0) {
262 ++$i; --$Cnt;
263 push @CompileOpts, $ARGV[$i];
264 push @LinkOpts, $ARGV[$i];
265 }
266 next;
267 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000268
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000269 # Ignored options.
270 if (defined $IgnoredOptionMap{$Arg}) {
271 my $Cnt = $IgnoredOptionMap{$Arg};
272 while ($Cnt > 0) {
273 ++$i; --$Cnt;
274 }
275 next;
276 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000277
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000278 # Compile mode flags.
279 if ($Arg =~ /^-[D,I,U](.*)$/) {
280 my $Tmp = $Arg;
281 if ($1 eq '') {
282 # FIXME: Check if we are going off the end.
283 ++$i;
284 $Tmp = $Arg . $ARGV[$i];
285 }
286 push @CompileOpts,$Tmp;
287 next;
288 }
289
290 # Language.
291 if ($Arg eq '-x') {
292 $Lang = $ARGV[$i+1];
293 ++$i; next;
294 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000295
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000296 # Output file.
297 if ($Arg eq '-o') {
298 ++$i;
299 $Output = $ARGV[$i];
300 next;
301 }
302
303 # Get the link mode.
304 if ($Arg =~ /^-[l,L,O]/) {
305 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
306 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
307 else { push @LinkOpts,$Arg; }
308 next;
309 }
310
311 if ($Arg =~ /^-std=/) {
312 push @CompileOpts,$Arg;
313 next;
314 }
315
316# if ($Arg =~ /^-f/) {
317# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
318# push @CompileOpts,$Arg;
319# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
320# }
321
322 # Get the compiler/link mode.
323 if ($Arg =~ /^-F(.+)$/) {
324 my $Tmp = $Arg;
325 if ($1 eq '') {
326 # FIXME: Check if we are going off the end.
327 ++$i;
328 $Tmp = $Arg . $ARGV[$i];
329 }
330 push @CompileOpts,$Tmp;
331 push @LinkOpts,$Tmp;
332 next;
333 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000334
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000335 # Input files.
336 if ($Arg eq '-filelist') {
337 # FIXME: Make sure we aren't walking off the end.
338 open(IN, $ARGV[$i+1]);
339 while (<IN>) { s/\015?\012//; push @Files,$_; }
340 close(IN);
341 ++$i; next;
342 }
343
344 if (!($Arg =~ /^-/)) {
345 push @Files,$Arg; next;
346 }
347}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000348
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000349if ($Action eq 'compile' or $Action eq 'link') {
350 foreach my $file (@Files) {
351 # Determine the language for the file.
352 my $FileLang = $Lang;
353
354 if (!defined($FileLang)) {
355 # Infer the language from the extension.
356 if ($file =~ /[.]([^.]+)$/) {
357 $FileLang = $LangMap{$1};
358 }
359 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000360
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000361 next if (!defined $FileLang);
362
363 my @AnalyzeArgs;
364
365 if ($FileLang ne 'unknown') {
366 push @AnalyzeArgs,'-x';
367 push @AnalyzeArgs,$FileLang;
368 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000369
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000370 push @AnalyzeArgs,@CompileOpts;
371 push @AnalyzeArgs,$file;
372
373 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
374 $Verbose, $HtmlDir, $file, $Analyses);
375 }
376}
Ted Kremenekb0982882008-03-25 22:35:32 +0000377
Ted Kremenek948e06b2008-08-27 22:30:34 +0000378exit($Status >> 8);
379
380