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