andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 1 | # The Clang Static Analyzer |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 2 | |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 3 | See the [official clang static analyzer page](http://clang-analyzer.llvm.org/) |
| 4 | for background. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 5 | |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 6 | We don't run this regularly (because the analyzer's |
| 7 | [support for C++ isn't great yet](http://clang-analyzer.llvm.org/dev_cxx.html)), |
| 8 | so everything on this page is likely broken. The last time I checked, the |
| 9 | analyzer reported mostly uninteresting things. This assumes you're |
| 10 | [building chromium with clang](clang.md). |
| 11 | |
| 12 | You need an llvm checkout to get `scan-build` and `scan-view`; the easiest way |
| 13 | to get that is to run |
| 14 | |
| 15 | ```shell |
Nico Weber | e250e6a | 2015-12-02 19:31:56 | [diff] [blame] | 16 | tools/clang/scripts/update.py --force-local-build --without-android |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 17 | ``` |
| 18 | |
| 19 | ## With make |
| 20 | |
| 21 | To build base, if you use the make build: |
| 22 | |
| 23 | ``` |
| 24 | builddir_name=out_analyze \ |
| 25 | PATH=$PWD/third_party/llvm-build/Release+Asserts/bin:$PATH \ |
| 26 | third_party/llvm/tools/clang/tools/scan-build/scan-build \ |
| 27 | --keep-going --use-cc clang --use-c++ clang++ \ |
| 28 | make -j8 base |
| 29 | ``` |
| 30 | |
| 31 | (`builddir_name` is set to force a clobber build.) |
| 32 | |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 33 | Once that's done, run `third_party/llvm/tools/clang/tools/scan-view/scan-view` |
| 34 | to see the results; pass in the pass that `scan-build` outputs. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 35 | |
| 36 | ## With ninja |
| 37 | |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 38 | scan-build does its stuff by mucking with $CC/$CXX, which ninja ignores. gyp |
| 39 | does look at $CC/$CXX however, so you need to first run gyp\_chromium under |
| 40 | scan-build: |
| 41 | |
| 42 | ```shell |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 43 | time GYP_GENERATORS=ninja \ |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 44 | GYP_DEFINES='component=shared_library clang_use_chrome_plugins=0 \ |
| 45 | mac_strip_release=0 dcheck_always_on=1' \ |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 46 | third_party/llvm/tools/clang/tools/scan-build/scan-build \ |
| 47 | --use-analyzer $PWD/third_party/llvm-build/Release+Asserts/bin/clang \ |
| 48 | build/gyp_chromium -Goutput_dir=out_analyze |
| 49 | ``` |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 50 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 51 | You then need to run the build under scan-build too, to get a HTML report: |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 52 | |
| 53 | ```shell |
| 54 | time third_party/llvm/tools/clang/tools/scan-build/scan-build \ |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 55 | --use-analyzer $PWD/third_party/llvm-build/Release+Asserts/bin/clang \ |
| 56 | ninja -C out_analyze/Release/ base |
| 57 | ``` |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 58 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 59 | Then run `scan-view` as described above. |
| 60 | |
| 61 | ## Known False Positives |
| 62 | |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 63 | * http://llvm.org/bugs/show_bug.cgi?id=11425 |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 64 | |
| 65 | ## Stuff found by the static analyzer |
| 66 | |
andybons | 222c4ee | 2015-08-25 16:51:03 | [diff] [blame] | 67 | * https://code.google.com/p/skia/issues/detail?id=399 |
| 68 | * https://code.google.com/p/skia/issues/detail?id=400 |
| 69 | * https://codereview.chromium.org/8308008/ |
| 70 | * https://codereview.chromium.org/8313008/ |
| 71 | * https://codereview.chromium.org/8308009/ |
| 72 | * https://codereview.chromium.org/10031018/ |
| 73 | * https://codereview.chromium.org/12390058/ |