blob: 97554a875cada5a2d9f727a125ca58397a188832 [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 Kremenekcb344d02008-08-28 01:18:44 +0000195if ($Status) { exit($Status >> 8); }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000196
197# Get the analysis options.
198my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
199if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
200
201# Determine the level of verbosity.
202my $Verbose = 0;
203if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
204if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
205
206# Determine what clang executable to use.
207my $Clang = $ENV{'CLANG'};
208if (!defined $Clang) { $Clang = 'clang'; }
209
210# Get the HTML output directory.
211my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
212
213
214# Process the arguments.
215foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
216 my $Arg = $ARGV[$i];
217
218 # Modes ccc-analyzer supports
219 if ($Arg eq '-E') { $Action = 'preprocess'; }
220 elsif ($Arg eq '-c') { $Action = 'compile'; }
221 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenekb0982882008-03-25 22:35:32 +0000222
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000223 # Options with possible arguments that should pass through to compiler.
224 if (defined $CompileOptionMap{$Arg}) {
225 my $Cnt = $CompileOptionMap{$Arg};
226 push @CompileOpts,$Arg;
227 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
228 next;
229 }
230
231 # Options with possible arguments that should pass through to linker.
232 if (defined $LinkerOptionMap{$Arg}) {
233 my $Cnt = $LinkerOptionMap{$Arg};
234 push @LinkOpts,$Arg;
235 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
236 next;
237 }
238
239 # Options with possible arguments that should pass through to both compiler
240 # and the linker.
241 if (defined $CompilerLinkerOptionMap{$Arg}) {
242 my $Cnt = $CompilerLinkerOptionMap{$Arg};
243 push @CompileOpts,$Arg;
244 push @LinkOpts,$Arg;
245 while ($Cnt > 0) {
246 ++$i; --$Cnt;
247 push @CompileOpts, $ARGV[$i];
248 push @LinkOpts, $ARGV[$i];
249 }
250 next;
251 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000252
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000253 # Ignored options.
254 if (defined $IgnoredOptionMap{$Arg}) {
255 my $Cnt = $IgnoredOptionMap{$Arg};
256 while ($Cnt > 0) {
257 ++$i; --$Cnt;
258 }
259 next;
260 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000261
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000262 # Compile mode flags.
263 if ($Arg =~ /^-[D,I,U](.*)$/) {
264 my $Tmp = $Arg;
265 if ($1 eq '') {
266 # FIXME: Check if we are going off the end.
267 ++$i;
268 $Tmp = $Arg . $ARGV[$i];
269 }
270 push @CompileOpts,$Tmp;
271 next;
272 }
273
274 # Language.
275 if ($Arg eq '-x') {
276 $Lang = $ARGV[$i+1];
277 ++$i; next;
278 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000279
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000280 # Output file.
281 if ($Arg eq '-o') {
282 ++$i;
283 $Output = $ARGV[$i];
284 next;
285 }
286
287 # Get the link mode.
288 if ($Arg =~ /^-[l,L,O]/) {
289 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
290 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
291 else { push @LinkOpts,$Arg; }
292 next;
293 }
294
295 if ($Arg =~ /^-std=/) {
296 push @CompileOpts,$Arg;
297 next;
298 }
299
300# if ($Arg =~ /^-f/) {
301# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
302# push @CompileOpts,$Arg;
303# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
304# }
305
306 # Get the compiler/link mode.
307 if ($Arg =~ /^-F(.+)$/) {
308 my $Tmp = $Arg;
309 if ($1 eq '') {
310 # FIXME: Check if we are going off the end.
311 ++$i;
312 $Tmp = $Arg . $ARGV[$i];
313 }
314 push @CompileOpts,$Tmp;
315 push @LinkOpts,$Tmp;
316 next;
317 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000318
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000319 # Input files.
320 if ($Arg eq '-filelist') {
321 # FIXME: Make sure we aren't walking off the end.
322 open(IN, $ARGV[$i+1]);
323 while (<IN>) { s/\015?\012//; push @Files,$_; }
324 close(IN);
325 ++$i; next;
326 }
327
328 if (!($Arg =~ /^-/)) {
329 push @Files,$Arg; next;
330 }
331}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000332
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000333if ($Action eq 'compile' or $Action eq 'link') {
334 foreach my $file (@Files) {
335 # Determine the language for the file.
336 my $FileLang = $Lang;
337
338 if (!defined($FileLang)) {
339 # Infer the language from the extension.
340 if ($file =~ /[.]([^.]+)$/) {
341 $FileLang = $LangMap{$1};
342 }
343 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000344
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000345 next if (!defined $FileLang);
346
347 my @AnalyzeArgs;
348
349 if ($FileLang ne 'unknown') {
350 push @AnalyzeArgs,'-x';
351 push @AnalyzeArgs,$FileLang;
352 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000353
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000354 push @AnalyzeArgs,@CompileOpts;
355 push @AnalyzeArgs,$file;
356
357 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
358 $Verbose, $HtmlDir, $file, $Analyses);
359 }
360}
Ted Kremenekb0982882008-03-25 22:35:32 +0000361
Ted Kremenek948e06b2008-08-27 22:30:34 +0000362exit($Status >> 8);
363
364