From 92127a984a47113a918720fadfa6b7e6fc1387e1 Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Thu, 1 Oct 2026 22:01:32 -0400 Subject: [PATCH 1/2] cmake: Pass -w to include-what-you-use Stop the compiler warning flags in CXXFLAGS from turning clang warnings into errors in the IWYU pass. IWYU's clang frontend receives the same flags as the real compiler, so in the GCC CI jobs (-Werror -Wpedantic) every KJ_TEST reports "'__COUNTER__' is a C2y extension" and the GCC-only -Wtrailing-whitespace is an unknown option. Once a file has enough tests to reach clang's 20-error limit (test/mp/test/test.cpp currently has 16), clang gives up, IWYU exits 1 without analyzing the file, and the build fails with no diagnostics. Real IWYU findings are still reported with -w. This change was written with Claude Opus 5.5 (1M context). --- CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4eb0e694..6d9fd74f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,7 +96,10 @@ if(MP_ENABLE_IWYU) if(NOT IWYU_EXECUTABLE) message(FATAL_ERROR "MP_ENABLE_IWYU is ON but include-what-you-use was not found.") endif() - set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${IWYU_EXECUTABLE};-Xiwyu;--error") + # Pass -w so CXXFLAGS meant for the compiler (-Werror -Wpedantic, GCC-only -W + # options) do not turn clang warnings into errors in the IWYU pass. Clang + # stops after 19 errors, and IWYU then exits nonzero without checking includes. + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${IWYU_EXECUTABLE};-Xiwyu;--error;-w") if(DEFINED ENV{IWYU_MAPPING_FILE}) list(APPEND CMAKE_CXX_INCLUDE_WHAT_YOU_USE "-Xiwyu" "--mapping_file=$ENV{IWYU_MAPPING_FILE}") endif() From f0a14754ef4f922c8e391c201f3d4329cb69ee0e Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Thu, 1 Oct 2026 22:01:45 -0400 Subject: [PATCH 2/2] cmake: Show include-what-you-use diagnostics when it fails Run include-what-you-use through a wrapper script that makes CMake print its output when it fails without suggesting includes. CMake only relays IWYU output containing "should add these lines:" or "should remove these lines:", so a clang error in the IWYU pass (like the too-many-errors abort fixed in the previous commit) failed the build with a bare "Error 1" and nothing to debug. This change was written with Claude Opus 5.5 (1M context). --- CMakeLists.txt | 4 +++- cmake/iwyu-wrapper.sh | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100755 cmake/iwyu-wrapper.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d9fd74f..3462f3e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,7 +99,9 @@ if(MP_ENABLE_IWYU) # Pass -w so CXXFLAGS meant for the compiler (-Werror -Wpedantic, GCC-only -W # options) do not turn clang warnings into errors in the IWYU pass. Clang # stops after 19 errors, and IWYU then exits nonzero without checking includes. - set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${IWYU_EXECUTABLE};-Xiwyu;--error;-w") + # Run IWYU through a wrapper so its diagnostics are shown when it fails + # without suggesting includes (see cmake/iwyu-wrapper.sh). + set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/iwyu-wrapper.sh;${IWYU_EXECUTABLE};-Xiwyu;--error;-w") if(DEFINED ENV{IWYU_MAPPING_FILE}) list(APPEND CMAKE_CXX_INCLUDE_WHAT_YOU_USE "-Xiwyu" "--mapping_file=$ENV{IWYU_MAPPING_FILE}") endif() diff --git a/cmake/iwyu-wrapper.sh b/cmake/iwyu-wrapper.sh new file mode 100755 index 00000000..fa2fd1a0 --- /dev/null +++ b/cmake/iwyu-wrapper.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +# Run include-what-you-use for CMAKE_CXX_INCLUDE_WHAT_YOU_USE and make sure its +# output is shown when it fails. Usage: iwyu-wrapper.sh IWYU [ARGS...] +# +# "cmake -E __run_co_compile" only prints IWYU output that contains "should add +# these lines:" or "should remove these lines:" (HandleIWYU in CMake's +# Source/cmcmd.cxx). If IWYU fails for another reason, such as a clang error, +# the build stops with a bare "Error 1". In that case this script appends a line +# containing the marker text, so CMake prints the clang diagnostics too. +# +# This works around https://gitlab.kitware.com/cmake/cmake/-/issues/28020 and +# can be removed once CMake prints IWYU output whenever IWYU fails. +set -o nounset +iwyu=$1 +shift +out=$("$iwyu" "$@" 2>&1) +ret=$? +printf '%s\n' "$out" >&2 +if [ "$ret" -ne 0 ] && ! grep -q -e 'should add these lines:' -e 'should remove these lines:' <<<"$out"; then + printf 'include-what-you-use failed with exit status %s before suggesting includes (marker for CMake: "should add these lines:"); see the diagnostics above.\n' "$ret" >&2 +fi +exit "$ret"