-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccc-analyzer
executable file
·312 lines (263 loc) · 7.19 KB
/
ccc-analyzer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env perl
#
# The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
##===----------------------------------------------------------------------===##
#
# A script designed to interpose between the build system and gcc. It invokes
# both gcc and the static analyzer.
#
##===----------------------------------------------------------------------===##
use strict;
use warnings;
use Cwd;
##----------------------------------------------------------------------------##
# Running the analyzer.
##----------------------------------------------------------------------------##
sub Analyze {
my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_;
# Skip anything related to C++.
return if ($Lang =~ /c[+][+]/);
my $RunAnalyzer = 0;
my $Cmd;
my @CmdArgs;
if ($Lang =~ /header/) {
exit 0 if (!defined ($Output));
$Cmd = 'cp';
push @CmdArgs,$file;
# Remove the PCH extension.
$Output =~ s/[.]gch$//;
push @CmdArgs,$Output;
}
else {
$Cmd = $Clang;
push @CmdArgs,(split /\s/,$Analyses);
push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))';
push @CmdArgs,@$Args;
$RunAnalyzer = 1;
}
my @PrintArgs;
my $dir;
if ($Verbose) {
$dir = getcwd();
print STDERR "\n[LOCATION]: $dir\n";
push @PrintArgs,"'$Cmd'";
foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; }
}
if ($Verbose == 1) {
# We MUST print to stderr. Some clients use the stdout output of
# gcc for various purposes.
print STDERR join(' ',@PrintArgs);
print STDERR "\n";
}
elsif ($Verbose == 2) {
print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n";
}
if ($RunAnalyzer and defined($HtmlDir)) {
push @CmdArgs,'-o';
push @CmdArgs,$HtmlDir;
}
system $Cmd,@CmdArgs;
}
##----------------------------------------------------------------------------##
# Lookup tables.
##----------------------------------------------------------------------------##
my %CompileOptionMap = (
'-nostdinc' => 0,
'-fobjc-gc-only' => 0,
'-fobjc-gc' => 0,
'-include' => 1,
'-idirafter' => 1,
'-iprefix' => 1,
'-iquote' => 1,
'-isystem' => 1,
'-iwithprefix' => 1,
'-iwithprefixbefore' => 1
);
my %LinkerOptionMap = (
'-framework' => 1
);
my %CompilerLinkerOptionMap = (
'-isysroot' => 1,
'-arch' => 1,
'-v' => 0
);
my %IgnoredOptionMap = (
'-MT' => 1, # Ignore these preprocessor options.
'-MF' => 1,
'-fsyntax-only' => 0,
'-save-temps' => 0,
'-install_name' => 1,
'-exported_symbols_list' => 1,
'-current_version' => 1,
'-compatibility_version' => 1,
'-init' => 1,
'-e' => 1,
'-seg1addr' => 1,
'-bundle_loader' => 1,
'-multiply_defined' => 1,
'-sectorder' => 3,
'--param' => 1,
'-u' => 1
);
my %LangMap = (
'c' => 'c',
'cpp' => 'c++',
'cc' => 'c++',
'i' => 'c-cpp-output',
'm' => 'objective-c',
'mi' => 'objective-c-cpp-output'
);
##----------------------------------------------------------------------------##
# Main Logic.
##----------------------------------------------------------------------------##
my $Action = 'link';
my @CompileOpts;
my @LinkOpts;
my @Files;
my $Lang;
my $Output;
# Forward arguments to gcc.
my $Status = system("gcc",@ARGV);
if ($Status) { exit($Status); }
# Get the analysis options.
my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'};
if (!defined($Analyses)) { $Analyses = '-checker-cfref'; }
# Determine the level of verbosity.
my $Verbose = 0;
if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; }
if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; }
# Determine what clang executable to use.
my $Clang = $ENV{'CLANG'};
if (!defined $Clang) { $Clang = 'clang'; }
# Get the HTML output directory.
my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'};
# Process the arguments.
foreach (my $i = 0; $i < scalar(@ARGV); ++$i) {
my $Arg = $ARGV[$i];
# Modes ccc-analyzer supports
if ($Arg eq '-E') { $Action = 'preprocess'; }
elsif ($Arg eq '-c') { $Action = 'compile'; }
elsif ($Arg =~ /^-print-prog-name/) { exit 0; }
# Options with possible arguments that should pass through to compiler.
if (defined $CompileOptionMap{$Arg}) {
my $Cnt = $CompileOptionMap{$Arg};
push @CompileOpts,$Arg;
while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; }
next;
}
# Options with possible arguments that should pass through to linker.
if (defined $LinkerOptionMap{$Arg}) {
my $Cnt = $LinkerOptionMap{$Arg};
push @LinkOpts,$Arg;
while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; }
next;
}
# Options with possible arguments that should pass through to both compiler
# and the linker.
if (defined $CompilerLinkerOptionMap{$Arg}) {
my $Cnt = $CompilerLinkerOptionMap{$Arg};
push @CompileOpts,$Arg;
push @LinkOpts,$Arg;
while ($Cnt > 0) {
++$i; --$Cnt;
push @CompileOpts, $ARGV[$i];
push @LinkOpts, $ARGV[$i];
}
next;
}
# Ignored options.
if (defined $IgnoredOptionMap{$Arg}) {
my $Cnt = $IgnoredOptionMap{$Arg};
while ($Cnt > 0) {
++$i; --$Cnt;
}
next;
}
# Compile mode flags.
if ($Arg =~ /^-[D,I,U](.*)$/) {
my $Tmp = $Arg;
if ($1 eq '') {
# FIXME: Check if we are going off the end.
++$i;
$Tmp = $Arg . $ARGV[$i];
}
push @CompileOpts,$Tmp;
next;
}
# Language.
if ($Arg eq '-x') {
$Lang = $ARGV[$i+1];
++$i; next;
}
# Output file.
if ($Arg eq '-o') {
++$i;
$Output = $ARGV[$i];
next;
}
# Get the link mode.
if ($Arg =~ /^-[l,L,O]/) {
if ($Arg eq '-O') { push @LinkOpts,'-O1'; }
elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; }
else { push @LinkOpts,$Arg; }
next;
}
if ($Arg =~ /^-std=/) {
push @CompileOpts,$Arg;
next;
}
# if ($Arg =~ /^-f/) {
# # FIXME: Not sure if the remaining -fxxxx options have no arguments.
# push @CompileOpts,$Arg;
# push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts.
# }
# Get the compiler/link mode.
if ($Arg =~ /^-F(.+)$/) {
my $Tmp = $Arg;
if ($1 eq '') {
# FIXME: Check if we are going off the end.
++$i;
$Tmp = $Arg . $ARGV[$i];
}
push @CompileOpts,$Tmp;
push @LinkOpts,$Tmp;
next;
}
# Input files.
if ($Arg eq '-filelist') {
# FIXME: Make sure we aren't walking off the end.
open(IN, $ARGV[$i+1]);
while (<IN>) { s/\015?\012//; push @Files,$_; }
close(IN);
++$i; next;
}
if (!($Arg =~ /^-/)) {
push @Files,$Arg; next;
}
}
if ($Action eq 'compile' or $Action eq 'link') {
foreach my $file (@Files) {
# Determine the language for the file.
my $FileLang = $Lang;
if (!defined($FileLang)) {
# Infer the language from the extension.
if ($file =~ /[.]([^.]+)$/) {
$FileLang = $LangMap{$1};
}
}
next if (!defined $FileLang);
my @AnalyzeArgs;
if ($FileLang ne 'unknown') {
push @AnalyzeArgs,'-x';
push @AnalyzeArgs,$FileLang;
}
push @AnalyzeArgs,@CompileOpts;
push @AnalyzeArgs,$file;
Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output,
$Verbose, $HtmlDir, $file, $Analyses);
}
}