Skip to content

Commit 2b385a4

Browse files
author
Matthias Wittgen
committed
Add section on static C++ code analysis
1 parent 89330f5 commit 2b385a4

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

cpp/code-analysis.rst

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Overview of Code Style Guides & Policies
196196
cpp/eigen
197197
cpp/profiling
198198
cpp/compilation-db
199+
cpp/code-analysis
199200

200201
.. _part-cpp:
201202

@@ -211,6 +212,7 @@ C++
211212
- :doc:`cpp/eigen`
212213
- :doc:`cpp/profiling`
213214
- :doc:`cpp/compilation-db`
215+
- :doc:`cpp/code-analysis`
214216

215217
.. PYTHON SECTION ============================================================
216218

0 commit comments

Comments
 (0)