Menu

False preprocessorErrorDirective with Protobuf

Kai
2022-04-10
2022-12-05
  • Kai

    Kai - 2022-04-10

    Hi,

    I'm working with CMake and the --project option of cppcheck to do some
    static code analysis and I've encountered an error when trying to check
    files generated by the protoc compiler. Cppcheck tells me that there is
    a preprocessorErrorDirective in one of the generated classes. The error
    results from this check here:

    #if 3017003 < PROTOBUF_MIN_PROTOC_VERSION
    #error This file was generated by an older version of protoc which is
    #error incompatible with your Protocol Buffer headers. Please
    #error regenerate this file with a newer version of protoc.
    #endif
    

    It seems that cppcheck assumes that PROTOBUF_MIN_PROTOC_VERSION is
    not defined, although it get's defined earlier by an include of
    <google protobuf="" port_def.inc="">. The problem only arises when I add a
    target_compile_definitions in CMake. If I leave this out, cppcheck
    correctly expands the macro.</google>

    I've attached an example test case where you can reproduce the error
    with the current 2.7.4 version of cppcheck. To run the test, build the
    docker image with docker builder . and run the image with docker run -it --rm -v "$(pwd)":/workspace {HASH_FROM_BUILD}. Run cd /workspace && ./runtest.sh inside the docker image. You can toggle
    target_compile_definitions inside the CMake file and see the
    behavior.

    BR Kai

     
  • mMontu

    mMontu - 2022-05-27

    Hi

    I'm facing the same issue. Adding "-I${Protobuf_INCLUDE_DIRS}" to the CMAKE_CXX_CPPCHECK bypass this error, but then it fails like this:

    /usr/local/include/google/protobuf/stubs/port.h:123:0: error: #error "Protobuf requires at least C++11." [preprocessorErrorDirective]
    #error "Protobuf requires at least C++11."
    

    The error happens even though I'm using C++14, very likely because the cplusplus macro is not received by cppcheck.

     
  • Bastien P

    Bastien P - 2022-12-05

    Hello,

    Same issue.
    Here is the current solution I found:
    Need to replace isystem include to Protobuf path by -I include.
    As @mMontu, the same error appears.
    So, I generated default predefine: echo | g++ -dM -E -x c++ - > gcc_predefined.h
    And finally, using cppcheck by adding --include=./gcc_predefined.h

    Do you have a better solution?

    Is possible that ccpcheck retrieve by itself theses defines that gcc is able to dump?

     

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.