1e5dd7070Spatrickscan-build 2e5dd7070Spatrick========== 3e5dd7070Spatrick 4e5dd7070SpatrickA package designed to wrap a build so that all calls to gcc/clang are 5e5dd7070Spatrickintercepted and logged into a [compilation database][1] and/or piped to 6e5dd7070Spatrickthe clang static analyzer. Includes intercept-build tool, which logs 7e5dd7070Spatrickthe build, as well as scan-build tool, which logs the build and runs 8e5dd7070Spatrickthe clang static analyzer on it. 9e5dd7070Spatrick 10e5dd7070SpatrickPortability 11e5dd7070Spatrick----------- 12e5dd7070Spatrick 13e5dd7070SpatrickShould be working on UNIX operating systems. 14e5dd7070Spatrick 15e5dd7070Spatrick- It has been tested on FreeBSD, GNU/Linux and OS X. 16e5dd7070Spatrick- Prepared to work on windows, but need help to make it. 17e5dd7070Spatrick 18e5dd7070Spatrick 19e5dd7070SpatrickPrerequisites 20e5dd7070Spatrick------------- 21e5dd7070Spatrick 22*a9ac8606Spatrick1. **python** interpreter (version 3.6 or later). 23e5dd7070Spatrick 24e5dd7070Spatrick 25e5dd7070SpatrickHow to use 26e5dd7070Spatrick---------- 27e5dd7070Spatrick 28e5dd7070SpatrickTo run the Clang static analyzer against a project goes like this: 29e5dd7070Spatrick 30e5dd7070Spatrick $ scan-build <your build command> 31e5dd7070Spatrick 32e5dd7070SpatrickTo generate a compilation database file goes like this: 33e5dd7070Spatrick 34e5dd7070Spatrick $ intercept-build <your build command> 35e5dd7070Spatrick 36e5dd7070SpatrickTo run the Clang static analyzer against a project with compilation database 37e5dd7070Spatrickgoes like this: 38e5dd7070Spatrick 39e5dd7070Spatrick $ analyze-build 40e5dd7070Spatrick 41e5dd7070SpatrickUse `--help` to know more about the commands. 42e5dd7070Spatrick 43e5dd7070Spatrick 44e5dd7070SpatrickHow to use the experimental Cross Translation Unit analysis 45e5dd7070Spatrick----------------------------------------------------------- 46e5dd7070Spatrick 47e5dd7070SpatrickTo run the CTU analysis, a compilation database file has to be created: 48e5dd7070Spatrick 49e5dd7070Spatrick $ intercept-build <your build command> 50e5dd7070Spatrick 51e5dd7070SpatrickTo run the Clang Static Analyzer against a compilation database 52e5dd7070Spatrickwith CTU analysis enabled, execute: 53e5dd7070Spatrick 54e5dd7070Spatrick $ analyze-build --ctu 55e5dd7070Spatrick 56e5dd7070SpatrickFor CTU analysis an additional (external definition) collection-phase is required. 57e5dd7070SpatrickFor debugging purposes, it is possible to separately execute the collection 58e5dd7070Spatrickand the analysis phase. By doing this, the intermediate files used for 59e5dd7070Spatrickthe analysis are kept on the disk in `./ctu-dir`. 60e5dd7070Spatrick 61e5dd7070Spatrick # Collect and store the data required by the CTU analysis 62e5dd7070Spatrick $ analyze-build --ctu-collect-only 63e5dd7070Spatrick 64e5dd7070Spatrick # Analyze using the previously collected data 65e5dd7070Spatrick $ analyze-build --ctu-analyze-only 66e5dd7070Spatrick 67e5dd7070SpatrickUse `--help` to get more information about the commands. 68e5dd7070Spatrick 69e5dd7070Spatrick 70e5dd7070SpatrickLimitations 71e5dd7070Spatrick----------- 72e5dd7070Spatrick 73e5dd7070SpatrickGenerally speaking, the `intercept-build` and `analyze-build` tools together 74e5dd7070Spatrickdoes the same job as `scan-build` does. So, you can expect the same output 75e5dd7070Spatrickfrom this line as simple `scan-build` would do: 76e5dd7070Spatrick 77e5dd7070Spatrick $ intercept-build <your build command> && analyze-build 78e5dd7070Spatrick 79e5dd7070SpatrickThe major difference is how and when the analyzer is run. The `scan-build` 80e5dd7070Spatricktool has three distinct model to run the analyzer: 81e5dd7070Spatrick 82e5dd7070Spatrick1. Use compiler wrappers to make actions. 83e5dd7070Spatrick The compiler wrappers does run the real compiler and the analyzer. 84e5dd7070Spatrick This is the default behaviour, can be enforced with `--override-compiler` 85e5dd7070Spatrick flag. 86e5dd7070Spatrick 87e5dd7070Spatrick2. Use special library to intercept compiler calls during the build process. 88e5dd7070Spatrick The analyzer run against each modules after the build finished. 89e5dd7070Spatrick Use `--intercept-first` flag to get this model. 90e5dd7070Spatrick 91e5dd7070Spatrick3. Use compiler wrappers to intercept compiler calls during the build process. 92e5dd7070Spatrick The analyzer run against each modules after the build finished. 93e5dd7070Spatrick Use `--intercept-first` and `--override-compiler` flags together to get 94e5dd7070Spatrick this model. 95e5dd7070Spatrick 96e5dd7070SpatrickThe 1. and 3. are using compiler wrappers, which works only if the build 97e5dd7070Spatrickprocess respects the `CC` and `CXX` environment variables. (Some build 98e5dd7070Spatrickprocess can override these variable as command line parameter only. This case 99e5dd7070Spatrickyou need to pass the compiler wrappers manually. eg.: `intercept-build 100e5dd7070Spatrick--override-compiler make CC=intercept-cc CXX=intercept-c++ all` where the 101e5dd7070Spatrickoriginal build command would have been `make all` only.) 102e5dd7070Spatrick 103e5dd7070SpatrickThe 1. runs the analyzer right after the real compilation. So, if the build 104e5dd7070Spatrickprocess removes removes intermediate modules (generated sources) the analyzer 105e5dd7070Spatrickoutput still kept. 106e5dd7070Spatrick 107e5dd7070SpatrickThe 2. and 3. generate the compilation database first, and filters out those 108e5dd7070Spatrickmodules which are not exists. So, it's suitable for incremental analysis during 109e5dd7070Spatrickthe development. 110e5dd7070Spatrick 111e5dd7070SpatrickThe 2. mode is available only on FreeBSD and Linux. Where library preload 112e5dd7070Spatrickis available from the dynamic loader. Not supported on OS X (unless System 113e5dd7070SpatrickIntegrity Protection feature is turned off). 114e5dd7070Spatrick 115e5dd7070Spatrick`intercept-build` command uses only the 2. and 3. mode to generate the 116e5dd7070Spatrickcompilation database. `analyze-build` does only run the analyzer against the 117e5dd7070Spatrickcaptured compiler calls. 118e5dd7070Spatrick 119e5dd7070Spatrick 120e5dd7070SpatrickKnown problems 121e5dd7070Spatrick-------------- 122e5dd7070Spatrick 123e5dd7070SpatrickBecause it uses `LD_PRELOAD` or `DYLD_INSERT_LIBRARIES` environment variables, 124e5dd7070Spatrickit does not append to it, but overrides it. So builds which are using these 125e5dd7070Spatrickvariables might not work. (I don't know any build tool which does that, but 126e5dd7070Spatrickplease let me know if you do.) 127e5dd7070Spatrick 128e5dd7070Spatrick 129e5dd7070SpatrickProblem reports 130e5dd7070Spatrick--------------- 131e5dd7070Spatrick 132e5dd7070SpatrickIf you find a bug in this documentation or elsewhere in the program or would 133e5dd7070Spatricklike to propose an improvement, please use the project's [issue tracker][3]. 134e5dd7070SpatrickPlease describing the bug and where you found it. If you have a suggestion 135e5dd7070Spatrickhow to fix it, include that as well. Patches are also welcome. 136e5dd7070Spatrick 137e5dd7070Spatrick 138e5dd7070SpatrickLicense 139e5dd7070Spatrick------- 140e5dd7070Spatrick 141e5dd7070SpatrickThe project is licensed under Apache-2.0 with LLVM exceptions. 142e5dd7070SpatrickSee LICENSE.TXT for details. 143e5dd7070Spatrick 144e5dd7070Spatrick [1]: http://clang.llvm.org/docs/JSONCompilationDatabase.html 145e5dd7070Spatrick [2]: https://pypi.python.org/pypi/scan-build 146e5dd7070Spatrick [3]: https://llvm.org/bugs/enter_bug.cgi?product=clang 147