|
| 1 | +########################### |
| 2 | +Static Analysis of C++ code |
| 3 | +########################### |
| 4 | + |
| 5 | +`CodeChecker <https://codechecker.readthedocs.io>`_, |
| 6 | +`clang static analyzer <https://clang-analyzer.llvm.org>`_ and |
| 7 | +`clang-tidy documentation <https://clang.llvm.org/extra/clang-tidy>`_ can be |
| 8 | +used to detect potential C++ problems enforce C++ coding standards. |
| 9 | + |
| 10 | +============================== |
| 11 | +Install tools from conda-forge |
| 12 | +============================== |
| 13 | + |
| 14 | +``CodeChecker`` depends on ``clang-tools`` and the clang compilers for static analysis. |
| 15 | + |
| 16 | +To install the compilers and tools into an LSST Science Pipelines conda stack for Linux x86_64 |
| 17 | +.. code-block:: bash |
| 18 | +
|
| 19 | + mamba install -c conda-forge clang clangxx clang-tools |
| 20 | +
|
| 21 | +To install ``CodeChecker`` |
| 22 | + |
| 23 | +.. code-block:: bash |
| 24 | +
|
| 25 | + mamba install -c CodeChecker |
| 26 | +
|
| 27 | +================================ |
| 28 | +Running ``clang-tidy`` on builds |
| 29 | +================================ |
| 30 | + |
| 31 | +``CodeChecker`` intercepts calls to the compiler executables, builds a compilation database and runs |
| 32 | +analyses on the source code. |
| 33 | + |
| 34 | +To run ``CodeChecker`` in an lsstsw build environment |
| 35 | +.. code-block:: bash |
| 36 | +
|
| 37 | + C=gcc CXX=g++ CC_BIN=CodeChecker CodeChecker check -j4 --build "rebuild lsst_distrib" --analyzers clang-tidy -o results |
| 38 | +
|
| 39 | +``--analyzers clang-tidy`` limits the analysis to ``clang-tidy`` checks and disables ``clang`` static analysis, which |
| 40 | +is very CPU intensive. |
| 41 | +The build command can be replaced as needed. For example, to run on ``scons`` builds |
| 42 | + |
| 43 | +.. code-block:: bash |
| 44 | +
|
| 45 | + C=gcc CXX=g++ CC_BIN=CodeChecker CodeChecker check -j4 --build "scons -j4 --analyzers clang-tidy" -o results |
| 46 | +
|
| 47 | +The standard output format of ``CodeChecker`` is ``plist``. |
| 48 | +To produce a html report of the analysis results |
| 49 | + |
| 50 | +.. code-block:: bash |
| 51 | +
|
| 52 | + CodeChecker parse --trim-path-prefix $(pwd) -e html results -o results/html |
| 53 | +
|
| 54 | +Adding ``--trim-path-prefix`` avoids absolute references in the html output. |
| 55 | +The result can be browsed with |
| 56 | +.. code-block:: bash |
| 57 | +
|
| 58 | + firefox html/index.html |
0 commit comments