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