blob: c760a864fdd1a87e27e312fe501737bff7bb942c [file] [log] [blame]
Ted Kremenek2c67d1b2008-07-19 06:11:04 +00001#!/usr/bin/env perl
Ted Kremenek18e72bb2008-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 Kremenek2c67d1b2008-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 Kremenek18e72bb2008-03-25 22:35:32 +000012#
13##===----------------------------------------------------------------------===##
14
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000015use strict;
16use warnings;
17use Cwd;
Ted Kremenek61c656b2008-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 Kremenekcd7c9202008-08-18 18:38:29 +000031sub ProcessClangFailure {
32 my ($Lang, $file, $Args, $HtmlDir, $ErrorType) = @_;
Ted Kremenek61c656b2008-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 Kremenekcd7c9202008-08-18 18:38:29 +000039 system "gcc", @$Args, "-E", "-o", $PPFile;
Ted Kremenek61c656b2008-08-08 20:46:42 +000040 close ($PPH);
41 open (OUT, ">", "$PPFile.info") or die "Cannot open $PPFile.info\n";
Ted Kremenekcd7c9202008-08-18 18:38:29 +000042 print OUT "$file\n";
43 print OUT "$ErrorType\n";
Ted Kremenekd367e822008-08-18 20:55:25 +000044 print OUT "@$Args\n";
Ted Kremenek61c656b2008-08-08 20:46:42 +000045 close OUT;
46}
Ted Kremenek18e72bb2008-03-25 22:35:32 +000047
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000048##----------------------------------------------------------------------------##
49# Running the analyzer.
50##----------------------------------------------------------------------------##
Ted Kremenek18e72bb2008-03-25 22:35:32 +000051
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000052sub Analyze {
53 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeon877866b2008-04-04 11:02:21 +000054
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000055 # Skip anything related to C++.
56 return if ($Lang =~ /c[+][+]/);
Ted Kremenekcd7c9202008-08-18 18:38:29 +000057
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000058 my $RunAnalyzer = 0;
59 my $Cmd;
60 my @CmdArgs;
Ted Kremenek61c656b2008-08-08 20:46:42 +000061 my @CmdArgsSansAnalyses;
Ted Kremenek5696dfe2008-05-24 15:58:54 +000062
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000063 if ($Lang =~ /header/) {
64 exit 0 if (!defined ($Output));
65 $Cmd = 'cp';
66 push @CmdArgs,$file;
67 # Remove the PCH extension.
68 $Output =~ s/[.]gch$//;
69 push @CmdArgs,$Output;
Ted Kremenek61c656b2008-08-08 20:46:42 +000070 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000071 }
72 else {
73 $Cmd = $Clang;
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000074 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
75 push @CmdArgs,@$Args;
Ted Kremenek61c656b2008-08-08 20:46:42 +000076 @CmdArgsSansAnalyses = @CmdArgs;
77 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000078 $RunAnalyzer = 1;
79 }
80
81 my @PrintArgs;
82 my $dir;
83
84 if ($Verbose) {
85 $dir = getcwd();
86 print STDERR "\n[LOCATION]: $dir\n";
87 push @PrintArgs,"'$Cmd'";
88 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
89 }
90
91 if ($Verbose == 1) {
Ted Kremenek5696dfe2008-05-24 15:58:54 +000092 # We MUST print to stderr. Some clients use the stdout output of
93 # gcc for various purposes.
Ted Kremenek2c67d1b2008-07-19 06:11:04 +000094 print STDERR join(' ',@PrintArgs);
95 print STDERR "\n";
96 }
97 elsif ($Verbose == 2) {
98 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
99 }
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000100
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000101 if ($RunAnalyzer and defined($HtmlDir)) {
102 push @CmdArgs,'-o';
103 push @CmdArgs,$HtmlDir;
104 }
Ted Kremenek61c656b2008-08-08 20:46:42 +0000105
106 system $Cmd,@CmdArgs;
107
108 # Did the command die because of a signal?
109 if ($? & 127 and $Cmd eq $Clang and defined $HtmlDir) {
Ted Kremenekcd7c9202008-08-18 18:38:29 +0000110 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
111 "Crash");
112 }
113 elsif ($?) {
114 ProcessClangFailure($Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir,
115 "Parser Rejects");
Ted Kremenek61c656b2008-08-08 20:46:42 +0000116 }
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000117}
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000118
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000119##----------------------------------------------------------------------------##
120# Lookup tables.
121##----------------------------------------------------------------------------##
122
123my %CompileOptionMap = (
124 '-nostdinc' => 0,
125 '-fobjc-gc-only' => 0,
126 '-fobjc-gc' => 0,
127 '-include' => 1,
128 '-idirafter' => 1,
129 '-iprefix' => 1,
130 '-iquote' => 1,
131 '-isystem' => 1,
132 '-iwithprefix' => 1,
133 '-iwithprefixbefore' => 1
134);
135
136my %LinkerOptionMap = (
137 '-framework' => 1
138);
139
140my %CompilerLinkerOptionMap = (
141 '-isysroot' => 1,
142 '-arch' => 1,
143 '-v' => 0
144);
145
146my %IgnoredOptionMap = (
Ted Kremenek22128c62008-07-24 03:52:21 +0000147 '-MT' => 1, # Ignore these preprocessor options.
148 '-MF' => 1,
149
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000150 '-fsyntax-only' => 0,
151 '-save-temps' => 0,
152 '-install_name' => 1,
153 '-exported_symbols_list' => 1,
154 '-current_version' => 1,
155 '-compatibility_version' => 1,
156 '-init' => 1,
157 '-e' => 1,
158 '-seg1addr' => 1,
159 '-bundle_loader' => 1,
160 '-multiply_defined' => 1,
161 '-sectorder' => 3,
162 '--param' => 1,
163 '-u' => 1
164);
165
166my %LangMap = (
167 'c' => 'c',
168 'cpp' => 'c++',
169 'cc' => 'c++',
170 'i' => 'c-cpp-output',
171 'm' => 'objective-c',
172 'mi' => 'objective-c-cpp-output'
173);
174
175##----------------------------------------------------------------------------##
176# Main Logic.
177##----------------------------------------------------------------------------##
178
179my $Action = 'link';
180my @CompileOpts;
181my @LinkOpts;
182my @Files;
183my $Lang;
184my $Output;
185
186# Forward arguments to gcc.
Ted Kremenek91ec36e2008-08-21 21:47:09 +0000187my $CC = $ENV{'CCC_CC'};
188if (!defined $CC) { $CC = "gcc"; }
189my $Status = system($CC,@ARGV);
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000190if ($Status) { exit($Status); }
191
192# Get the analysis options.
193my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
194if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
195
196# Determine the level of verbosity.
197my $Verbose = 0;
198if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
199if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
200
201# Determine what clang executable to use.
202my $Clang = $ENV{'CLANG'};
203if (!defined $Clang) { $Clang = 'clang'; }
204
205# Get the HTML output directory.
206my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
207
208
209# Process the arguments.
210foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
211 my $Arg = $ARGV[$i];
212
213 # Modes ccc-analyzer supports
214 if ($Arg eq '-E') { $Action = 'preprocess'; }
215 elsif ($Arg eq '-c') { $Action = 'compile'; }
216 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenek18e72bb2008-03-25 22:35:32 +0000217
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000218 # Options with possible arguments that should pass through to compiler.
219 if (defined $CompileOptionMap{$Arg}) {
220 my $Cnt = $CompileOptionMap{$Arg};
221 push @CompileOpts,$Arg;
222 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
223 next;
224 }
225
226 # Options with possible arguments that should pass through to linker.
227 if (defined $LinkerOptionMap{$Arg}) {
228 my $Cnt = $LinkerOptionMap{$Arg};
229 push @LinkOpts,$Arg;
230 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
231 next;
232 }
233
234 # Options with possible arguments that should pass through to both compiler
235 # and the linker.
236 if (defined $CompilerLinkerOptionMap{$Arg}) {
237 my $Cnt = $CompilerLinkerOptionMap{$Arg};
238 push @CompileOpts,$Arg;
239 push @LinkOpts,$Arg;
240 while ($Cnt > 0) {
241 ++$i; --$Cnt;
242 push @CompileOpts, $ARGV[$i];
243 push @LinkOpts, $ARGV[$i];
244 }
245 next;
246 }
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000247
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000248 # Ignored options.
249 if (defined $IgnoredOptionMap{$Arg}) {
250 my $Cnt = $IgnoredOptionMap{$Arg};
251 while ($Cnt > 0) {
252 ++$i; --$Cnt;
253 }
254 next;
255 }
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000256
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000257 # Compile mode flags.
258 if ($Arg =~ /^-[D,I,U](.*)$/) {
259 my $Tmp = $Arg;
260 if ($1 eq '') {
261 # FIXME: Check if we are going off the end.
262 ++$i;
263 $Tmp = $Arg . $ARGV[$i];
264 }
265 push @CompileOpts,$Tmp;
266 next;
267 }
268
269 # Language.
270 if ($Arg eq '-x') {
271 $Lang = $ARGV[$i+1];
272 ++$i; next;
273 }
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000274
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000275 # Output file.
276 if ($Arg eq '-o') {
277 ++$i;
278 $Output = $ARGV[$i];
279 next;
280 }
281
282 # Get the link mode.
283 if ($Arg =~ /^-[l,L,O]/) {
284 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
285 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
286 else { push @LinkOpts,$Arg; }
287 next;
288 }
289
290 if ($Arg =~ /^-std=/) {
291 push @CompileOpts,$Arg;
292 next;
293 }
294
295# if ($Arg =~ /^-f/) {
296# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
297# push @CompileOpts,$Arg;
298# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
299# }
300
301 # Get the compiler/link mode.
302 if ($Arg =~ /^-F(.+)$/) {
303 my $Tmp = $Arg;
304 if ($1 eq '') {
305 # FIXME: Check if we are going off the end.
306 ++$i;
307 $Tmp = $Arg . $ARGV[$i];
308 }
309 push @CompileOpts,$Tmp;
310 push @LinkOpts,$Tmp;
311 next;
312 }
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000313
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000314 # Input files.
315 if ($Arg eq '-filelist') {
316 # FIXME: Make sure we aren't walking off the end.
317 open(IN, $ARGV[$i+1]);
318 while (<IN>) { s/\015?\012//; push @Files,$_; }
319 close(IN);
320 ++$i; next;
321 }
322
323 if (!($Arg =~ /^-/)) {
324 push @Files,$Arg; next;
325 }
326}
Ted Kremenek5696dfe2008-05-24 15:58:54 +0000327
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000328if ($Action eq 'compile' or $Action eq 'link') {
329 foreach my $file (@Files) {
330 # Determine the language for the file.
331 my $FileLang = $Lang;
332
333 if (!defined($FileLang)) {
334 # Infer the language from the extension.
335 if ($file =~ /[.]([^.]+)$/) {
336 $FileLang = $LangMap{$1};
337 }
338 }
Ted Kremenek5979b082008-05-14 20:10:33 +0000339
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000340 next if (!defined $FileLang);
341
342 my @AnalyzeArgs;
343
344 if ($FileLang ne 'unknown') {
345 push @AnalyzeArgs,'-x';
346 push @AnalyzeArgs,$FileLang;
347 }
Ted Kremenek18e72bb2008-03-25 22:35:32 +0000348
Ted Kremenek2c67d1b2008-07-19 06:11:04 +0000349 push @AnalyzeArgs,@CompileOpts;
350 push @AnalyzeArgs,$file;
351
352 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
353 $Verbose, $HtmlDir, $file, $Analyses);
354 }
355}
Ted Kremenek18e72bb2008-03-25 22:35:32 +0000356