blob: fd24a8b2ad8a0d8135b5a73c36e1ef27ecd150a7 [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
31sub ProcessClangCrash {
32 my ($Clang, $Lang, $file, $Args, $HtmlDir) = @_;
33 my $Dir = "$HtmlDir/crashes";
34 mkpath $Dir;
35 my ($PPH, $PPFile) = tempfile("clang_crash_XXXXXX",
36 SUFFIX => GetPPExt($Lang),
37 DIR => $Dir);
38
39 system $Clang, @$Args, "-E", "-o", $PPFile;
40 close ($PPH);
41 open (OUT, ">", "$PPFile.info") or die "Cannot open $PPFile.info\n";
42 print OUT "$file";
43 close OUT;
44}
Ted Kremenekb0982882008-03-25 22:35:32 +000045
Ted Kremenekfbeeca82008-07-19 06:11:04 +000046##----------------------------------------------------------------------------##
47# Running the analyzer.
48##----------------------------------------------------------------------------##
Ted Kremenekb0982882008-03-25 22:35:32 +000049
Ted Kremenekfbeeca82008-07-19 06:11:04 +000050sub Analyze {
51 my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
Seo Sanghyeond3894652008-04-04 11:02:21 +000052
Ted Kremenekfbeeca82008-07-19 06:11:04 +000053 # Skip anything related to C++.
54 return if ($Lang =~ /c[+][+]/);
Ted Kremenekb0982882008-03-25 22:35:32 +000055
Ted Kremenekfbeeca82008-07-19 06:11:04 +000056 my $RunAnalyzer = 0;
57 my $Cmd;
58 my @CmdArgs;
Ted Kremenek991c54b2008-08-08 20:46:42 +000059 my @CmdArgsSansAnalyses;
Ted Kremenek61cd9882008-05-24 15:58:54 +000060
Ted Kremenekfbeeca82008-07-19 06:11:04 +000061 if ($Lang =~ /header/) {
62 exit 0 if (!defined ($Output));
63 $Cmd = 'cp';
64 push @CmdArgs,$file;
65 # Remove the PCH extension.
66 $Output =~ s/[.]gch$//;
67 push @CmdArgs,$Output;
Ted Kremenek991c54b2008-08-08 20:46:42 +000068 @CmdArgsSansAnalyses = @CmdArgs;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000069 }
70 else {
71 $Cmd = $Clang;
Ted Kremenekfbeeca82008-07-19 06:11:04 +000072 push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
73 push @CmdArgs,@$Args;
Ted Kremenek991c54b2008-08-08 20:46:42 +000074 @CmdArgsSansAnalyses = @CmdArgs;
75 push @CmdArgs,(split /\s/,$Analyses);
Ted Kremenekfbeeca82008-07-19 06:11:04 +000076 $RunAnalyzer = 1;
77 }
78
79 my @PrintArgs;
80 my $dir;
81
82 if ($Verbose) {
83 $dir = getcwd();
84 print STDERR "\n[LOCATION]: $dir\n";
85 push @PrintArgs,"'$Cmd'";
86 foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
87 }
88
89 if ($Verbose == 1) {
Ted Kremenek61cd9882008-05-24 15:58:54 +000090 # We MUST print to stderr. Some clients use the stdout output of
91 # gcc for various purposes.
Ted Kremenekfbeeca82008-07-19 06:11:04 +000092 print STDERR join(' ',@PrintArgs);
93 print STDERR "\n";
94 }
95 elsif ($Verbose == 2) {
96 print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
97 }
Ted Kremenek61cd9882008-05-24 15:58:54 +000098
Ted Kremenekfbeeca82008-07-19 06:11:04 +000099 if ($RunAnalyzer and defined($HtmlDir)) {
100 push @CmdArgs,'-o';
101 push @CmdArgs,$HtmlDir;
102 }
Ted Kremenek991c54b2008-08-08 20:46:42 +0000103
104 system $Cmd,@CmdArgs;
105
106 # Did the command die because of a signal?
107 if ($? & 127 and $Cmd eq $Clang and defined $HtmlDir) {
108 ProcessClangCrash($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir);
109 }
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000110}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000111
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000112##----------------------------------------------------------------------------##
113# Lookup tables.
114##----------------------------------------------------------------------------##
115
116my %CompileOptionMap = (
117 '-nostdinc' => 0,
118 '-fobjc-gc-only' => 0,
119 '-fobjc-gc' => 0,
120 '-include' => 1,
121 '-idirafter' => 1,
122 '-iprefix' => 1,
123 '-iquote' => 1,
124 '-isystem' => 1,
125 '-iwithprefix' => 1,
126 '-iwithprefixbefore' => 1
127);
128
129my %LinkerOptionMap = (
130 '-framework' => 1
131);
132
133my %CompilerLinkerOptionMap = (
134 '-isysroot' => 1,
135 '-arch' => 1,
136 '-v' => 0
137);
138
139my %IgnoredOptionMap = (
Ted Kremenek94026092008-07-24 03:52:21 +0000140 '-MT' => 1, # Ignore these preprocessor options.
141 '-MF' => 1,
142
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000143 '-fsyntax-only' => 0,
144 '-save-temps' => 0,
145 '-install_name' => 1,
146 '-exported_symbols_list' => 1,
147 '-current_version' => 1,
148 '-compatibility_version' => 1,
149 '-init' => 1,
150 '-e' => 1,
151 '-seg1addr' => 1,
152 '-bundle_loader' => 1,
153 '-multiply_defined' => 1,
154 '-sectorder' => 3,
155 '--param' => 1,
156 '-u' => 1
157);
158
159my %LangMap = (
160 'c' => 'c',
161 'cpp' => 'c++',
162 'cc' => 'c++',
163 'i' => 'c-cpp-output',
164 'm' => 'objective-c',
165 'mi' => 'objective-c-cpp-output'
166);
167
168##----------------------------------------------------------------------------##
169# Main Logic.
170##----------------------------------------------------------------------------##
171
172my $Action = 'link';
173my @CompileOpts;
174my @LinkOpts;
175my @Files;
176my $Lang;
177my $Output;
178
179# Forward arguments to gcc.
180my $Status = system("gcc",@ARGV);
181if ($Status) { exit($Status); }
182
183# Get the analysis options.
184my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
185if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
186
187# Determine the level of verbosity.
188my $Verbose = 0;
189if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
190if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
191
192# Determine what clang executable to use.
193my $Clang = $ENV{'CLANG'};
194if (!defined $Clang) { $Clang = 'clang'; }
195
196# Get the HTML output directory.
197my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
198
199
200# Process the arguments.
201foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
202 my $Arg = $ARGV[$i];
203
204 # Modes ccc-analyzer supports
205 if ($Arg eq '-E') { $Action = 'preprocess'; }
206 elsif ($Arg eq '-c') { $Action = 'compile'; }
207 elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
Ted Kremenekb0982882008-03-25 22:35:32 +0000208
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000209 # Options with possible arguments that should pass through to compiler.
210 if (defined $CompileOptionMap{$Arg}) {
211 my $Cnt = $CompileOptionMap{$Arg};
212 push @CompileOpts,$Arg;
213 while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
214 next;
215 }
216
217 # Options with possible arguments that should pass through to linker.
218 if (defined $LinkerOptionMap{$Arg}) {
219 my $Cnt = $LinkerOptionMap{$Arg};
220 push @LinkOpts,$Arg;
221 while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
222 next;
223 }
224
225 # Options with possible arguments that should pass through to both compiler
226 # and the linker.
227 if (defined $CompilerLinkerOptionMap{$Arg}) {
228 my $Cnt = $CompilerLinkerOptionMap{$Arg};
229 push @CompileOpts,$Arg;
230 push @LinkOpts,$Arg;
231 while ($Cnt > 0) {
232 ++$i; --$Cnt;
233 push @CompileOpts, $ARGV[$i];
234 push @LinkOpts, $ARGV[$i];
235 }
236 next;
237 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000238
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000239 # Ignored options.
240 if (defined $IgnoredOptionMap{$Arg}) {
241 my $Cnt = $IgnoredOptionMap{$Arg};
242 while ($Cnt > 0) {
243 ++$i; --$Cnt;
244 }
245 next;
246 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000247
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000248 # Compile mode flags.
249 if ($Arg =~ /^-[D,I,U](.*)$/) {
250 my $Tmp = $Arg;
251 if ($1 eq '') {
252 # FIXME: Check if we are going off the end.
253 ++$i;
254 $Tmp = $Arg . $ARGV[$i];
255 }
256 push @CompileOpts,$Tmp;
257 next;
258 }
259
260 # Language.
261 if ($Arg eq '-x') {
262 $Lang = $ARGV[$i+1];
263 ++$i; next;
264 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000265
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000266 # Output file.
267 if ($Arg eq '-o') {
268 ++$i;
269 $Output = $ARGV[$i];
270 next;
271 }
272
273 # Get the link mode.
274 if ($Arg =~ /^-[l,L,O]/) {
275 if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
276 elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
277 else { push @LinkOpts,$Arg; }
278 next;
279 }
280
281 if ($Arg =~ /^-std=/) {
282 push @CompileOpts,$Arg;
283 next;
284 }
285
286# if ($Arg =~ /^-f/) {
287# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
288# push @CompileOpts,$Arg;
289# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
290# }
291
292 # Get the compiler/link mode.
293 if ($Arg =~ /^-F(.+)$/) {
294 my $Tmp = $Arg;
295 if ($1 eq '') {
296 # FIXME: Check if we are going off the end.
297 ++$i;
298 $Tmp = $Arg . $ARGV[$i];
299 }
300 push @CompileOpts,$Tmp;
301 push @LinkOpts,$Tmp;
302 next;
303 }
Ted Kremenek61cd9882008-05-24 15:58:54 +0000304
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000305 # Input files.
306 if ($Arg eq '-filelist') {
307 # FIXME: Make sure we aren't walking off the end.
308 open(IN, $ARGV[$i+1]);
309 while (<IN>) { s/\015?\012//; push @Files,$_; }
310 close(IN);
311 ++$i; next;
312 }
313
314 if (!($Arg =~ /^-/)) {
315 push @Files,$Arg; next;
316 }
317}
Ted Kremenek61cd9882008-05-24 15:58:54 +0000318
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000319if ($Action eq 'compile' or $Action eq 'link') {
320 foreach my $file (@Files) {
321 # Determine the language for the file.
322 my $FileLang = $Lang;
323
324 if (!defined($FileLang)) {
325 # Infer the language from the extension.
326 if ($file =~ /[.]([^.]+)$/) {
327 $FileLang = $LangMap{$1};
328 }
329 }
Ted Kremenek1262fc42008-05-14 20:10:33 +0000330
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000331 next if (!defined $FileLang);
332
333 my @AnalyzeArgs;
334
335 if ($FileLang ne 'unknown') {
336 push @AnalyzeArgs,'-x';
337 push @AnalyzeArgs,$FileLang;
338 }
Ted Kremenekb0982882008-03-25 22:35:32 +0000339
Ted Kremenekfbeeca82008-07-19 06:11:04 +0000340 push @AnalyzeArgs,@CompileOpts;
341 push @AnalyzeArgs,$file;
342
343 Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
344 $Verbose, $HtmlDir, $file, $Analyses);
345 }
346}
Ted Kremenekb0982882008-03-25 22:35:32 +0000347