blob: 6aa03bc9220e33a95f3eb0d25e5b00da1f9749da [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 Kremenek948e06b2008-08-27 22:30:34 +0000108
109 if (defined $ENV{'CCC_UBI'}) {
110 push @CmdArgs,"--analyzer-viz-egraph-ubigraph";
111 }
112
113 system $Cmd,@CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +0000114
115 # Did the command die because of a signal?
116 if ($? & 127 and $Cmd eq $Clang and defined $HtmlDir) {
Ted Kremenek5d31f832008-08-18 18:38:29 +0000117 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
118 "Crash");
119 }
120 elsif ($?) {
121 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
122 "Parser Rejects");
Ted Kremenek991c54b2008-08-08 20:46:42 +0000123 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000124}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000125
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000126##----------------------------------------------------------------------------##
127# Lookup tables.
128##----------------------------------------------------------------------------##
129
130my %CompileOptionMap = (
131 '-nostdinc' => 0,
132 '-fobjc-gc-only' => 0,
133 '-fobjc-gc' => 0,
134 '-include' => 1,
135 '-idirafter' => 1,
136 '-iprefix' => 1,
137 '-iquote' => 1,
138 '-isystem' => 1,
139 '-iwithprefix' => 1,
140 '-iwithprefixbefore' => 1
141);
142
143my %LinkerOptionMap = (
144 '-framework' => 1
145);
146
147my %CompilerLinkerOptionMap = (
148 '-isysroot' => 1,
149 '-arch' => 1,
150 '-v' => 0
151);
152
153my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000154 '-MT' => 1, # Ignore these preprocessor options.
155 '-MF' => 1,
156
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000157 '-fsyntax-only' => 0,
158 '-save-temps' => 0,
159 '-install_name' => 1,
160 '-exported_symbols_list' => 1,
161 '-current_version' => 1,
162 '-compatibility_version' => 1,
163 '-init' => 1,
164 '-e' => 1,
165 '-seg1addr' => 1,
166 '-bundle_loader' => 1,
167 '-multiply_defined' => 1,
168 '-sectorder' => 3,
169 '--param' => 1,
170 '-u' => 1
171);
172
173my %LangMap = (
174 'c' => 'c',
175 'cpp' => 'c++',
176 'cc' => 'c++',
177 'i' => 'c-cpp-output',
178 'm' => 'objective-c',
179 'mi' => 'objective-c-cpp-output'
180);
181
182##----------------------------------------------------------------------------##
183# Main Logic.
184##----------------------------------------------------------------------------##
185
186my $Action = 'link';
187my @CompileOpts;
188my @LinkOpts;
189my @Files;
190my $Lang;
191my $Output;
192
193# Forward arguments to gcc.
Ted Kremenekf17ef3c2008-08-21 21:47:09 +0000194my $Status = system($CC,@ARGV);
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000195
196# Get the analysis options.
197my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
198if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
199
200# Determine the level of verbosity.
201my $Verbose = 0;
202if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
203if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
204
205# Determine what clang executable to use.
206my $Clang = $ENV{'CLANG'};
207if (!defined $Clang) { $Clang = 'clang'; }
208
209# Get the HTML output directory.
210my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
211
212
213# Process the arguments.
214foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
215 my $Arg = $ARGV[$i];
216
217 # Modes ccc-analyzer supports
218 if ($Arg eq '-E') { $Action = 'preprocess'; }
219 elsif ($Arg eq '-c') { $Action = 'compile'; }
220 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenekb0982882008-03-25 22:35:32 +0000221
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000222 # Options with possible arguments that should pass through to compiler.
223 if (defined $CompileOptionMap{$Arg}) {
224 my $Cnt = $CompileOptionMap{$Arg};
225 push @CompileOpts,$Arg;
226 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
227 next;
228 }
229
230 # Options with possible arguments that should pass through to linker.
231 if (defined $LinkerOptionMap{$Arg}) {
232 my $Cnt = $LinkerOptionMap{$Arg};
233 push @LinkOpts,$Arg;
234 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
235 next;
236 }
237
238 # Options with possible arguments that should pass through to both compiler
239 # and the linker.
240 if (defined $CompilerLinkerOptionMap{$Arg}) {
241 my $Cnt = $CompilerLinkerOptionMap{$Arg};
242 push @CompileOpts,$Arg;
243 push @LinkOpts,$Arg;
244 while ($Cnt > 0) {
245 ++$i; --$Cnt;
246 push @CompileOpts, $ARGV[$i];
247 push @LinkOpts, $ARGV[$i];
248 }
249 next;
250 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000251
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000252 # Ignored options.
253 if (defined $IgnoredOptionMap{$Arg}) {
254 my $Cnt = $IgnoredOptionMap{$Arg};
255 while ($Cnt > 0) {
256 ++$i; --$Cnt;
257 }
258 next;
259 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000260
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000261 # Compile mode flags.
262 if ($Arg =~ /^-[D,I,U](.*)$/) {
263 my $Tmp = $Arg;
264 if ($1 eq '') {
265 # FIXME: Check if we are going off the end.
266 ++$i;
267 $Tmp = $Arg . $ARGV[$i];
268 }
269 push @CompileOpts,$Tmp;
270 next;
271 }
272
273 # Language.
274 if ($Arg eq '-x') {
275 $Lang = $ARGV[$i+1];
276 ++$i; next;
277 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000278
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000279 # Output file.
280 if ($Arg eq '-o') {
281 ++$i;
282 $Output = $ARGV[$i];
283 next;
284 }
285
286 # Get the link mode.
287 if ($Arg =~ /^-[l,L,O]/) {
288 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
289 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
290 else { push @LinkOpts,$Arg; }
291 next;
292 }
293
294 if ($Arg =~ /^-std=/) {
295 push @CompileOpts,$Arg;
296 next;
297 }
298
299# if ($Arg =~ /^-f/) {
300# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
301# push @CompileOpts,$Arg;
302# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
303# }
304
305 # Get the compiler/link mode.
306 if ($Arg =~ /^-F(.+)$/) {
307 my $Tmp = $Arg;
308 if ($1 eq '') {
309 # FIXME: Check if we are going off the end.
310 ++$i;
311 $Tmp = $Arg . $ARGV[$i];
312 }
313 push @CompileOpts,$Tmp;
314 push @LinkOpts,$Tmp;
315 next;
316 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000317
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000318 # Input files.
319 if ($Arg eq '-filelist') {
320 # FIXME: Make sure we aren't walking off the end.
321 open(IN, $ARGV[$i+1]);
322 while (<IN>) { s/\015?\012//; push @Files,$_; }
323 close(IN);
324 ++$i; next;
325 }
326
327 if (!($Arg =~ /^-/)) {
328 push @Files,$Arg; next;
329 }
330}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000331
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000332if ($Action eq 'compile' or $Action eq 'link') {
333 foreach my $file (@Files) {
334 # Determine the language for the file.
335 my $FileLang = $Lang;
336
337 if (!defined($FileLang)) {
338 # Infer the language from the extension.
339 if ($file =~ /[.]([^.]+)$/) {
340 $FileLang = $LangMap{$1};
341 }
342 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000343
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000344 next if (!defined $FileLang);
345
346 my @AnalyzeArgs;
347
348 if ($FileLang ne 'unknown') {
349 push @AnalyzeArgs,'-x';
350 push @AnalyzeArgs,$FileLang;
351 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000352
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000353 push @AnalyzeArgs,@CompileOpts;
354 push @AnalyzeArgs,$file;
355
356 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
357 $Verbose, $HtmlDir, $file, $Analyses);
358 }
359}
Ted Kremenekb0982882008-03-25 22:35:32 +0000360
Ted Kremenek948e06b2008-08-27 22:30:34 +0000361exit($Status >> 8);
362
363