Skip to content

Conversation

@rettichschnidi
Copy link
Contributor

@rettichschnidi rettichschnidi commented Nov 6, 2024

This allows to use cppcheck directly, without going trough CodeChecker.

Warning: As of 2025-09-17, both, Cppcheck Premium as well as the latest FOSS Git version has problems with parsing the Zephyr code base.

Cppcheck Premium 25.8.2:

$ west build --pristine --board sim3u1xx_dk zephyr/samples/hello_world/ -- -DZEPHYR_SCA_VARIANT=cppcheck -DCPPCHECK_EXE=~/opt/cppcheck-git/bin/cppcheck
-- west build: making build dir /home/reto/code/3rd-party/zephyrproject/build pristine
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /home/reto/code/3rd-party/zephyrproject/zephyr/samples/hello_world
-- CMake version: 3.31.6
-- Found Python3: /home/reto/tmp/venv/home/reto/code/3rd-party/zephyrproject/bin/python3 (found suitable version "3.13.5", minimum required is "3.10") found components: Interpreter
-- Cache files will be written to: /home/reto/.cache/zephyr
-- Zephyr version: 4.2.99 (/home/reto/code/3rd-party/zephyrproject/zephyr)
-- Found west (found suitable version "1.3.0", minimum required is "0.14.0")
-- Board: sim3u1xx_dk, qualifiers: sim3u167
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
-- Found host-tools: zephyr 0.17.2 (/home/reto/.local/opt/zephyr-sdk-0.17.2)
-- Found toolchain: zephyr 0.17.2 (/home/reto/.local/opt/zephyr-sdk-0.17.2)
-- Found Dtc: /home/reto/.local/opt/zephyr-sdk-0.17.2/sysroots/x86_64-pokysdk-linux/usr/bin/dtc (found suitable version "1.7.0", minimum required is "1.4.6")
-- Found BOARD.dts: /home/reto/code/3rd-party/zephyrproject/zephyr/boards/silabs/dev_kits/sim3u1xx_dk/sim3u1xx_dk.dts
-- Generated zephyr.dts: /home/reto/code/3rd-party/zephyrproject/build/zephyr/zephyr.dts
-- Generated pickled edt: /home/reto/code/3rd-party/zephyrproject/build/zephyr/edt.pickle
-- Generated devicetree_generated.h: /home/reto/code/3rd-party/zephyrproject/build/zephyr/include/generated/zephyr/devicetree_generated.h
Parsing /home/reto/code/3rd-party/zephyrproject/zephyr/Kconfig
Loaded configuration '/home/reto/code/3rd-party/zephyrproject/zephyr/boards/silabs/dev_kits/sim3u1xx_dk/sim3u1xx_dk_defconfig'
Merged configuration '/home/reto/code/3rd-party/zephyrproject/zephyr/samples/hello_world/prj.conf'
Configuration saved to '/home/reto/code/3rd-party/zephyrproject/build/zephyr/.config'
Kconfig header saved to '/home/reto/code/3rd-party/zephyrproject/build/zephyr/include/generated/zephyr/autoconf.h'
-- Found GnuLd: /home/reto/opt/zephyr-sdk-0.17.2/arm-zephyr-eabi/arm-zephyr-eabi/bin/ld.bfd (found version "2.38")
-- Found SCA: cppcheck (/home/reto/opt/cppcheck-git/bin/cppcheck)
-- Using the Open Source version of Cppcheck
-- Found SCA: cppcheck-htmlreport (/usr/local/bin/cppcheck-htmlreport)
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/reto/.local/opt/zephyr-sdk-0.17.2/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
-- Using ccache: /usr/bin/ccache
-- Found gen_kobject_list: /home/reto/code/3rd-party/zephyrproject/zephyr/scripts/build/gen_kobject_list.py
-- Configuring done (5.4s)
-- Generating done (0.2s)
-- Build files have been written to: /home/reto/code/3rd-party/zephyrproject/build
-- west build: building application
[1/128] Preparing syscall dependency handling

[3/128] Generating include/generated/zephyr/version.h
-- Zephyr version: 4.2.99 (/home/reto/code/3rd-party/zephyrproject/zephyr), build: v4.2.0-3673-gb4718ac55ae9
[126/128] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       12076 B       256 KB      4.61%
             RAM:        3944 B        32 KB     12.04%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/reto/code/3rd-party/zephyrproject/build/zephyr/zephyr.elf for board: sim3u1xx_dk
[128/128] Running utility command for cppcheck
Parsing xml report.
Creating /home/reto/code/3rd-party/zephyrproject/build/sca/cppcheck directory
Processing errors
  /home/reto/code/3rd-party/zephyrproject/modules/hal/cmsis_6/CMSIS/Core/Include/cmsis_gcc.h
  /home/reto/code/3rd-party/zephyrproject/zephyr/arch/common/init.c
  /home/reto/code/3rd-party/zephyrproject/zephyr/include/zephyr/sys/util.h
  /home/reto/code/3rd-party/zephyrproject/zephyr/lib/os/cbprintf_packaged.c
