Menu

cppcheck versus a (static /dynamic) library

2015-07-06
2015-07-07
  • Lieven de Cock

    Lieven de Cock - 2015-07-06

    When for example one has a project which is a static library, there could for example be in the simplest approach be 1 header file and 1 implementation file.

    Again stripping a lot of stuff to have a pure reproduction.
    Put 2 files together : foo.h and foo.cpp

    foo.h

    #ifndef FOO_H
    #define FOO_H
    
    namespace Foo
    {
    
    bool remount();
    
    } // namespace Foo
    
    #endif // FOO_H
    

    foo.cpp

    #include "foo.h"
    
    namespace Foo
    {
    
    bool remount()
    {
        return true;
    }
    
    } // namespace Foo
    

    run cppcheck as : cppcheck --verbose --enable=all --enable=style --xml -I"." foo.*

    ===>

    <results> Checking foo.cpp... 1/2 files checked 47% done Checking foo.h... 2/2 files checked 100% done <error file="foo.cpp" line="6" id="unusedFunction" severity="style" msg="The function 'remount' is never used."/> </results>

    How can we kind of instruct cppcheck what are exported things, and as such should not be considered unused ? Having a local function in a cpp file not being used is interesting information one wants to know (note gcc 49 can spot those).

    I mentioned "export", because one can see 2 levels, declared and header and implemented in cpp ==> do not complain about unused, but in a library it is possible to have a local .h/.cpp pair which is implementation detail and not export out of the library, so in there an unused (declared + implemented) is again interesting information.

    I don't recall have this problem in the past (at least 1.5 year ago).

    For now I only see one solution that is suppressing this , but I should already also suppress "--suppress=missingIncludeSystem" because on linux this is also generating noise otherwise since it does not seem to find the C/C++ headers.

    Personally I have the feeling it is becoming a noise generator requiring a lot of suppresses ?

    Please advice.

     

    Last edit: Lieven de Cock 2015-07-06
  • Lieven de Cock

    Lieven de Cock - 2015-07-06

    the example above is a free method, but the same happens on the public classes of a (exported) class in case the implementation itself is not calling those member methods itself

    ==> very noisy.

     
  • Daniel Marjamäki

    the unusedFunction checker does not work for libraries. I recommend that you don't enable this at all. I have tried to inform about this in the help screen and in the manual ... but I think it can be documented better.

    I would personally recommend that you don't use --enable=all for analysis. The information messages are meant to be used for configuration, not analysis. I'd say --enable=style is better for analysis.

    I also ignore "missingIncludeSystem" most of the time. except if there is some particular 3rd party library I want to include.

    run cppcheck as : cppcheck --verbose --enable=all --enable=style --xml -I"." foo.*

    you should not check headers like this. you are telling cppcheck to create a translation unit based on foo.h and check it. you can get for instance false positives. since foo.h is included by foo.cpp, foo.h is analysed properly when foo.cpp is analysed.

     

Log in to post a comment.