1.. _gcc:
2
3GCC static analysis support
4###########################
5
6Static analysis was introduced in `GCC <https://gcc.gnu.org/>`__ 10 and it is enabled
7with the option ``-fanalyzer``. This option performs a much more expensive and thorough
8analysis of the code than traditional warnings.
9
10Run GCC static analysis
11***********************
12
13To run GCC static analysis, :ref:`west build <west-building>` should be
14called with a ``-DZEPHYR_SCA_VARIANT=gcc`` parameter, e.g.
15
16.. zephyr-app-commands::
17   :zephyr-app: samples/userspace/hello_world_user
18   :board: qemu_x86
19   :gen-args: -DZEPHYR_SCA_VARIANT=gcc
20   :goals: build
21   :compact:
22
23Configuring GCC static analyzer
24*******************************
25
26GCC static analyzer can be controlled using specific options.
27
28* `Options controlling the
29  analyzer <https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html>`__
30* `Options controlling the diagnostic message
31  formatting <https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Message-Formatting-Options.html>`__
32
33.. list-table::
34   :header-rows: 1
35
36   * - Parameter
37     - Description
38   * - ``GCC_SCA_OPTS``
39     - A semicolon separated list of GCC analyzer options.
40
41These parameters can be passed on the command line, or be set as environment variables.
42
43.. zephyr-app-commands::
44   :zephyr-app: samples/hello_world
45   :board: stm32h573i_dk
46   :gen-args: -DZEPHYR_SCA_VARIANT=gcc -DGCC_SCA_OPTS="-fdiagnostics-format=json;-fanalyzer-verbosity=3"
47   :goals: build
48   :compact:
49
50.. note::
51
52   GCC static analyzer is under active development, and each new release comes with new options.
53   This `page <https://gcc.gnu.org/wiki/StaticAnalyzer>`__ gives an overview of options and fix
54   introduced with each new release of the analyzer.
55
56
57Latest version of the analyzer
58******************************
59
60Since the Zephyr toolchain may not include the most recent version of the GCC static analyzer,
61the GCC static analysis can be run with a more recent `GNU Arm embedded toolchain
62<https://docs.zephyrproject.org/latest/develop/toolchains/gnu_arm_embedded.html>`__
63to take advantage of the latest analyzer version.
64
65.. zephyr-app-commands::
66   :zephyr-app: samples/hello_world
67   :board: stm32h573i_dk
68   :gen-args: -DZEPHYR_SCA_VARIANT=gcc -DZEPHYR_TOOLCHAIN_VARIANT=gnuarmemb -DGNUARMEMB_TOOLCHAIN_PATH=...
69   :goals: build
70   :compact:
71