0

Not exactly sure how to title this nicely.

I've got a large CMake project that is targeting the MSVC compiler with Ninja build files. I'd like to integrate Clang-Tidy to analyze our code but am hitting a few roadblocks.

Differing Flags between Clang-tidy and MSVC

Our project uses certain MSVC options like /FI to include certain files. These are not recognized when clang-tidy. This represents a barrier for using the CMAKE_<LANG>_CLANG_TIDY as I would essentially have to do a different configuration for actually compiling the files and another for just doing tidy.

How do I remedy an issue like this beyond just find and replace the flags from the variables used by each source file (ie, string(REPLACE "/FI" "-include" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})). Doing this is likely to break whenever a new option is added. I believe that this directly relates to my next part of the question.

Clang Tidy Integration per-Target

When you set CMAKE_<LANG>_CLANG_TIDY, you get tidy run on the build command of every source file rather than a seperate "tidy" target. This is generally fine for a debug build or a single target.

I'd be interested to have tidy run as a separate target (--target tidy) that just reports the warnings / fixes from clang-tidy. I would imagine doing this something like (pseudo code):

function(tidy-target tgt)
    add_custom_target(${tgt}_tidy)
    for source in tgt:
        add_custom_target(${source}_tidy "${CLANG_TIDY} source")
        # Copy all flags, includes, options, defines and pass to clang tidy, how?
        add_dependency(${tgt}_tidy ${source}_tidy)
endfunction()

This would give me the benefit of integrating this check slowly onto the larger project (as there will be a lot of issues to move through). As well, it gives the ability to easily separate a dry-run from a fix invocation of tidy. Just wanted thoughts on this process.

I think the most finicky part will be setting up the custom targets to ensure they have all of the correct options as the real build. I wonder if there is a built-in way around something like this. Perhaps, how does CMake do this when using the traditional CMAKE_<LANG>_CLANG_TIDY variable? Or am I stuck just copying each _FLAGS variable into the custom target?

EDIT:

Seems like maybe some of the options such as /FI are properly respected by clang-tidy, but not all the time. Some of my files might be generating with poorly formed commands...

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.