forked from bloomberg/bde_verify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbb_cppverify
More file actions
executable file
·480 lines (414 loc) · 16.6 KB
/
bb_cppverify
File metadata and controls
executable file
·480 lines (414 loc) · 16.6 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env perl
# bb_cppverify -*-perl-*-
# Bb_cppverify is a static analysis tool that verifies that source code adheres
# to the BDE coding standards. Many of the checks are specific to Bloomberg LP
# and this wrapper script contains default options for the Bloomberg LP
# environment.
use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case);
use Cwd;
my @known_systems = ('AIX', 'HP-UX', 'Linux', 'SunOS', 'Darwin');
chomp(my $system = `uname -s`);
$system = 'Linux' unless grep(/$system/, @known_systems);
my $cc = ($system eq 'Darwin') ? 'clang++' : 'g++';
my @known_hw = ('powerpc', 'ia64', 'x86_64', 'sparc', 'i386');
chomp(my $hw = `uname -p`);
$hw = 'x86_64' unless grep(/$hw/, @known_hw);
my $bb = "/bb/build/$system-$hw-64/release/robolibs/trunk";
$bb = '' unless -d $bb;
my $me = Cwd::abs_path(${0});
my $pt = $me;
$pt =~ s{(bin/|scripts/)?[^/]*$}{};
my $nm = ${0};
$nm =~ s{.*/}{};
my $macro = ${nm};
$macro =~ s{^([^.]*).*}{\U$1\E};
my $config = "${pt}etc/bde-verify/${nm}.cfg";
$config = "${pt}${nm}.cfg" unless -r $config;
my $enm = "bde_verify_bin";
my $exe = "${pt}libexec/bde-verify/$enm";
$exe = "${pt}$system-g++/$enm" unless -x $exe;
$exe = "${pt}$system-clang++/$enm" unless -x $exe;
$exe = "${pt}bin/$enm" unless -x $exe;
$exe = "${pt}scripts/$enm" unless -x $exe;
$exe = "${pt}$enm" unless -x $exe;
my @cl;
chomp($cc = $ENV{CXX} || `which $cc`);
my $debug = "";
my $verbose = "";
my $version = "";
my $help = "";
my $definc = -1;
my $defdef = -1;
my $logdir = "/home/hrosen4/mbig/logs/${nm}";
my $nsa = -d $logdir || 0;
my @incs;
my @defs;
my @wflags;
my @lflags = (
"cxx-exceptions",
"exceptions",
"diagnostics-show-note-include-stack",
"diagnostics-show-option",
"error-limit=0",
);
my $dummy;
my @dummy;
my $std = "";
my $warnoff;
my $diagnose = "component";
my $rwd;
my $rwf;
my $tag;
my $ovr = 0;
my $m32;
my $m64;
my $diff = "";
my $comp_db_dir = "";
my $command = join " \\\n ", $0, map { join "\\ ", split(/ /, $_, -1) } @ARGV;
sub defdef() { $defdef = 1 - ($comp_db_dir ne "") if $defdef == -1; $defdef }
sub definc() { $definc = 1 - ($comp_db_dir ne "") if $definc == -1; $definc }
sub usage()
{
print "
usage: $0 [options] [additional compiler options] file.cpp ...
-I{directory}
Add {directory} to the include path.
-D{macro}
Define {macro} for the compilation.
-W{warning}
Enable the specified compiler {warning}.
-w
Disable compiler warnings (but not $nm warnings).
-f{flag}
Pass the specifed {flag} through to the compiler. This is
for often-specified compiler options such as -fexceptions.
-config config_file [$config]
Specify the file containing $nm configuration options.
-cl {config_line}
Treat the specified {config_line} as if it were appended
to the configuration file.
-bb {directory} [$bb]
Specify the trunk {directory} where Bloomberg software is
installed. $nm will add directories to the include path
from here unless -nodefdef is specified.
-exe {program} [$exe]
Specify the underlying $nm executable {program} that this
script will invoke. This is generally needed only by
developers testing new versions of $nm.
-cc /path/to/g++ [$cc]
Sepecify the location of a GNU C++ compiler that $nm will
use to find system and compiler-dependent header files.
This will default to the value of the CXX environment
variable if present, and a g++ found in the path otherwise.
Typically, you shoud specify the same version that you use
to build your software.
-diff {file} (- for stdin) [$diff]
Specify a {file} in 'diff' format (such as might be produced
by running 'git diff'). If this is specified, $nm warnigs
will be produced only for lines that appear in the 'diff'
file. This is used to restrict $nm output to only those
lines that have been changed instead of the entire file.
If '-' is used as the file name, $nm will read standard
input for diffs, facilitating piping:
% git diff | $nm -diff - file.cpp...
-[no]definc [@{[definc()]}]
Have $nm supply or not supply default include paths.
-[no]defdef [@{[defdef()]}]
Have $nm supply or not supply default macro definitions.
The BDE_VERIFY macro will be defined regardless.
-[no]ovr
Have $nm define or undefine BSL_OVERRIDES_STD. Note that
this macro is now obsolete, so -noovr is the default.
-[no]nsa [$nsa]
$nm logs its uses for tracking and evaluation purposes.
Specifying -nonsa turns off this logging.
-p {directory} [$comp_db_dir]
Specify a {directory} containing a compilation database
(named 'compile_commands.json') from which $nm will
extract -I and -D options for the files being checked.
(Compilation databases are build artifacts created by
request during compilation.) When using this option,
-nodefdef and -nodefinc become the default to avoid
collisions with the compilation database.
-rewrite-dir {directory}
-rewrite {directory}
-rd {directory}
Certain $nm checks optionally produce rewritten files
containing corrections or changes to the input files.
To enable rewriting, specify this option, and rewritten
files will be placed in the specified {directory} with
'-rewritten' affixed to their names.
-rewrite-file {file}
For those $nm checks that produce rewritten files, use
the specified {file} to accumulate changes that will be
eventually written out when $nm is invoked with the
'-rewrite-dir' option. This is not normally needed,
but might be used to accumulate changes from multiple
runs of different checks on the same files.
-diagnose {main,component,nogen,all}
Specify for which files $nm will display warnings.
main Warn only for files given on the command
line, but not on any files they include.
component Warn for any files given on the command
line and header files of that component
(e.g., if foo.cpp is specified on the
command line, $nm will warn about foo.cpp
and foo.h, but not about an also-included
bar.h). This is the default.
nogen Do not warn about files that appear to be
automatically generated.
all Warn about all files included in the
translation unit. Gneerally unuseful.
-std {version} [$std]
Pass the desired C++ standard {version} to the compiler.
-tag {string}
Include '[string]' in warning messages emitted by $nm.
This can help to identify which messages in the output
come from $nm and which from the underlying compiler.
-debug
Output a very noisy representation of the program while
processing it. Only meant for $nm developers.
-verbose
Output the full command line passed to the underlying
executable program. (Note that -D and -I options from
a specified compilation database are not visible here
because the database is processed by the underlying
program.)
-version
Display the version numbers of $nm and of the
LLVM/CLang software incorporated.
-help
-?
Display this information.
Usage Examples
--------------
$nm my_comp.cpp
Run $nm using all defaults. This will use a set of -D and -I
directives appropriate to the Bloomberg environment, and a
configuration file that has most checks enabled.
$nm -I/my/include/directory -DIMPORTANT_FLAG my_comp.cpp
Run $nm using all defaults, and also include custom macro
definitions and include paths.
$nm -nodefinc -I/my/include/directory -nodefdef -DFLAG my_comp.cpp
Run $nm using no default macros or include paths, supplying our
own. The default configuration file is still used.
(The $macro macro is always defined.)
$nm -p my/build/directory my_comp.cpp
Run $nm using no default macros or include paths, extracting
appropriate ones from a compilation database file named
compile_commands.json that must be present in the specified directory.
$nm -config my_bv.cfg my_comp.cpp
Run $nm using default macros and paths, but with a custom
configuration file.
$nm \\
-cl 'all off' \\
-cl 'check longlines on' \\
-cl 'check headline on' \\
my_comp.cpp
Run $nm using defaults, with all but the 'longlines' and
'headline' checks disabled.
$nm -cl 'append dictionary presquash untreeify' my_comp.cpp
Run $nm using defaults, modifying the 'dictionary' configuration
setting (used by the spelling checker) to include extra words.
$nm \\
-cl 'all off' \\
-cl 'check headline on' \\
-rewrite-dir my/rw/dir \\
my_comp.cpp
Run $nm using defaults, with all but the 'headline' checks
disabled. If the first line of the file is malformed, produce a
corrected version named 'my/rw/dir/my_comp.cpp-rewritten'.
For full documentation, see <https://bloomberg.github.io/bde_verify/>.
Invoked as:
$command
";
exit(1);
}
@ARGV = map { /^(-+[DIOWf])(.+)/ ? ($1, $2) : $_ } @ARGV;
GetOptions(
'bb=s' => \$bb,
'config=s' => \$config,
'cl=s' => \@cl,
'debug' => \$debug,
'help|?' => \$help,
'cc=s' => \$cc,
'diff=s' => \$diff,
'exe=s' => \$exe,
'verbose|v' => \$verbose,
'version' => \$version,
'definc!' => \$definc,
'defdef!' => \$defdef,
'ovr!' => \$ovr,
'nsa!' => \$nsa,
'rewrite|rewrite-dir|rd=s' => \$rwd,
'rewrite-file|rf=s' => \$rwf,
'w' => \$warnoff,
"I=s" => \@incs,
"D=s" => \@defs,
"W=s" => \@wflags,
"f=s" => \@lflags,
"std=s" => \$std,
"p=s" => \$comp_db_dir,
"toplevel" => sub { $diagnose = "main" },
"diagnose=s" => sub {
my %opts = ("main" => 1, "component" => 1, "nogen" => 1, "all" => 1);
die "Unknown value '$_[1]' for '-$_[0]='\n" unless $opts{$_[1]};
$diagnose = $_[1];
},
'tag=s' => \$tag,
'llvm=s' => \$dummy,
"m32" => \$m32,
"m64" => \$m64,
"pipe|pthread|MMD|g|c|S" => \$dummy,
"O|MF|o|march|mtune=s" => \@dummy,
) and !$help and ($#ARGV >= 0 or $version or $comp_db_dir) or usage();
sub xclang(@) { return map { ( "-Xclang", $_ ) } @_; }
sub plugin(@) { return xclang( "-plugin-arg-bde_verify", @_ ); }
my @config = plugin("config=$config") if $config;
my @debug = plugin("debug-on") if $debug;
my @tlo = plugin("diagnose=$diagnose");
my @rwd = plugin("rewrite-dir=$rwd") if $rwd;
print "No such directory $rwd\n" if $rwd and ! -d $rwd;
my @rwf = plugin("rewrite-file=$rwf") if $rwf;
my @tag = plugin("tool=$tag") if $tag;
my @diff = plugin("diff=$diff") if $diff;
my %uf = ( "no-strict-aliasing" => 1,
"PIC" => 1,
"asynchronous-unwind-tables" => 1,
"diagnostics-color=auto" => 1,
);
@lflags = map { "-f$_" } grep { not exists $uf{$_} } @lflags;
@wflags = map { "-W$_" } grep { not /,/ } @wflags;
unshift @wflags, "-w" if $warnoff;
push(@defs, ($macro));
push(@defs, (
"BDE_API",
"BDE_BUILD_TARGET_DBG",
"BDE_BUILD_TARGET_EXC",
"BDE_BUILD_TARGET_MT",
"BDE_DCL",
"BB_THREADED",
"_LINUX_SOURCE",
"_REENTRANT",
"_SYS_SYSMACROS_H",
"_THREAD_SAFE",
"__USE_XOPEN2K8",
)) if defdef();
if ($ovr == 1 || $ovr != 0 && defdef() && !grep(m{\bb[sd]l[^/]*$}, @ARGV)) {
push(@defs, "BSL_OVERRIDES_STD");
}
@defs = map { "-D$_" } @defs;
if ($ovr == 0) {
push(@defs, "-UBSL_OVERRIDES_STD");
}
my $gccdir = "";
if (-f "${cc}" && -x "${cc}") {
chomp($gccdir = qx{"${cc}" -### 2>&1 |
sed -n 's/.*--prefix=\\([^ ]*\\).*/\\1/p'});
}
elsif (-d "${cc}") {
$gccdir = $cc;
}
for (@ARGV) {
if (! -f) {
warn "Cannot find file $_\n";
} elsif (m{^(.*)/.*$}) {
push(@incs, $1) unless grep($_ eq $1, @incs);
} else {
push(@incs, ".") unless grep($_ eq ".", @incs);
}
}
for (@incs) { warn "cannot find directory $_\n" unless -d; }
push(@incs, (
"$bb/opt/bb/include",
"$bb/opt/bb/include/00depbuild",
"$bb/opt/bb/include/00deployed",
"$bb/opt/bb/include/00offlonly",
"/bb/build/share/stp/include/00offlonly",
"/bbsrc/thirdparty/bbit/include",
"$bb/opt/bb/include/stlport",
)) if $bb and definc();
@incs = map { ( "-I", $_ ) }
map { $_ eq Cwd::cwd() ? "." : $_ }
map { Cwd::abs_path($_) }
grep { -d } @incs;
my @pass;
push(@pass, "-m32") if $m32;
push(@pass, "-m64") if $m64;
push(@pass, "-std=$std") if $std;
if ($system eq 'Darwin') {
# Get the compiler include paths.
unshift(@incs, xclang(
map { ( "-isystem", $_ ) }
map { Cwd::abs_path($_) }
grep { -d }
map { chomp; split /\s/, $_; }
qx{ $cc -xc++ @pass -E -v /dev/null 2>&1 |
sed -n '/^#include/,/^End of search list/p' }));
}
push(@pass, "--gcc-toolchain=${gccdir}") if -d "${gccdir}";
push(@pass, "--version") if $version;
push(@pass, "--p=${comp_db_dir}") if -d "${comp_db_dir}";
@cl = map { plugin("config-line=$_") } @cl;
my @command = (
"$exe",
xclang("-plugin", "bde_verify"),
"-resource-dir", "${pt}include/bde-verify/clang",
"-fsyntax-only",
"-xc++",
@pass,
@debug,
@config,
@tlo,
@rwd,
@rwf,
@tag,
@diff,
@cl,
@defs,
@incs,
@lflags,
@wflags,
"--",
@ARGV);
print join(" \\\n ", map { join "\\ ", split(/ /, $_, -1) } @command), "\n"
if $verbose;
if ($nsa) {
my ($ss,$mm,$HH,$dd,$MM,$YYYY) = gmtime;
$YYYY += 1900;
$MM += 1;
my $now = sprintf('%.4d' . ('%.2d' x 5), $YYYY, $MM, $dd, $HH, $mm, $ss);
chomp(my $host = `uname -n`);
my $log = "${logdir}/${nm}-${now}-${host}-$$.log";
if (open my $fh, ">>", $log) {
print $fh $ENV{USER}, " ", $now, map { " $_" } @command, "\n";
close $fh;
}
}
# use BSD::Resource; setrlimit(RLIMIT_CORE, 0, 0);
if ($system eq "Darwin") {
# Not sure if L is 32-bits or 64-bits. It is harmless to assume that it's
# only 32 bits and that we need two to equal 64 bits.
my $struct = pack('LLLL', 0, 0, 0, 0);
syscall(195, 4, $struct);
} else {
syscall($system eq 'Linux' ? 160 : 128, 4, pack('LL', 0, 0));
}
exec @command;
## ----------------------------------------------------------------------------
## Copyright (C) 2014 Bloomberg Finance L.P.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ----------------------------- END-OF-FILE ----------------------------------