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; |
| 17 | use Cwd; |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 18 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 19 | ##----------------------------------------------------------------------------## |
| 20 | # Running the analyzer. |
| 21 | ##----------------------------------------------------------------------------## |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 22 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 23 | sub Analyze { |
| 24 | my ($Clang, $Args, $Lang, $Output, $Verbose, $HtmlDir, $file, $Analyses) = @_; |
Seo Sanghyeon | d389465 | 2008-04-04 11:02:21 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 26 | # Skip anything related to C++. |
| 27 | return if ($Lang =~ /c[+][+]/); |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 28 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 29 | my $RunAnalyzer = 0; |
| 30 | my $Cmd; |
| 31 | my @CmdArgs; |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 33 | if ($Lang =~ /header/) { |
| 34 | exit 0 if (!defined ($Output)); |
| 35 | $Cmd = 'cp'; |
| 36 | push @CmdArgs,$file; |
| 37 | # Remove the PCH extension. |
| 38 | $Output =~ s/[.]gch$//; |
| 39 | push @CmdArgs,$Output; |
| 40 | } |
| 41 | else { |
| 42 | $Cmd = $Clang; |
| 43 | push @CmdArgs,(split /\s/,$Analyses); |
| 44 | push @CmdArgs,'-DIBOutlet=__attribute__((iboutlet))'; |
| 45 | push @CmdArgs,@$Args; |
| 46 | $RunAnalyzer = 1; |
| 47 | } |
| 48 | |
| 49 | my @PrintArgs; |
| 50 | my $dir; |
| 51 | |
| 52 | if ($Verbose) { |
| 53 | $dir = getcwd(); |
| 54 | print STDERR "\n[LOCATION]: $dir\n"; |
| 55 | push @PrintArgs,"'$Cmd'"; |
| 56 | foreach my $arg (@CmdArgs) { push @PrintArgs,"\'$arg\'"; } |
| 57 | } |
| 58 | |
| 59 | if ($Verbose == 1) { |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 60 | # We MUST print to stderr. Some clients use the stdout output of |
| 61 | # gcc for various purposes. |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 62 | print STDERR join(' ',@PrintArgs); |
| 63 | print STDERR "\n"; |
| 64 | } |
| 65 | elsif ($Verbose == 2) { |
| 66 | print STDERR "#SHELL (cd '$dir' && @PrintArgs)\n"; |
| 67 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 69 | if ($RunAnalyzer and defined($HtmlDir)) { |
| 70 | push @CmdArgs,'-o'; |
| 71 | push @CmdArgs,$HtmlDir; |
| 72 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 73 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 74 | system $Cmd,@CmdArgs; |
| 75 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 77 | ##----------------------------------------------------------------------------## |
| 78 | # Lookup tables. |
| 79 | ##----------------------------------------------------------------------------## |
| 80 | |
| 81 | my %CompileOptionMap = ( |
| 82 | '-nostdinc' => 0, |
| 83 | '-fobjc-gc-only' => 0, |
| 84 | '-fobjc-gc' => 0, |
| 85 | '-include' => 1, |
| 86 | '-idirafter' => 1, |
| 87 | '-iprefix' => 1, |
| 88 | '-iquote' => 1, |
| 89 | '-isystem' => 1, |
| 90 | '-iwithprefix' => 1, |
| 91 | '-iwithprefixbefore' => 1 |
| 92 | ); |
| 93 | |
| 94 | my %LinkerOptionMap = ( |
| 95 | '-framework' => 1 |
| 96 | ); |
| 97 | |
| 98 | my %CompilerLinkerOptionMap = ( |
| 99 | '-isysroot' => 1, |
| 100 | '-arch' => 1, |
| 101 | '-v' => 0 |
| 102 | ); |
| 103 | |
| 104 | my %IgnoredOptionMap = ( |
| 105 | '-fsyntax-only' => 0, |
| 106 | '-save-temps' => 0, |
| 107 | '-install_name' => 1, |
| 108 | '-exported_symbols_list' => 1, |
| 109 | '-current_version' => 1, |
| 110 | '-compatibility_version' => 1, |
| 111 | '-init' => 1, |
| 112 | '-e' => 1, |
| 113 | '-seg1addr' => 1, |
| 114 | '-bundle_loader' => 1, |
| 115 | '-multiply_defined' => 1, |
| 116 | '-sectorder' => 3, |
| 117 | '--param' => 1, |
| 118 | '-u' => 1 |
| 119 | ); |
| 120 | |
| 121 | my %LangMap = ( |
| 122 | 'c' => 'c', |
| 123 | 'cpp' => 'c++', |
| 124 | 'cc' => 'c++', |
| 125 | 'i' => 'c-cpp-output', |
| 126 | 'm' => 'objective-c', |
| 127 | 'mi' => 'objective-c-cpp-output' |
| 128 | ); |
| 129 | |
| 130 | ##----------------------------------------------------------------------------## |
| 131 | # Main Logic. |
| 132 | ##----------------------------------------------------------------------------## |
| 133 | |
| 134 | my $Action = 'link'; |
| 135 | my @CompileOpts; |
| 136 | my @LinkOpts; |
| 137 | my @Files; |
| 138 | my $Lang; |
| 139 | my $Output; |
| 140 | |
| 141 | # Forward arguments to gcc. |
| 142 | my $Status = system("gcc",@ARGV); |
| 143 | if ($Status) { exit($Status); } |
| 144 | |
| 145 | # Get the analysis options. |
| 146 | my $Analyses = $ENV{'CCC_ANALYZER_ANALYSIS'}; |
| 147 | if (!defined($Analyses)) { $Analyses = '-checker-cfref'; } |
| 148 | |
| 149 | # Determine the level of verbosity. |
| 150 | my $Verbose = 0; |
| 151 | if (defined $ENV{CCC_ANALYZER_VERBOSE}) { $Verbose = 1; } |
| 152 | if (defined $ENV{CCC_ANALYZER_LOG}) { $Verbose = 2; } |
| 153 | |
| 154 | # Determine what clang executable to use. |
| 155 | my $Clang = $ENV{'CLANG'}; |
| 156 | if (!defined $Clang) { $Clang = 'clang'; } |
| 157 | |
| 158 | # Get the HTML output directory. |
| 159 | my $HtmlDir = $ENV{'CCC_ANALYZER_HTML'}; |
| 160 | |
| 161 | |
| 162 | # Process the arguments. |
| 163 | foreach (my $i = 0; $i < scalar(@ARGV); ++$i) { |
| 164 | my $Arg = $ARGV[$i]; |
| 165 | |
| 166 | # Modes ccc-analyzer supports |
| 167 | if ($Arg eq '-E') { $Action = 'preprocess'; } |
| 168 | elsif ($Arg eq '-c') { $Action = 'compile'; } |
| 169 | elsif ($Arg =~ /^-print-prog-name/) { exit 0; } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 170 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 171 | # Options with possible arguments that should pass through to compiler. |
| 172 | if (defined $CompileOptionMap{$Arg}) { |
| 173 | my $Cnt = $CompileOptionMap{$Arg}; |
| 174 | push @CompileOpts,$Arg; |
| 175 | while ($Cnt > 0) { ++$i; --$Cnt; push @CompileOpts, $ARGV[$i]; } |
| 176 | next; |
| 177 | } |
| 178 | |
| 179 | # Options with possible arguments that should pass through to linker. |
| 180 | if (defined $LinkerOptionMap{$Arg}) { |
| 181 | my $Cnt = $LinkerOptionMap{$Arg}; |
| 182 | push @LinkOpts,$Arg; |
| 183 | while ($Cnt > 0) { ++$i; --$Cnt; push @LinkOpts, $ARGV[$i]; } |
| 184 | next; |
| 185 | } |
| 186 | |
| 187 | # Options with possible arguments that should pass through to both compiler |
| 188 | # and the linker. |
| 189 | if (defined $CompilerLinkerOptionMap{$Arg}) { |
| 190 | my $Cnt = $CompilerLinkerOptionMap{$Arg}; |
| 191 | push @CompileOpts,$Arg; |
| 192 | push @LinkOpts,$Arg; |
| 193 | while ($Cnt > 0) { |
| 194 | ++$i; --$Cnt; |
| 195 | push @CompileOpts, $ARGV[$i]; |
| 196 | push @LinkOpts, $ARGV[$i]; |
| 197 | } |
| 198 | next; |
| 199 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 201 | # Ignored options. |
| 202 | if (defined $IgnoredOptionMap{$Arg}) { |
| 203 | my $Cnt = $IgnoredOptionMap{$Arg}; |
| 204 | while ($Cnt > 0) { |
| 205 | ++$i; --$Cnt; |
| 206 | } |
| 207 | next; |
| 208 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 210 | # Compile mode flags. |
| 211 | if ($Arg =~ /^-[D,I,U](.*)$/) { |
| 212 | my $Tmp = $Arg; |
| 213 | if ($1 eq '') { |
| 214 | # FIXME: Check if we are going off the end. |
| 215 | ++$i; |
| 216 | $Tmp = $Arg . $ARGV[$i]; |
| 217 | } |
| 218 | push @CompileOpts,$Tmp; |
| 219 | next; |
| 220 | } |
| 221 | |
| 222 | # Language. |
| 223 | if ($Arg eq '-x') { |
| 224 | $Lang = $ARGV[$i+1]; |
| 225 | ++$i; next; |
| 226 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 228 | # Output file. |
| 229 | if ($Arg eq '-o') { |
| 230 | ++$i; |
| 231 | $Output = $ARGV[$i]; |
| 232 | next; |
| 233 | } |
| 234 | |
| 235 | # Get the link mode. |
| 236 | if ($Arg =~ /^-[l,L,O]/) { |
| 237 | if ($Arg eq '-O') { push @LinkOpts,'-O1'; } |
| 238 | elsif ($Arg eq '-Os') { push @LinkOpts,'-O2'; } |
| 239 | else { push @LinkOpts,$Arg; } |
| 240 | next; |
| 241 | } |
| 242 | |
| 243 | if ($Arg =~ /^-std=/) { |
| 244 | push @CompileOpts,$Arg; |
| 245 | next; |
| 246 | } |
| 247 | |
| 248 | # if ($Arg =~ /^-f/) { |
| 249 | # # FIXME: Not sure if the remaining -fxxxx options have no arguments. |
| 250 | # push @CompileOpts,$Arg; |
| 251 | # push @LinkOpts,$Arg; # FIXME: Not sure if these are link opts. |
| 252 | # } |
| 253 | |
| 254 | # Get the compiler/link mode. |
| 255 | if ($Arg =~ /^-F(.+)$/) { |
| 256 | my $Tmp = $Arg; |
| 257 | if ($1 eq '') { |
| 258 | # FIXME: Check if we are going off the end. |
| 259 | ++$i; |
| 260 | $Tmp = $Arg . $ARGV[$i]; |
| 261 | } |
| 262 | push @CompileOpts,$Tmp; |
| 263 | push @LinkOpts,$Tmp; |
| 264 | next; |
| 265 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 266 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 267 | # Input files. |
| 268 | if ($Arg eq '-filelist') { |
| 269 | # FIXME: Make sure we aren't walking off the end. |
| 270 | open(IN, $ARGV[$i+1]); |
| 271 | while (<IN>) { s/\015?\012//; push @Files,$_; } |
| 272 | close(IN); |
| 273 | ++$i; next; |
| 274 | } |
| 275 | |
| 276 | if (!($Arg =~ /^-/)) { |
| 277 | push @Files,$Arg; next; |
| 278 | } |
| 279 | } |
Ted Kremenek | 61cd988 | 2008-05-24 15:58:54 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 281 | if ($Action eq 'compile' or $Action eq 'link') { |
| 282 | foreach my $file (@Files) { |
| 283 | # Determine the language for the file. |
| 284 | my $FileLang = $Lang; |
| 285 | |
| 286 | if (!defined($FileLang)) { |
| 287 | # Infer the language from the extension. |
| 288 | if ($file =~ /[.]([^.]+)$/) { |
| 289 | $FileLang = $LangMap{$1}; |
| 290 | } |
| 291 | } |
Ted Kremenek | 1262fc4 | 2008-05-14 20:10:33 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 293 | next if (!defined $FileLang); |
| 294 | |
| 295 | my @AnalyzeArgs; |
| 296 | |
| 297 | if ($FileLang ne 'unknown') { |
| 298 | push @AnalyzeArgs,'-x'; |
| 299 | push @AnalyzeArgs,$FileLang; |
| 300 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | fbeeca8 | 2008-07-19 06:11:04 +0000 | [diff] [blame] | 302 | push @AnalyzeArgs,@CompileOpts; |
| 303 | push @AnalyzeArgs,$file; |
| 304 | |
| 305 | Analyze($Clang, \@AnalyzeArgs, $FileLang, $Output, |
| 306 | $Verbose, $HtmlDir, $file, $Analyses); |
| 307 | } |
| 308 | } |
Ted Kremenek | b098288 | 2008-03-25 22:35:32 +0000 | [diff] [blame] | 309 | |