Menu

Possible new rule for duplicated #include

2016-05-19
2016-05-20
  • Dominique Pelle

    Dominique Pelle - 2016-05-19

    cpp source files sometimes contain duplicated includes by mistake. cppcheck could warn about that. In theory, it's not a problem as headers be protected against multiple inclusing, but multiple inclusion in in the same is probably 99.9% useless to include the same header file more than once.
    Headers that are included multiple times indirectly should of course not be reported as this is ubiquitous in c++, so I'm only talking about headers being included more than once in the same source file as in the example below:

    $ cat foo.cpp
    
    // Notice that the same header cstdio is included twice in foo.cpp, which is useless.
    #include <cstdio>
    #include <cstdio>
    
    in main()
    {
      printf("test\n");
    }
    
    $ g++ -Wall -Wextra foo.cpp
    (no warning, no error)
    
    $ cppcheck --version
    Cppcheck 1.74 dev
    
    $ cppcheck --inconclusive --enable=all foo.cpp
    Checking foo.cpp...
    [foo.cpp:1]: (information) Include file: <cstdio> not found. Please note: Cppcheck does not need standard library headers to get proper results.
    [foo.cpp:2]: (information) Include file: <cstdio> not found. Please note: Cppcheck does not need standard library headers to get proper results.
    

    There is no warning about the duplicated include <cstdio> in foo.cpp.
    I also wonder why cppcheck warns about not finding the standard header, but that's another topic.

     

    Last edit: Dominique Pelle 2016-05-19
  • Dominique Pelle

    Dominique Pelle - 2016-05-19

    In fact, cppcheck source files have a useless duplicated include in cppcheck/test/cfg/std.cpp:

    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <cctype>
    #include <complex>
    #include <cassert>
    #include <cwchar>
    #include <cfenv>
    #include <csetjmp>
    #include <cmath>
    #include <csignal>
    #include <csetjmp>
    #include <iostream>
    #include <iomanip>
    #include <cinttypes>

    Notice <csetjmp> being included twice (useless).

    If this warning is implemented, it should not warn about multiple inclusions in between different #ifdef as in cppcheck/cli/cppcheckexecutor.cpp for example, where <ucontext.h> is included twice which is fine:

    #if defined(APPLE)
    # define _XOPEN_SOURCE // ucontext.h APIs can only be used on Mac OSX >= 10.7 if _XOPEN_SOURCE is defined
    # include <ucontext.h>
    # undef _XOPEN_SOURCE
    #elif !defined(OpenBSD)
    # include <ucontext.h>
    #endif

     
  • Alexander Mai

    Alexander Mai - 2016-05-19

    cppcheck aims to detect errors. Including headers twice is nowhere close to an error (if there is the compiler will tell you).
    https://github.com/danmar/checkheaders is maybe closer to what you are looking for.

     
  • Daniel Marjamäki

    I agree with amai that checkheaders is basically better suited for this. The cppcheck source code is not well designed for preprocessor rules.

    But it sounds fine to implement this as an cppcheck addon.

    In fact, cppcheck source files have a useless duplicated include in cppcheck/test/cfg/std.cpp:

    interesting I doubt that was by intention

     
  • Dominique Pelle

    Dominique Pelle - 2016-05-20

    Alexander Mai wrote:

    Including headers twice is nowhere close to an error

    I agree that it's not an error as headers protect (generally) against multiple inclusion with include guards. However, including the same header is still useless in 99.9% of the time and a warning would be fair. The example of multiple includes happen even in cppcheck source code (cppcheck/test/cfg/std.cpp) and is clearly a mistake, even if it's harmless.

    Cppcheck does not only warn about errors, it also gives warnings, such as useless std::vector variables not being used, that gcc or clang do not warn about.

     
  • raynebc

    raynebc - 2016-05-20

    Declaring a variable but not using it is much more likely to lead to unintended behavior, ie. copy/paste mistakes. Including a header twice does not change the program's behavior, or even the compiler's behavior if the header was made correctly.

     

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.