Search code examples
c++gccg++static-analysissoftware-quality

gcc static analyzer (Weffc++): exclude directories


I have a big project that uses many, many libraries. Some of them are HDF5, PugiXML, Boost.ASIO, Qt, MuParser, and many others. Some of these libraries are included by header, and some are pre-compiled, and some of them I compiled myself. I'd like to use the gcc option -Weffc++ to ensure the quality of my code.

The problem is that I got over 2000 warnings when I enabled that option, and when I went quickly through the list, there was like 1 of them related to my project, and almost everything else was from the headers I included! It's very not practical to go through the whole list. Is there a way to tell gcc to either analyze files from given directories, or exclude directies in some way (regex maybe?).


Solution

  • The comments in the documentation are not good news:

    When selecting this option, be aware that the standard library headers do not obey all of these guidelines; use ‘grep -v’ to filter out those warnings.

    You can disable the warning temporarily with pragmas:

    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Weffc++"
    #include <header1>
    #include <header2>
    #pragma GCC diagnostic pop
    

    You might also be able to use -isystem to specify include paths, which suppresses certain warnings from files included through those paths.

    Recommendation

    Just don't use -Weffc++. It gives garbage even in your own project. For example, it will ask you to define virtual destructors for all base classes, regardless of whether that makes even a small amount of sense. It will warn if you don't initialize every member in aggregate initialization. After throwing my hands in the air and saying, "That warning is just stupid!" for the hundredth time, I turned -Weffc++ off and moved on with my life.

    There are much better tools out there for code quality and enforcing style. Clang tools like clang-tidy, clang-modernize, and clang-format come to minde.