c++cmakeprotocol-buffersclang-tidy

Prevent clang-tidy from running on generated files


We have a project that includes protobuf files that get pre-compiled into C++ files. Unfortunately, those files (like the other source files in the project) get checked by clang-tidy and generate a large number of warnings.

The relevant top-level CMakeLists.txt statements are:

# For all targets
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_EXECUTABLE}; --header-filter=.)

# Generated protobufs
include_directories(SYSTEM ${CMAKE_BINARY_DIR}/grpc_interface)

target_link_libraries(${PROJECT_NAME}
    # ...
    grpc_services
)

And the grpc_interface/CMakeLists.txt contains:

project(grpc_services)

set(PROTO_IMPORT_DIRS
    ${CMAKE_SOURCE_DIR}/protocols/protobuf/
)

set(PROTOS
    ${CMAKE_SOURCE_DIR}/protocols/protobuf/test_message.proto
)

add_library(${PROJECT_NAME} STATIC ${PROTOS})

target_link_libraries(${PROJECT_NAME}
    PUBLIC
        protobuf::libprotobuf
        gRPC::grpc++
)

get_target_property(GRPC_CPP_PLUGIN gRPC::grpc_cpp_plugin LOCATION)

protobuf_generate(
    TARGET ${PROJECT_NAME}
    IMPORT_DIRS ${PROTO_IMPORT_DIRS}
    LANGUAGE cpp
)

protobuf_generate(
    TARGET ${PROJECT_NAME}
    IMPORT_DIRS ${PROTO_IMPORT_DIRS}
    LANGUAGE grpc
    GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc
    PLUGIN "protoc-gen-grpc=${GRPC_CPP_PLUGIN}"
)

One option is to copy a .clang-tidy file that disables all warnings into the ${CMAKE_BINARY_DIR}/grpc_interface directory, but I think that that would still incur the time penalty of clang-tidy parsing those files.

The "grpc_services" target only contains generated files.

Is there a better way of disabling clang-tidy processing in just that folder?


Solution

  • Since the "grpc_services" target only contains generated files, you can just set the CXX_CLANG_TIDY target property on that target to be nothing.