Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 2 | # |
| 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 Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 10 | # A script designed to interpose between the build system and gcc. It invokes |
| 11 | # both gcc and the static analyzer. |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 12 | # |
| 13 | ##===----------------------------------------------------------------------===## |
| 14 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 15 | use strict; |
| 16 | use warnings; |
Ted Kremenek | b315a39 | 2008-09-21 19:56:14 +0000 | [diff] [blame] | 17 | use Cwd qw/ getcwd abs_path /; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 18 | use File::Temp qw/ tempfile /; |
| 19 | use File::Path qw / mkpath /; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 20 | use File::Basename; |
Ted Kremenek | 1df35e3 | 2009-05-11 23:29:51 +0000 | [diff] [blame] | 21 | use Text::ParseWords; |
Ted Kremenek | 2ec5cd5 | 2008-08-25 20:44:31 +0000 | [diff] [blame] | 22 | |
| 23 | my $CC = $ENV{'CCC_CC'}; |
| 24 | if (!defined $CC) { $CC = "gcc"; } |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 25 | my $CleanupFile; |
| 26 | my $ResultFile; |
| 27 | |
| 28 | # Remove any stale files at exit. |
| 29 | END { |
| 30 | if (defined $CleanupFile && -z $CleanupFile) { |
| 31 | `rm -f $CleanupFile`; |
| 32 | } |
| 33 | } |
| 34 | |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 35 | ##----------------------------------------------------------------------------## |
| 36 | # Process Clang Crashes. |
| 37 | ##----------------------------------------------------------------------------## |
| 38 | |
| 39 | sub GetPPExt { |
| 40 | my $Lang = shift; |
| 41 | if ($Lang =~ /objective-c/) { return ".mi"; } |
| 42 | return ".i"; |
| 43 | } |
| 44 | |
Ted Kremenek | a4d8cde | 2009-04-28 17:37:44 +0000 | [diff] [blame] | 45 | # Set this to 1 if we want to include 'parser rejects' files. |
| 46 | my $IncludeParserRejects = 0; |
Ted Kremenek | 5daa3be | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 47 | my $ParserRejects = "Parser Rejects"; |
Ted Kremenek | a4d8cde | 2009-04-28 17:37:44 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 49 | my $AttributeIgnored = "Attribute Ignored"; |
Ted Kremenek | 5daa3be | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 51 | sub ProcessClangFailure { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 52 | my ($ClangCC, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 53 | my $Dir = "$HtmlDir/failures"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 54 | mkpath $Dir; |
Ted Kremenek | 5daa3be | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 55 | |
| 56 | my $prefix = "clang_crash"; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 57 | if ($ErrorType eq $ParserRejects) { |
| 58 | $prefix = "clang_parser_rejects"; |
| 59 | } |
| 60 | elsif ($ErrorType eq $AttributeIgnored) { |
| 61 | $prefix = "clang_attribute_ignored"; |
| 62 | } |
Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 63 | |
| 64 | # Generate the preprocessed file with cc (i.e., gcc). |
Ted Kremenek | 5daa3be | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 65 | my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX", |
| 66 | SUFFIX => GetPPExt($Lang), |
| 67 | DIR => $Dir); |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 2ec5cd5 | 2008-08-25 20:44:31 +0000 | [diff] [blame] | 69 | system $CC, @$Args, "-E", "-o", $PPFile; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 70 | close ($PPH); |
Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 71 | |
| 72 | # Generate the preprocessed file with clang. |
| 73 | my $PPFile_Clang = $PPFile; |
| 74 | $PPFile_Clang =~ s/[.](.+)$/.clang.$1/; |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 75 | system $ClangCC, @$Args, "-E", "-o", "$PPFile_Clang"; |
Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 76 | |
| 77 | # Create the info file. |
Ted Kremenek | 82a1253 | 2008-09-25 00:25:16 +0000 | [diff] [blame] | 78 | open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n"; |
Ted Kremenek | 5f2825f | 2008-09-21 18:04:49 +0000 | [diff] [blame] | 79 | print OUT abs_path($file), "\n"; |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 80 | print OUT "$ErrorType\n"; |
Ted Kremenek | 2dd7ad1 | 2008-08-18 20:55:25 +0000 | [diff] [blame] | 81 | print OUT "@$Args\n"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 82 | close OUT; |
Ted Kremenek | 82a1253 | 2008-09-25 00:25:16 +0000 | [diff] [blame] | 83 | `uname -a >> $PPFile.info.txt 2>&1`; |
| 84 | `$CC -v >> $PPFile.info.txt 2>&1`; |
Ted Kremenek | 9f9b1fd | 2008-09-12 22:49:36 +0000 | [diff] [blame] | 85 | system 'mv',$ofile,"$PPFile.stderr.txt"; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 86 | return (basename $PPFile); |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 87 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 89 | ##----------------------------------------------------------------------------## |
| 90 | # Running the analyzer. |
| 91 | ##----------------------------------------------------------------------------## |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 93 | # Determine what clang executable to use. |
| 94 | my $Clang = $ENV{'CLANG'}; |
| 95 | if (!defined $Clang) { $Clang = 'clang'; } |
| 96 | |
| 97 | sub GetCCArgs { |
| 98 | my $Args = shift; |
| 99 | |
| 100 | pipe (FROM_CHILD, TO_PARENT); |
| 101 | my $pid = fork(); |
| 102 | if ($pid == 0) { |
| 103 | close FROM_CHILD; |
| 104 | open(STDOUT,">&", \*TO_PARENT); |
| 105 | open(STDERR,">&", \*TO_PARENT); |
| 106 | exec $Clang, "-###", "-fsyntax-only", @$Args; |
| 107 | } |
| 108 | close(TO_PARENT); |
| 109 | my $line; |
| 110 | while (<FROM_CHILD>) { |
| 111 | next if (!/clang-cc/); |
| 112 | $line = $_; |
| 113 | } |
| 114 | |
| 115 | waitpid($pid,0); |
| 116 | close(FROM_CHILD); |
| 117 | |
| 118 | die "could not find clang-cc line\n" if (!defined $line); |
Ted Kremenek | 1df35e3 | 2009-05-11 23:29:51 +0000 | [diff] [blame] | 119 | # Strip the newline and initial whitspace |
| 120 | chomp $line; |
| 121 | $line =~ s/^\s+//; |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 621cc4e | 2009-05-15 04:07:18 +0000 | [diff] [blame] | 123 | my @items = quotewords('\s+', 1, $line); |
| 124 | for (my $i = 0 ; $ i < scalar(@items); ++$i) { |
| 125 | $items[$i] =~ s/^\"//; |
| 126 | $items[$i] =~ s/\"$//; |
| 127 | } |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 128 | my $cmd = shift @items; |
| 129 | die "cannot find 'clang-cc' in 'clang' command\n" if (!($cmd =~ /clang-cc/)); |
| 130 | return \@items; |
| 131 | } |
| 132 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 133 | sub Analyze { |
Ted Kremenek | 9a9e0d4 | 2009-05-15 04:20:31 +0000 | [diff] [blame] | 134 | my ($ClangCC, $Args, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir, |
| 135 | $file, $Analyses) = @_; |
Ted Kremenek | efaf191 | 2009-05-11 21:08:34 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 137 | $Args = GetCCArgs($Args); |
Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 139 | # Skip anything related to C++. |
| 140 | return if ($Lang =~ /c[+][+]/); |
Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 142 | my $RunAnalyzer = 0; |
| 143 | my $Cmd; |
| 144 | my @CmdArgs; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 145 | my @CmdArgsSansAnalyses; |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 147 | if ($Lang =~ /header/) { |
| 148 | exit 0 if (!defined ($Output)); |
| 149 | $Cmd = 'cp'; |
| 150 | push @CmdArgs,$file; |
| 151 | # Remove the PCH extension. |
| 152 | $Output =~ s/[.]gch$//; |
| 153 | push @CmdArgs,$Output; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 154 | @CmdArgsSansAnalyses = @CmdArgs; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 155 | } |
| 156 | else { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 157 | $Cmd = $ClangCC; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 158 | push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))'; |
| 159 | push @CmdArgs,@$Args; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 160 | @CmdArgsSansAnalyses = @CmdArgs; |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 161 | push @CmdArgs,'-analyze'; |
| 162 | push @CmdArgs,"-analyzer-display-progress"; |
Ted Kremenek | a2bdaf5 | 2009-02-26 17:36:31 +0000 | [diff] [blame] | 163 | push @CmdArgs,"-analyzer-eagerly-assume"; |
Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 164 | push @CmdArgs,(split /\s/,$Analyses); |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 165 | $RunAnalyzer = 1; |
| 166 | } |
| 167 | |
Ted Kremenek | 9a9e0d4 | 2009-05-15 04:20:31 +0000 | [diff] [blame] | 168 | # Add the analysis arguments passed down from scan-build. |
| 169 | foreach my $Arg (@$AnalyzeArgs) { |
| 170 | push @CmdArgs, $Arg; |
| 171 | } |
| 172 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 173 | my @PrintArgs; |
| 174 | my $dir; |
| 175 | |
| 176 | if ($Verbose) { |
| 177 | $dir = getcwd(); |
| 178 | print STDERR "\n[LOCATION]: $dir\n"; |
| 179 | push @PrintArgs,"'$Cmd'"; |
| 180 | foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; } |
| 181 | } |
| 182 | |
| 183 | if ($Verbose == 1) { |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 184 | # We MUST print to stderr. Some clients use the stdout output of |
| 185 | # gcc for various purposes. |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 186 | print STDERR join(' ',@PrintArgs); |
| 187 | print STDERR "\n"; |
| 188 | } |
| 189 | elsif ($Verbose == 2) { |
| 190 | print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n"; |
| 191 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 192 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 193 | if ($RunAnalyzer) { |
| 194 | if (defined $ResultFile) { |
| 195 | push @CmdArgs,'-o'; |
| 196 | push @CmdArgs, $ResultFile; |
| 197 | } |
| 198 | elsif (defined $HtmlDir) { |
| 199 | push @CmdArgs,'-o'; |
| 200 | push @CmdArgs, $HtmlDir; |
| 201 | } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 202 | } |
Ted Kremenek | 948e06b | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 203 | |
| 204 | if (defined $ENV{'CCC_UBI'}) { |
| 205 | push @CmdArgs,"--analyzer-viz-egraph-ubigraph"; |
| 206 | } |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 207 | |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 208 | # Capture the STDERR of clang and send it to a temporary file. |
| 209 | # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR. |
| 210 | # We save the output file in the 'crashes' directory if clang encounters |
| 211 | # any problems with the file. |
Ted Kremenek | 1346268 | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 212 | pipe (FROM_CHILD, TO_PARENT); |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 213 | my $pid = fork(); |
| 214 | if ($pid == 0) { |
Ted Kremenek | 1346268 | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 215 | close FROM_CHILD; |
| 216 | open(STDOUT,">&", \*TO_PARENT); |
| 217 | open(STDERR,">&", \*TO_PARENT); |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 218 | exec $Cmd, @CmdArgs; |
| 219 | } |
Ted Kremenek | 1346268 | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 220 | |
| 221 | close TO_PARENT; |
| 222 | my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir); |
| 223 | |
| 224 | while (<FROM_CHILD>) { |
| 225 | print $ofh $_; |
| 226 | print STDERR $_; |
| 227 | } |
| 228 | |
| 229 | waitpid($pid,0); |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 230 | close(FROM_CHILD); |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 231 | my $Result = $?; |
| 232 | |
| 233 | # Did the command die because of a signal? |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 234 | if ($Result & 127 and $Cmd eq $ClangCC and defined $HtmlDir) { |
| 235 | ProcessClangFailure($ClangCC, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir, |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 236 | "Crash", $ofile); |
| 237 | } |
| 238 | elsif ($Result) { |
Ted Kremenek | a4d8cde | 2009-04-28 17:37:44 +0000 | [diff] [blame] | 239 | if ($IncludeParserRejects && !($file =~/conftest/)) { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 240 | ProcessClangFailure($ClangCC, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir, |
Ted Kremenek | 5de4092 | 2009-02-27 06:17:38 +0000 | [diff] [blame] | 241 | $ParserRejects, $ofile); |
| 242 | } |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 243 | } |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 244 | else { |
| 245 | # Check if there were any unhandled attributes. |
| 246 | if (open(CHILD, $ofile)) { |
| 247 | my %attributes_not_handled; |
Ted Kremenek | 32e9464 | 2009-04-22 13:42:27 +0000 | [diff] [blame] | 248 | |
| 249 | # Don't flag warnings about the following attributes that we |
| 250 | # know are currently not supported by Clang. |
| 251 | $attributes_not_handled{"cdecl"} = 1; |
| 252 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 253 | my $ppfile; |
| 254 | while (<CHILD>) { |
| 255 | next if (! /warning: '([^\']+)' attribute ignored/); |
| 256 | |
| 257 | # Have we already spotted this unhandled attribute? |
| 258 | next if (defined $attributes_not_handled{$1}); |
| 259 | $attributes_not_handled{$1} = 1; |
| 260 | |
Ted Kremenek | 7e5bd6f | 2009-02-21 04:46:20 +0000 | [diff] [blame] | 261 | # Get the name of the attribute file. |
| 262 | my $dir = "$HtmlDir/failures"; |
| 263 | my $afile = "$dir/attribute_ignored_$1.txt"; |
| 264 | |
| 265 | # Only create another preprocessed file if the attribute file |
| 266 | # doesn't exist yet. |
| 267 | next if (-e $afile); |
| 268 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 269 | # Add this file to the list of files that contained this attribute. |
| 270 | # Generate a preprocessed file if we haven't already. |
| 271 | if (!(defined $ppfile)) { |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 272 | $ppfile = ProcessClangFailure($ClangCC, $Lang, $file, |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 273 | \@CmdArgsSansAnalyses, |
| 274 | $HtmlDir, $AttributeIgnored, $ofile); |
| 275 | } |
| 276 | |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 277 | mkpath $dir; |
Ted Kremenek | 7e5bd6f | 2009-02-21 04:46:20 +0000 | [diff] [blame] | 278 | open(AFILE, ">$afile"); |
Ted Kremenek | 938eef1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 279 | print AFILE "$ppfile\n"; |
| 280 | close(AFILE); |
| 281 | } |
| 282 | close CHILD; |
| 283 | } |
| 284 | } |
Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 285 | |
| 286 | `rm -f $ofile`; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 287 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 289 | ##----------------------------------------------------------------------------## |
| 290 | # Lookup tables. |
| 291 | ##----------------------------------------------------------------------------## |
| 292 | |
| 293 | my %CompileOptionMap = ( |
| 294 | '-nostdinc' => 0, |
Anders Carlsson | 06c58b1 | 2008-12-19 20:56:23 +0000 | [diff] [blame] | 295 | '-fblocks' => 0, |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 296 | '-fobjc-gc-only' => 0, |
Ted Kremenek | 6c4312d | 2009-02-26 23:09:43 +0000 | [diff] [blame] | 297 | '-fobjc-gc' => 0, |
| 298 | '-ffreestanding' => 0, |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 299 | '-include' => 1, |
| 300 | '-idirafter' => 1, |
| 301 | '-iprefix' => 1, |
| 302 | '-iquote' => 1, |
| 303 | '-isystem' => 1, |
| 304 | '-iwithprefix' => 1, |
| 305 | '-iwithprefixbefore' => 1 |
| 306 | ); |
| 307 | |
| 308 | my %LinkerOptionMap = ( |
| 309 | '-framework' => 1 |
| 310 | ); |
| 311 | |
| 312 | my %CompilerLinkerOptionMap = ( |
| 313 | '-isysroot' => 1, |
| 314 | '-arch' => 1, |
Ted Kremenek | e4f6952 | 2008-09-29 22:45:28 +0000 | [diff] [blame] | 315 | '-v' => 0, |
Ted Kremenek | b10362a | 2008-09-30 23:40:25 +0000 | [diff] [blame] | 316 | '-fpascal-strings' => 0, |
Daniel Dunbar | 8d33cd7 | 2009-04-10 19:52:24 +0000 | [diff] [blame] | 317 | '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '=' |
| 318 | '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '=' |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 319 | ); |
| 320 | |
| 321 | my %IgnoredOptionMap = ( |
Ted Kremenek | 9402609 | 2008-07-24 03:52:21 +0000 | [diff] [blame] | 322 | '-MT' => 1, # Ignore these preprocessor options. |
| 323 | '-MF' => 1, |
| 324 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 325 | '-fsyntax-only' => 0, |
| 326 | '-save-temps' => 0, |
| 327 | '-install_name' => 1, |
| 328 | '-exported_symbols_list' => 1, |
| 329 | '-current_version' => 1, |
| 330 | '-compatibility_version' => 1, |
| 331 | '-init' => 1, |
| 332 | '-e' => 1, |
| 333 | '-seg1addr' => 1, |
| 334 | '-bundle_loader' => 1, |
| 335 | '-multiply_defined' => 1, |
| 336 | '-sectorder' => 3, |
| 337 | '--param' => 1, |
| 338 | '-u' => 1 |
| 339 | ); |
| 340 | |
| 341 | my %LangMap = ( |
| 342 | 'c' => 'c', |
| 343 | 'cpp' => 'c++', |
| 344 | 'cc' => 'c++', |
| 345 | 'i' => 'c-cpp-output', |
| 346 | 'm' => 'objective-c', |
| 347 | 'mi' => 'objective-c-cpp-output' |
| 348 | ); |
| 349 | |
Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 350 | my %UniqueOptions = ( |
| 351 | '-isysroot' => 0 |
| 352 | ); |
| 353 | |
Ted Kremenek | efaf191 | 2009-05-11 21:08:34 +0000 | [diff] [blame] | 354 | my %LangsAccepted = ( |
| 355 | "objective-c" => 1, |
| 356 | "c" => 1 |
| 357 | ); |
| 358 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 359 | ##----------------------------------------------------------------------------## |
| 360 | # Main Logic. |
| 361 | ##----------------------------------------------------------------------------## |
| 362 | |
| 363 | my $Action = 'link'; |
| 364 | my @CompileOpts; |
| 365 | my @LinkOpts; |
| 366 | my @Files; |
| 367 | my $Lang; |
| 368 | my $Output; |
Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 369 | my %Uniqued; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 370 | |
| 371 | # Forward arguments to gcc. |
Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 372 | my $Status = system($CC,@ARGV); |
Ted Kremenek | cb344d0 | 2008-08-28 01:18:44 +0000 | [diff] [blame] | 373 | if ($Status) { exit($Status >> 8); } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 374 | |
| 375 | # Get the analysis options. |
| 376 | my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'}; |
| 377 | if (!defined($Analyses)) { $Analyses = '-checker-cfref'; } |
| 378 | |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 379 | # Get the store model. |
| 380 | my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'}; |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 381 | if (!defined $StoreModel) { $StoreModel = "basic"; } |
| 382 | |
| 383 | # Get the constraints engine. |
| 384 | my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'}; |
Ted Kremenek | 9f4ecb3 | 2009-02-20 21:49:22 +0000 | [diff] [blame] | 385 | if (!defined $ConstraintsModel) { $ConstraintsModel = "range"; } |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 387 | # Get the output format. |
| 388 | my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'}; |
Ted Kremenek | 3891a15 | 2009-02-17 05:01:10 +0000 | [diff] [blame] | 389 | if (!defined $OutputFormat) { $OutputFormat = "html"; } |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 390 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 391 | # Determine the level of verbosity. |
| 392 | my $Verbose = 0; |
| 393 | if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; } |
| 394 | if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; } |
| 395 | |
Ted Kremenek | fd9df0e | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 396 | # Determine what clang-cc executable to use. |
| 397 | my $ClangCC = $ENV{'CLANG_CC'}; |
| 398 | if (!defined $ClangCC) { $ClangCC = 'clang-cc'; } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 399 | |
| 400 | # Get the HTML output directory. |
| 401 | my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'}; |
| 402 | |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 403 | my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1); |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 404 | my %ArchsSeen; |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 405 | my $HadArch = 0; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 406 | |
| 407 | # Process the arguments. |
| 408 | foreach (my $i = 0; $i < scalar(@ARGV); ++$i) { |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 409 | my $Arg = $ARGV[$i]; |
| 410 | my ($ArgKey) = split /=/,$Arg,2; |
| 411 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 412 | # Modes ccc-analyzer supports |
| 413 | if ($Arg eq '-E') { $Action = 'preprocess'; } |
| 414 | elsif ($Arg eq '-c') { $Action = 'compile'; } |
| 415 | elsif ($Arg =~ /^-print-prog-name/) { exit 0; } |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 416 | |
| 417 | # Specially handle duplicate cases of -arch |
| 418 | if ($Arg eq "-arch") { |
| 419 | my $arch = $ARGV[$i+1]; |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 420 | # We don't want to process 'ppc' because of Clang's lack of support |
| 421 | # for Altivec (also some #defines won't likely be defined correctly, etc.) |
| 422 | if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; } |
| 423 | $HadArch = 1; |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 424 | ++$i; |
| 425 | next; |
| 426 | } |
| 427 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 428 | # Options with possible arguments that should pass through to compiler. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 429 | if (defined $CompileOptionMap{$ArgKey}) { |
| 430 | my $Cnt = $CompileOptionMap{$ArgKey}; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 431 | push @CompileOpts,$Arg; |
| 432 | while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; } |
| 433 | next; |
| 434 | } |
| 435 | |
| 436 | # Options with possible arguments that should pass through to linker. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 437 | if (defined $LinkerOptionMap{$ArgKey}) { |
| 438 | my $Cnt = $LinkerOptionMap{$ArgKey}; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 439 | push @LinkOpts,$Arg; |
| 440 | while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; } |
| 441 | next; |
| 442 | } |
| 443 | |
| 444 | # Options with possible arguments that should pass through to both compiler |
| 445 | # and the linker. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 446 | if (defined $CompilerLinkerOptionMap{$ArgKey}) { |
| 447 | my $Cnt = $CompilerLinkerOptionMap{$ArgKey}; |
Ted Kremenek | 47fc25f | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 448 | |
Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 449 | # Check if this is an option that should have a unique value, and if so |
| 450 | # determine if the value was checked before. |
| 451 | if ($UniqueOptions{$Arg}) { |
| 452 | if (defined $Uniqued{$Arg}) { |
| 453 | $i += $Cnt; |
| 454 | next; |
| 455 | } |
| 456 | $Uniqued{$Arg} = 1; |
| 457 | } |
| 458 | |
Ted Kremenek | 47fc25f | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 459 | push @CompileOpts,$Arg; |
| 460 | push @LinkOpts,$Arg; |
| 461 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 462 | while ($Cnt > 0) { |
| 463 | ++$i; --$Cnt; |
| 464 | push @CompileOpts, $ARGV[$i]; |
| 465 | push @LinkOpts, $ARGV[$i]; |
| 466 | } |
| 467 | next; |
| 468 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 469 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 470 | # Ignored options. |
Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 471 | if (defined $IgnoredOptionMap{$ArgKey}) { |
| 472 | my $Cnt = $IgnoredOptionMap{$ArgKey}; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 473 | while ($Cnt > 0) { |
| 474 | ++$i; --$Cnt; |
| 475 | } |
| 476 | next; |
| 477 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 478 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 479 | # Compile mode flags. |
| 480 | if ($Arg =~ /^-[D,I,U](.*)$/) { |
| 481 | my $Tmp = $Arg; |
| 482 | if ($1 eq '') { |
| 483 | # FIXME: Check if we are going off the end. |
| 484 | ++$i; |
| 485 | $Tmp = $Arg . $ARGV[$i]; |
| 486 | } |
| 487 | push @CompileOpts,$Tmp; |
| 488 | next; |
| 489 | } |
| 490 | |
| 491 | # Language. |
| 492 | if ($Arg eq '-x') { |
| 493 | $Lang = $ARGV[$i+1]; |
| 494 | ++$i; next; |
| 495 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 496 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 497 | # Output file. |
| 498 | if ($Arg eq '-o') { |
| 499 | ++$i; |
| 500 | $Output = $ARGV[$i]; |
| 501 | next; |
| 502 | } |
| 503 | |
| 504 | # Get the link mode. |
| 505 | if ($Arg =~ /^-[l,L,O]/) { |
| 506 | if ($Arg eq '-O') { push @LinkOpts,'-O1'; } |
| 507 | elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; } |
| 508 | else { push @LinkOpts,$Arg; } |
| 509 | next; |
| 510 | } |
| 511 | |
| 512 | if ($Arg =~ /^-std=/) { |
| 513 | push @CompileOpts,$Arg; |
| 514 | next; |
| 515 | } |
| 516 | |
| 517 | # if ($Arg =~ /^-f/) { |
| 518 | # # FIXME: Not sure if the remaining -fxxxx options have no arguments. |
| 519 | # push @CompileOpts,$Arg; |
| 520 | # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts. |
| 521 | # } |
| 522 | |
| 523 | # Get the compiler/link mode. |
| 524 | if ($Arg =~ /^-F(.+)$/) { |
| 525 | my $Tmp = $Arg; |
| 526 | if ($1 eq '') { |
| 527 | # FIXME: Check if we are going off the end. |
| 528 | ++$i; |
| 529 | $Tmp = $Arg . $ARGV[$i]; |
| 530 | } |
| 531 | push @CompileOpts,$Tmp; |
| 532 | push @LinkOpts,$Tmp; |
| 533 | next; |
| 534 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 535 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 536 | # Input files. |
| 537 | if ($Arg eq '-filelist') { |
| 538 | # FIXME: Make sure we aren't walking off the end. |
| 539 | open(IN, $ARGV[$i+1]); |
| 540 | while (<IN>) { s/\015?\012//; push @Files,$_; } |
| 541 | close(IN); |
| 542 | ++$i; next; |
| 543 | } |
| 544 | |
| 545 | if (!($Arg =~ /^-/)) { |
| 546 | push @Files,$Arg; next; |
| 547 | } |
| 548 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 549 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 550 | if ($Action eq 'compile' or $Action eq 'link') { |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 551 | my @Archs = keys %ArchsSeen; |
| 552 | # Skip the file if we don't support the architectures specified. |
Ted Kremenek | 0e0eb8b | 2009-02-25 00:10:37 +0000 | [diff] [blame] | 553 | exit 0 if ($HadArch && scalar(@Archs) == 0); |
Ted Kremenek | 8459132 | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 554 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 555 | foreach my $file (@Files) { |
| 556 | # Determine the language for the file. |
| 557 | my $FileLang = $Lang; |
| 558 | |
| 559 | if (!defined($FileLang)) { |
| 560 | # Infer the language from the extension. |
| 561 | if ($file =~ /[.]([^.]+)$/) { |
| 562 | $FileLang = $LangMap{$1}; |
| 563 | } |
| 564 | } |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 565 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 566 | next if (!defined $FileLang); |
Ted Kremenek | efaf191 | 2009-05-11 21:08:34 +0000 | [diff] [blame] | 567 | next if (!defined $LangsAccepted{$FileLang}); |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 568 | |
Ted Kremenek | 9a9e0d4 | 2009-05-15 04:20:31 +0000 | [diff] [blame] | 569 | my @CmdArgs; |
| 570 | my @AnalyzeArgs; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 571 | |
| 572 | if ($FileLang ne 'unknown') { |
Ted Kremenek | 9a9e0d4 | 2009-05-15 04:20:31 +0000 | [diff] [blame] | 573 | push @CmdArgs,'-x'; |
| 574 | push @CmdArgs,$FileLang; |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 575 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 576 | |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 577 | if (defined $StoreModel) { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 578 | push @AnalyzeArgs, "-analyzer-store=$StoreModel"; |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 579 | } |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 580 | |
| 581 | if (defined $ConstraintsModel) { |
| 582 | push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel"; |
| 583 | } |
| 584 | |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 585 | if (defined $OutputFormat) { |
Ted Kremenek | be1fe1e | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 586 | push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 587 | if ($OutputFormat eq "plist") { |
| 588 | # Change "Output" to be a file. |
| 589 | my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist", |
| 590 | DIR => $HtmlDir); |
| 591 | $ResultFile = $f; |
| 592 | $CleanupFile = $f; |
| 593 | } |
Ted Kremenek | db4f5f2 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 594 | } |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 595 | |
Ted Kremenek | 9a9e0d4 | 2009-05-15 04:20:31 +0000 | [diff] [blame] | 596 | push @CmdArgs,@CompileOpts; |
| 597 | push @CmdArgs,$file; |
Zhongxing Xu | 07c3767 | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 598 | |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 599 | if (scalar @Archs) { |
| 600 | foreach my $arch (@Archs) { |
| 601 | my @NewArgs; |
| 602 | push @NewArgs, '-arch'; |
| 603 | push @NewArgs, $arch; |
Ted Kremenek | 9a9e0d4 | 2009-05-15 04:20:31 +0000 | [diff] [blame] | 604 | push @NewArgs, @CmdArgs; |
| 605 | Analyze($ClangCC, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output, |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 606 | $Verbose, $HtmlDir, $file, $Analyses); |
| 607 | } |
| 608 | } |
| 609 | else { |
Ted Kremenek | 9a9e0d4 | 2009-05-15 04:20:31 +0000 | [diff] [blame] | 610 | Analyze($ClangCC, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output, |
Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 611 | $Verbose, $HtmlDir, $file, $Analyses); |
| 612 | } |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 615 | |
Ted Kremenek | 948e06b | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 616 | exit($Status >> 8); |
| 617 | |