Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | Static Analyzer qualification infrastructure. |
| 5 | |
| 6 | The goal is to test the analyzer against different projects, check for failures, |
| 7 | compare results, and measure performance. |
| 8 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 9 | Repository Directory will contain sources of the projects as well as the |
| 10 | information on how to build them and the expected output. |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 11 | Repository Directory structure: |
| 12 | - ProjectMap file |
| 13 | - Historical Performance Data |
| 14 | - Project Dir1 |
| 15 | - ReferenceOutput |
| 16 | - Project Dir2 |
| 17 | - ReferenceOutput |
| 18 | .. |
Gabor Horvath | c3177f2 | 2015-07-08 18:39:31 +0000 | [diff] [blame] | 19 | Note that the build tree must be inside the project dir. |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 20 | |
| 21 | To test the build of the analyzer one would: |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 22 | - Copy over a copy of the Repository Directory. (TODO: Prefer to ensure that |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 23 | the build directory does not pollute the repository to min network traffic). |
| 24 | - Build all projects, until error. Produce logs to report errors. |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 25 | - Compare results. |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 27 | The files which should be kept around for failure investigations: |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 28 | RepositoryCopy/Project DirI/ScanBuildResults |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 29 | RepositoryCopy/Project DirI/run_static_analyzer.log |
| 30 | |
| 31 | Assumptions (TODO: shouldn't need to assume these.): |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 32 | The script is being run from the Repository Directory. |
Anna Zaks | 42a4463 | 2011-11-02 20:46:50 +0000 | [diff] [blame] | 33 | The compiler for scan-build and scan-build are in the PATH. |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 34 | export PATH=/Users/zaks/workspace/c2llvm/build/Release+Asserts/bin:$PATH |
| 35 | |
| 36 | For more logging, set the env variables: |
| 37 | zaks:TI zaks$ export CCC_ANALYZER_LOG=1 |
| 38 | zaks:TI zaks$ export CCC_ANALYZER_VERBOSE=1 |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 39 | |
Gabor Horvath | da32a86 | 2015-08-20 22:59:49 +0000 | [diff] [blame] | 40 | The list of checkers tested are hardcoded in the Checkers variable. |
| 41 | For testing additional checkers, use the SA_ADDITIONAL_CHECKERS environment |
| 42 | variable. It should contain a comma separated list. |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 43 | """ |
| 44 | import CmpRuns |
| 45 | |
| 46 | import os |
| 47 | import csv |
| 48 | import sys |
| 49 | import glob |
Ted Kremenek | f9a539d | 2012-08-28 20:40:04 +0000 | [diff] [blame] | 50 | import math |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 51 | import shutil |
| 52 | import time |
| 53 | import plistlib |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 54 | import argparse |
Devin Coughlin | bace032 | 2015-09-14 21:22:24 +0000 | [diff] [blame] | 55 | from subprocess import check_call, check_output, CalledProcessError |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | 42c1442 | 2012-08-28 20:40:02 +0000 | [diff] [blame] | 57 | #------------------------------------------------------------------------------ |
| 58 | # Helper functions. |
| 59 | #------------------------------------------------------------------------------ |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | f9a539d | 2012-08-28 20:40:04 +0000 | [diff] [blame] | 61 | def detectCPUs(): |
| 62 | """ |
| 63 | Detects the number of CPUs on a system. Cribbed from pp. |
| 64 | """ |
| 65 | # Linux, Unix and MacOS: |
| 66 | if hasattr(os, "sysconf"): |
| 67 | if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"): |
| 68 | # Linux & Unix: |
| 69 | ncpus = os.sysconf("SC_NPROCESSORS_ONLN") |
| 70 | if isinstance(ncpus, int) and ncpus > 0: |
| 71 | return ncpus |
| 72 | else: # OSX: |
| 73 | return int(capture(['sysctl', '-n', 'hw.ncpu'])) |
| 74 | # Windows: |
| 75 | if os.environ.has_key("NUMBER_OF_PROCESSORS"): |
| 76 | ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]) |
| 77 | if ncpus > 0: |
| 78 | return ncpus |
| 79 | return 1 # Default |
| 80 | |
Ted Kremenek | 6cb2080 | 2012-08-28 20:20:52 +0000 | [diff] [blame] | 81 | def which(command, paths = None): |
| 82 | """which(command, [paths]) - Look up the given command in the paths string |
| 83 | (or the PATH environment variable, if unspecified).""" |
| 84 | |
| 85 | if paths is None: |
| 86 | paths = os.environ.get('PATH','') |
| 87 | |
| 88 | # Check for absolute match first. |
| 89 | if os.path.exists(command): |
| 90 | return command |
| 91 | |
| 92 | # Would be nice if Python had a lib function for this. |
| 93 | if not paths: |
| 94 | paths = os.defpath |
| 95 | |
| 96 | # Get suffixes to search. |
| 97 | # On Cygwin, 'PATHEXT' may exist but it should not be used. |
| 98 | if os.pathsep == ';': |
| 99 | pathext = os.environ.get('PATHEXT', '').split(';') |
| 100 | else: |
| 101 | pathext = [''] |
| 102 | |
| 103 | # Search the paths... |
| 104 | for path in paths.split(os.pathsep): |
| 105 | for ext in pathext: |
| 106 | p = os.path.join(path, command + ext) |
| 107 | if os.path.exists(p): |
| 108 | return p |
| 109 | |
| 110 | return None |
| 111 | |
Anna Zaks | de1f7f8b | 2012-01-10 18:10:25 +0000 | [diff] [blame] | 112 | # Make sure we flush the output after every print statement. |
| 113 | class flushfile(object): |
| 114 | def __init__(self, f): |
| 115 | self.f = f |
| 116 | def write(self, x): |
| 117 | self.f.write(x) |
| 118 | self.f.flush() |
| 119 | |
| 120 | sys.stdout = flushfile(sys.stdout) |
| 121 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 122 | def getProjectMapPath(): |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 123 | ProjectMapPath = os.path.join(os.path.abspath(os.curdir), |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 124 | ProjectMapFile) |
| 125 | if not os.path.exists(ProjectMapPath): |
| 126 | print "Error: Cannot find the Project Map file " + ProjectMapPath +\ |
| 127 | "\nRunning script for the wrong directory?" |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 128 | sys.exit(-1) |
| 129 | return ProjectMapPath |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 130 | |
| 131 | def getProjectDir(ID): |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 132 | return os.path.join(os.path.abspath(os.curdir), ID) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 133 | |
Jordan Rose | 01ac572 | 2012-06-01 16:24:38 +0000 | [diff] [blame] | 134 | def getSBOutputDirName(IsReferenceBuild) : |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 135 | if IsReferenceBuild == True : |
| 136 | return SBOutputDirReferencePrefix + SBOutputDirName |
| 137 | else : |
| 138 | return SBOutputDirName |
| 139 | |
Ted Kremenek | 42c1442 | 2012-08-28 20:40:02 +0000 | [diff] [blame] | 140 | #------------------------------------------------------------------------------ |
| 141 | # Configuration setup. |
| 142 | #------------------------------------------------------------------------------ |
| 143 | |
| 144 | # Find Clang for static analysis. |
| 145 | Clang = which("clang", os.environ['PATH']) |
| 146 | if not Clang: |
| 147 | print "Error: cannot find 'clang' in PATH" |
| 148 | sys.exit(-1) |
| 149 | |
Ted Kremenek | f9a539d | 2012-08-28 20:40:04 +0000 | [diff] [blame] | 150 | # Number of jobs. |
Jordan Rose | 8832924 | 2012-11-16 17:41:21 +0000 | [diff] [blame] | 151 | Jobs = int(math.ceil(detectCPUs() * 0.75)) |
Ted Kremenek | f9a539d | 2012-08-28 20:40:04 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 42c1442 | 2012-08-28 20:40:02 +0000 | [diff] [blame] | 153 | # Project map stores info about all the "registered" projects. |
| 154 | ProjectMapFile = "projectMap.csv" |
| 155 | |
| 156 | # Names of the project specific scripts. |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 157 | # The script that downloads the project. |
| 158 | DownloadScript = "download_project.sh" |
Ted Kremenek | 42c1442 | 2012-08-28 20:40:02 +0000 | [diff] [blame] | 159 | # The script that needs to be executed before the build can start. |
| 160 | CleanupScript = "cleanup_run_static_analyzer.sh" |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 161 | # This is a file containing commands for scan-build. |
Ted Kremenek | 42c1442 | 2012-08-28 20:40:02 +0000 | [diff] [blame] | 162 | BuildScript = "run_static_analyzer.cmd" |
| 163 | |
| 164 | # The log file name. |
| 165 | LogFolderName = "Logs" |
| 166 | BuildLogName = "run_static_analyzer.log" |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 167 | # Summary file - contains the summary of the failures. Ex: This info can be be |
Ted Kremenek | 42c1442 | 2012-08-28 20:40:02 +0000 | [diff] [blame] | 168 | # displayed when buildbot detects a build failure. |
| 169 | NumOfFailuresInSummary = 10 |
| 170 | FailuresSummaryFileName = "failures.txt" |
| 171 | # Summary of the result diffs. |
| 172 | DiffsSummaryFileName = "diffs.txt" |
| 173 | |
| 174 | # The scan-build result directory. |
| 175 | SBOutputDirName = "ScanBuildResults" |
| 176 | SBOutputDirReferencePrefix = "Ref" |
| 177 | |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 178 | # The name of the directory storing the cached project source. If this directory |
| 179 | # does not exist, the download script will be executed. That script should |
| 180 | # create the "CachedSource" directory and download the project source into it. |
| 181 | CachedSourceDirName = "CachedSource" |
| 182 | |
| 183 | # The name of the directory containing the source code that will be analyzed. |
| 184 | # Each time a project is analyzed, a fresh copy of its CachedSource directory |
| 185 | # will be copied to the PatchedSource directory and then the local patches |
| 186 | # in PatchfileName will be applied (if PatchfileName exists). |
| 187 | PatchedSourceDirName = "PatchedSource" |
| 188 | |
| 189 | # The name of the patchfile specifying any changes that should be applied |
| 190 | # to the CachedSource before analyzing. |
| 191 | PatchfileName = "changes_for_analyzer.patch" |
| 192 | |
Ted Kremenek | 42c1442 | 2012-08-28 20:40:02 +0000 | [diff] [blame] | 193 | # The list of checkers used during analyzes. |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 194 | # Currently, consists of all the non-experimental checkers, plus a few alpha |
Jordan Rose | 10ad081 | 2013-04-05 17:55:07 +0000 | [diff] [blame] | 195 | # checkers we don't want to regress on. |
Anna Zaks | 8a02031 | 2014-10-31 17:40:14 +0000 | [diff] [blame] | 196 | Checkers="alpha.unix.SimpleStream,alpha.security.taint,cplusplus.NewDeleteLeaks,core,cplusplus,deadcode,security,unix,osx" |
Ted Kremenek | 42c1442 | 2012-08-28 20:40:02 +0000 | [diff] [blame] | 197 | |
| 198 | Verbose = 1 |
| 199 | |
| 200 | #------------------------------------------------------------------------------ |
| 201 | # Test harness logic. |
| 202 | #------------------------------------------------------------------------------ |
| 203 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 204 | # Run pre-processing script if any. |
Anna Zaks | 42a4463 | 2011-11-02 20:46:50 +0000 | [diff] [blame] | 205 | def runCleanupScript(Dir, PBuildLogFile): |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 206 | Cwd = os.path.join(Dir, PatchedSourceDirName) |
Anna Zaks | 42a4463 | 2011-11-02 20:46:50 +0000 | [diff] [blame] | 207 | ScriptPath = os.path.join(Dir, CleanupScript) |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 208 | runScript(ScriptPath, PBuildLogFile, Cwd) |
| 209 | |
| 210 | # Run the script to download the project, if it exists. |
| 211 | def runDownloadScript(Dir, PBuildLogFile): |
| 212 | ScriptPath = os.path.join(Dir, DownloadScript) |
| 213 | runScript(ScriptPath, PBuildLogFile, Dir) |
| 214 | |
| 215 | # Run the provided script if it exists. |
| 216 | def runScript(ScriptPath, PBuildLogFile, Cwd): |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 217 | if os.path.exists(ScriptPath): |
| 218 | try: |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 219 | if Verbose == 1: |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 220 | print " Executing: %s" % (ScriptPath,) |
Devin Coughlin | ab95cd2 | 2016-01-22 07:08:06 +0000 | [diff] [blame] | 221 | check_call("chmod +x '%s'" % ScriptPath, cwd = Cwd, |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 222 | stderr=PBuildLogFile, |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 223 | stdout=PBuildLogFile, |
| 224 | shell=True) |
Devin Coughlin | ab95cd2 | 2016-01-22 07:08:06 +0000 | [diff] [blame] | 225 | check_call("'%s'" % ScriptPath, cwd = Cwd, stderr=PBuildLogFile, |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 226 | stdout=PBuildLogFile, |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 227 | shell=True) |
| 228 | except: |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 229 | print "Error: Running %s failed. See %s for details." % (ScriptPath, |
| 230 | PBuildLogFile.name) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 231 | sys.exit(-1) |
| 232 | |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 233 | # Download the project and apply the local patchfile if it exists. |
| 234 | def downloadAndPatch(Dir, PBuildLogFile): |
| 235 | CachedSourceDirPath = os.path.join(Dir, CachedSourceDirName) |
| 236 | |
| 237 | # If the we don't already have the cached source, run the project's |
| 238 | # download script to download it. |
| 239 | if not os.path.exists(CachedSourceDirPath): |
| 240 | runDownloadScript(Dir, PBuildLogFile) |
| 241 | if not os.path.exists(CachedSourceDirPath): |
| 242 | print "Error: '%s' not found after download." % (CachedSourceDirPath) |
| 243 | exit(-1) |
| 244 | |
| 245 | PatchedSourceDirPath = os.path.join(Dir, PatchedSourceDirName) |
| 246 | |
| 247 | # Remove potentially stale patched source. |
| 248 | if os.path.exists(PatchedSourceDirPath): |
| 249 | shutil.rmtree(PatchedSourceDirPath) |
| 250 | |
| 251 | # Copy the cached source and apply any patches to the copy. |
| 252 | shutil.copytree(CachedSourceDirPath, PatchedSourceDirPath, symlinks=True) |
| 253 | applyPatch(Dir, PBuildLogFile) |
| 254 | |
| 255 | def applyPatch(Dir, PBuildLogFile): |
| 256 | PatchfilePath = os.path.join(Dir, PatchfileName) |
| 257 | PatchedSourceDirPath = os.path.join(Dir, PatchedSourceDirName) |
| 258 | if not os.path.exists(PatchfilePath): |
| 259 | print " No local patches." |
| 260 | return |
| 261 | |
| 262 | print " Applying patch." |
| 263 | try: |
Devin Coughlin | ab95cd2 | 2016-01-22 07:08:06 +0000 | [diff] [blame] | 264 | check_call("patch -p1 < '%s'" % (PatchfilePath), |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 265 | cwd = PatchedSourceDirPath, |
| 266 | stderr=PBuildLogFile, |
| 267 | stdout=PBuildLogFile, |
| 268 | shell=True) |
| 269 | except: |
| 270 | print "Error: Patch failed. See %s for details." % (PBuildLogFile.name) |
| 271 | sys.exit(-1) |
| 272 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 273 | # Build the project with scan-build by reading in the commands and |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 274 | # prefixing them with the scan-build options. |
| 275 | def runScanBuild(Dir, SBOutputDir, PBuildLogFile): |
| 276 | BuildScriptPath = os.path.join(Dir, BuildScript) |
| 277 | if not os.path.exists(BuildScriptPath): |
| 278 | print "Error: build script is not defined: %s" % BuildScriptPath |
Ted Kremenek | 6cb2080 | 2012-08-28 20:20:52 +0000 | [diff] [blame] | 279 | sys.exit(-1) |
Devin Coughlin | f3695c8 | 2015-09-16 01:52:32 +0000 | [diff] [blame] | 280 | |
| 281 | AllCheckers = Checkers |
| 282 | if os.environ.has_key('SA_ADDITIONAL_CHECKERS'): |
| 283 | AllCheckers = AllCheckers + ',' + os.environ['SA_ADDITIONAL_CHECKERS'] |
| 284 | |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 285 | # Run scan-build from within the patched source directory. |
| 286 | SBCwd = os.path.join(Dir, PatchedSourceDirName) |
| 287 | |
Devin Coughlin | 86f61a9 | 2016-01-22 18:45:22 +0000 | [diff] [blame] | 288 | SBOptions = "--use-analyzer '%s' " % Clang |
| 289 | SBOptions += "-plist-html -o '%s' " % SBOutputDir |
Devin Coughlin | f3695c8 | 2015-09-16 01:52:32 +0000 | [diff] [blame] | 290 | SBOptions += "-enable-checker " + AllCheckers + " " |
Jordan Rose | b18179d | 2013-01-24 23:07:59 +0000 | [diff] [blame] | 291 | SBOptions += "--keep-empty " |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 292 | # Always use ccc-analyze to ensure that we can locate the failures |
Anna Zaks | 7b4f8a4 | 2013-05-31 02:31:09 +0000 | [diff] [blame] | 293 | # directory. |
| 294 | SBOptions += "--override-compiler " |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 295 | try: |
| 296 | SBCommandFile = open(BuildScriptPath, "r") |
| 297 | SBPrefix = "scan-build " + SBOptions + " " |
| 298 | for Command in SBCommandFile: |
Jordan Rose | 7bd9186 | 2013-09-06 16:12:41 +0000 | [diff] [blame] | 299 | Command = Command.strip() |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 300 | if len(Command) == 0: |
| 301 | continue; |
Ted Kremenek | f9a539d | 2012-08-28 20:40:04 +0000 | [diff] [blame] | 302 | # If using 'make', auto imply a -jX argument |
| 303 | # to speed up analysis. xcodebuild will |
| 304 | # automatically use the maximum number of cores. |
Jordan Rose | 64e4cf0 | 2012-11-26 19:59:57 +0000 | [diff] [blame] | 305 | if (Command.startswith("make ") or Command == "make") and \ |
| 306 | "-j" not in Command: |
Jordan Rose | 8832924 | 2012-11-16 17:41:21 +0000 | [diff] [blame] | 307 | Command += " -j%d" % Jobs |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 308 | SBCommand = SBPrefix + Command |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 309 | if Verbose == 1: |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 310 | print " Executing: %s" % (SBCommand,) |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 311 | check_call(SBCommand, cwd = SBCwd, stderr=PBuildLogFile, |
| 312 | stdout=PBuildLogFile, |
| 313 | shell=True) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 314 | except: |
| 315 | print "Error: scan-build failed. See ",PBuildLogFile.name,\ |
| 316 | " for details." |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 317 | raise |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 318 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 319 | def hasNoExtension(FileName): |
| 320 | (Root, Ext) = os.path.splitext(FileName) |
| 321 | if ((Ext == "")) : |
| 322 | return True |
| 323 | return False |
| 324 | |
| 325 | def isValidSingleInputFile(FileName): |
| 326 | (Root, Ext) = os.path.splitext(FileName) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 327 | if ((Ext == ".i") | (Ext == ".ii") | |
| 328 | (Ext == ".c") | (Ext == ".cpp") | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 329 | (Ext == ".m") | (Ext == "")) : |
| 330 | return True |
| 331 | return False |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 332 | |
Devin Coughlin | bace032 | 2015-09-14 21:22:24 +0000 | [diff] [blame] | 333 | # Get the path to the SDK for the given SDK name. Returns None if |
| 334 | # the path cannot be determined. |
| 335 | def getSDKPath(SDKName): |
| 336 | if which("xcrun") is None: |
| 337 | return None |
| 338 | |
| 339 | Cmd = "xcrun --sdk " + SDKName + " --show-sdk-path" |
| 340 | return check_output(Cmd, shell=True).rstrip() |
| 341 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 342 | # Run analysis on a set of preprocessed files. |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 343 | def runAnalyzePreprocessed(Dir, SBOutputDir, Mode): |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 344 | if os.path.exists(os.path.join(Dir, BuildScript)): |
| 345 | print "Error: The preprocessed files project should not contain %s" % \ |
| 346 | BuildScript |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 347 | raise Exception() |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 348 | |
Devin Coughlin | bace032 | 2015-09-14 21:22:24 +0000 | [diff] [blame] | 349 | CmdPrefix = Clang + " -cc1 " |
| 350 | |
| 351 | # For now, we assume the preprocessed files should be analyzed |
| 352 | # with the OS X SDK. |
| 353 | SDKPath = getSDKPath("macosx") |
| 354 | if SDKPath is not None: |
| 355 | CmdPrefix += "-isysroot " + SDKPath + " " |
| 356 | |
| 357 | CmdPrefix += "-analyze -analyzer-output=plist -w " |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 358 | CmdPrefix += "-analyzer-checker=" + Checkers +" -fcxx-exceptions -fblocks " |
| 359 | |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 360 | if (Mode == 2) : |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 361 | CmdPrefix += "-std=c++11 " |
| 362 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 363 | PlistPath = os.path.join(Dir, SBOutputDir, "date") |
| 364 | FailPath = os.path.join(PlistPath, "failures"); |
| 365 | os.makedirs(FailPath); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 366 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 367 | for FullFileName in glob.glob(Dir + "/*"): |
| 368 | FileName = os.path.basename(FullFileName) |
| 369 | Failed = False |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 370 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 371 | # Only run the analyzes on supported files. |
| 372 | if (hasNoExtension(FileName)): |
| 373 | continue |
| 374 | if (isValidSingleInputFile(FileName) == False): |
| 375 | print "Error: Invalid single input file %s." % (FullFileName,) |
| 376 | raise Exception() |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 377 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 378 | # Build and call the analyzer command. |
Devin Coughlin | ab95cd2 | 2016-01-22 07:08:06 +0000 | [diff] [blame] | 379 | OutputOption = "-o '%s.plist' " % os.path.join(PlistPath, FileName) |
| 380 | Command = CmdPrefix + OutputOption + ("'%s'" % FileName) |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 381 | LogFile = open(os.path.join(FailPath, FileName + ".stderr.txt"), "w+b") |
| 382 | try: |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 383 | if Verbose == 1: |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 384 | print " Executing: %s" % (Command,) |
| 385 | check_call(Command, cwd = Dir, stderr=LogFile, |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 386 | stdout=LogFile, |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 387 | shell=True) |
| 388 | except CalledProcessError, e: |
| 389 | print "Error: Analyzes of %s failed. See %s for details." \ |
| 390 | "Error code %d." % \ |
| 391 | (FullFileName, LogFile.name, e.returncode) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 392 | Failed = True |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 393 | finally: |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 394 | LogFile.close() |
| 395 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 396 | # If command did not fail, erase the log file. |
| 397 | if Failed == False: |
| 398 | os.remove(LogFile.name); |
| 399 | |
Devin Coughlin | 9ea8033 | 2016-01-23 01:09:07 +0000 | [diff] [blame] | 400 | def getBuildLogPath(SBOutputDir): |
| 401 | return os.path.join(SBOutputDir, LogFolderName, BuildLogName) |
| 402 | |
| 403 | def removeLogFile(SBOutputDir): |
| 404 | BuildLogPath = getBuildLogPath(SBOutputDir) |
| 405 | # Clean up the log file. |
| 406 | if (os.path.exists(BuildLogPath)) : |
| 407 | RmCommand = "rm '%s'" % BuildLogPath |
| 408 | if Verbose == 1: |
| 409 | print " Executing: %s" % (RmCommand,) |
| 410 | check_call(RmCommand, shell=True) |
| 411 | |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 412 | def buildProject(Dir, SBOutputDir, ProjectBuildMode, IsReferenceBuild): |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 413 | TBegin = time.time() |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 414 | |
Devin Coughlin | 9ea8033 | 2016-01-23 01:09:07 +0000 | [diff] [blame] | 415 | BuildLogPath = getBuildLogPath(SBOutputDir) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 416 | print "Log file: %s" % (BuildLogPath,) |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 417 | print "Output directory: %s" %(SBOutputDir, ) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 418 | |
Devin Coughlin | 9ea8033 | 2016-01-23 01:09:07 +0000 | [diff] [blame] | 419 | removeLogFile(SBOutputDir) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 420 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 421 | # Clean up scan build results. |
| 422 | if (os.path.exists(SBOutputDir)) : |
Devin Coughlin | ab95cd2 | 2016-01-22 07:08:06 +0000 | [diff] [blame] | 423 | RmCommand = "rm -r '%s'" % SBOutputDir |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 424 | if Verbose == 1: |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 425 | print " Executing: %s" % (RmCommand,) |
| 426 | check_call(RmCommand, shell=True) |
| 427 | assert(not os.path.exists(SBOutputDir)) |
| 428 | os.makedirs(os.path.join(SBOutputDir, LogFolderName)) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 429 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 430 | # Open the log file. |
| 431 | PBuildLogFile = open(BuildLogPath, "wb+") |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 432 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 433 | # Build and analyze the project. |
| 434 | try: |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 435 | if (ProjectBuildMode == 1): |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 436 | downloadAndPatch(Dir, PBuildLogFile) |
| 437 | runCleanupScript(Dir, PBuildLogFile) |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 438 | runScanBuild(Dir, SBOutputDir, PBuildLogFile) |
| 439 | else: |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 440 | runAnalyzePreprocessed(Dir, SBOutputDir, ProjectBuildMode) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 441 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 442 | if IsReferenceBuild : |
Anna Zaks | 42a4463 | 2011-11-02 20:46:50 +0000 | [diff] [blame] | 443 | runCleanupScript(Dir, PBuildLogFile) |
Gabor Horvath | c3177f2 | 2015-07-08 18:39:31 +0000 | [diff] [blame] | 444 | |
| 445 | # Make the absolute paths relative in the reference results. |
| 446 | for (DirPath, Dirnames, Filenames) in os.walk(SBOutputDir): |
| 447 | for F in Filenames: |
| 448 | if (not F.endswith('plist')): |
| 449 | continue |
| 450 | Plist = os.path.join(DirPath, F) |
| 451 | Data = plistlib.readPlist(Plist) |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 452 | PathPrefix = Dir |
| 453 | if (ProjectBuildMode == 1): |
| 454 | PathPrefix = os.path.join(Dir, PatchedSourceDirName) |
| 455 | Paths = [SourceFile[len(PathPrefix)+1:]\ |
| 456 | if SourceFile.startswith(PathPrefix)\ |
| 457 | else SourceFile for SourceFile in Data['files']] |
Gabor Horvath | c3177f2 | 2015-07-08 18:39:31 +0000 | [diff] [blame] | 458 | Data['files'] = Paths |
| 459 | plistlib.writePlist(Data, Plist) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 460 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 461 | finally: |
| 462 | PBuildLogFile.close() |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 463 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 464 | print "Build complete (time: %.2f). See the log for more details: %s" % \ |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 465 | ((time.time()-TBegin), BuildLogPath) |
| 466 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 467 | # A plist file is created for each call to the analyzer(each source file). |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 468 | # We are only interested on the once that have bug reports, so delete the rest. |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 469 | def CleanUpEmptyPlists(SBOutputDir): |
| 470 | for F in glob.glob(SBOutputDir + "/*/*.plist"): |
| 471 | P = os.path.join(SBOutputDir, F) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 472 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 473 | Data = plistlib.readPlist(P) |
| 474 | # Delete empty reports. |
| 475 | if not Data['files']: |
| 476 | os.remove(P) |
| 477 | continue |
| 478 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 479 | # Given the scan-build output directory, checks if the build failed |
| 480 | # (by searching for the failures directories). If there are failures, it |
| 481 | # creates a summary file in the output directory. |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 482 | def checkBuild(SBOutputDir): |
| 483 | # Check if there are failures. |
| 484 | Failures = glob.glob(SBOutputDir + "/*/failures/*.stderr.txt") |
| 485 | TotalFailed = len(Failures); |
| 486 | if TotalFailed == 0: |
Jordan Rose | 9858b12 | 2012-08-31 00:36:30 +0000 | [diff] [blame] | 487 | CleanUpEmptyPlists(SBOutputDir) |
| 488 | Plists = glob.glob(SBOutputDir + "/*/*.plist") |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 489 | print "Number of bug reports (non-empty plist files) produced: %d" %\ |
Jordan Rose | 9858b12 | 2012-08-31 00:36:30 +0000 | [diff] [blame] | 490 | len(Plists) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 491 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 492 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 493 | # Create summary file to display when the build fails. |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 494 | SummaryPath = os.path.join(SBOutputDir, LogFolderName, FailuresSummaryFileName) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 495 | if (Verbose > 0): |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 496 | print " Creating the failures summary file %s" % (SummaryPath,) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 497 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 498 | SummaryLog = open(SummaryPath, "w+") |
| 499 | try: |
| 500 | SummaryLog.write("Total of %d failures discovered.\n" % (TotalFailed,)) |
| 501 | if TotalFailed > NumOfFailuresInSummary: |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 502 | SummaryLog.write("See the first %d below.\n" |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 503 | % (NumOfFailuresInSummary,)) |
| 504 | # TODO: Add a line "See the results folder for more." |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 505 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 506 | FailuresCopied = NumOfFailuresInSummary |
| 507 | Idx = 0 |
Jordan Rose | dc191a1 | 2012-06-01 16:24:43 +0000 | [diff] [blame] | 508 | for FailLogPathI in Failures: |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 509 | if Idx >= NumOfFailuresInSummary: |
| 510 | break; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 511 | Idx += 1 |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 512 | SummaryLog.write("\n-- Error #%d -----------\n" % (Idx,)); |
| 513 | FailLogI = open(FailLogPathI, "r"); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 514 | try: |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 515 | shutil.copyfileobj(FailLogI, SummaryLog); |
| 516 | finally: |
| 517 | FailLogI.close() |
| 518 | finally: |
| 519 | SummaryLog.close() |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 520 | |
Anna Zaks | 5acd960 | 2012-01-04 23:53:50 +0000 | [diff] [blame] | 521 | print "Error: analysis failed. See ", SummaryPath |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 522 | sys.exit(-1) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 523 | |
| 524 | # Auxiliary object to discard stdout. |
| 525 | class Discarder(object): |
| 526 | def write(self, text): |
| 527 | pass # do nothing |
| 528 | |
| 529 | # Compare the warnings produced by scan-build. |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 530 | # Strictness defines the success criteria for the test: |
| 531 | # 0 - success if there are no crashes or analyzer failure. |
| 532 | # 1 - success if there are no difference in the number of reported bugs. |
| 533 | # 2 - success if all the bug reports are identical. |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 534 | def runCmpResults(Dir, Strictness = 0): |
| 535 | TBegin = time.time() |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 536 | |
| 537 | RefDir = os.path.join(Dir, SBOutputDirReferencePrefix + SBOutputDirName) |
| 538 | NewDir = os.path.join(Dir, SBOutputDirName) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 539 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 540 | # We have to go one level down the directory tree. |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 541 | RefList = glob.glob(RefDir + "/*") |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 542 | NewList = glob.glob(NewDir + "/*") |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 543 | |
Jordan Rose | c7b992e | 2013-06-10 19:34:30 +0000 | [diff] [blame] | 544 | # Log folders are also located in the results dir, so ignore them. |
| 545 | RefLogDir = os.path.join(RefDir, LogFolderName) |
| 546 | if RefLogDir in RefList: |
| 547 | RefList.remove(RefLogDir) |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 548 | NewList.remove(os.path.join(NewDir, LogFolderName)) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 549 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 550 | if len(RefList) == 0 or len(NewList) == 0: |
| 551 | return False |
| 552 | assert(len(RefList) == len(NewList)) |
| 553 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 554 | # There might be more then one folder underneath - one per each scan-build |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 555 | # command (Ex: one for configure and one for make). |
| 556 | if (len(RefList) > 1): |
| 557 | # Assume that the corresponding folders have the same names. |
| 558 | RefList.sort() |
| 559 | NewList.sort() |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 560 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 561 | # Iterate and find the differences. |
Anna Zaks | 767d356 | 2011-11-08 19:56:31 +0000 | [diff] [blame] | 562 | NumDiffs = 0 |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 563 | PairList = zip(RefList, NewList) |
| 564 | for P in PairList: |
| 565 | RefDir = P[0] |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 566 | NewDir = P[1] |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 567 | |
| 568 | assert(RefDir != NewDir) |
| 569 | if Verbose == 1: |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 570 | print " Comparing Results: %s %s" % (RefDir, NewDir) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 571 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 572 | DiffsPath = os.path.join(NewDir, DiffsSummaryFileName) |
Devin Coughlin | 2cb767d | 2015-11-07 18:27:35 +0000 | [diff] [blame] | 573 | PatchedSourceDirPath = os.path.join(Dir, PatchedSourceDirName) |
| 574 | Opts = CmpRuns.CmpOptions(DiffsPath, "", PatchedSourceDirPath) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 575 | # Discard everything coming out of stdout (CmpRun produces a lot of them). |
| 576 | OLD_STDOUT = sys.stdout |
| 577 | sys.stdout = Discarder() |
| 578 | # Scan the results, delete empty plist files. |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 579 | NumDiffs, ReportsInRef, ReportsInNew = \ |
| 580 | CmpRuns.dumpScanBuildResultsDiff(RefDir, NewDir, Opts, False) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 581 | sys.stdout = OLD_STDOUT |
Anna Zaks | 767d356 | 2011-11-08 19:56:31 +0000 | [diff] [blame] | 582 | if (NumDiffs > 0) : |
| 583 | print "Warning: %r differences in diagnostics. See %s" % \ |
| 584 | (NumDiffs, DiffsPath,) |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 585 | if Strictness >= 2 and NumDiffs > 0: |
| 586 | print "Error: Diffs found in strict mode (2)." |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 587 | sys.exit(-1) |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 588 | elif Strictness >= 1 and ReportsInRef != ReportsInNew: |
| 589 | print "Error: The number of results are different in strict mode (1)." |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 590 | sys.exit(-1) |
| 591 | |
| 592 | print "Diagnostic comparison complete (time: %.2f)." % (time.time()-TBegin) |
Anna Zaks | 767d356 | 2011-11-08 19:56:31 +0000 | [diff] [blame] | 593 | return (NumDiffs > 0) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 594 | |
Devin Coughlin | 9ea8033 | 2016-01-23 01:09:07 +0000 | [diff] [blame] | 595 | def cleanupReferenceResults(SBOutputDir): |
| 596 | # Delete html, css, and js files from reference results. These can |
| 597 | # include multiple copies of the benchmark source and so get very large. |
| 598 | Extensions = ["html", "css", "js"] |
| 599 | for E in Extensions: |
| 600 | for F in glob.glob("%s/*/*.%s" % (SBOutputDir, E)): |
| 601 | P = os.path.join(SBOutputDir, F) |
| 602 | RmCommand = "rm '%s'" % P |
| 603 | check_call(RmCommand, shell=True) |
| 604 | |
| 605 | # Remove the log file. It leaks absolute path names. |
| 606 | removeLogFile(SBOutputDir) |
| 607 | |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 608 | def updateSVN(Mode, ProjectsMap): |
| 609 | try: |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 610 | ProjectsMap.seek(0) |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 611 | for I in csv.reader(ProjectsMap): |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 612 | ProjName = I[0] |
Jordan Rose | 01ac572 | 2012-06-01 16:24:38 +0000 | [diff] [blame] | 613 | Path = os.path.join(ProjName, getSBOutputDirName(True)) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 614 | |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 615 | if Mode == "delete": |
Devin Coughlin | ab95cd2 | 2016-01-22 07:08:06 +0000 | [diff] [blame] | 616 | Command = "svn delete '%s'" % (Path,) |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 617 | else: |
Devin Coughlin | ab95cd2 | 2016-01-22 07:08:06 +0000 | [diff] [blame] | 618 | Command = "svn add '%s'" % (Path,) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 619 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 620 | if Verbose == 1: |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 621 | print " Executing: %s" % (Command,) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 622 | check_call(Command, shell=True) |
| 623 | |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 624 | if Mode == "delete": |
| 625 | CommitCommand = "svn commit -m \"[analyzer tests] Remove " \ |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 626 | "reference results.\"" |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 627 | else: |
| 628 | CommitCommand = "svn commit -m \"[analyzer tests] Add new " \ |
| 629 | "reference results.\"" |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 630 | if Verbose == 1: |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 631 | print " Executing: %s" % (CommitCommand,) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 632 | check_call(CommitCommand, shell=True) |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 633 | except: |
| 634 | print "Error: SVN update failed." |
| 635 | sys.exit(-1) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 636 | |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 637 | def testProject(ID, ProjectBuildMode, IsReferenceBuild=False, Dir=None, Strictness = 0): |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 638 | print " \n\n--- Building project %s" % (ID,) |
| 639 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 640 | TBegin = time.time() |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 641 | |
| 642 | if Dir is None : |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 643 | Dir = getProjectDir(ID) |
| 644 | if Verbose == 1: |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 645 | print " Build directory: %s." % (Dir,) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 646 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 647 | # Set the build results directory. |
Jordan Rose | 01ac572 | 2012-06-01 16:24:38 +0000 | [diff] [blame] | 648 | RelOutputDir = getSBOutputDirName(IsReferenceBuild) |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 649 | SBOutputDir = os.path.join(Dir, RelOutputDir) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 650 | |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 651 | buildProject(Dir, SBOutputDir, ProjectBuildMode, IsReferenceBuild) |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 652 | |
| 653 | checkBuild(SBOutputDir) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 654 | |
Jordan Rose | 9858b12 | 2012-08-31 00:36:30 +0000 | [diff] [blame] | 655 | if IsReferenceBuild == False: |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 656 | runCmpResults(Dir, Strictness) |
Devin Coughlin | 9ea8033 | 2016-01-23 01:09:07 +0000 | [diff] [blame] | 657 | else: |
| 658 | cleanupReferenceResults(SBOutputDir) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 659 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 660 | print "Completed tests for project %s (time: %.2f)." % \ |
| 661 | (ID, (time.time()-TBegin)) |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 662 | |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 663 | def testAll(IsReferenceBuild = False, UpdateSVN = False, Strictness = 0): |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 664 | PMapFile = open(getProjectMapPath(), "rb") |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 665 | try: |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 666 | # Validate the input. |
| 667 | for I in csv.reader(PMapFile): |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 668 | if (len(I) != 2) : |
| 669 | print "Error: Rows in the ProjectMapFile should have 3 entries." |
| 670 | raise Exception() |
Anna Zaks | a2f970b | 2012-09-06 23:30:27 +0000 | [diff] [blame] | 671 | if (not ((I[1] == "0") | (I[1] == "1") | (I[1] == "2"))): |
| 672 | print "Error: Second entry in the ProjectMapFile should be 0" \ |
| 673 | " (single file), 1 (project), or 2(single file c++11)." |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 674 | raise Exception() |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 675 | |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 676 | # When we are regenerating the reference results, we might need to |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 677 | # update svn. Remove reference results from SVN. |
| 678 | if UpdateSVN == True: |
Jordan Rose | 01ac572 | 2012-06-01 16:24:38 +0000 | [diff] [blame] | 679 | assert(IsReferenceBuild == True); |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 680 | updateSVN("delete", PMapFile); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 681 | |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 682 | # Test the projects. |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 683 | PMapFile.seek(0) |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 684 | for I in csv.reader(PMapFile): |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 685 | testProject(I[0], int(I[1]), IsReferenceBuild, None, Strictness) |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 686 | |
| 687 | # Add reference results to SVN. |
| 688 | if UpdateSVN == True: |
| 689 | updateSVN("add", PMapFile); |
| 690 | |
Anna Zaks | 4720a73 | 2011-11-05 05:20:48 +0000 | [diff] [blame] | 691 | except: |
| 692 | print "Error occurred. Premature termination." |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 693 | raise |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 694 | finally: |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 695 | PMapFile.close() |
| 696 | |
Anna Zaks | f0c4116 | 2011-10-06 23:26:27 +0000 | [diff] [blame] | 697 | if __name__ == '__main__': |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 698 | # Parse command line arguments. |
| 699 | Parser = argparse.ArgumentParser(description='Test the Clang Static Analyzer.') |
| 700 | Parser.add_argument('--strictness', dest='strictness', type=int, default=0, |
| 701 | help='0 to fail on runtime errors, 1 to fail when the number\ |
| 702 | of found bugs are different from the reference, 2 to \ |
| 703 | fail on any difference from the reference. Default is 0.') |
| 704 | Parser.add_argument('-r', dest='regenerate', action='store_true', default=False, |
| 705 | help='Regenerate reference output.') |
| 706 | Parser.add_argument('-rs', dest='update_reference', action='store_true', |
| 707 | default=False, help='Regenerate reference output and update svn.') |
| 708 | Args = Parser.parse_args() |
| 709 | |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 710 | IsReference = False |
| 711 | UpdateSVN = False |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 712 | Strictness = Args.strictness |
| 713 | if Args.regenerate: |
| 714 | IsReference = True |
| 715 | elif Args.update_reference: |
| 716 | IsReference = True |
| 717 | UpdateSVN = True |
Anna Zaks | 4c1ef976 | 2012-02-03 06:35:23 +0000 | [diff] [blame] | 718 | |
Gabor Horvath | 93fde94 | 2015-06-30 15:31:17 +0000 | [diff] [blame] | 719 | testAll(IsReference, UpdateSVN, Strictness) |