| 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 | 2ec5cd5 | 2008-08-25 20:44:31 +0000 | [diff] [blame] | 20 |  | 
|  | 21 | my $CC = $ENV{'CCC_CC'}; | 
|  | 22 | if (!defined $CC) { $CC = "gcc"; } | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 23 |  | 
|  | 24 | ##----------------------------------------------------------------------------## | 
|  | 25 | #  Process Clang Crashes. | 
|  | 26 | ##----------------------------------------------------------------------------## | 
|  | 27 |  | 
|  | 28 | sub GetPPExt { | 
|  | 29 | my $Lang = shift; | 
|  | 30 | if ($Lang =~ /objective-c/) { return ".mi"; } | 
|  | 31 | return ".i"; | 
|  | 32 | } | 
|  | 33 |  | 
| Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 34 | sub ProcessClangFailure { | 
| Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 35 | my ($Clang, $Lang, $file, $Args, $HtmlDir, $ErrorType, $ofile) = @_; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 36 | my $Dir = "$HtmlDir/crashes"; | 
|  | 37 | mkpath $Dir; | 
| Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 38 |  | 
|  | 39 | # Generate the preprocessed file with cc (i.e., gcc). | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 40 | my ($PPH, $PPFile) = tempfile("clang_crash_XXXXXX", | 
|  | 41 | SUFFIX => GetPPExt($Lang), | 
|  | 42 | DIR => $Dir); | 
|  | 43 |  | 
| Ted Kremenek | 2ec5cd5 | 2008-08-25 20:44:31 +0000 | [diff] [blame] | 44 | system $CC, @$Args, "-E", "-o", $PPFile; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 45 | close ($PPH); | 
| Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 46 |  | 
|  | 47 | # Generate the preprocessed file with clang. | 
|  | 48 | my $PPFile_Clang = $PPFile; | 
|  | 49 | $PPFile_Clang =~ s/[.](.+)$/.clang.$1/; | 
|  | 50 | system $Clang, @$Args, "-E", "-o", "$PPFile_Clang"; | 
|  | 51 |  | 
|  | 52 | # Create the info file. | 
| Ted Kremenek | 82a1253 | 2008-09-25 00:25:16 +0000 | [diff] [blame] | 53 | 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] | 54 | print OUT abs_path($file), "\n"; | 
| Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 55 | print OUT "$ErrorType\n"; | 
| Ted Kremenek | 2dd7ad1 | 2008-08-18 20:55:25 +0000 | [diff] [blame] | 56 | print OUT "@$Args\n"; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 57 | close OUT; | 
| Ted Kremenek | 82a1253 | 2008-09-25 00:25:16 +0000 | [diff] [blame] | 58 | `uname -a >> $PPFile.info.txt 2>&1`; | 
|  | 59 | `$CC -v >> $PPFile.info.txt 2>&1`; | 
| Ted Kremenek | 9f9b1fd | 2008-09-12 22:49:36 +0000 | [diff] [blame] | 60 | system 'mv',$ofile,"$PPFile.stderr.txt"; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 61 | } | 
| Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 62 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 63 | ##----------------------------------------------------------------------------## | 
|  | 64 | #  Running the analyzer. | 
|  | 65 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 66 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 67 | sub Analyze { | 
|  | 68 | my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_; | 
| Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 69 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 70 | # Skip anything related to C++. | 
|  | 71 | return if ($Lang =~ /c[+][+]/); | 
| Ted Kremenek | 5d31f83 | 2008-08-18 18:38:29 +0000 | [diff] [blame] | 72 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 73 | my $RunAnalyzer = 0; | 
|  | 74 | my $Cmd; | 
|  | 75 | my @CmdArgs; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 76 | my @CmdArgsSansAnalyses; | 
| Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 77 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 78 | if ($Lang =~ /header/) { | 
|  | 79 | exit 0 if (!defined ($Output)); | 
|  | 80 | $Cmd = 'cp'; | 
|  | 81 | push @CmdArgs,$file; | 
|  | 82 | # Remove the PCH extension. | 
|  | 83 | $Output =~ s/[.]gch$//; | 
|  | 84 | push @CmdArgs,$Output; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 85 | @CmdArgsSansAnalyses = @CmdArgs; | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 86 | } | 
|  | 87 | else { | 
|  | 88 | $Cmd = $Clang; | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 89 | push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))'; | 
|  | 90 | push @CmdArgs,@$Args; | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 91 | @CmdArgsSansAnalyses = @CmdArgs; | 
|  | 92 | push @CmdArgs,(split /\s/,$Analyses); | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 93 | $RunAnalyzer = 1; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | my @PrintArgs; | 
|  | 97 | my $dir; | 
|  | 98 |  | 
|  | 99 | if ($Verbose) { | 
|  | 100 | $dir = getcwd(); | 
|  | 101 | print STDERR "\n[LOCATION]: $dir\n"; | 
|  | 102 | push @PrintArgs,"'$Cmd'"; | 
|  | 103 | foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; } | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | if ($Verbose == 1) { | 
| Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 107 | # We MUST print to stderr.  Some clients use the stdout output of | 
|  | 108 | # gcc for various purposes. | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 109 | print STDERR join(' ',@PrintArgs); | 
|  | 110 | print STDERR "\n"; | 
|  | 111 | } | 
|  | 112 | elsif ($Verbose == 2) { | 
|  | 113 | print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n"; | 
|  | 114 | } | 
| Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 115 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 116 | if ($RunAnalyzer and defined($HtmlDir)) { | 
|  | 117 | push @CmdArgs,'-o'; | 
|  | 118 | push @CmdArgs,$HtmlDir; | 
|  | 119 | } | 
| Ted Kremenek | 948e06b | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 120 |  | 
|  | 121 | if (defined $ENV{'CCC_UBI'}) { | 
|  | 122 | push @CmdArgs,"--analyzer-viz-egraph-ubigraph"; | 
|  | 123 | } | 
| Ted Kremenek | 991c54b | 2008-08-08 20:46:42 +0000 | [diff] [blame] | 124 |  | 
| Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 125 | # Capture the STDERR of clang and send it to a temporary file. | 
|  | 126 | # Capture the STDOUT of clang and reroute it to ccc-analyzer's STDERR. | 
|  | 127 | # We save the output file in the 'crashes' directory if clang encounters | 
|  | 128 | # any problems with the file. | 
| Ted Kremenek | 1346268 | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 129 | pipe (FROM_CHILD, TO_PARENT); | 
| Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 130 | my $pid = fork(); | 
|  | 131 | if ($pid == 0) { | 
| Ted Kremenek | 1346268 | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 132 | close FROM_CHILD; | 
|  | 133 | open(STDOUT,">&", \*TO_PARENT); | 
|  | 134 | open(STDERR,">&", \*TO_PARENT); | 
| Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 135 | exec $Cmd, @CmdArgs; | 
|  | 136 | } | 
| Ted Kremenek | 1346268 | 2008-09-11 23:05:26 +0000 | [diff] [blame] | 137 |  | 
|  | 138 | close TO_PARENT; | 
|  | 139 | my ($ofh, $ofile) = tempfile("clang_output_XXXXXX", DIR => $HtmlDir); | 
|  | 140 |  | 
|  | 141 | while (<FROM_CHILD>) { | 
|  | 142 | print $ofh $_; | 
|  | 143 | print STDERR $_; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | waitpid($pid,0); | 
| Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 147 | my $Result = $?; | 
|  | 148 |  | 
|  | 149 | # Did the command die because of a signal? | 
|  | 150 | if ($Result & 127 and $Cmd eq $Clang and defined $HtmlDir) { | 
| Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 151 | ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir, | 
| Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 152 | "Crash", $ofile); | 
|  | 153 | } | 
|  | 154 | elsif ($Result) { | 
| Ted Kremenek | c3998fa | 2008-09-25 00:51:44 +0000 | [diff] [blame] | 155 | ProcessClangFailure($Clang, $Lang, $file, \@CmdArgsSansAnalyses, $HtmlDir, | 
| Ted Kremenek | 9a3c7da | 2008-09-04 00:02:34 +0000 | [diff] [blame] | 156 | "Parser Rejects", $ofile); | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | `rm -f $ofile`; | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 160 | } | 
| Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 161 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 162 | ##----------------------------------------------------------------------------## | 
|  | 163 | #  Lookup tables. | 
|  | 164 | ##----------------------------------------------------------------------------## | 
|  | 165 |  | 
|  | 166 | my %CompileOptionMap = ( | 
|  | 167 | '-nostdinc' => 0, | 
|  | 168 | '-fobjc-gc-only' => 0, | 
|  | 169 | '-fobjc-gc' => 0, | 
|  | 170 | '-include' => 1, | 
|  | 171 | '-idirafter' => 1, | 
|  | 172 | '-iprefix' => 1, | 
|  | 173 | '-iquote' => 1, | 
|  | 174 | '-isystem' => 1, | 
|  | 175 | '-iwithprefix' => 1, | 
|  | 176 | '-iwithprefixbefore' => 1 | 
|  | 177 | ); | 
|  | 178 |  | 
|  | 179 | my %LinkerOptionMap = ( | 
|  | 180 | '-framework' => 1 | 
|  | 181 | ); | 
|  | 182 |  | 
|  | 183 | my %CompilerLinkerOptionMap = ( | 
|  | 184 | '-isysroot' => 1, | 
|  | 185 | '-arch' => 1, | 
| Ted Kremenek | e4f6952 | 2008-09-29 22:45:28 +0000 | [diff] [blame] | 186 | '-v' => 0, | 
| Ted Kremenek | b10362a | 2008-09-30 23:40:25 +0000 | [diff] [blame] | 187 | '-fpascal-strings' => 0, | 
|  | 188 | '-mmacosx-version-min' => 0 # This is really a 1 argument, but always has '=' | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 189 | ); | 
|  | 190 |  | 
|  | 191 | my %IgnoredOptionMap = ( | 
| Ted Kremenek | 9402609 | 2008-07-24 03:52:21 +0000 | [diff] [blame] | 192 | '-MT' => 1,  # Ignore these preprocessor options. | 
|  | 193 | '-MF' => 1, | 
|  | 194 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 195 | '-fsyntax-only' => 0, | 
|  | 196 | '-save-temps' => 0, | 
|  | 197 | '-install_name' => 1, | 
|  | 198 | '-exported_symbols_list' => 1, | 
|  | 199 | '-current_version' => 1, | 
|  | 200 | '-compatibility_version' => 1, | 
|  | 201 | '-init' => 1, | 
|  | 202 | '-e' => 1, | 
|  | 203 | '-seg1addr' => 1, | 
|  | 204 | '-bundle_loader' => 1, | 
|  | 205 | '-multiply_defined' => 1, | 
|  | 206 | '-sectorder' => 3, | 
|  | 207 | '--param' => 1, | 
|  | 208 | '-u' => 1 | 
|  | 209 | ); | 
|  | 210 |  | 
|  | 211 | my %LangMap = ( | 
|  | 212 | 'c'   => 'c', | 
|  | 213 | 'cpp' => 'c++', | 
|  | 214 | 'cc'  => 'c++', | 
|  | 215 | 'i'   => 'c-cpp-output', | 
|  | 216 | 'm'   => 'objective-c', | 
|  | 217 | 'mi'  => 'objective-c-cpp-output' | 
|  | 218 | ); | 
|  | 219 |  | 
| Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 220 | my %UniqueOptions = ( | 
|  | 221 | '-isysroot' => 0 | 
|  | 222 | ); | 
|  | 223 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 224 | ##----------------------------------------------------------------------------## | 
|  | 225 | #  Main Logic. | 
|  | 226 | ##----------------------------------------------------------------------------## | 
|  | 227 |  | 
|  | 228 | my $Action = 'link'; | 
|  | 229 | my @CompileOpts; | 
|  | 230 | my @LinkOpts; | 
|  | 231 | my @Files; | 
|  | 232 | my $Lang; | 
|  | 233 | my $Output; | 
| Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 234 | my %Uniqued; | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 235 |  | 
|  | 236 | # Forward arguments to gcc. | 
| Ted Kremenek | f17ef3c | 2008-08-21 21:47:09 +0000 | [diff] [blame] | 237 | my $Status = system($CC,@ARGV); | 
| Ted Kremenek | cb344d0 | 2008-08-28 01:18:44 +0000 | [diff] [blame] | 238 | if ($Status) { exit($Status >> 8); } | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 239 |  | 
|  | 240 | # Get the analysis options. | 
|  | 241 | my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'}; | 
|  | 242 | if (!defined($Analyses)) { $Analyses = '-checker-cfref'; } | 
|  | 243 |  | 
|  | 244 | # Determine the level of verbosity. | 
|  | 245 | my $Verbose = 0; | 
|  | 246 | if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; } | 
|  | 247 | if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; } | 
|  | 248 |  | 
|  | 249 | # Determine what clang executable to use. | 
|  | 250 | my $Clang = $ENV{'CLANG'}; | 
|  | 251 | if (!defined $Clang) { $Clang = 'clang'; } | 
|  | 252 |  | 
|  | 253 | # Get the HTML output directory. | 
|  | 254 | my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'}; | 
|  | 255 |  | 
| Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 256 | my %ArchsSeen; | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 257 |  | 
|  | 258 | # Process the arguments. | 
|  | 259 | foreach (my $i = 0; $i < scalar(@ARGV); ++$i) { | 
| Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame^] | 260 | my $Arg = $ARGV[$i]; | 
|  | 261 | my ($ArgKey) = split /=/,$Arg,2; | 
|  | 262 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 263 | # Modes ccc-analyzer supports | 
|  | 264 | if ($Arg eq '-E') { $Action = 'preprocess'; } | 
|  | 265 | elsif ($Arg eq '-c') { $Action = 'compile'; } | 
|  | 266 | elsif ($Arg =~ /^-print-prog-name/) { exit 0; } | 
| Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 267 |  | 
|  | 268 | # Specially handle duplicate cases of -arch | 
|  | 269 | if ($Arg eq "-arch") { | 
|  | 270 | my $arch = $ARGV[$i+1]; | 
|  | 271 | $ArchsSeen{$arch} = 1; | 
|  | 272 | ++$i; | 
|  | 273 | next; | 
|  | 274 | } | 
|  | 275 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 276 | # Options with possible arguments that should pass through to compiler. | 
| Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame^] | 277 | if (defined $CompileOptionMap{$ArgKey}) { | 
|  | 278 | my $Cnt = $CompileOptionMap{$ArgKey}; | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 279 | push @CompileOpts,$Arg; | 
|  | 280 | while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; } | 
|  | 281 | next; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | # Options with possible arguments that should pass through to linker. | 
| Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame^] | 285 | if (defined $LinkerOptionMap{$ArgKey}) { | 
|  | 286 | my $Cnt = $LinkerOptionMap{$ArgKey}; | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 287 | push @LinkOpts,$Arg; | 
|  | 288 | while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; } | 
|  | 289 | next; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | # Options with possible arguments that should pass through to both compiler | 
|  | 293 | # and the linker. | 
| Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame^] | 294 | if (defined $CompilerLinkerOptionMap{$ArgKey}) { | 
|  | 295 | my $Cnt = $CompilerLinkerOptionMap{$ArgKey}; | 
| Ted Kremenek | 47fc25f | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 296 |  | 
| Ted Kremenek | a30730e | 2008-09-29 16:15:20 +0000 | [diff] [blame] | 297 | # Check if this is an option that should have a unique value, and if so | 
|  | 298 | # determine if the value was checked before. | 
|  | 299 | if ($UniqueOptions{$Arg}) { | 
|  | 300 | if (defined $Uniqued{$Arg}) { | 
|  | 301 | $i += $Cnt; | 
|  | 302 | next; | 
|  | 303 | } | 
|  | 304 | $Uniqued{$Arg} = 1; | 
|  | 305 | } | 
|  | 306 |  | 
| Ted Kremenek | 47fc25f | 2008-09-29 23:06:09 +0000 | [diff] [blame] | 307 | push @CompileOpts,$Arg; | 
|  | 308 | push @LinkOpts,$Arg; | 
|  | 309 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 310 | while ($Cnt > 0) { | 
|  | 311 | ++$i; --$Cnt; | 
|  | 312 | push @CompileOpts, $ARGV[$i]; | 
|  | 313 | push @LinkOpts, $ARGV[$i]; | 
|  | 314 | } | 
|  | 315 | next; | 
|  | 316 | } | 
| Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 317 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 318 | # Ignored options. | 
| Ted Kremenek | 89c4fcf | 2008-10-19 06:42:38 +0000 | [diff] [blame^] | 319 | if (defined $IgnoredOptionMap{$ArgKey}) { | 
|  | 320 | my $Cnt = $IgnoredOptionMap{$ArgKey}; | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 321 | while ($Cnt > 0) { | 
|  | 322 | ++$i; --$Cnt; | 
|  | 323 | } | 
|  | 324 | next; | 
|  | 325 | } | 
| Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 326 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 327 | # Compile mode flags. | 
|  | 328 | if ($Arg =~ /^-[D,I,U](.*)$/) { | 
|  | 329 | my $Tmp = $Arg; | 
|  | 330 | if ($1 eq '') { | 
|  | 331 | # FIXME: Check if we are going off the end. | 
|  | 332 | ++$i; | 
|  | 333 | $Tmp = $Arg . $ARGV[$i]; | 
|  | 334 | } | 
|  | 335 | push @CompileOpts,$Tmp; | 
|  | 336 | next; | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | # Language. | 
|  | 340 | if ($Arg eq '-x') { | 
|  | 341 | $Lang = $ARGV[$i+1]; | 
|  | 342 | ++$i; next; | 
|  | 343 | } | 
| Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 344 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 345 | # Output file. | 
|  | 346 | if ($Arg eq '-o') { | 
|  | 347 | ++$i; | 
|  | 348 | $Output = $ARGV[$i]; | 
|  | 349 | next; | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | # Get the link mode. | 
|  | 353 | if ($Arg =~ /^-[l,L,O]/) { | 
|  | 354 | if ($Arg eq '-O') { push @LinkOpts,'-O1'; } | 
|  | 355 | elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; } | 
|  | 356 | else { push @LinkOpts,$Arg; } | 
|  | 357 | next; | 
|  | 358 | } | 
|  | 359 |  | 
|  | 360 | if ($Arg =~ /^-std=/) { | 
|  | 361 | push @CompileOpts,$Arg; | 
|  | 362 | next; | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | #  if ($Arg =~ /^-f/) { | 
|  | 366 | #    # FIXME: Not sure if the remaining -fxxxx options have no arguments. | 
|  | 367 | #    push @CompileOpts,$Arg; | 
|  | 368 | #    push @LinkOpts,$Arg;  # FIXME: Not sure if these are link opts. | 
|  | 369 | #  } | 
|  | 370 |  | 
|  | 371 | # Get the compiler/link mode. | 
|  | 372 | if ($Arg =~ /^-F(.+)$/) { | 
|  | 373 | my $Tmp = $Arg; | 
|  | 374 | if ($1 eq '') { | 
|  | 375 | # FIXME: Check if we are going off the end. | 
|  | 376 | ++$i; | 
|  | 377 | $Tmp = $Arg . $ARGV[$i]; | 
|  | 378 | } | 
|  | 379 | push @CompileOpts,$Tmp; | 
|  | 380 | push @LinkOpts,$Tmp; | 
|  | 381 | next; | 
|  | 382 | } | 
| Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 383 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 384 | # Input files. | 
|  | 385 | if ($Arg eq '-filelist') { | 
|  | 386 | # FIXME: Make sure we aren't walking off the end. | 
|  | 387 | open(IN, $ARGV[$i+1]); | 
|  | 388 | while (<IN>) { s/\015?\012//; push @Files,$_; } | 
|  | 389 | close(IN); | 
|  | 390 | ++$i; next; | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | if (!($Arg =~ /^-/)) { | 
|  | 394 | push @Files,$Arg; next; | 
|  | 395 | } | 
|  | 396 | } | 
| Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 397 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 398 | if ($Action eq 'compile' or $Action eq 'link') { | 
|  | 399 | foreach my $file (@Files) { | 
|  | 400 | # Determine the language for the file. | 
|  | 401 | my $FileLang = $Lang; | 
|  | 402 |  | 
|  | 403 | if (!defined($FileLang)) { | 
|  | 404 | # Infer the language from the extension. | 
|  | 405 | if ($file =~ /[.]([^.]+)$/) { | 
|  | 406 | $FileLang = $LangMap{$1}; | 
|  | 407 | } | 
|  | 408 | } | 
| Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 409 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 410 | next if (!defined $FileLang); | 
|  | 411 |  | 
|  | 412 | my @AnalyzeArgs; | 
|  | 413 |  | 
|  | 414 | if ($FileLang ne 'unknown') { | 
|  | 415 | push @AnalyzeArgs,'-x'; | 
|  | 416 | push @AnalyzeArgs,$FileLang; | 
|  | 417 | } | 
| Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 418 |  | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 419 | push @AnalyzeArgs,@CompileOpts; | 
|  | 420 | push @AnalyzeArgs,$file; | 
|  | 421 |  | 
| Ted Kremenek | 27783eb | 2008-09-25 20:17:57 +0000 | [diff] [blame] | 422 | my @Archs = keys %ArchsSeen; | 
|  | 423 | if (scalar @Archs) { | 
|  | 424 | foreach my $arch (@Archs) { | 
|  | 425 | my @NewArgs; | 
|  | 426 | push @NewArgs, '-arch'; | 
|  | 427 | push @NewArgs, $arch; | 
|  | 428 | push @NewArgs, @AnalyzeArgs; | 
|  | 429 | Analyze($Clang, \@NewArgs, $FileLang, $Output, | 
|  | 430 | $Verbose, $HtmlDir, $file, $Analyses); | 
|  | 431 | } | 
|  | 432 | } | 
|  | 433 | else { | 
|  | 434 | Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output, | 
|  | 435 | $Verbose, $HtmlDir, $file, $Analyses); | 
|  | 436 | } | 
| Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 437 | } | 
|  | 438 | } | 
| Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 439 |  | 
| Ted Kremenek | 948e06b | 2008-08-27 22:30:34 +0000 | [diff] [blame] | 440 | exit($Status >> 8); | 
|  | 441 |  |