Creating index.html
Creating style.css file
Creating stats.html (statistics)


Open '/home/reto/code/3rd-party/zephyrproject/build/sca/cppcheck/index.html' to see the results.

Cppcheck FOSS :

$ west build --pristine --board sim3u1xx_dk zephyr/samples/hello_world/ -- -DZEPHYR_SCA_VARIANT=cppcheck -DCPPCHECK_EXE=/opt/cppcheckpremium/cppcheck
-- west build: making build dir /home/reto/code/3rd-party/zephyrproject/build pristine
-- west build: generating a build system
Loading Zephyr default modules (Zephyr base).
-- Application: /home/reto/code/3rd-party/zephyrproject/zephyr/samples/hello_world
-- CMake version: 3.31.6
-- Found Python3: /home/reto/tmp/venv/home/reto/code/3rd-party/zephyrproject/bin/python3 (found suitable version "3.13.5", minimum required is "3.10") found components: Interpreter
-- Cache files will be written to: /home/reto/.cache/zephyr
-- Zephyr version: 4.2.99 (/home/reto/code/3rd-party/zephyrproject/zephyr)
-- Found west (found suitable version "1.3.0", minimum required is "0.14.0")
-- Board: sim3u1xx_dk, qualifiers: sim3u167
-- ZEPHYR_TOOLCHAIN_VARIANT not set, trying to locate Zephyr SDK
-- Found host-tools: zephyr 0.17.2 (/home/reto/.local/opt/zephyr-sdk-0.17.2)
-- Found toolchain: zephyr 0.17.2 (/home/reto/.local/opt/zephyr-sdk-0.17.2)
-- Found Dtc: /home/reto/.local/opt/zephyr-sdk-0.17.2/sysroots/x86_64-pokysdk-linux/usr/bin/dtc (found suitable version "1.7.0", minimum required is "1.4.6")
-- Found BOARD.dts: /home/reto/code/3rd-party/zephyrproject/zephyr/boards/silabs/dev_kits/sim3u1xx_dk/sim3u1xx_dk.dts
-- Generated zephyr.dts: /home/reto/code/3rd-party/zephyrproject/build/zephyr/zephyr.dts
-- Generated pickled edt: /home/reto/code/3rd-party/zephyrproject/build/zephyr/edt.pickle
-- Generated devicetree_generated.h: /home/reto/code/3rd-party/zephyrproject/build/zephyr/include/generated/zephyr/devicetree_generated.h
Parsing /home/reto/code/3rd-party/zephyrproject/zephyr/Kconfig
Loaded configuration '/home/reto/code/3rd-party/zephyrproject/zephyr/boards/silabs/dev_kits/sim3u1xx_dk/sim3u1xx_dk_defconfig'
Merged configuration '/home/reto/code/3rd-party/zephyrproject/zephyr/samples/hello_world/prj.conf'
Configuration saved to '/home/reto/code/3rd-party/zephyrproject/build/zephyr/.config'
Kconfig header saved to '/home/reto/code/3rd-party/zephyrproject/build/zephyr/include/generated/zephyr/autoconf.h'
-- Found GnuLd: /home/reto/opt/zephyr-sdk-0.17.2/arm-zephyr-eabi/arm-zephyr-eabi/bin/ld.bfd (found version "2.38")
-- Found SCA: cppcheck (/opt/cppcheckpremium/cppcheck)
-- Using the Premium version of Cppcheck
-- Found SCA: cppcheck-htmlreport (/usr/local/bin/cppcheck-htmlreport)
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/reto/.local/opt/zephyr-sdk-0.17.2/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc
-- Using ccache: /usr/bin/ccache
-- Found gen_kobject_list: /home/reto/code/3rd-party/zephyrproject/zephyr/scripts/build/gen_kobject_list.py
-- Configuring done (5.5s)
-- Generating done (0.2s)
-- Build files have been written to: /home/reto/code/3rd-party/zephyrproject/build
-- west build: building application
[1/128] Preparing syscall dependency handling

[3/128] Generating include/generated/zephyr/version.h
-- Zephyr version: 4.2.99 (/home/reto/code/3rd-party/zephyrproject/zephyr), build: v4.2.0-3673-gb4718ac55ae9
[126/128] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       12076 B       256 KB      4.61%
             RAM:        3944 B        32 KB     12.04%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/reto/code/3rd-party/zephyrproject/build/zephyr/zephyr.elf for board: sim3u1xx_dk
[128/128] Running utility command for cppcheck
ERROR: Source file '/home/reto/code/3rd-party/zephyrproject/build/Cppcheck Premium' not found.
Parsing xml report.
Creating /home/reto/code/3rd-party/zephyrproject/build/sca/cppcheck directory
Processing errors
  /home/reto/code/3rd-party/zephyrproject/modules/hal/cmsis_6/CMSIS/Core/Include/cmsis_gcc.h
  /home/reto/code/3rd-party/zephyrproject/zephyr/arch/common/init.c
  /home/reto/code/3rd-party/zephyrproject/zephyr/include/zephyr/sys/util.h
