| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl | 
| Ted Kremenek | 5efdf84 | 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 | f7ffd66 | 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 | 5efdf84 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 12 | # | 
|  | 13 | ##===----------------------------------------------------------------------===## | 
|  | 14 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 15 | use strict; | 
|  | 16 | use warnings; | 
| Ted Kremenek | f65a0c6 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 17 | use FindBin; | 
| Ted Kremenek | 23432d4 | 2008-09-21 19:56:14 +0000 | [diff] [blame] | 18 | use Cwd qw/ getcwd abs_path /; | 
| Ted Kremenek | 994c8e3 | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 19 | use File::Temp qw/ tempfile /; | 
|  | 20 | use File::Path qw / mkpath /; | 
| Ted Kremenek | 1374716 | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 21 | use File::Basename; | 
| Ted Kremenek | 25421bb | 2009-05-11 23:29:51 +0000 | [diff] [blame] | 22 | use Text::ParseWords; | 
| Ted Kremenek | 6aa1ad0 | 2008-08-25 20:44:31 +0000 | [diff] [blame] | 23 |  | 
| Ted Kremenek | f65a0c6 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 24 | ##===----------------------------------------------------------------------===## | 
|  | 25 | # Compiler command setup. | 
|  | 26 | ##===----------------------------------------------------------------------===## | 
|  | 27 |  | 
|  | 28 | my $Compiler; | 
|  | 29 | my $Clang; | 
| Anna Zaks | 1d39152 | 2012-01-06 01:54:05 +0000 | [diff] [blame^] | 30 | my $DefaultCCompiler; | 
|  | 31 | my $DefaultCXXCompiler; | 
|  | 32 |  | 
|  | 33 | if (`uname -a` =~ m/Darwin/) { | 
|  | 34 | $DefaultCCompiler = 'clang'; | 
|  | 35 | $DefaultCXXCompiler = 'clang++'; | 
|  | 36 | } else { | 
|  | 37 | $DefaultCCompiler = 'gcc'; | 
|  | 38 | $DefaultCXXCompiler = 'g++'; | 
|  | 39 | } | 
| Ted Kremenek | f65a0c6 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 40 |  | 
|  | 41 | if ($FindBin::Script =~ /c\+\+-analyzer/) { | 
|  | 42 | $Compiler = $ENV{'CCC_CXX'}; | 
| Anna Zaks | 1d39152 | 2012-01-06 01:54:05 +0000 | [diff] [blame^] | 43 | if (!defined $Compiler) { $Compiler = $DefaultCXXCompiler; } | 
| Ted Kremenek | f65a0c6 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 44 |  | 
|  | 45 | $Clang = $ENV{'CLANG_CXX'}; | 
|  | 46 | if (!defined $Clang) { $Clang = 'clang++'; } | 
|  | 47 | } | 
|  | 48 | else { | 
|  | 49 | $Compiler = $ENV{'CCC_CC'}; | 
| Anna Zaks | 1d39152 | 2012-01-06 01:54:05 +0000 | [diff] [blame^] | 50 | if (!defined $Compiler) { $Compiler = $DefaultCCompiler; } | 
| Ted Kremenek | f65a0c6 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 51 |  | 
|  | 52 | $Clang = $ENV{'CLANG'}; | 
|  | 53 | if (!defined $Clang) { $Clang = 'clang'; } | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | ##===----------------------------------------------------------------------===## | 
|  | 57 | # Cleanup. | 
|  | 58 | ##===----------------------------------------------------------------------===## | 
| Ted Kremenek | bfe393f | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 59 |  | 
|  | 60 | my $ReportFailures = $ENV{'CCC_REPORT_FAILURES'}; | 
|  | 61 | if (!defined $ReportFailures) { $ReportFailures = 1; } | 
|  | 62 |  | 
| Ted Kremenek | 1374716 | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 63 | my $CleanupFile; | 
|  | 64 | my $ResultFile; | 
|  | 65 |  | 
|  | 66 | # Remove any stale files at exit. | 
|  | 67 | END { | 
| Anna Zaks | 45ce1bf | 2011-09-09 18:43:53 +0000 | [diff] [blame] | 68 | if (defined $ResultFile && -z $ResultFile) { | 
|  | 69 | `rm -f $ResultFile`; | 
|  | 70 | } | 
|  | 71 | if (defined $CleanupFile) { | 
| Ted Kremenek | 1374716 | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 72 | `rm -f $CleanupFile`; | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 |  | 
| Ted Kremenek | 994c8e3 | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 76 | ##----------------------------------------------------------------------------## | 
|  | 77 | #  Process Clang Crashes. | 
|  | 78 | ##----------------------------------------------------------------------------## | 
|  | 79 |  | 
|  | 80 | sub GetPPExt { | 
|  | 81 | my $Lang = shift; | 
| Ted Kremenek | 22a8a4b | 2009-12-16 18:32:41 +0000 | [diff] [blame] | 82 | if ($Lang =~ /objective-c\+\+/) { return ".mii" }; | 
| Ted Kremenek | 994c8e3 | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 83 | if ($Lang =~ /objective-c/) { return ".mi"; } | 
| Ted Kremenek | dca6816 | 2009-12-16 05:02:47 +0000 | [diff] [blame] | 84 | if ($Lang =~ /c\+\+/) { return ".ii"; } | 
| Ted Kremenek | 994c8e3 | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 85 | return ".i"; | 
|  | 86 | } | 
|  | 87 |  | 
| Ted Kremenek | ce6b865 | 2009-04-28 17:37:44 +0000 | [diff] [blame] | 88 | # Set this to 1 if we want to include 'parser rejects' files. | 
|  | 89 | my $IncludeParserRejects = 0; | 
| Ted Kremenek | 725fb43 | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 90 | my $ParserRejects = "Parser Rejects"; | 
| Ted Kremenek | 13ed6f1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 91 | my $AttributeIgnored = "Attribute Ignored"; | 
| Anna Zaks | 9fed084 | 2011-11-07 22:38:10 +0000 | [diff] [blame] | 92 | my $OtherError = "Other Error"; | 
| Ted Kremenek | 725fb43 | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 93 |  | 
| Ted Kremenek | 5abf546 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 94 | sub ProcessClangFailure { | 
| Ted Kremenek | 5c512e6 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 95 | my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_; | 
| Ted Kremenek | 13ed6f1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 96 | my $Dir = "$HtmlDir/failures"; | 
| Ted Kremenek | 994c8e3 | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 97 | mkpath $Dir; | 
| Ted Kremenek | 725fb43 | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 98 |  | 
|  | 99 | my $prefix = "clang_crash"; | 
| Ted Kremenek | 13ed6f1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 100 | if ($ErrorType eq $ParserRejects) { | 
|  | 101 | $prefix = "clang_parser_rejects"; | 
|  | 102 | } | 
|  | 103 | elsif ($ErrorType eq $AttributeIgnored) { | 
|  | 104 | $prefix = "clang_attribute_ignored"; | 
|  | 105 | } | 
| Anna Zaks | 9fed084 | 2011-11-07 22:38:10 +0000 | [diff] [blame] | 106 | elsif ($ErrorType eq $OtherError) { | 
|  | 107 | $prefix = "clang_other_error"; | 
|  | 108 | } | 
| Ted Kremenek | 72e4b0b | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 109 |  | 
| Ted Kremenek | 0799d4f | 2009-07-28 00:14:21 +0000 | [diff] [blame] | 110 | # Generate the preprocessed file with Clang. | 
| Ted Kremenek | 725fb43 | 2009-01-27 01:19:08 +0000 | [diff] [blame] | 111 | my ($PPH, $PPFile) = tempfile( $prefix . "_XXXXXX", | 
|  | 112 | SUFFIX => GetPPExt($Lang), | 
|  | 113 | DIR => $Dir); | 
| Ted Kremenek | 5c512e6 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 114 | system $Clang, @$Args, "-E", "-o", $PPFile; | 
| Ted Kremenek | 994c8e3 | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 115 | close ($PPH); | 
| Ted Kremenek | 72e4b0b | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 116 |  | 
|  | 117 | # Create the info file. | 
| Ted Kremenek | 1ad3b3d | 2008-09-25 00:25:16 +0000 | [diff] [blame] | 118 | open (OUT, ">", "$PPFile.info.txt") or die "Cannot open $PPFile.info.txt\n"; | 
| Ted Kremenek | 32c1181 | 2008-09-21 18:04:49 +0000 | [diff] [blame] | 119 | print OUT abs_path($file), "\n"; | 
| Ted Kremenek | 5abf546 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 120 | print OUT "$ErrorType\n"; | 
| Ted Kremenek | b3c98d3 | 2008-08-18 20:55:25 +0000 | [diff] [blame] | 121 | print OUT "@$Args\n"; | 
| Ted Kremenek | 994c8e3 | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 122 | close OUT; | 
| Ted Kremenek | 1ad3b3d | 2008-09-25 00:25:16 +0000 | [diff] [blame] | 123 | `uname -a >> $PPFile.info.txt 2>&1`; | 
| Ted Kremenek | f65a0c6 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 124 | `$Compiler -v >> $PPFile.info.txt 2>&1`; | 
| Ted Kremenek | 907921f | 2008-09-12 22:49:36 +0000 | [diff] [blame] | 125 | system 'mv',$ofile,"$PPFile.stderr.txt"; | 
| Ted Kremenek | 13ed6f1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 126 | return (basename $PPFile); | 
| Ted Kremenek | 994c8e3 | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 127 | } | 
| Ted Kremenek | 5efdf84 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 128 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 129 | ##----------------------------------------------------------------------------## | 
|  | 130 | #  Running the analyzer. | 
|  | 131 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 5efdf84 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 132 |  | 
| Ted Kremenek | 1f991f0 | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 133 | sub GetCCArgs { | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 134 | my $mode = shift; | 
| Ted Kremenek | 1f991f0 | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 135 | my $Args = shift; | 
|  | 136 |  | 
|  | 137 | pipe (FROM_CHILD, TO_PARENT); | 
|  | 138 | my $pid = fork(); | 
|  | 139 | if ($pid == 0) { | 
|  | 140 | close FROM_CHILD; | 
|  | 141 | open(STDOUT,">&", \*TO_PARENT); | 
|  | 142 | open(STDERR,">&", \*TO_PARENT); | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 143 | exec $Clang, "-###", $mode, @$Args; | 
| Ted Kremenek | 1f991f0 | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 144 | } | 
|  | 145 | close(TO_PARENT); | 
|  | 146 | my $line; | 
|  | 147 | while (<FROM_CHILD>) { | 
| Ted Kremenek | f92b446 | 2009-12-11 23:12:52 +0000 | [diff] [blame] | 148 | next if (!/-cc1/); | 
| Ted Kremenek | 1f991f0 | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 149 | $line = $_; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | waitpid($pid,0); | 
|  | 153 | close(FROM_CHILD); | 
|  | 154 |  | 
| Ted Kremenek | f92b446 | 2009-12-11 23:12:52 +0000 | [diff] [blame] | 155 | die "could not find clang line\n" if (!defined $line); | 
| Ted Kremenek | 25421bb | 2009-05-11 23:29:51 +0000 | [diff] [blame] | 156 | # Strip the newline and initial whitspace | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 157 | chomp $line; | 
| Ted Kremenek | 25421bb | 2009-05-11 23:29:51 +0000 | [diff] [blame] | 158 | $line =~ s/^\s+//; | 
| Ted Kremenek | 7deb705 | 2010-04-06 19:41:24 +0000 | [diff] [blame] | 159 | my @items = quotewords('\s+', 0, $line); | 
| Ted Kremenek | 1f991f0 | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 160 | my $cmd = shift @items; | 
| Ted Kremenek | f92b446 | 2009-12-11 23:12:52 +0000 | [diff] [blame] | 161 | die "cannot find 'clang' in 'clang' command\n" if (!($cmd =~ /clang/)); | 
| Ted Kremenek | 1f991f0 | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 162 | return \@items; | 
|  | 163 | } | 
|  | 164 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 165 | sub Analyze { | 
| Ted Kremenek | 339f7c3 | 2011-03-10 21:10:08 +0000 | [diff] [blame] | 166 | my ($Clang, $OriginalArgs, $AnalyzeArgs, $Lang, $Output, $Verbose, $HtmlDir, | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 167 | $file) = @_; | 
| Seo Sanghyeon | b7bf0f3 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 168 |  | 
| Ted Kremenek | 339f7c3 | 2011-03-10 21:10:08 +0000 | [diff] [blame] | 169 | my @Args = @$OriginalArgs; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 170 | my $Cmd; | 
|  | 171 | my @CmdArgs; | 
| Ted Kremenek | 994c8e3 | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 172 | my @CmdArgsSansAnalyses; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 173 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 174 | if ($Lang =~ /header/) { | 
|  | 175 | exit 0 if (!defined ($Output)); | 
|  | 176 | $Cmd = 'cp'; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 177 | push @CmdArgs, $file; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 178 | # Remove the PCH extension. | 
|  | 179 | $Output =~ s/[.]gch$//; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 180 | push @CmdArgs, $Output; | 
|  | 181 | @CmdArgsSansAnalyses = @CmdArgs; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 182 | } | 
|  | 183 | else { | 
| Ted Kremenek | 5c512e6 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 184 | $Cmd = $Clang; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 185 | if ($Lang eq "objective-c" || $Lang eq "objective-c++") { | 
| Ted Kremenek | 339f7c3 | 2011-03-10 21:10:08 +0000 | [diff] [blame] | 186 | push @Args,'-DIBOutlet=__attribute__((iboutlet))'; | 
|  | 187 | push @Args,'-DIBOutletCollection(ClassName)=__attribute__((iboutletcollection)))'; | 
|  | 188 | push @Args,'-DIBAction=void)__attribute__((ibaction)'; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
|  | 191 | # Create arguments for doing regular parsing. | 
| Ted Kremenek | 339f7c3 | 2011-03-10 21:10:08 +0000 | [diff] [blame] | 192 | my $SyntaxArgs = GetCCArgs("-fsyntax-only", \@Args); | 
|  | 193 | @CmdArgsSansAnalyses = @$SyntaxArgs; | 
|  | 194 |  | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 195 | # Create arguments for doing static analysis. | 
|  | 196 | if (defined $ResultFile) { | 
| Ted Kremenek | d4bcb4f | 2011-03-16 21:10:42 +0000 | [diff] [blame] | 197 | push @Args, '-o', $ResultFile; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 198 | } | 
|  | 199 | elsif (defined $HtmlDir) { | 
| Ted Kremenek | d4bcb4f | 2011-03-16 21:10:42 +0000 | [diff] [blame] | 200 | push @Args, '-o', $HtmlDir; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 201 | } | 
| Ted Kremenek | 9acb346 | 2011-03-21 20:12:21 +0000 | [diff] [blame] | 202 | if ($Verbose) { | 
|  | 203 | push @Args, "-Xclang", "-analyzer-display-progress"; | 
|  | 204 | } | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 205 |  | 
|  | 206 | foreach my $arg (@$AnalyzeArgs) { | 
| Ted Kremenek | d4bcb4f | 2011-03-16 21:10:42 +0000 | [diff] [blame] | 207 | push @Args, "-Xclang", $arg; | 
| Ted Kremenek | 4ef13f8 | 2009-11-13 18:46:29 +0000 | [diff] [blame] | 208 | } | 
| Ted Kremenek | d4bcb4f | 2011-03-16 21:10:42 +0000 | [diff] [blame] | 209 |  | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 210 | # Display Ubiviz graph? | 
|  | 211 | if (defined $ENV{'CCC_UBI'}) { | 
| Ted Kremenek | d4bcb4f | 2011-03-16 21:10:42 +0000 | [diff] [blame] | 212 | push @Args, "-Xclang", "-analyzer-viz-egraph-ubigraph"; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 213 | } | 
|  | 214 |  | 
| Ted Kremenek | 339f7c3 | 2011-03-10 21:10:08 +0000 | [diff] [blame] | 215 | my $AnalysisArgs = GetCCArgs("--analyze", \@Args); | 
|  | 216 | @CmdArgs = @$AnalysisArgs; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 217 | } | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 218 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 219 | my @PrintArgs; | 
|  | 220 | my $dir; | 
| Ted Kremenek | 7ac29bb | 2009-08-02 05:42:46 +0000 | [diff] [blame] | 221 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 222 | if ($Verbose) { | 
|  | 223 | $dir = getcwd(); | 
|  | 224 | print STDERR "\n[LOCATION]: $dir\n"; | 
|  | 225 | push @PrintArgs,"'$Cmd'"; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 226 | foreach my $arg (@CmdArgs) { | 
|  | 227 | push @PrintArgs,"\'$arg\'"; | 
|  | 228 | } | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 229 | } | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 230 | if ($Verbose == 1) { | 
| Ted Kremenek | f18f460 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 231 | # We MUST print to stderr.  Some clients use the stdout output of | 
|  | 232 | # gcc for various purposes. | 
| Ted Kremenek | 339f7c3 | 2011-03-10 21:10:08 +0000 | [diff] [blame] | 233 | print STDERR join(' ', @PrintArgs); | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 234 | print STDERR "\n"; | 
|  | 235 | } | 
|  | 236 | elsif ($Verbose == 2) { | 
|  | 237 | print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n"; | 
|  | 238 | } | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 239 |  | 
| Ted Kremenek | 370de84 | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 240 | # Capture the STDERR of clang and send it to a temporary file. | 
|  | 241 | # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR. | 
|  | 242 | # We save the output file in the 'crashes' directory if clang encounters | 
|  | 243 | # any problems with the file. | 
| Ted Kremenek | 4ab81cb | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 244 | pipe (FROM_CHILD, TO_PARENT); | 
| Ted Kremenek | 370de84 | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 245 | my $pid = fork(); | 
|  | 246 | if ($pid == 0) { | 
| Ted Kremenek | 4ab81cb | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 247 | close FROM_CHILD; | 
|  | 248 | open(STDOUT,">&", \*TO_PARENT); | 
|  | 249 | open(STDERR,">&", \*TO_PARENT); | 
| Ted Kremenek | 370de84 | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 250 | exec $Cmd, @CmdArgs; | 
|  | 251 | } | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 252 |  | 
| Ted Kremenek | 4ab81cb | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 253 | close TO_PARENT; | 
|  | 254 | my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir); | 
|  | 255 |  | 
|  | 256 | while (<FROM_CHILD>) { | 
|  | 257 | print $ofh $_; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 258 | print STDERR $_; | 
| Ted Kremenek | 4ab81cb | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 259 | } | 
|  | 260 |  | 
|  | 261 | waitpid($pid,0); | 
| Ted Kremenek | 1f991f0 | 2009-05-09 19:19:28 +0000 | [diff] [blame] | 262 | close(FROM_CHILD); | 
| Ted Kremenek | 370de84 | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 263 | my $Result = $?; | 
|  | 264 |  | 
|  | 265 | # Did the command die because of a signal? | 
| Ted Kremenek | bfe393f | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 266 | if ($ReportFailures) { | 
| Ted Kremenek | 5c512e6 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 267 | if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) { | 
|  | 268 | ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, | 
| Ted Kremenek | bfe393f | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 269 | $HtmlDir, "Crash", $ofile); | 
| Ted Kremenek | 078b887 | 2009-02-27 06:17:38 +0000 | [diff] [blame] | 270 | } | 
| Ted Kremenek | bfe393f | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 271 | elsif ($Result) { | 
|  | 272 | if ($IncludeParserRejects && !($file =~/conftest/)) { | 
| Ted Kremenek | 5c512e6 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 273 | ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, | 
| Ted Kremenek | bfe393f | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 274 | $HtmlDir, $ParserRejects, $ofile); | 
| Anna Zaks | 9fed084 | 2011-11-07 22:38:10 +0000 | [diff] [blame] | 275 | } else { | 
|  | 276 | ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, | 
|  | 277 | $HtmlDir, $OtherError, $ofile); | 
| Ted Kremenek | 13ed6f1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 278 | } | 
| Ted Kremenek | bfe393f | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 279 | } | 
|  | 280 | else { | 
|  | 281 | # Check if there were any unhandled attributes. | 
|  | 282 | if (open(CHILD, $ofile)) { | 
|  | 283 | my %attributes_not_handled; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 284 |  | 
| Ted Kremenek | bfe393f | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 285 | # Don't flag warnings about the following attributes that we | 
|  | 286 | # know are currently not supported by Clang. | 
|  | 287 | $attributes_not_handled{"cdecl"} = 1; | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 288 |  | 
| Ted Kremenek | bfe393f | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 289 | my $ppfile; | 
|  | 290 | while (<CHILD>) { | 
|  | 291 | next if (! /warning: '([^\']+)' attribute ignored/); | 
|  | 292 |  | 
|  | 293 | # Have we already spotted this unhandled attribute? | 
|  | 294 | next if (defined $attributes_not_handled{$1}); | 
|  | 295 | $attributes_not_handled{$1} = 1; | 
|  | 296 |  | 
|  | 297 | # Get the name of the attribute file. | 
|  | 298 | my $dir = "$HtmlDir/failures"; | 
|  | 299 | my $afile = "$dir/attribute_ignored_$1.txt"; | 
|  | 300 |  | 
|  | 301 | # Only create another preprocessed file if the attribute file | 
|  | 302 | # doesn't exist yet. | 
|  | 303 | next if (-e $afile); | 
|  | 304 |  | 
|  | 305 | # Add this file to the list of files that contained this attribute. | 
|  | 306 | # Generate a preprocessed file if we haven't already. | 
|  | 307 | if (!(defined $ppfile)) { | 
| Ted Kremenek | 5c512e6 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 308 | $ppfile = ProcessClangFailure($Clang, $Lang, $file, | 
| Ted Kremenek | bfe393f | 2009-07-30 23:55:19 +0000 | [diff] [blame] | 309 | \@CmdArgsSansAnalyses, | 
|  | 310 | $HtmlDir, $AttributeIgnored, $ofile); | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | mkpath $dir; | 
|  | 314 | open(AFILE, ">$afile"); | 
|  | 315 | print AFILE "$ppfile\n"; | 
|  | 316 | close(AFILE); | 
|  | 317 | } | 
|  | 318 | close CHILD; | 
|  | 319 | } | 
| Ted Kremenek | 13ed6f1 | 2009-02-17 23:31:05 +0000 | [diff] [blame] | 320 | } | 
|  | 321 | } | 
| Ted Kremenek | 370de84 | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 322 |  | 
| Ted Kremenek | 90fc8a4 | 2009-08-04 00:55:59 +0000 | [diff] [blame] | 323 | unlink($ofile); | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 324 | } | 
| Ted Kremenek | f18f460 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 325 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 326 | ##----------------------------------------------------------------------------## | 
|  | 327 | #  Lookup tables. | 
|  | 328 | ##----------------------------------------------------------------------------## | 
|  | 329 |  | 
|  | 330 | my %CompileOptionMap = ( | 
|  | 331 | '-nostdinc' => 0, | 
| Anders Carlsson | 0911347 | 2008-12-19 20:56:23 +0000 | [diff] [blame] | 332 | '-fblocks' => 0, | 
| Shantonu Sen | df44f74 | 2010-07-03 03:08:23 +0000 | [diff] [blame] | 333 | '-fno-builtin' => 0, | 
| Ted Kremenek | 0e9843b | 2011-11-16 05:14:42 +0000 | [diff] [blame] | 334 | '-fobjc-arc' => 0, | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 335 | '-fobjc-gc-only' => 0, | 
| Ted Kremenek | 1b44ba4 | 2009-02-26 23:09:43 +0000 | [diff] [blame] | 336 | '-fobjc-gc' => 0, | 
|  | 337 | '-ffreestanding' => 0, | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 338 | '-include' => 1, | 
|  | 339 | '-idirafter' => 1, | 
| Ted Kremenek | e869a18 | 2010-06-08 18:27:55 +0000 | [diff] [blame] | 340 | '-imacros' => 1, | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 341 | '-iprefix' => 1, | 
|  | 342 | '-iquote' => 1, | 
|  | 343 | '-isystem' => 1, | 
|  | 344 | '-iwithprefix' => 1, | 
|  | 345 | '-iwithprefixbefore' => 1 | 
|  | 346 | ); | 
|  | 347 |  | 
|  | 348 | my %LinkerOptionMap = ( | 
|  | 349 | '-framework' => 1 | 
|  | 350 | ); | 
|  | 351 |  | 
|  | 352 | my %CompilerLinkerOptionMap = ( | 
|  | 353 | '-isysroot' => 1, | 
|  | 354 | '-arch' => 1, | 
| Charles Davis | dde71b9 | 2010-03-02 15:26:41 +0000 | [diff] [blame] | 355 | '-m32' => 0, | 
|  | 356 | '-m64' => 0, | 
| Ted Kremenek | 6b2e07a | 2008-09-29 22:45:28 +0000 | [diff] [blame] | 357 | '-v' => 0, | 
| Ted Kremenek | e8450fe | 2008-09-30 23:40:25 +0000 | [diff] [blame] | 358 | '-fpascal-strings' => 0, | 
| Daniel Dunbar | 497ff13 | 2009-04-10 19:52:24 +0000 | [diff] [blame] | 359 | '-mmacosx-version-min' => 0, # This is really a 1 argument, but always has '=' | 
|  | 360 | '-miphoneos-version-min' => 0 # This is really a 1 argument, but always has '=' | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 361 | ); | 
|  | 362 |  | 
|  | 363 | my %IgnoredOptionMap = ( | 
| Ted Kremenek | 4a154b2 | 2008-07-24 03:52:21 +0000 | [diff] [blame] | 364 | '-MT' => 1,  # Ignore these preprocessor options. | 
|  | 365 | '-MF' => 1, | 
|  | 366 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 367 | '-fsyntax-only' => 0, | 
|  | 368 | '-save-temps' => 0, | 
|  | 369 | '-install_name' => 1, | 
|  | 370 | '-exported_symbols_list' => 1, | 
|  | 371 | '-current_version' => 1, | 
|  | 372 | '-compatibility_version' => 1, | 
|  | 373 | '-init' => 1, | 
|  | 374 | '-e' => 1, | 
|  | 375 | '-seg1addr' => 1, | 
|  | 376 | '-bundle_loader' => 1, | 
|  | 377 | '-multiply_defined' => 1, | 
|  | 378 | '-sectorder' => 3, | 
|  | 379 | '--param' => 1, | 
| Anna Zaks | 3a7f73d | 2012-01-06 01:54:02 +0000 | [diff] [blame] | 380 | '-u' => 1, | 
|  | 381 | '--serialize-diagnostics' => 1 | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 382 | ); | 
|  | 383 |  | 
|  | 384 | my %LangMap = ( | 
|  | 385 | 'c'   => 'c', | 
| Shantonu Sen | df44f74 | 2010-07-03 03:08:23 +0000 | [diff] [blame] | 386 | 'cp'  => 'c++', | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 387 | 'cpp' => 'c++', | 
|  | 388 | 'cc'  => 'c++', | 
| Anna Zaks | 5efad63 | 2011-08-31 23:53:24 +0000 | [diff] [blame] | 389 | 'ii'  => 'c++', | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 390 | 'i'   => 'c-cpp-output', | 
|  | 391 | 'm'   => 'objective-c', | 
| Anna Zaks | 5efad63 | 2011-08-31 23:53:24 +0000 | [diff] [blame] | 392 | 'mi'  => 'objective-c-cpp-output', | 
|  | 393 | 'mm'  => 'objective-c++' | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 394 | ); | 
|  | 395 |  | 
| Ted Kremenek | 8b89a65 | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 396 | my %UniqueOptions = ( | 
|  | 397 | '-isysroot' => 0 | 
|  | 398 | ); | 
|  | 399 |  | 
| Ted Kremenek | adccbca | 2010-03-08 19:06:44 +0000 | [diff] [blame] | 400 | ##----------------------------------------------------------------------------## | 
|  | 401 | # Languages accepted. | 
|  | 402 | ##----------------------------------------------------------------------------## | 
|  | 403 |  | 
| Ted Kremenek | dc99ec4 | 2009-05-11 21:08:34 +0000 | [diff] [blame] | 404 | my %LangsAccepted = ( | 
|  | 405 | "objective-c" => 1, | 
| Ted Kremenek | d33c4d3 | 2011-08-11 22:47:20 +0000 | [diff] [blame] | 406 | "c" => 1, | 
|  | 407 | "c++" => 1, | 
|  | 408 | "objective-c++" => 1 | 
| Ted Kremenek | dc99ec4 | 2009-05-11 21:08:34 +0000 | [diff] [blame] | 409 | ); | 
|  | 410 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 411 | ##----------------------------------------------------------------------------## | 
|  | 412 | #  Main Logic. | 
|  | 413 | ##----------------------------------------------------------------------------## | 
|  | 414 |  | 
|  | 415 | my $Action = 'link'; | 
|  | 416 | my @CompileOpts; | 
|  | 417 | my @LinkOpts; | 
|  | 418 | my @Files; | 
|  | 419 | my $Lang; | 
|  | 420 | my $Output; | 
| Ted Kremenek | 8b89a65 | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 421 | my %Uniqued; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 422 |  | 
|  | 423 | # Forward arguments to gcc. | 
| Ted Kremenek | f65a0c6 | 2009-12-15 02:35:54 +0000 | [diff] [blame] | 424 | my $Status = system($Compiler,@ARGV); | 
| Tom Care | a5f13c86 | 2010-09-29 23:48:31 +0000 | [diff] [blame] | 425 | if  (defined $ENV{'CCC_ANALYZER_LOG'}) { | 
|  | 426 | print "$Compiler @ARGV\n"; | 
|  | 427 | } | 
| Ted Kremenek | 1a42278 | 2008-08-28 01:18:44 +0000 | [diff] [blame] | 428 | if ($Status) { exit($Status >> 8); } | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 429 |  | 
|  | 430 | # Get the analysis options. | 
|  | 431 | my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'}; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 432 |  | 
| Zhongxing Xu | ad4c3de | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 433 | # Get the store model. | 
|  | 434 | my $StoreModel = $ENV{'CCC_ANALYZER_STORE_MODEL'}; | 
| Ted Kremenek | b535181 | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 435 |  | 
|  | 436 | # Get the constraints engine. | 
|  | 437 | my $ConstraintsModel = $ENV{'CCC_ANALYZER_CONSTRAINTS_MODEL'}; | 
| Zhongxing Xu | ad4c3de | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 438 |  | 
| Ted Kremenek | 9023055 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 439 | # Get the output format. | 
|  | 440 | my $OutputFormat = $ENV{'CCC_ANALYZER_OUTPUT_FORMAT'}; | 
| Ted Kremenek | d3d16aa | 2009-02-17 05:01:10 +0000 | [diff] [blame] | 441 | if (!defined $OutputFormat) { $OutputFormat = "html"; } | 
| Ted Kremenek | 9023055 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 442 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 443 | # Determine the level of verbosity. | 
|  | 444 | my $Verbose = 0; | 
|  | 445 | if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; } | 
|  | 446 | if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; } | 
|  | 447 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 448 | # Get the HTML output directory. | 
|  | 449 | my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'}; | 
|  | 450 |  | 
| Ted Kremenek | 9b15eff | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 451 | my %DisabledArchs = ('ppc' => 1, 'ppc64' => 1); | 
| Ted Kremenek | 15146a5 | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 452 | my %ArchsSeen; | 
| Ted Kremenek | 9b15eff | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 453 | my $HadArch = 0; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 454 |  | 
|  | 455 | # Process the arguments. | 
|  | 456 | foreach (my $i = 0; $i < scalar(@ARGV); ++$i) { | 
| Ted Kremenek | ad4a57d | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 457 | my $Arg = $ARGV[$i]; | 
|  | 458 | my ($ArgKey) = split /=/,$Arg,2; | 
|  | 459 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 460 | # Modes ccc-analyzer supports | 
| Ted Kremenek | ba8d7fc | 2009-08-04 00:57:12 +0000 | [diff] [blame] | 461 | if ($Arg =~ /^-(E|MM?)$/) { $Action = 'preprocess'; } | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 462 | elsif ($Arg eq '-c') { $Action = 'compile'; } | 
|  | 463 | elsif ($Arg =~ /^-print-prog-name/) { exit 0; } | 
| Ted Kremenek | 15146a5 | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 464 |  | 
|  | 465 | # Specially handle duplicate cases of -arch | 
|  | 466 | if ($Arg eq "-arch") { | 
|  | 467 | my $arch = $ARGV[$i+1]; | 
| Ted Kremenek | 9b15eff | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 468 | # We don't want to process 'ppc' because of Clang's lack of support | 
|  | 469 | # for Altivec (also some #defines won't likely be defined correctly, etc.) | 
|  | 470 | if (!(defined $DisabledArchs{$arch})) { $ArchsSeen{$arch} = 1; } | 
|  | 471 | $HadArch = 1; | 
| Ted Kremenek | 15146a5 | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 472 | ++$i; | 
|  | 473 | next; | 
|  | 474 | } | 
|  | 475 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 476 | # Options with possible arguments that should pass through to compiler. | 
| Ted Kremenek | ad4a57d | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 477 | if (defined $CompileOptionMap{$ArgKey}) { | 
|  | 478 | my $Cnt = $CompileOptionMap{$ArgKey}; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 479 | push @CompileOpts,$Arg; | 
|  | 480 | while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; } | 
|  | 481 | next; | 
|  | 482 | } | 
|  | 483 |  | 
|  | 484 | # Options with possible arguments that should pass through to linker. | 
| Ted Kremenek | ad4a57d | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 485 | if (defined $LinkerOptionMap{$ArgKey}) { | 
|  | 486 | my $Cnt = $LinkerOptionMap{$ArgKey}; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 487 | push @LinkOpts,$Arg; | 
|  | 488 | while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; } | 
|  | 489 | next; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | # Options with possible arguments that should pass through to both compiler | 
|  | 493 | # and the linker. | 
| Ted Kremenek | ad4a57d | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 494 | if (defined $CompilerLinkerOptionMap{$ArgKey}) { | 
|  | 495 | my $Cnt = $CompilerLinkerOptionMap{$ArgKey}; | 
| Ted Kremenek | 887c49d | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 496 |  | 
| Ted Kremenek | 8b89a65 | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 497 | # Check if this is an option that should have a unique value, and if so | 
|  | 498 | # determine if the value was checked before. | 
|  | 499 | if ($UniqueOptions{$Arg}) { | 
|  | 500 | if (defined $Uniqued{$Arg}) { | 
|  | 501 | $i += $Cnt; | 
|  | 502 | next; | 
|  | 503 | } | 
|  | 504 | $Uniqued{$Arg} = 1; | 
|  | 505 | } | 
|  | 506 |  | 
| Ted Kremenek | 887c49d | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 507 | push @CompileOpts,$Arg; | 
|  | 508 | push @LinkOpts,$Arg; | 
|  | 509 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 510 | while ($Cnt > 0) { | 
|  | 511 | ++$i; --$Cnt; | 
|  | 512 | push @CompileOpts, $ARGV[$i]; | 
|  | 513 | push @LinkOpts, $ARGV[$i]; | 
|  | 514 | } | 
|  | 515 | next; | 
|  | 516 | } | 
| Ted Kremenek | f18f460 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 517 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 518 | # Ignored options. | 
| Ted Kremenek | ad4a57d | 2008-10-19 06:42:38 +0000 | [diff] [blame] | 519 | if (defined $IgnoredOptionMap{$ArgKey}) { | 
|  | 520 | my $Cnt = $IgnoredOptionMap{$ArgKey}; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 521 | while ($Cnt > 0) { | 
|  | 522 | ++$i; --$Cnt; | 
|  | 523 | } | 
|  | 524 | next; | 
|  | 525 | } | 
| Ted Kremenek | f18f460 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 526 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 527 | # Compile mode flags. | 
|  | 528 | if ($Arg =~ /^-[D,I,U](.*)$/) { | 
|  | 529 | my $Tmp = $Arg; | 
|  | 530 | if ($1 eq '') { | 
|  | 531 | # FIXME: Check if we are going off the end. | 
|  | 532 | ++$i; | 
|  | 533 | $Tmp = $Arg . $ARGV[$i]; | 
|  | 534 | } | 
|  | 535 | push @CompileOpts,$Tmp; | 
|  | 536 | next; | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | # Language. | 
|  | 540 | if ($Arg eq '-x') { | 
|  | 541 | $Lang = $ARGV[$i+1]; | 
|  | 542 | ++$i; next; | 
|  | 543 | } | 
| Ted Kremenek | f18f460 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 544 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 545 | # Output file. | 
|  | 546 | if ($Arg eq '-o') { | 
|  | 547 | ++$i; | 
|  | 548 | $Output = $ARGV[$i]; | 
|  | 549 | next; | 
|  | 550 | } | 
|  | 551 |  | 
|  | 552 | # Get the link mode. | 
|  | 553 | if ($Arg =~ /^-[l,L,O]/) { | 
|  | 554 | if ($Arg eq '-O') { push @LinkOpts,'-O1'; } | 
|  | 555 | elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; } | 
|  | 556 | else { push @LinkOpts,$Arg; } | 
|  | 557 | next; | 
|  | 558 | } | 
|  | 559 |  | 
|  | 560 | if ($Arg =~ /^-std=/) { | 
|  | 561 | push @CompileOpts,$Arg; | 
|  | 562 | next; | 
|  | 563 | } | 
|  | 564 |  | 
|  | 565 | #  if ($Arg =~ /^-f/) { | 
|  | 566 | #    # FIXME: Not sure if the remaining -fxxxx options have no arguments. | 
|  | 567 | #    push @CompileOpts,$Arg; | 
|  | 568 | #    push @LinkOpts,$Arg;  # FIXME: Not sure if these are link opts. | 
|  | 569 | #  } | 
|  | 570 |  | 
|  | 571 | # Get the compiler/link mode. | 
|  | 572 | if ($Arg =~ /^-F(.+)$/) { | 
|  | 573 | my $Tmp = $Arg; | 
|  | 574 | if ($1 eq '') { | 
|  | 575 | # FIXME: Check if we are going off the end. | 
|  | 576 | ++$i; | 
|  | 577 | $Tmp = $Arg . $ARGV[$i]; | 
|  | 578 | } | 
|  | 579 | push @CompileOpts,$Tmp; | 
|  | 580 | push @LinkOpts,$Tmp; | 
|  | 581 | next; | 
|  | 582 | } | 
| Ted Kremenek | f18f460 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 583 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 584 | # Input files. | 
|  | 585 | if ($Arg eq '-filelist') { | 
|  | 586 | # FIXME: Make sure we aren't walking off the end. | 
|  | 587 | open(IN, $ARGV[$i+1]); | 
|  | 588 | while (<IN>) { s/\015?\012//; push @Files,$_; } | 
|  | 589 | close(IN); | 
| Ted Kremenek | d0d7256 | 2009-08-14 18:20:50 +0000 | [diff] [blame] | 590 | ++$i; | 
|  | 591 | next; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 592 | } | 
|  | 593 |  | 
| Ted Kremenek | d0d7256 | 2009-08-14 18:20:50 +0000 | [diff] [blame] | 594 | # Handle -Wno-.  We don't care about extra warnings, but | 
|  | 595 | # we should suppress ones that we don't want to see. | 
|  | 596 | if ($Arg =~ /^-Wno-/) { | 
|  | 597 | push @CompileOpts, $Arg; | 
|  | 598 | next; | 
|  | 599 | } | 
|  | 600 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 601 | if (!($Arg =~ /^-/)) { | 
| Ted Kremenek | d0d7256 | 2009-08-14 18:20:50 +0000 | [diff] [blame] | 602 | push @Files, $Arg; | 
|  | 603 | next; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 604 | } | 
|  | 605 | } | 
| Ted Kremenek | f18f460 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 606 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 607 | if ($Action eq 'compile' or $Action eq 'link') { | 
| Ted Kremenek | 9b15eff | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 608 | my @Archs = keys %ArchsSeen; | 
|  | 609 | # Skip the file if we don't support the architectures specified. | 
| Ted Kremenek | 86cb75a | 2009-02-25 00:10:37 +0000 | [diff] [blame] | 610 | exit 0 if ($HadArch && scalar(@Archs) == 0); | 
| Ted Kremenek | 9b15eff | 2009-02-24 22:07:12 +0000 | [diff] [blame] | 611 |  | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 612 | foreach my $file (@Files) { | 
|  | 613 | # Determine the language for the file. | 
|  | 614 | my $FileLang = $Lang; | 
|  | 615 |  | 
|  | 616 | if (!defined($FileLang)) { | 
|  | 617 | # Infer the language from the extension. | 
|  | 618 | if ($file =~ /[.]([^.]+)$/) { | 
|  | 619 | $FileLang = $LangMap{$1}; | 
|  | 620 | } | 
|  | 621 | } | 
| Ted Kremenek | e3fc13a | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 622 |  | 
| Ted Kremenek | 14015de | 2010-02-12 00:10:34 +0000 | [diff] [blame] | 623 | # FileLang still not defined?  Skip the file. | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 624 | next if (!defined $FileLang); | 
| Ted Kremenek | 14015de | 2010-02-12 00:10:34 +0000 | [diff] [blame] | 625 |  | 
|  | 626 | # Language not accepted? | 
| Ted Kremenek | dc99ec4 | 2009-05-11 21:08:34 +0000 | [diff] [blame] | 627 | next if (!defined $LangsAccepted{$FileLang}); | 
| Ted Kremenek | 14015de | 2010-02-12 00:10:34 +0000 | [diff] [blame] | 628 |  | 
| Ted Kremenek | 46727df | 2009-05-15 04:20:31 +0000 | [diff] [blame] | 629 | my @CmdArgs; | 
|  | 630 | my @AnalyzeArgs; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 631 |  | 
|  | 632 | if ($FileLang ne 'unknown') { | 
| Ted Kremenek | d4bcb4f | 2011-03-16 21:10:42 +0000 | [diff] [blame] | 633 | push @CmdArgs, '-x', $FileLang; | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 634 | } | 
| Ted Kremenek | 5efdf84 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 635 |  | 
| Zhongxing Xu | ad4c3de | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 636 | if (defined $StoreModel) { | 
| Ted Kremenek | b535181 | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 637 | push @AnalyzeArgs, "-analyzer-store=$StoreModel"; | 
| Zhongxing Xu | ad4c3de | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 638 | } | 
| Ted Kremenek | b535181 | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 639 |  | 
|  | 640 | if (defined $ConstraintsModel) { | 
|  | 641 | push @AnalyzeArgs, "-analyzer-constraints=$ConstraintsModel"; | 
|  | 642 | } | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 643 |  | 
| Anna Zaks | 5efad63 | 2011-08-31 23:53:24 +0000 | [diff] [blame] | 644 | if (defined $Analyses) { | 
|  | 645 | push @AnalyzeArgs, split '\s+', $Analyses; | 
|  | 646 | } | 
| Ted Kremenek | b535181 | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 647 |  | 
| Ted Kremenek | 9023055 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 648 | if (defined $OutputFormat) { | 
| Ted Kremenek | b535181 | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 649 | push @AnalyzeArgs, "-analyzer-output=" . $OutputFormat; | 
| Ted Kremenek | 5cc5486 | 2009-07-27 22:10:34 +0000 | [diff] [blame] | 650 | if ($OutputFormat =~ /plist/) { | 
| Ted Kremenek | 1374716 | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 651 | # Change "Output" to be a file. | 
|  | 652 | my ($h, $f) = tempfile("report-XXXXXX", SUFFIX => ".plist", | 
|  | 653 | DIR => $HtmlDir); | 
|  | 654 | $ResultFile = $f; | 
| Anna Zaks | 45ce1bf | 2011-09-09 18:43:53 +0000 | [diff] [blame] | 655 | # If the HtmlDir is not set, we sould clean up the plist files. | 
|  | 656 | if (!defined $HtmlDir || -z $HtmlDir) { | 
|  | 657 | $CleanupFile = $f; | 
|  | 658 | } | 
| Ted Kremenek | 1374716 | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 659 | } | 
| Ted Kremenek | 9023055 | 2008-11-04 00:02:53 +0000 | [diff] [blame] | 660 | } | 
| Zhongxing Xu | ad4c3de | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 661 |  | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 662 | push @CmdArgs, @CompileOpts; | 
|  | 663 | push @CmdArgs, $file; | 
| Zhongxing Xu | ad4c3de | 2008-10-27 14:26:32 +0000 | [diff] [blame] | 664 |  | 
| Ted Kremenek | 15146a5 | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 665 | if (scalar @Archs) { | 
|  | 666 | foreach my $arch (@Archs) { | 
|  | 667 | my @NewArgs; | 
| Ted Kremenek | d4bcb4f | 2011-03-16 21:10:42 +0000 | [diff] [blame] | 668 | push @NewArgs, '-arch', $arch; | 
| Ted Kremenek | 46727df | 2009-05-15 04:20:31 +0000 | [diff] [blame] | 669 | push @NewArgs, @CmdArgs; | 
| Ted Kremenek | 5c512e6 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 670 | Analyze($Clang, \@NewArgs, \@AnalyzeArgs, $FileLang, $Output, | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 671 | $Verbose, $HtmlDir, $file); | 
| Ted Kremenek | 15146a5 | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 672 | } | 
|  | 673 | } | 
|  | 674 | else { | 
| Ted Kremenek | 5c512e6 | 2009-12-11 22:44:53 +0000 | [diff] [blame] | 675 | Analyze($Clang, \@CmdArgs, \@AnalyzeArgs, $FileLang, $Output, | 
| Ted Kremenek | 42ec914 | 2011-02-17 02:28:30 +0000 | [diff] [blame] | 676 | $Verbose, $HtmlDir, $file); | 
| Ted Kremenek | 15146a5 | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 677 | } | 
| Ted Kremenek | f7ffd66 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 678 | } | 
|  | 679 | } | 
| Ted Kremenek | 5efdf84 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 680 |  | 
| Ted Kremenek | 7b62806 | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 681 | exit($Status >> 8); | 
|  | 682 |  |