blob: ad7b412b0be3dfb4f7487511b324d07c6f9ab65a [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 /;
20
21##----------------------------------------------------------------------------##
22# Process Clang Crashes.
23##----------------------------------------------------------------------------##
24
25sub GetPPExt {
26 my $Lang = shift;
27 if ($Lang =~ /objective-c/) { return ".mi"; }
28 return ".i";
29}
30
Ted Kremenek5d31f832008-08-18 18:38:29 +000031sub ProcessClangFailure {
32 my ($Lang, $file, $Args, $HtmlDir, $ErrorType) = @_;
Ted Kremenek991c54b2008-08-08 20:46:42 +000033 my $Dir = "$HtmlDir/crashes";
34 mkpath $Dir;
35 my ($PPH, $PPFile) = tempfile("clang_crash_XXXXXX",
36 SUFFIX => GetPPExt($Lang),
37 DIR => $Dir);
38
Ted Kremenek5d31f832008-08-18 18:38:29 +000039 system "gcc", @$Args, "-E", "-o", $PPFile;
Ted Kremenek991c54b2008-08-08 20:46:42 +000040 close ($PPH);
41 open (OUT, ">", "$PPFile.info") or die "Cannot open $PPFile.info\n";
Ted Kremenek5d31f832008-08-18 18:38:29 +000042 print OUT "$file\n";
43 print OUT "$ErrorType\n";
Ted Kremenek991c54b2008-08-08 20:46:42 +000044 close OUT;
45}
Ted Kremenekb0982882008-03-25 22:35:32 +000046
Ted Kremenekfbeeca82008-07-19 06:11:04 +000047##----------------------------------------------------------------------------##
48# Running the analyzer.
49##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +000050
Ted Kremenekfbeeca82008-07-19 06:11:04 +000051sub Analyze {
52 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +000053
Ted Kremenekfbeeca82008-07-19 06:11:04 +000054 # Skip anything related to C++.
55 return if ($Lang =~ /c[+][+]/);
Ted Kremenek5d31f832008-08-18 18:38:29 +000056
Ted Kremenekfbeeca82008-07-19 06:11:04 +000057 my $RunAnalyzer = 0;
58 my $Cmd;
59 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +000060 my @CmdArgsSansAnalyses;
Ted Kremenek61cd9882008-05-24 15:58:54 +000061
Ted Kremenekfbeeca82008-07-19 06:11:04 +000062 if ($Lang =~ /header/) {
63 exit 0 if (!defined ($Output));
64 $Cmd = 'cp';
65 push @CmdArgs,$file;
66 # Remove the PCH extension.
67 $Output =~ s/[.]gch$//;
68 push @CmdArgs,$Output;
Ted Kremenek991c54b2008-08-08 20:46:42 +000069 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000070 }
71 else {
72 $Cmd = $Clang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000073 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
74 push @CmdArgs,@$Args;
Ted Kremenek991c54b2008-08-08 20:46:42 +000075 @CmdArgsSansAnalyses = @CmdArgs;
76 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenekfbeeca82008-07-19 06:11:04 +000077 $RunAnalyzer = 1;
78 }
79
80 my @PrintArgs;
81 my $dir;
82
83 if ($Verbose) {
84 $dir = getcwd();
85 print STDERR "\n[LOCATION]: $dir\n";
86 push @PrintArgs,"'$Cmd'";
87 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
88 }
89
90 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +000091 # We MUST print to stderr. Some clients use the stdout output of
92 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +000093 print STDERR join(' ',@PrintArgs);
94 print STDERR "\n";
95 }
96 elsif ($Verbose == 2) {
97 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
98 }
Ted Kremenek61cd9882008-05-24 15:58:54 +000099
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000100 if ($RunAnalyzer and defined($HtmlDir)) {
101 push @CmdArgs,'-o';
102 push @CmdArgs,$HtmlDir;
103 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000104
105 system $Cmd,@CmdArgs;
106
107 # Did the command die because of a signal?
108 if ($? & 127 and $Cmd eq $Clang and defined $HtmlDir) {
Ted Kremenek5d31f832008-08-18 18:38:29 +0000109 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
110 "Crash");
111 }
112 elsif ($?) {
113 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
114 "Parser Rejects");
Ted Kremenek991c54b2008-08-08 20:46:42 +0000115 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000116}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000117
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000118##----------------------------------------------------------------------------##
119# Lookup tables.
120##----------------------------------------------------------------------------##
121
122my %CompileOptionMap = (
123 '-nostdinc' => 0,
124 '-fobjc-gc-only' => 0,
125 '-fobjc-gc' => 0,
126 '-include' => 1,
127 '-idirafter' => 1,
128 '-iprefix' => 1,
129 '-iquote' => 1,
130 '-isystem' => 1,
131 '-iwithprefix' => 1,
132 '-iwithprefixbefore' => 1
133);
134
135my %LinkerOptionMap = (
136 '-framework' => 1
137);
138
139my %CompilerLinkerOptionMap = (
140 '-isysroot' => 1,
141 '-arch' => 1,
142 '-v' => 0
143);
144
145my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000146 '-MT' => 1, # Ignore these preprocessor options.
147 '-MF' => 1,
148
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000149 '-fsyntax-only' => 0,
150 '-save-temps' => 0,
151 '-install_name' => 1,
152 '-exported_symbols_list' => 1,
153 '-current_version' => 1,
154 '-compatibility_version' => 1,
155 '-init' => 1,
156 '-e' => 1,
157 '-seg1addr' => 1,
158 '-bundle_loader' => 1,
159 '-multiply_defined' => 1,
160 '-sectorder' => 3,
161 '--param' => 1,
162 '-u' => 1
163);
164
165my %LangMap = (
166 'c' => 'c',
167 'cpp' => 'c++',
168 'cc' => 'c++',
169 'i' => 'c-cpp-output',
170 'm' => 'objective-c',
171 'mi' => 'objective-c-cpp-output'
172);
173
174##----------------------------------------------------------------------------##
175# Main Logic.
176##----------------------------------------------------------------------------##
177
178my $Action = 'link';
179my @CompileOpts;
180my @LinkOpts;
181my @Files;
182my $Lang;
183my $Output;
184
185# Forward arguments to gcc.
186my $Status = system("gcc",@ARGV);
187if ($Status) { exit($Status); }
188
189# Get the analysis options.
190my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
191if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
192
193# Determine the level of verbosity.
194my $Verbose = 0;
195if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
196if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
197
198# Determine what clang executable to use.
199my $Clang = $ENV{'CLANG'};
200if (!defined $Clang) { $Clang = 'clang'; }
201
202# Get the HTML output directory.
203my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
204
205
206# Process the arguments.
207foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
208 my $Arg = $ARGV[$i];
209
210 # Modes ccc-analyzer supports
211 if ($Arg eq '-E') { $Action = 'preprocess'; }
212 elsif ($Arg eq '-c') { $Action = 'compile'; }
213 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenekb0982882008-03-25 22:35:32 +0000214
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000215 # Options with possible arguments that should pass through to compiler.
216 if (defined $CompileOptionMap{$Arg}) {
217 my $Cnt = $CompileOptionMap{$Arg};
218 push @CompileOpts,$Arg;
219 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
220 next;
221 }
222
223 # Options with possible arguments that should pass through to linker.
224 if (defined $LinkerOptionMap{$Arg}) {
225 my $Cnt = $LinkerOptionMap{$Arg};
226 push @LinkOpts,$Arg;
227 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
228 next;
229 }
230
231 # Options with possible arguments that should pass through to both compiler
232 # and the linker.
233 if (defined $CompilerLinkerOptionMap{$Arg}) {
234 my $Cnt = $CompilerLinkerOptionMap{$Arg};
235 push @CompileOpts,$Arg;
236 push @LinkOpts,$Arg;
237 while ($Cnt > 0) {
238 ++$i; --$Cnt;
239 push @CompileOpts, $ARGV[$i];
240 push @LinkOpts, $ARGV[$i];
241 }
242 next;
243 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000244
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000245 # Ignored options.
246 if (defined $IgnoredOptionMap{$Arg}) {
247 my $Cnt = $IgnoredOptionMap{$Arg};
248 while ($Cnt > 0) {
249 ++$i; --$Cnt;
250 }
251 next;
252 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000253
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000254 # Compile mode flags.
255 if ($Arg =~ /^-[D,I,U](.*)$/) {
256 my $Tmp = $Arg;
257 if ($1 eq '') {
258 # FIXME: Check if we are going off the end.
259 ++$i;
260 $Tmp = $Arg . $ARGV[$i];
261 }
262 push @CompileOpts,$Tmp;
263 next;
264 }
265
266 # Language.
267 if ($Arg eq '-x') {
268 $Lang = $ARGV[$i+1];
269 ++$i; next;
270 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000271
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000272 # Output file.
273 if ($Arg eq '-o') {
274 ++$i;
275 $Output = $ARGV[$i];
276 next;
277 }
278
279 # Get the link mode.
280 if ($Arg =~ /^-[l,L,O]/) {
281 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
282 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
283 else { push @LinkOpts,$Arg; }
284 next;
285 }
286
287 if ($Arg =~ /^-std=/) {
288 push @CompileOpts,$Arg;
289 next;
290 }
291
292# if ($Arg =~ /^-f/) {
293# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
294# push @CompileOpts,$Arg;
295# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
296# }
297
298 # Get the compiler/link mode.
299 if ($Arg =~ /^-F(.+)$/) {
300 my $Tmp = $Arg;
301 if ($1 eq '') {
302 # FIXME: Check if we are going off the end.
303 ++$i;
304 $Tmp = $Arg . $ARGV[$i];
305 }
306 push @CompileOpts,$Tmp;
307 push @LinkOpts,$Tmp;
308 next;
309 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000310
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000311 # Input files.
312 if ($Arg eq '-filelist') {
313 # FIXME: Make sure we aren't walking off the end.
314 open(IN, $ARGV[$i+1]);
315 while (<IN>) { s/\015?\012//; push @Files,$_; }
316 close(IN);
317 ++$i; next;
318 }
319
320 if (!($Arg =~ /^-/)) {
321 push @Files,$Arg; next;
322 }
323}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000324
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000325if ($Action eq 'compile' or $Action eq 'link') {
326 foreach my $file (@Files) {
327 # Determine the language for the file.
328 my $FileLang = $Lang;
329
330 if (!defined($FileLang)) {
331 # Infer the language from the extension.
332 if ($file =~ /[.]([^.]+)$/) {
333 $FileLang = $LangMap{$1};
334 }
335 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000336
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000337 next if (!defined $FileLang);
338
339 my @AnalyzeArgs;
340
341 if ($FileLang ne 'unknown') {
342 push @AnalyzeArgs,'-x';
343 push @AnalyzeArgs,$FileLang;
344 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000345
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000346 push @AnalyzeArgs,@CompileOpts;
347 push @AnalyzeArgs,$file;
348
349 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
350 $Verbose, $HtmlDir, $file, $Analyses);
351 }
352}
Ted Kremenekb0982882008-03-25 22:35:32 +0000353