Search code examples
c++linuxjenkinscppcheck

cppcheck finding "redundant code: found a statement that begins with numeric constant" for macro


We're trying to do static analysis using cppcheck for our code base for linux using the jenkins plugin. For some reason, it's finding numerous errors for the following type of thing:

CREATE_DERIVED_EXCEPTION_CLASS(ExceptionOSApiError, 5)

and also for:

CREATE_EXCEPTION_CLASS_DERIVED_FROM_SPECIFIC_BASE(ExceptionFileApiError, ExceptionOSApiError, 6)

Where it's defined as (but not giving an error on the lines):

#define CREATE_DERIVED_EXCEPTION_CLASS( new_exception_name, unique_numeric_code ) \
    CREATE_EXCEPTION_CLASS_DERIVED_FROM_SPECIFIC_BASE( new_exception_name, Exception, unique_numeric_code )

#ifdef _IN_EXCEPTION_CPP

    #define CREATE_EXCEPTION_CLASS_DERIVED_FROM_SPECIFIC_BASE( new_exception_name, base_exception_name, unique_numeric_code ) \
        new_exception_name::new_exception_name( const char *message, LASTERROR_TYPE lastError, const IDebugContext& debugContextWhereThrown ) \
            : base_exception_name( message, lastError, debugContextWhereThrown ) {} \
        new_exception_name::new_exception_name( std::string&& message, LASTERROR_TYPE lastError, const IDebugContext& debugContextWhereThrown ) \
            : base_exception_name( std::move(message), lastError, debugContextWhereThrown ) {} \
        new_exception_name::new_exception_name( LASTERROR_TYPE lastError, const IDebugContext& debugContextWhereThrown ) \
            : base_exception_name( lastError, debugContextWhereThrown ) {} \
        new_exception_name::new_exception_name(new_exception_name&& source) \
            : base_exception_name(std::move(source)) {} \
        new_exception_name& new_exception_name::operator=(new_exception_name&& rightHandSide) \
            { \
                if (this != &rightHandSide) \
                { \
                    base_exception_name::operator=(std::move(rightHandSide)); \
                    /* No derived class data members to move */ \
                } \
                return(*this); \
            } \
        UTILDLL_EXPORT int new_exception_name::getExceptionTypeByNumericCode() const \
            { \
               return( unique_numeric_code ); /* This must be UNIQUE! */ \
            }

#else // !_IN_CPEXCEPTION_CPP
#define CREATE_EXCEPTION_CLASS_DERIVED_FROM_SPECIFIC_BASE( new_exception_name, base_exception_name, unique_numeric_code ) \
    class UTILDLL_EXPORT new_exception_name : public base_exception_name \
    { \
    public: \
        new_exception_name( const char *message, LASTERROR_TYPE lastError, const IDebugContext& debugContextWhereThrown ); \
        new_exception_name( std::string&& message, LASTERROR_TYPE lastError, const IDebugContext& debugContextWhereThrown ); \
        new_exception_name( LASTERROR_TYPE lastError, const IDebugContext& debugContextWhereThrown ); \
        new_exception_name( const new_exception_name& source ) = default; \
        new_exception_name& operator=(const new_exception_name& rightHandSide) = default; \
        new_exception_name(new_exception_name&& source); \
        new_exception_name& operator=(new_exception_name&& rightHandSide); \
        virtual ~new_exception_name() = default; \
        virtual int getExceptionTypeByNumericCode() const; \
    };
#endif // !_IN_EXCEPTION_CPP

Any ideas?

I found this type of info online: redundant with pragma once ==(It looks like there’s a known problem with cppcheck and #pragma once (#ifndef #define is the fix, but I don’t think we want that). I think we’d see a lot more of this issue if it was #pragma once.

Here’s a second blurb for the redundant code issue: enum or ifndef for redundant code issue ==(I don’t think it’s an enum issue) (could be that we don’t have #ifndef #define the .h …I don’t see it)
cppcheck thinks I have “Redundant code: Found a statement that begins with numeric constant”

The whole .h file these are defined in just contains the macros and use of the macros. The owner doesn't think he needs a #ifndef around the #define since the cppcheck is complaining about all the lines under the #define that use the macros.

In addition: there is no #ifndef around the whole .h file, but the owner doesn't think it's needed.

Build info: For our build normally:

g++ -std=c++11 -ggdb -Wall -Wextra -Werror -pedantic -fdiagnostic-show-option -fPIC -DLinuxx86_64

What I'm seeing in jenkins for cppcheck setup:

cppcheck --enable=all --xml --xml-version=2 -DLinuxx86_64 --platform=unix64 --include=Src/Headers/CommonPrecomp.h -v --report-progress -DOPTIMUS_CDECL -DUTILDLL_EXPORT -DOPTIMUS_COREDLL_EXPORT -DREADERDLL_EXPORT -DWRITERDLL_EXPORT -E Src 2> cppcheck.xml

I'm not sure it's doing the build the same in jenkins for cppcheck as it does for our normal build.

Any other ideas? Thanks!


Solution

  • It looks like the issue was that it wasn't getting to our include directories. We had them brought in from a file. We had to add them to the jenkins build setup:

    cppcheck -j 2 --xml --xml-version=1 --enable=all -DLinuxx86_64 --platform=unix64 -v --report-progress --suppress=missingIncludeSystem --include=Sr/Head/Common.h -i Sr/Doc/Win.cpp -i Sr/P/S/S.cpp -DOPT_CDECL -DUTILDLL_EXPORT -DOPT_COREDLL_EXPORT -DREADLL_EXPORT -DWDLL_EXPORT -ISr/Head -ISr/Head/libopc -ISr/Head/lib/config -ISrc/Head/lib/lib2 -ISr/Snot -ISr/Doc -ISr/Doc -ISr/PDers/XP -ISr/PDers/Ca -ISr/PDers/Ca/Head -IVersionAndBuildConfig -ISr/Head/GoogleTest/include -ISr/Head/GoogleTest/include/gtest Sr/Doc > cppcheck.xml