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

Inclusion of ArduinoJSON causes cppcheck to not recognize a call to a function #3797

Closed
maxgerhardt opened this issue Jan 8, 2021 · 5 comments
Assignees
Milestone

Comments

@maxgerhardt
Copy link
Contributor

maxgerhardt commented Jan 8, 2021

What kind of issue is this?

  • [X ] PlatformIO Core.
    If you’ve found a bug, please provide an information below.

Configuration

Operating system: Win x64

PlatformIO Version (platformio --version): PlatformIO Core, version 5.0.5a1

Description of problem

Well, this is a weird one. See https://community.platformio.org/t/wrong-issues-from-cppcheck/18410.

At first, a project is created for an ESP32Dev board. Then, a display.h and display.cpp with two functions is added. Those two functions are called in main.cpp.

#include <Arduino.h>
#include "display.h"

// cppcheck-suppress unusedFunction
void setup() {
   setup_display();
}

// cppcheck-suppress unusedFunction
void loop() {
   show_display("Test");
}

Running the a check gives

>pio check --skip-packages
Checking esp32dev > cppcheck (platform: espressif32; board: esp32dev; framework: arduino)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
No defects found
============ [PASSED] Took 0.16 seconds ============

However, if one does a #include <ArduinoJson.h> at the top, one gets

>pio check --skip-packages
Checking esp32dev > cppcheck (platform: espressif32; board: esp32dev; framework: arduino)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
src\display.cpp:10: [low:style] The function 'setup_display' is never used. [unusedFunction]
src\display.cpp:20: [low:style] The function 'show_display' is never used. [unusedFunction]

Steps to Reproduce

  1. Clone reproduction project at https://github.com/maxgerhardt/pio-cppcheck-arduinojson
  2. Make sure it compiles: pio run
  3. Run pio check --skip-packages to see that there are no defects
  4. Modify the #include <ArduinoJson.h> in main.cpp
  5. Rerun pio check --skip-packages
  6. Observe wrong defects

Actual Results

Detected defects which are no defects since function is called in main.cpp

Expected Results

No defects are found. Inclusion of a library does not change the workings of the cppcheck tool.

If problems with PlatformIO Build System:

All files found in reproduction project

Additional info

>pio system info
--------------------------  --------------------------------------------------------------------------
PlatformIO Core             5.0.5a1
Python                      3.8.6-final.0
System Type                 windows_amd64
Platform                    Windows-10
File System Encoding        utf-8
Locale Encoding             cp1252
PlatformIO Core Directory   C:\Users\Max\.platformio
PlatformIO Core Executable  C:\Users\Max\AppData\Local\Programs\Python\Python38\Scripts\platformio.exe
Python Executable           c:\users\max\appdata\local\programs\python\python38\python.exe
Global Libraries            0
Development Platforms       14
Tools & Toolchains          99
--------------------------  --------------------------------------------------------------------------
@maxgerhardt
Copy link
Contributor Author

Addendum: When I manually overwrite the files in C:\Users\<user>\.platformio\packages\tool-cppcheck with the 2.1.0 version (except package.json and .piopm file), to force the usage of version 2.1.0 instead of the latest 2.3.0, it does not report a defect.

@gudnimg
Copy link

gudnimg commented Jan 9, 2021

I'm seeing the same issue on my end after following the reproduction steps. Strange 🤔 Isn't this a bug on cppcheck's end? Maybe would should mention this at https://sourceforge.net/p/cppcheck/discussion/

@maxgerhardt
Copy link
Contributor Author

The question is whether it's an error in the way PlatformIO calls cppcheck or whether it's a bug in cppcheck itself. I could e.g. imagine that, since ArduinoJSON has a lot of .ino example files which also implement setup() and loop() of course, that those are somehow mistaken as the project's setup() and loop() implementations, in which the display functions are not called of course. But that could of course be entirely not the case, too.

I'll oben a bugtracker ticket on https://trac.cppcheck.net/, since that's where it should end up.. The page says if "just" a forum post is opened it takes another person to actually create the ticket.

@maxgerhardt
Copy link
Contributor Author

I could further narrow it down to which part in ArduinoJson.h causes the defect: #include "ArduinoJson/Variant/VariantCompare.hpp". The reproduction project has been updated. A ticket was opened: https://trac.cppcheck.net/ticket/10103

@ivankravets ivankravets modified the milestones: 5.1.0, 5.1.1 Jan 27, 2021
@ivankravets ivankravets assigned ivankravets and valeros and unassigned ivankravets Mar 4, 2021
valeros added a commit that referenced this issue Mar 11, 2021
Cppcheck doesn't provide a proper report when one of the files in the check list is broken.
If we run the analysis on a per-file basis, then Cppcheck will be able report at least defects
from valid source files.
@valeros
Copy link
Member

valeros commented Mar 11, 2021

Hi @maxgerhardt ! Actually, the result when you include ArduinoJSON headers is the right one. Cppcheck is conifgured to analyze all source files in the src folder. Both files display.cpp and main.cpp contain unused functions and if we take into account suppressing two functions in the main.cpp file, then we should see two unusedFunction defects from display.cpp which is correctly reported in your second check with ArduinoJSON includes. But the problem here is that even if we remove the suppress comments in the main.cpp we still don't see the defects from this file and it seems like another issue with Cppcheck preprocessor. The situation is worsened in our case when sources are analyzed in bulk and Cppcheck doesn't report defects at all, even from other sources not affected by ArduinoJSON.

As you mentioned, Cppcheck v2.1 works better in this case, but downgrading the package to v2.1 could possibly introduce even more issues for other projects because v2.3 contains several fixes for their preprocessor. The best we can do here is to at least report defects from files independently from broken ones.

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

No branches or pull requests

4 participants