Menu

Mixed C/C++ project analysis in bulk mode using --file-list

Andrew
2021-09-10
2021-09-11
  • Andrew

    Andrew - 2021-09-10

    Hi guys, I've looked through the entire forum for a hint, but didn't find anything similar to my question.

    I'm trying to configure Cppcheck to analyze a basic project that contains both C and C++ sources using the "--file-list" option. The problem here is that I need to pass a different set of flags/defines for C and C++ files (e.g. -std=c99 and -std=c++14). Obviously, I could invoke Cppcheck twice for C and C++ separately, but in this case I get a bunch of "unusedFunction" false positives.

    Any hints are welcome. Thanks in advance.

     
  • Daniel Marjamäki

    hmm.. can't you use both --std=c99 --std=c++14? As far as I remember the standard will be chosen depending on file extension. Maybe I am mistaken.

    In case you have not seen our --project option I suggest you take a look at that. If it's possible to import a compile database or your build configuration that might be better.

     
  • Andrew

    Andrew - 2021-09-11

    Hi Daniel, Thanks a lot for your tip! I mentioned "--std=c99" and "--std=c++14" just as an example. There are other cases like different defines for C and C++ that I need to pass to the Cppcheck preprocessor in order to allow it properly parse my codebase (for example -DMACRO=VALUE_FOR_CPP and -DMACRO=VALUE_FOR_C). As for the compiledb, unfortunately, that's not an option for me as I work on a deeply embedded project with archaic build system.

     
  • Daniel Marjamäki

    As for the compiledb

    you could try to generate the compile database yourself it's just a json file. I assume that something like this works:

    [
    {
      "directory": "checkpath",
      "command": "cppcheck  -DFOO -Ipath -std=c++14 -c file1.cpp",
      "file": ".../file1.cpp"
    },
    {
      "directory": "checkpath",
      "command": "cppcheck  -DBAR -Iotherpath -std=c99 -c file2.c",
      "file": ".../file2.c"
    }
    ]
    

    I am not 100% sure but I have the feeling that in the compile commands file you need to use -std with 1 dash.

    There are also tools to generate it automatically during compilation. You run "make" or whatever and then those tools will listen on compiler commands and create the compile database. I have used this: https://github.com/rizsotto/Bear and it worked well for me.. but I think that only worked in Linux. There are other tools also.

     
  • Andrew

    Andrew - 2021-09-11

    Thanks again! As I mentioned earlier, I'm working on a project for an embedded target and this project is meant to be crosscompiled (via GCC for ARM and GCC for RISC-V) which means I need to pass to Cppcheck target-specific flags as well (e.g. -D__arm__, -D__thumb__ (and many more) that GCC implicitly adds when compiling for specific targets). Without these flags Cppcheck won't be able to properly parse my codebase (as well as third party sources like CMSIS which detects used compiler.)

     

    Last edit: Andrew 2021-09-11

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.