my $UseColor = (defined $TERM and $TERM =~ 'xterm-.*color' and -t STDOUT
and defined $ENV{'SCAN_BUILD_COLOR'});
-# Portability: getpwuid is not implemented for Win32 (see Perl language
+# Portability: getpwuid is not implemented for Win32 (see Perl language
# reference, perlport), use getlogin instead.
my $UserName = HtmlEscape(getlogin() || getpwuid($<) || 'unknown');
my $HostName = HtmlEscape(hostname() || 'unknown');
}
else {
print "$Prog: @_";
- }
+ }
}
sub ErrorDiag {
print STDERR RESET;
} else {
print STDERR "$Prog: @_";
- }
+ }
}
sub DiagCrashes {
# GetHTMLRunDir - Construct an HTML directory name for the current sub-run.
##----------------------------------------------------------------------------##
-sub GetHTMLRunDir {
- die "Not enough arguments." if (@_ == 0);
- my $Dir = shift @_;
+sub GetHTMLRunDir {
+ die "Not enough arguments." if (@_ == 0);
+ my $Dir = shift @_;
my $TmpMode = 0;
if (!defined $Dir) {
$Dir = $ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || "/tmp";
$TmpMode = 1;
}
-
+
# Chop off any trailing '/' characters.
while ($Dir =~ /\/$/) { chop $Dir; }
# Get current date and time.
- my @CurrentTime = localtime();
+ my @CurrentTime = localtime();
my $year = $CurrentTime[5] + 1900;
my $day = $CurrentTime[3];
my $month = $CurrentTime[4] + 1;
my $TimeString = sprintf("%02d%02d%02d", $hour, $min, $sec);
my $DateString = sprintf("%d-%02d-%02d-%s-$$",
$year, $month, $day, $TimeString);
-
- # Determine the run number.
+
+ # Determine the run number.
my $RunNumber;
-
- if (-d $Dir) {
+
+ if (-d $Dir) {
if (! -r $Dir) {
DieDiag("directory '$Dir' exists but is not readable.\n");
- }
- # Iterate over all files in the specified directory.
- my $max = 0;
+ }
+ # Iterate over all files in the specified directory.
+ my $max = 0;
opendir(DIR, $Dir);
my @FILES = grep { -d "$Dir/$_" } readdir(DIR);
closedir(DIR);
next if ($x[2] != $day);
next if ($x[3] != $TimeString);
next if ($x[4] != $$);
-
+
if ($x[5] > $max) {
$max = $x[5];
- }
+ }
}
-
+
$RunNumber = $max + 1;
}
else {
-
+
if (-x $Dir) {
DieDiag("'$Dir' exists but is not a directory.\n");
}
DieDiag("The directory '/tmp' does not exist or cannot be accessed.\n");
}
- # $Dir does not exist. It will be automatically created by the
- # clang driver. Set the run number to 1.
+ # $Dir does not exist. It will be automatically created by the
+ # clang driver. Set the run number to 1.
$RunNumber = 1;
}
-
+
die "RunNumber must be defined!" if (!defined $RunNumber);
-
+
# Append the run number.
my $NewDir;
if ($TmpMode) {
}
sub SetHtmlEnv {
-
+
die "Wrong number of arguments." if (scalar(@_) != 2);
-
+
my $Args = shift;
my $Dir = shift;
-
+
die "No build command." if (scalar(@$Args) == 0);
-
+
my $Cmd = $$Args[0];
if ($Cmd =~ /configure/ || $Cmd =~ /autogen/) {
return;
}
-
+
if ($Verbose) {
Diag("Emitting reports for this run to '$Dir'.\n");
}
-
+
$ENV{'CCC_ANALYZER_HTML'} = $Dir;
}
sub ComputeDigest {
my $FName = shift;
- DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
-
+ DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
+
# Use Digest::MD5. We don't have to be cryptographically secure. We're
# just looking for duplicate files that come from a non-malicious source.
# We use Digest::MD5 because it is a standard Perl module that should
- # come bundled on most systems.
+ # come bundled on most systems.
open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
binmode FILE;
my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
close(FILE);
-
- # Return the digest.
+
+ # Return the digest.
return $Result;
}
$Prefix = $x;
return;
}
-
+
chop $Prefix while (!($x =~ /^\Q$Prefix/));
}
my %AlreadyScanned;
sub ScanFile {
-
+
my $Index = shift;
my $Dir = shift;
my $FName = shift;
my $Stats = shift;
-
+
# Compute a digest for the report file. Determine if we have already
# scanned a file that looks just like it.
-
+
my $digest = ComputeDigest("$Dir/$FName");
if (defined $AlreadyScanned{$digest}) {
system ("rm", "-f", "$Dir/$FName");
return;
}
-
+
$AlreadyScanned{$digest} = 1;
-
+
# At this point the report file is not world readable. Make it happen.
system ("chmod", "644", "$Dir/$FName");
-
+
# Scan the report file for tags.
open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
$BugPathLength = $1;
}
elsif (/<!-- BUGLINE (.*) -->$/) {
- $BugLine = $1;
+ $BugLine = $1;
}
elsif (/<!-- BUGCATEGORY (.*) -->$/) {
$BugCategory = $1;
}
close(IN);
-
+
if (!defined $BugCategory) {
$BugCategory = "Other";
}
AddStatLine($BugDescription, $Stats, $BugFile);
return;
}
-
+
push @$Index,[ $FName, $BugCategory, $BugType, $BugFile, $BugLine,
$BugPathLength ];
}
my $Dir = shift;
my $JS = Cwd::realpath("$RealBin/sorttable.js");
-
+
DieDiag("Cannot find 'sorttable.js'.\n")
- if (! -r $JS);
+ if (! -r $JS);
system ("cp", $JS, "$Dir");
DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
if (! -r "$Dir/sorttable.js");
-
+
my $CSS = Cwd::realpath("$RealBin/scanview.css");
-
+
DieDiag("Cannot find 'scanview.css'.\n")
if (! -r $CSS);
my @filesFound;
my $baseDir;
-sub FileWanted {
+sub FileWanted {
my $baseDirRegEx = quotemeta $baseDir;
my $file = $File::Find::name;
if ($file =~ /report-.*\.html$/) {
}
sub Postprocess {
-
+
my $Dir = shift;
my $BaseDir = shift;
my $AnalyzerStats = shift;
my $KeepEmpty = shift;
-
+
die "No directory specified." if (!defined $Dir);
-
+
if (! -d $Dir) {
Diag("No bugs found.\n");
return 0;
Diag("No bugs found.\n");
return 0;
}
-
- # Scan each report file and build an index.
+
+ # Scan each report file and build an index.
my @Index;
my @Stats;
foreach my $file (@filesFound) { ScanFile(\@Index, $Dir, $file, \@Stats); }
-
+
# Scan the failures directory and use the information in the .info files
# to update the common prefix directory.
my @failures;
opendir(DIR, "$Dir/failures");
@failures = grep { /[.]info.txt$/ && !/attribute_ignored/; } readdir(DIR);
closedir(DIR);
- opendir(DIR, "$Dir/failures");
+ opendir(DIR, "$Dir/failures");
@attributes_ignored = grep { /^attribute_ignored/; } readdir(DIR);
closedir(DIR);
foreach my $file (@failures) {
my $Path = <IN>;
if (defined $Path) { UpdatePrefix($Path); }
close IN;
- }
+ }
}
-
- # Generate an index.html file.
- my $FName = "$Dir/index.html";
+
+ # Generate an index.html file.
+ my $FName = "$Dir/index.html";
open(OUT, ">", $FName) or DieDiag("Cannot create file '$FName'\n");
-
+
# Print out the header.
-
+
print OUT <<ENDTEXT;
<html>
<head>
}
function returnObjById( id ) {
- if (document.getElementById)
+ if (document.getElementById)
var returnVar = document.getElementById(id);
else if (document.all)
var returnVar = document.all[id];
- else if (document.layers)
+ else if (document.layers)
var returnVar = document.layers[id];
- return returnVar;
+ return returnVar;
}
var NumUnchecked = 0;
if (defined $BuildName) {
print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
}
-
+
my $TotalBugs = scalar(@Index);
print OUT <<ENDTEXT;
<table>
<thead><tr><td>Bug Type</td><td>Quantity</td><td class="sorttable_nosort">Display?</td></tr></thead>
<tr style="font-weight:bold"><td class="SUMM_DESC">All Bugs</td><td class="Q">$TotalBugs</td><td><center><input type="checkbox" id="AllBugsCheck" onClick="CopyCheckedStateToCheckButtons(this);" checked/></center></td></tr>
ENDTEXT
-
+
my $last_category;
for my $key (
my $res = $x->[1] cmp $y->[1];
$res = $x->[2] cmp $y->[2] if ($res == 0);
$res
- } keys %Totals )
+ } keys %Totals )
{
my $val = $Totals{$key};
my $category = $val->[1];
if (!defined $last_category or $last_category ne $category) {
$last_category = $category;
print OUT "<tr><th>$category</th><th colspan=2></th></tr>\n";
- }
+ }
my $x = lc $key;
$x =~ s/[ ,'":\/()]+/_/g;
print OUT "<tr><td class=\"SUMM_DESC\">";
my $regex;
my $InFileRegex;
my $InFilePrefix = "File:</td><td>";
-
- if (defined $prefix) {
- $regex = qr/^\Q$prefix\E/is;
+
+ if (defined $prefix) {
+ $regex = qr/^\Q$prefix\E/is;
$InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
- }
+ }
for my $row ( sort { $a->[2] cmp $b->[2] } @Index ) {
my $x = "$row->[1]:$row->[2]";
$x = lc $x;
$x =~ s/[ ,'":\/()]+/_/g;
-
+
my $ReportFile = $row->[0];
-
+
print OUT "<tr class=\"bt_$x\">";
print OUT "<td class=\"DESC\">";
print OUT $row->[1];
print OUT "<td class=\"DESC\">";
print OUT $row->[2];
print OUT "</td>";
-
- # Update the file prefix.
+
+ # Update the file prefix.
my $fname = $row->[3];
if (defined $regex) {
$fname =~ s/$regex//;
UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
}
-
- print OUT "<td>";
+
+ print OUT "<td>";
my @fname = split /\//,$fname;
if ($#fname > 0) {
while ($#fname >= 0) {
}
else {
print OUT $fname;
- }
+ }
print OUT "</td>";
-
+
# Print out the quantities.
for my $j ( 4 .. 5 ) {
- print OUT "<td class=\"Q\">$row->[$j]</td>";
+ print OUT "<td class=\"Q\">$row->[$j]</td>";
}
-
+
# Print the rest of the columns.
for (my $j = 6; $j <= $#{$row}; ++$j) {
print OUT "<td>$row->[$j]</td>"
# Emit the "View" link.
print OUT "<td><a href=\"$ReportFile#EndPath\">View Report</a></td>";
-
+
# Emit REPORTBUG markers.
print OUT "\n<!-- REPORTBUG id=\"$ReportFile\" -->\n";
-
+
# End the row.
print OUT "</tr>\n";
}
-
+
print OUT "</tbody>\n</table>\n\n";
}
if (scalar (@failures) || scalar(@attributes_ignored)) {
print OUT "<h2>Analyzer Failures</h2>\n";
-
+
if (scalar @attributes_ignored) {
print OUT "The analyzer's parser ignored the following attributes:<p>\n";
print OUT "<table>\n";
}
print OUT "</table>\n";
}
-
+
if (scalar @failures) {
print OUT "<p>The analyzer had problems processing the following files:</p>\n";
print OUT "<table>\n";
print OUT " <!-- REPORTPROBLEM src=\"$srcfile\" file=\"failures/$ppfile\" clangfile=\"failures/$ppfile_clang\" stderr=\"failures/$ppfile.stderr.txt\" info=\"failures/$ppfile.info.txt\" -->\n";
}
print OUT "</table>\n";
- }
+ }
print OUT "<p>Please consider submitting preprocessed files as <a href=\"http://clang-analyzer.llvm.org/filing_bugs.html\">bug reports</a>. <!-- REPORTCRASHES --> </p>\n";
}
-
- print OUT "</body></html>\n";
+
+ print OUT "</body></html>\n";
close(OUT);
CopyFiles($Dir);
if ($Num > 0 && -r "$Dir/index.html") {
Diag("Run 'scan-view $Dir' to examine bug reports.\n");
}
-
+
DiagCrashes($Dir) if (scalar @failures || scalar @attributes_ignored);
-
+
return $Num;
}
sub AddIfNotPresent {
my $Args = shift;
- my $Arg = shift;
+ my $Arg = shift;
my $found = 0;
-
+
foreach my $k (@$Args) {
if ($k eq $Arg) {
$found = 1;
last;
}
}
-
+
if ($found == 0) {
push @$Args, $Arg;
}
}
# The flag corresponding to the --override-compiler command line option.
-my $OverrideCompiler = 0;
+my $OverrideCompiler = 0;
sub RunXcodebuild {
my $Args = shift;
}
}
close(DETECT_XCODE);
-
- # If --override-compiler is explicitely requested, resort to the old
+
+ # If --override-compiler is explicitely requested, resort to the old
# behavior regardless of Xcode version.
if ($OverrideCompiler) {
$oldBehavior = 1;
}
-
+
if ($oldBehavior == 0) {
my $OutputDir = $Options->{"OUTPUT_DIR"};
my $CLANG = $Options->{"CLANG"};
return (system(@$Args) >> 8);
}
-
+
# Default to old behavior where we insert a bogus compiler.
SetEnv($Options);
-
+
# Check if using iPhone SDK 3.0 (simulator). If so the compiler being
# used should be gcc-4.2.
if (!defined $ENV{"CCC_CC"}) {
# Disable PCH files until clang supports them.
AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
-
+
# When 'CC' is set, xcodebuild uses it to do all linking, even if we are
# linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
# (via c++-analyzer) when linking such files.
$ENV{"LDPLUSPLUS"} = $CXXAnalyzer;
-
- return (system(@$Args) >> 8);
+
+ return (system(@$Args) >> 8);
}
-sub RunBuildCommand {
+sub RunBuildCommand {
my $Args = shift;
my $IgnoreErrors = shift;
my $Cmd = $Args->[0];
# Setup the environment.
SetEnv($Options);
-
- if ($Cmd =~ /(.*\/?gcc[^\/]*$)/ or
+
+ if ($Cmd =~ /(.*\/?gcc[^\/]*$)/ or
$Cmd =~ /(.*\/?cc[^\/]*$)/ or
$Cmd =~ /(.*\/?llvm-gcc[^\/]*$)/ or
- $Cmd =~ /(.*\/?clang$)/ or
+ $Cmd =~ /(.*\/?clang$)/ or
$Cmd =~ /(.*\/?ccc-analyzer[^\/]*$)/) {
if (!($Cmd =~ /ccc-analyzer/) and !defined $ENV{"CCC_CC"}) {
- $ENV{"CCC_CC"} = $1;
+ $ENV{"CCC_CC"} = $1;
}
-
+
shift @$Args;
unshift @$Args, $CCAnalyzer;
}
- elsif ($Cmd =~ /(.*\/?g\+\+[^\/]*$)/ or
+ elsif ($Cmd =~ /(.*\/?g\+\+[^\/]*$)/ or
$Cmd =~ /(.*\/?c\+\+[^\/]*$)/ or
$Cmd =~ /(.*\/?llvm-g\+\+[^\/]*$)/ or
$Cmd =~ /(.*\/?clang\+\+$)/ or
$Cmd =~ /(.*\/?c\+\+-analyzer[^\/]*$)/) {
if (!($Cmd =~ /c\+\+-analyzer/) and !defined $ENV{"CCC_CXX"}) {
- $ENV{"CCC_CXX"} = $1;
- }
+ $ENV{"CCC_CXX"} = $1;
+ }
shift @$Args;
unshift @$Args, $CXXAnalyzer;
}
AddIfNotPresent($Args,"-k");
AddIfNotPresent($Args,"-i");
}
- }
+ }
return (system(@$Args) >> 8);
}
##----------------------------------------------------------------------------##
sub DisplayHelp {
-
+
print <<ENDTEXT;
USAGE: $Prog [options] <build command> [build options]
OPTIONS:
-analyze-headers
-
+
Also analyze functions in #included files. By default, such functions
are skipped unless they are called by functions within the main source file.
-
+
-o <output location>
-
+
Specifies the output directory for analyzer reports. Subdirectories will be
created as needed to represent separate "runs" of the analyzer. If this
option is not specified, a directory is created in /tmp (TMPDIR on Mac OS X)
to store the reports.
- -h
+ -h
--help
Display this message.
-k
--keep-going
-
+
Add a "keep on going" option to the specified build command. This option
currently supports make and xcodebuild. This is a convenience option; one
can specify this behavior directly using build options.
title will be used.
-plist
-
+
By default the output of scan-build is a set of HTML files. This option
outputs the results as a set of .plist files.
-
+
-plist-html
-
+
By default the output of scan-build is a set of HTML files. This option
outputs the results as a set of HTML and .plist files.
-
+
--status-bugs
-
+
By default, the exit status of scan-build is the same as the executed build
command. Specifying this option causes the exit status of scan-build to be 1
if it found potential bugs and 0 otherwise.
- --use-cc [compiler path]
+ --use-cc [compiler path]
--use-cc=[compiler path]
-
+
scan-build analyzes a project by interposing a "fake compiler", which
executes a real compiler for compilation and the static analyzer for analysis.
Because of the current implementation of interposition, scan-build does not
know what compiler your project normally uses. Instead, it simply overrides
the CC environment variable, and guesses your default compiler.
-
+
In the future, this interposition mechanism to be improved, but if you need
scan-build to use a specific compiler for *compilation* then you can use
this option to specify a path to that compiler.
--use-c++ [compiler path]
--use-c++=[compiler path]
-
+
This is the same as "-use-cc" but for C++ code.
-
+
-v
-
+
Enable verbose output from scan-build. A second and third '-v' increases
verbosity.
ADVANCED OPTIONS:
-no-failure-reports
-
+
Do not create a 'failures' subdirectory that includes analyzer crash reports
and preprocessed source files.
-stats
-
+
Generates visitation statistics for the project being analyzed.
-maxloop <loop count>
-
+
Specifiy the number of times a block can be visited before giving up.
Default is 4. Increase for more comprehensive coverage at a cost of speed.
-
+
-internal-stats
-
+
Generate internal analyzer statistics.
-
- --use-analyzer [Xcode|path to clang]
+
+ --use-analyzer [Xcode|path to clang]
--use-analyzer=[Xcode|path to clang]
-
+
scan-build uses the 'clang' executable relative to itself for static
analysis. One can override this behavior with this option by using the
'clang' packaged with Xcode (on OS X) or from the PATH.
Don't remove the build results directory even if no issues were reported.
- --override-compiler
- Always resort to the ccc-analyzer even when better interposition methods
+ --override-compiler
+ Always resort to the ccc-analyzer even when better interposition methods
are available.
-analyzer-config <options>
Provide options to pass through to the analyzer's -analyzer-config flag.
-
+
CONTROLLING CHECKERS:
A default group of checkers are always run unless explicitly disabled.
-enable-checker [checker name]
-disable-checker [checker name]
-
+
LOADING CHECKERS:
Loading external checkers using the clang plugin interface:
close FROM_CHILD;
open(STDOUT,">&", \*TO_PARENT);
open(STDERR,">&", \*TO_PARENT);
- exec $Clang, ( @PluginLoadCommandline_xclang, '--analyze', '-x', $lang, '-', '-###');
+ exec $Clang, ( @PluginLoadCommandline_xclang, '--analyze', '-x', $lang, '-', '-###');
}
close(TO_PARENT);
while(<FROM_CHILD>) {
# append a dot, if an additional domain is added in the next iteration
$aggregate .= ".";
}
-
+
if ($enabled) {
print " + ";
}
EXAMPLE
scan-build -o /tmp/myhtmldir make -j4
-
+
The above example causes analysis reports to be deposited into a subdirectory
of "/tmp/myhtmldir" and to run "make" with the "-j4" option. A different
subdirectory is created each time scan-build analyzes a project. The analyzer
}
while (@ARGV) {
-
+
# Scan for options we recognize.
-
+
my $arg = $ARGV[0];
if ($arg eq "-h" or $arg eq "--help") {
shift @ARGV;
next;
}
-
+
if ($arg eq '-analyze-headers') {
- shift @ARGV;
+ shift @ARGV;
$AnalyzeHeaders = 1;
next;
}
-
+
if ($arg eq "-o") {
shift @ARGV;
-
+
if (!@ARGV) {
DieDiag("'-o' option requires a target directory name.\n");
}
-
+
# Construct an absolute path. Uses the current working directory
# as a base if the original path was not absolute.
$HtmlDir = abs_path(shift @ARGV);
-
+
next;
}
next;
}
-
+
if ($arg eq "-k" or $arg eq "--keep-going") {
shift @ARGV;
$IgnoreErrors = 1;
if ($arg =~ /^--use-cc(=(.+))?$/) {
shift @ARGV;
my $cc;
-
+
if (!defined $2 || $2 eq "") {
if (!@ARGV) {
DieDiag("'--use-cc' option requires a compiler executable name.\n");
else {
$cc = $2;
}
-
+
$ENV{"CCC_CC"} = $cc;
next;
}
-
+
if ($arg =~ /^--use-c\+\+(=(.+))?$/) {
shift @ARGV;
- my $cxx;
-
+ my $cxx;
+
if (!defined $2 || $2 eq "") {
if (!@ARGV) {
DieDiag("'--use-c++' option requires a compiler executable name.\n");
else {
$cxx = $2;
}
-
+
$ENV{"CCC_CXX"} = $cxx;
next;
}
-
+
if ($arg eq "-v") {
shift @ARGV;
$Verbose++;
next;
}
-
+
if ($arg eq "-V" or $arg eq "--view") {
shift @ARGV;
- $ViewResults = 1;
+ $ViewResults = 1;
next;
}
-
+
if ($arg eq "--status-bugs") {
shift @ARGV;
$ExitStatusFoundBugs = 1;
$StoreModel = shift @ARGV;
next;
}
-
+
if ($arg eq "-constraints") {
shift @ARGV;
$ConstraintsModel = shift @ARGV;
$InternalStats = 1;
next;
}
-
+
if ($arg eq "-plist") {
shift @ARGV;
$OutputFormat = "plist";
$OverrideCompiler = 1;
next;
}
-
+
DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
-
+
last;
}
" Consider using --use-analyzer to pick a version of 'clang' to use for static analysis.\n");
}
}
-}
-else {
+}
+else {
if ($AnalyzerDiscoveryMethod =~ /^[Xx]code$/) {
my $xcrun = `which xcrun`;
chomp $xcrun;
DieDiag("Cannot find 'xcrun' to find 'clang' for analysis.\n");
}
$Clang = `$xcrun -toolchain XcodeDefault -find clang`;
- chomp $Clang;
+ chomp $Clang;
if ($Clang eq "") {
- DieDiag("No 'clang' executable found by 'xcrun'\n");
+ DieDiag("No 'clang' executable found by 'xcrun'\n");
}
}
else {
my $Cmd = "$AbsRealBin/libexec/ccc-analyzer";
my $CmdCXX = "$AbsRealBin/libexec/c++-analyzer";
-# Portability: use less strict but portable check -e (file exists) instead of
+# Portability: use less strict but portable check -e (file exists) instead of
# non-portable -x (file is executable). On some windows ports -x just checks
-# file extension to determine if a file is executable (see Perl language
+# file extension to determine if a file is executable (see Perl language
# reference, perlport)
if (!defined $Cmd || ! -e $Cmd) {
$Cmd = "$AbsRealBin/ccc-analyzer";
}
exit $ExitStatus;
-