blob: a669e8924a1addd5836e58eea1f231b650e63f21 [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 {
35 my ($Lang, $file, $Args, $HtmlDir, $ErrorType) = @_;
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;
49}
Ted Kremenekb0982882008-03-25 22:35:32 +000050
Ted Kremenekfbeeca82008-07-19 06:11:04 +000051##----------------------------------------------------------------------------##
52# Running the analyzer.
53##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +000054
Ted Kremenekfbeeca82008-07-19 06:11:04 +000055sub Analyze {
56 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +000057
Ted Kremenekfbeeca82008-07-19 06:11:04 +000058 # Skip anything related to C++.
59 return if ($Lang =~ /c[+][+]/);
Ted Kremenek5d31f832008-08-18 18:38:29 +000060
Ted Kremenekfbeeca82008-07-19 06:11:04 +000061 my $RunAnalyzer = 0;
62 my $Cmd;
63 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +000064 my @CmdArgsSansAnalyses;
Ted Kremenek61cd9882008-05-24 15:58:54 +000065
Ted Kremenekfbeeca82008-07-19 06:11:04 +000066 if ($Lang =~ /header/) {
67 exit 0 if (!defined ($Output));
68 $Cmd = 'cp';
69 push @CmdArgs,$file;
70 # Remove the PCH extension.
71 $Output =~ s/[.]gch$//;
72 push @CmdArgs,$Output;
Ted Kremenek991c54b2008-08-08 20:46:42 +000073 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000074 }
75 else {
76 $Cmd = $Clang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000077 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
78 push @CmdArgs,@$Args;
Ted Kremenek991c54b2008-08-08 20:46:42 +000079 @CmdArgsSansAnalyses = @CmdArgs;
80 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenekfbeeca82008-07-19 06:11:04 +000081 $RunAnalyzer = 1;
82 }
83
84 my @PrintArgs;
85 my $dir;
86
87 if ($Verbose) {
88 $dir = getcwd();
89 print STDERR "\n[LOCATION]: $dir\n";
90 push @PrintArgs,"'$Cmd'";
91 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
92 }
93
94 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +000095 # We MUST print to stderr. Some clients use the stdout output of
96 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +000097 print STDERR join(' ',@PrintArgs);
98 print STDERR "\n";
99 }
100 elsif ($Verbose == 2) {
101 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
102 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000103
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000104 if ($RunAnalyzer and defined($HtmlDir)) {
105 push @CmdArgs,'-o';
106 push @CmdArgs,$HtmlDir;
107 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000108
109 system $Cmd,@CmdArgs;
110
111 # Did the command die because of a signal?
112 if ($? & 127 and $Cmd eq $Clang and defined $HtmlDir) {
Ted Kremenek5d31f832008-08-18 18:38:29 +0000113 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
114 "Crash");
115 }
116 elsif ($?) {
117 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
118 "Parser Rejects");
Ted Kremenek991c54b2008-08-08 20:46:42 +0000119 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000120}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000121
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000122##----------------------------------------------------------------------------##
123# Lookup tables.
124##----------------------------------------------------------------------------##
125
126my %CompileOptionMap = (
127 '-nostdinc' => 0,
128 '-fobjc-gc-only' => 0,
129 '-fobjc-gc' => 0,
130 '-include' => 1,
131 '-idirafter' => 1,
132 '-iprefix' => 1,
133 '-iquote' => 1,
134 '-isystem' => 1,
135 '-iwithprefix' => 1,
136 '-iwithprefixbefore' => 1
137);
138
139my %LinkerOptionMap = (
140 '-framework' => 1
141);
142
143my %CompilerLinkerOptionMap = (
144 '-isysroot' => 1,
145 '-arch' => 1,
146 '-v' => 0
147);
148
149my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000150 '-MT' => 1, # Ignore these preprocessor options.
151 '-MF' => 1,
152
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000153 '-fsyntax-only' => 0,
154 '-save-temps' => 0,
155 '-install_name' => 1,
156 '-exported_symbols_list' => 1,
157 '-current_version' => 1,
158 '-compatibility_version' => 1,
159 '-init' => 1,
160 '-e' => 1,
161 '-seg1addr' => 1,
162 '-bundle_loader' => 1,
163 '-multiply_defined' => 1,
164 '-sectorder' => 3,
165 '--param' => 1,
166 '-u' => 1
167);
168
169my %LangMap = (
170 'c' => 'c',
171 'cpp' => 'c++',
172 'cc' => 'c++',
173 'i' => 'c-cpp-output',
174 'm' => 'objective-c',
175 'mi' => 'objective-c-cpp-output'
176);
177
178##----------------------------------------------------------------------------##
179# Main Logic.
180##----------------------------------------------------------------------------##
181
182my $Action = 'link';
183my @CompileOpts;
184my @LinkOpts;
185my @Files;
186my $Lang;
187my $Output;
188
189# Forward arguments to gcc.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000190my $Status = system($CC,@ARGV);
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000191if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000192
193# Get the analysis options.
194my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
195if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
196
197# Determine the level of verbosity.
198my $Verbose = 0;
199if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
200if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
201
202# Determine what clang executable to use.
203my $Clang = $ENV{'CLANG'};
204if (!defined $Clang) { $Clang = 'clang'; }
205
206# Get the HTML output directory.
207my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
208
209
210# Process the arguments.
211foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
212 my $Arg = $ARGV[$i];
213
214 # Modes ccc-analyzer supports
215 if ($Arg eq '-E') { $Action = 'preprocess'; }
216 elsif ($Arg eq '-c') { $Action = 'compile'; }
217 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenekb0982882008-03-25 22:35:32 +0000218
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000219 # Options with possible arguments that should pass through to compiler.
220 if (defined $CompileOptionMap{$Arg}) {
221 my $Cnt = $CompileOptionMap{$Arg};
222 push @CompileOpts,$Arg;
223 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
224 next;
225 }
226
227 # Options with possible arguments that should pass through to linker.
228 if (defined $LinkerOptionMap{$Arg}) {
229 my $Cnt = $LinkerOptionMap{$Arg};
230 push @LinkOpts,$Arg;
231 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
232 next;
233 }
234
235 # Options with possible arguments that should pass through to both compiler
236 # and the linker.
237 if (defined $CompilerLinkerOptionMap{$Arg}) {
238 my $Cnt = $CompilerLinkerOptionMap{$Arg};
239 push @CompileOpts,$Arg;
240 push @LinkOpts,$Arg;
241 while ($Cnt > 0) {
242 ++$i; --$Cnt;
243 push @CompileOpts, $ARGV[$i];
244 push @LinkOpts, $ARGV[$i];
245 }
246 next;
247 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000248
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000249 # Ignored options.
250 if (defined $IgnoredOptionMap{$Arg}) {
251 my $Cnt = $IgnoredOptionMap{$Arg};
252 while ($Cnt > 0) {
253 ++$i; --$Cnt;
254 }
255 next;
256 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000257
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000258 # Compile mode flags.
259 if ($Arg =~ /^-[D,I,U](.*)$/) {
260 my $Tmp = $Arg;
261 if ($1 eq '') {
262 # FIXME: Check if we are going off the end.
263 ++$i;
264 $Tmp = $Arg . $ARGV[$i];
265 }
266 push @CompileOpts,$Tmp;
267 next;
268 }
269
270 # Language.
271 if ($Arg eq '-x') {
272 $Lang = $ARGV[$i+1];
273 ++$i; next;
274 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000275
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000276 # Output file.
277 if ($Arg eq '-o') {
278 ++$i;
279 $Output = $ARGV[$i];
280 next;
281 }
282
283 # Get the link mode.
284 if ($Arg =~ /^-[l,L,O]/) {
285 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
286 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
287 else { push @LinkOpts,$Arg; }
288 next;
289 }
290
291 if ($Arg =~ /^-std=/) {
292 push @CompileOpts,$Arg;
293 next;
294 }
295
296# if ($Arg =~ /^-f/) {
297# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
298# push @CompileOpts,$Arg;
299# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
300# }
301
302 # Get the compiler/link mode.
303 if ($Arg =~ /^-F(.+)$/) {
304 my $Tmp = $Arg;
305 if ($1 eq '') {
306 # FIXME: Check if we are going off the end.
307 ++$i;
308 $Tmp = $Arg . $ARGV[$i];
309 }
310 push @CompileOpts,$Tmp;
311 push @LinkOpts,$Tmp;
312 next;
313 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000314
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000315 # Input files.
316 if ($Arg eq '-filelist') {
317 # FIXME: Make sure we aren't walking off the end.
318 open(IN, $ARGV[$i+1]);
319 while (<IN>) { s/\015?\012//; push @Files,$_; }
320 close(IN);
321 ++$i; next;
322 }
323
324 if (!($Arg =~ /^-/)) {
325 push @Files,$Arg; next;
326 }
Ted Kremenek5a4ddaf2008-08-25 20:10:45 +0000327
328 exit 0;
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000329}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000330
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000331if ($Action eq 'compile' or $Action eq 'link') {
332 foreach my $file (@Files) {
333 # Determine the language for the file.
334 my $FileLang = $Lang;
335
336 if (!defined($FileLang)) {
337 # Infer the language from the extension.
338 if ($file =~ /[.]([^.]+)$/) {
339 $FileLang = $LangMap{$1};
340 }
341 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000342
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000343 next if (!defined $FileLang);
344
345 my @AnalyzeArgs;
346
347 if ($FileLang ne 'unknown') {
348 push @AnalyzeArgs,'-x';
349 push @AnalyzeArgs,$FileLang;
350 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000351
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000352 push @AnalyzeArgs,@CompileOpts;
353 push @AnalyzeArgs,$file;
354
355 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
356 $Verbose, $HtmlDir, $file, $Analyses);
357 }
358}
Ted Kremenekb0982882008-03-25 22:35:32 +0000359