platformio / platformio-core

Your Gateway to Embedded Software Development Excellence :alien:
https://platformio.org
Apache License 2.0
7.93k stars 792 forks source link

Allow to change Include paths for cppcheck #4398

Open dzid26 opened 2 years ago

dzid26 commented 2 years ago

Configuration

Operating system:

PlatformIO Version (platformio --version): PlatformIO Core, version 6.1.4

Description of problem

Lack of ability to exclude Includes from cppcheck analysis. Long analysis and a long output. Related conversation.

Steps to Reproduce

  1. Create project which includes hal headers from within the repository.
  2. Setup cppcheck with misra check:
  3. Perform static analysis platformio.exe check

Actual Results

Analysis that is slow and outputs thousands of MISRA violations

Component     HIGH    MEDIUM    LOW
-----------  ------  --------  -----
src            0        0        4
src\CMSIS      0        0      1173
src\Device     0        0      8279

Total          0        0      9456

Environment         Tool      Status    Duration
------------------  --------  --------  ------------
genericSTM32F103C8  cppcheck  PASSED    00:00:19.038

Expected Results

A quick analysis of user code only without entering hal headers

Component     HIGH    MEDIUM    LOW
-----------  ------  --------  -----
src            0        0        4

Total          0        0      4

Environment         Tool      Status    Duration
------------------  --------  --------  ------------
genericSTM32F103C8  cppcheck  PASSED    00:00:1.234

If problems with PlatformIO Build System:

The content of platformio.ini:

[env:genericSTM32F103C8]
platform = ststm32
board = genericSTM32F103C8
board_build.ldscript = src/Device/STM32F103XB_FLASH.ld
build_flags =
    -Isrc/CMSIS/
    -Isrc/Device

check_skip_packages = yes ;don't inlcude compiler package inludes
check_patterns = 
  src/*.c
  src/*.h
check_flags =
  cppcheck:--addon=misra --suppress=suppress.txt

Source file to reproduce issue: Working example: https://github.com/dzid26/pio-exclude-cppcheck.

valeros commented 2 years ago

Hi @dzid26 ! As you may know Cppcheck analyzes your code in terms of translation units. Hence, Cppcheck needs to expand all specified header files (including third-party libraries/frameworks/HALs/etc.). That's the main reason why the analysis may be slow, but on the other hand, you receive the most accurate results. If the accuracy is not an issue, then the --suppress option would be the best workaround at the moment.

dzid26 commented 2 years ago

Understood. Also, I imagine that for projects using Platformio's frameworks, checking framework headers could be avoided using check_skip_packages = yes?

Regarding the headers expansion though, cppcheck manual discourages including system headers: image

valeros commented 2 years ago

Also, I imagine that for projects using Platformio's frameworks, checking framework headers could be avoided using check_skip_packages = yes?

That's right. This feature was added as a fall-back option mainly because Cppcheck's preprocessor is simply not capable of parsing some of currently available embedded frameworks.

Regarding the headers expansion though, cppcheck manual discourages including system headers:

PlatformIO supports several static analysis tools and even though Cppcheck may discourage specifying all headers, other tools may rely on complete translation units. Besides, in PlatformIO scope, only the platform packages (toolchains, frameworks, etc.) are considered system headers. That's where check_skip_packages can be used to avoid these headers. IMO, once you put a low-level framework inside your project, the headers from that frameworks shouldn't be considered as system ones.