Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-29996: Add section on static C++ code analysis #531

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions cpp/code-analysis.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
###########################
Static Analysis of C++ code
###########################

`CodeChecker <https://codechecker.readthedocs.io>`_,
`clang static analyzer <https://clang-analyzer.llvm.org>`_ and
`clang-tidy <https://clang.llvm.org/extra/clang-tidy>`_ can be
used to detect potential C++ problems enforce C++ coding standards.

==============================
Install tools from conda-forge
==============================

``CodeChecker`` depends on ``clang-tools`` and the clang compiler for static analysis.
To install the compilers and tools into an LSST Science Pipelines conda stack for Linux x86_64

.. code-block:: bash

mamba install -c conda-forge clang clangxx clang-tools

To install ``CodeChecker``

.. code-block:: bash

mamba install -c CodeChecker

================================
Running ``clang-tidy`` on builds
================================

``CodeChecker`` intercepts calls to the compiler executables, builds a compilation database and runs
analyses on the source code.
To run ``CodeChecker`` in an lsstsw build environment

.. code-block:: bash

C=gcc CXX=g++ CC_BIN=CodeChecker CodeChecker check -j4 --build "rebuild lsst_distrib" --analyzers clang-tidy -o results

``--analyzers clang-tidy`` limits the analysis to ``clang-tidy`` checks and disables ``clang static analysis``, which
is very CPU intensive.

The build command can be replaced as needed. For example, to run on ``scons`` builds

.. code-block:: bash

C=gcc CXX=g++ CC_BIN=CodeChecker CodeChecker check -j4 --build "scons -j4 --analyzers clang-tidy" -o results

The standard output format of ``CodeChecker`` is ``plist``.
To produce a html report of the analysis results

.. code-block:: bash

CodeChecker parse --trim-path-prefix $(pwd) -e html results -o results/html

Adding ``--trim-path-prefix`` avoids absolute references in the html output.
The result can be browsed with

.. code-block:: bash

firefox html/index.html
2 changes: 2 additions & 0 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Overview of Code Style Guides & Policies
cpp/eigen
cpp/profiling
cpp/compilation-db
cpp/code-analysis

.. _part-cpp:

Expand All @@ -211,6 +212,7 @@ C++
- :doc:`cpp/eigen`
- :doc:`cpp/profiling`
- :doc:`cpp/compilation-db`
- :doc:`cpp/code-analysis`

.. PYTHON SECTION ============================================================

Expand Down