Menu

cppcheck fails to parse boost header

2024-11-19
2024-11-20
  • Frank Winklmeier

    We came across a case where parsing a Boost header seems to completely hide valid defects.

    Consider the following test case:

    #include <boost/thread/tss.hpp>
    
    struct Test {
      Test() {}
      bool  b;
    };
    

    and this compile_commands.json:

    [{
      "directory": ".",
      "command": "g++ -I/usr/include -o Test.cxx.o -c Test.cxx",
      "file": "Test.cxx",
      "output": "Test.cxx.o"
    }]
    

    with the boost headers installed under /usr/include. Note that we are using -I instead of -isystem due to a bug in our build system.

    > cppcheck --version
    Cppcheck 2.17 dev
    > cppcheck --project=compile_commands.json --enable=warning --library=boost
    Checking Test.cxx ...
    /usr/include/boost/config/requires_threads.hpp:86:0: error: #error "Compiler threading support is not turned on.  Please consult your compiler's documentation for the appropriate options to use" [preprocessorErrorDirective]
    #  error "Compiler threading support is not turned on.  Please consult your compiler's documentation for the appropriate options to use"
    ^
    

    The #error is triggered by not having BOOST_HAS_THREADS defined but adding -DBOOST_HAS_THREADS to the compile command or directly on the command line has no effect.

    Now remove the #include <boost/thread/tss.hpp> and you will see the expected defect:

    Test.cxx:4:3: warning: Member variable 'Test::b' is not initialized in the constructor. [uninitMemberVar]
    

    In our case, the apparent parsing error was completely hidden because we usually suppress 3rd party libraries with --suppress="*:/usr/include/boost/*". In which case, the user does not see any error and assumes there are no defects.

     
  • CHR

    CHR - 2024-11-19

    Boost headers should not be visible to cppcheck. Please pass --library=boost instead.

     
  • Frank Winklmeier

    Could you give a few more details how this should be done in practice? We are running cppcheck as part of our cmake build. So we don't have control over which headers cppcheck gets to see.

     
    • Tal

      Tal - 2024-11-20

      The way cppcheck treat the "#include" pragma in preprocessing is as follows:
      1. If the header is found in the relative or the include paths with the "-I" flag, then cppcheck includes it
      2. Otherwise, it ignores this line and continues.

      The design of cppcheck as a textual checker, is that intentionally it doesn't want the user to specify the "-I" flag for the std and boost libraries, but rather use the "packaged library" flags, that is enabled by default to std, but is needed to be explicitly given for boost with the cppcheck flag --library=boost

       
  • CHR

    CHR - 2024-11-20

    Maybe -i/usr/include/boost can be added to the cppcheck command line? Haven't tested this though.

     

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.