Using clang-tidy with CMake and excluding JUCE headers

I’m trying to run static analysis on my code using clang-tidy integrated into CMake, but I am having issues with clang-tidy running analysis on JUCE headers. Since these do not satisfy my rules in the .clang-tidy file, and I want to mark alle warning as errors, my compilation fails. I have not had any luck with excluding the juce headers from the check using the different methods available.

What I have tried:

  • Set HeaderFilterRegex: in .clang-tidy to only my source files.
  • Set the clang-tidy command line in CMakeLists.txt to exclude folders:
    set_target_properties(myApp PROPERTIES CXX_CLANG_TIDY "clang-tidy;--header-filter=^(!?${CMAKE_PREFIX_PATH})" )
  • I am able to run clang-tidy on only my source files if I run this command directly:
    clang-tidy --config-file=.clang-tidy -p /path/to/compile_commands.json src/*

I understand that my issue is that the JUCE library does not consist of pre-compiled library files, therefore the header files are technically part of my source, but there must be a way to solve this that I don’t know of. Does anybody have any experience with this?

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)

project(myApp VERSION 1.0.0)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(JUCE 7.0.9 CONFIG REQUIRED)

juce_add_gui_app(myApp
	VERSION 1.0.0
	PRODUCT_NAME myApp)

juce_add_binary_data(myAssets
	SOURCES
		"/asset/1"
		"/asset/2"
)

file(GLOB_RECURSE SRC_FILES
	source files
	...
)

target_sources(myApp PRIVATE ${SRC_FILES})

set_target_properties(myApp PROPERTIES
   CXX_CLANG_TIDY "clang-tidy;--header-filter=^(!?${CMAKE_PREFIX_PATH})"
)

target_link_libraries(myApp
	PRIVATE
		myAssets
		juce::juce_audio_basics
		juce::juce_audio_devices
		juce::juce_audio_formats
		juce::juce_audio_processors
		juce::juce_audio_utils
		juce::juce_core
		juce::juce_data_structures
		juce::juce_dsp
		juce::juce_events
		juce::juce_graphics
		juce::juce_gui_basics
		juce::juce_gui_extra
	PUBLIC
		juce::juce_recommended_config_flags
		juce::juce_recommended_lto_flags
		juce::juce_recommended_warning_flags
)

juce_generate_juce_header(myApp)

Hi, suggestion for a perhaps easier solution: clang tidy will look for .clang-tidy files and apply the settings to directories and subdirectories where those are found. as long as your juce sources reside in a directory tree separate from your application sources, you can put an empty .clang-tidy file there to make it ignore the juce headers.

Thanks for the suggestion. What I ended up doing was dropping juce_generate_juce_header in CMake and instead including each header individually in the necessary files. This way I could make use of target_include_directories(myTarget SYSTEM PRIVATE ${INCLUDE_DIR}) to avoid having clang-tidy looking into the sources of the JuceHeader.h. This solved it for me!

1 Like