Creating index.html
Creating style.css file
Creating stats.html (statistics)


Open '/home/reto/code/3rd-party/zephyrproject/build/sca/cppcheck/index.html' to see the results.

TODO:

  • Get cppcheck premium to not (always) exit with code 1
  • Test with newer (post 2.10) cppcheck (non premium) to see if zephyr.cfg is supported
  • Support all platforms, not just ARM 32-bit
  • Support variables set by Twister (similar to CodeChecker)
  • Export cppcheck command line to CMake (similar to CodeChecker)
  • Use parallelization properly (set -j <n>)
  • Fix asserts in include/zephyr/kernel/internal/mm.h
  • Fix true positive Zephyr issues found by Cppcheck

Background: I am doing this because I am currently evaluating Cppcheck Premium.

@rettichschnidi rettichschnidi force-pushed the gardena/rs/upstream/sca-cppcheck branch 2 times, most recently from a1554b5 to e8355ad Compare November 27, 2024 13:28
@zephyrbot
Copy link

zephyrbot commented Nov 27, 2024

The following west manifest projects have changed revision in this Pull Request:

Name Old Revision New Revision Diff

All manifest checks OK

Note: This message is automatically posted and updated by the Manifest GitHub Action.

@zephyrbot zephyrbot added manifest manifest-cmsis DNM This PR should not be merged (Do Not Merge) labels Nov 27, 2024
@rettichschnidi rettichschnidi force-pushed the gardena/rs/upstream/sca-cppcheck branch 2 times, most recently from 9db0694 to b917536 Compare December 3, 2024 22:11
@rettichschnidi rettichschnidi force-pushed the gardena/rs/upstream/sca-cppcheck branch 2 times, most recently from d703fb8 to c4e5050 Compare December 25, 2024 13:57
@rettichschnidi rettichschnidi force-pushed the gardena/rs/upstream/sca-cppcheck branch 2 times, most recently from 8b98c73 to de49dac Compare January 2, 2025 19:35
@rettichschnidi rettichschnidi force-pushed the gardena/rs/upstream/sca-cppcheck branch 2 times, most recently from d417452 to 1ac676d Compare January 7, 2025 16:14
@zephyrbot zephyrbot removed manifest manifest-cmsis DNM This PR should not be merged (Do Not Merge) labels Jan 7, 2025
@rettichschnidi rettichschnidi force-pushed the gardena/rs/upstream/sca-cppcheck branch from 1ac676d to 4d89828 Compare January 15, 2025 05:51
@rettichschnidi rettichschnidi force-pushed the gardena/rs/upstream/sca-cppcheck branch from 4d89828 to 228aa1d Compare January 28, 2025 02:28
@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@danmar
Copy link
Contributor

danmar commented Jun 17, 2025

@rettichschnidi Hi, there are some minor code fixes would it be possible to merge them separately into zephyr main branch soonish?

@danmar
Copy link
Contributor

danmar commented Jun 18, 2025

@rettichschnidi for your information we have a customer that wants to run Cppcheck on a Zephyr project and the analysis is aborted because of the preprocessor errors. :-(

@danmar
Copy link
Contributor

danmar commented Jun 24, 2025

@rettichschnidi I'll see if I have some luck to commit the UB fixes in the separate PR #92062

@rettichschnidi
Copy link
Contributor Author

@danmar Sorry for not replying any sooner and thanks a lot. Feel free to take over the work, as I'll probably not get to it before late summer/autumn.

@danmar
Copy link
Contributor

danmar commented Jun 28, 2025

@rettichschnidi no problem. my PR got merged so you can cleanup this PR.. :-)

@rettichschnidi rettichschnidi force-pushed the gardena/rs/upstream/sca-cppcheck branch from 228aa1d to 0393bdf Compare July 8, 2025 08:55
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jul 8, 2025

@github-actions
Copy link

github-actions bot commented Sep 7, 2025

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Sep 7, 2025
This commit adds cppcheck as a Static Analyser using Zephyr's SCA
framework.

By specifing -DZEPHYR_SCA_VARIANT=cppcheck to west build, the file
sca/cppcheck/index.html will be generated.

Signed-off-by: Reto Schneider <[email protected]>
@rettichschnidi rettichschnidi force-pushed the gardena/rs/upstream/sca-cppcheck branch from 0393bdf to b4718ac Compare September 15, 2025 22:29
@sonarqubecloud
Copy link

@github-actions github-actions bot removed the Stale label Sep 16, 2025
@rettichschnidi
Copy link
Contributor Author

@danmar I am back on track, would be great if we could get this in shape to be merged.

@rettichschnidi
Copy link
Contributor Author

The relevant issue in Cppcheck/simplecpp to be resolved is this: danmar/simplecpp#549

@github-actions
Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Nov 26, 2025
@danmar
Copy link
Contributor

danmar commented Nov 26, 2025

The relevant issue in Cppcheck/simplecpp to be resolved is this: danmar/simplecpp#549

It will not be solved in Cppcheck-2.19 :-(

Hope we can fix this in Cppcheck-2.20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants