Skip to content

VERIFY_INTERFACE_HEADER_SETS: Add IWYU pragma: associated to verification file

Daniel Hannon requested to merge danielh2942/cmake:master into master

I have encountered the following issue when using cmake with IWYU and came up with this solution

The problem

include-what-you-use does not give advice to headers when using -DCMAKE_VERIFY_INTERFACE_HEADER_SETS=ON

The solution

Adding // IWYU pragma: associated to every {PROJECT}_verify_header_sets/{header}.h.cxx file

What it does

According to the docs

Headers marked with // IWYU pragma: associated are treated as part of the overall language unit of the file


I wrote the following


#include <iostream>

int ab(int a, int b);


#include <foo.h>

int ab(int a, int b) {
    return a + b;


cmake_minimum_required(VERSION 3.24.0)

add_library(foo foo.cpp)

target_include_directories(foo PUBLIC .)

target_sources(blah PUBLIC FILE_SET internal_h_files TYPE HEADERS BASE_DIRS . FILES blah.h)

I then ran {CMAKE_BIN} -DCMAKE_VERIFY_INTERFACE_HEADER_SETS=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON . then -p compile_commands.json ( is from include-what-you-use)

relevant output before the change

foo/foo_verify_interface_header_sets/foo.h.cxx should add these lines:

foo/foo_verify_interface_header_sets/foo.h.cxx should remove these lines:
- #include <foo.h>  // lines 1-1

The full include-list for foo/foo_verify_interface_header_sets/foo.h.cxx:

relevant output after the change

foo/foo.h should add these lines:

foo/foo.h should remove these lines:
- #include <iostream> // lines 1-1

The full include-list for foo/foo.h:

Topic-rename: verify-header-sets-iwyu

Edited by Kyle Edwards

Merge request reports