From 60ed318a59a3e6d7e0bde7b22a4364b8ea50c5b8 Mon Sep 17 00:00:00 2001 From: Steve Downey Date: Sun, 1 Nov 2026 19:54:21 -0500 Subject: [PATCH] Read the EXPECT lines in the xfail driver, when the test runs The regex for a negative compile test was read from the source at configure time and baked into the test command. ctest loads the test definitions when it starts, so after editing an EXPECT line the first bare `ctest` run still matched the old regex: the nested build regenerated the test file, but under a ctest that had already read it. Only a build before ctest, as `make test` does, got the new one first time. The driver now receives the source path (-DBEMAN_XFAIL_EXPECT_FILE) and reads the EXPECT lines itself when the test runs, so an edit takes effect on the next run with neither a reconfigure nor a rebuild, and the source is no longer a configure dependency. The scan lives in cmake/beman-xfail-compile-expect.cmake, shared by the driver and by the registration function, which still rejects a source with no EXPECT line at configure time. The scan also stops going through file(STRINGS) and a list. A regex may contain an unbalanced "[" (as in "\[with R = .+foo"), and CMake does not split a list at a ";" that follows an unmatched "[", so two EXPECT lines merged into one that the module then rejected for containing a semicolon. beman.transcode hit this on its first use of the module. String operations over the file contents have no such rule. An EXPECT passed as an argument still travels as -DBEMAN_XFAIL_EXPECT and keeps its no-semicolon rule; a regex read from the source may contain one. --- cmake/beman-xfail-compile-driver.cmake | 33 +++++++++++++- cmake/beman-xfail-compile-expect.cmake | 49 +++++++++++++++++++++ cmake/beman-xfail-compile.cmake | 60 ++++++++++++-------------- 3 files changed, 107 insertions(+), 35 deletions(-) create mode 100644 cmake/beman-xfail-compile-expect.cmake diff --git a/cmake/beman-xfail-compile-driver.cmake b/cmake/beman-xfail-compile-driver.cmake index c62c120..7d52d3a 100644 --- a/cmake/beman-xfail-compile-driver.cmake +++ b/cmake/beman-xfail-compile-driver.cmake @@ -6,9 +6,13 @@ # cmake -DBEMAN_XFAIL_BUILD_DIR= # -DBEMAN_XFAIL_TARGET= # -DBEMAN_XFAIL_CONFIG= -# -DBEMAN_XFAIL_EXPECT= +# -DBEMAN_XFAIL_EXPECT= | -DBEMAN_XFAIL_EXPECT_FILE= # -P beman-xfail-compile-driver.cmake # +# The expectation is either a regex given outright, or read from the +# `// EXPECT: ""` lines of the source when the test runs, so an edit +# to those lines takes effect on the next run with no reconfigure. +# # It builds just the target and succeeds only when the build fails and its # output matches the regex. The three outcomes get three distinct reports: # the source compiled when it should not have; it failed, but not with the @@ -16,12 +20,37 @@ cmake_minimum_required(VERSION 3.30...4.3) -foreach(_var BEMAN_XFAIL_BUILD_DIR BEMAN_XFAIL_TARGET BEMAN_XFAIL_EXPECT) +foreach(_var BEMAN_XFAIL_BUILD_DIR BEMAN_XFAIL_TARGET) if(NOT DEFINED ${_var} OR "${${_var}}" STREQUAL "") message(FATAL_ERROR "beman-xfail-compile-driver: ${_var} is required") endif() endforeach() +if( + DEFINED BEMAN_XFAIL_EXPECT_FILE + AND NOT "${BEMAN_XFAIL_EXPECT_FILE}" STREQUAL "" +) + if(DEFINED BEMAN_XFAIL_EXPECT AND NOT "${BEMAN_XFAIL_EXPECT}" STREQUAL "") + message( + FATAL_ERROR + "beman-xfail-compile-driver: BEMAN_XFAIL_EXPECT and BEMAN_XFAIL_EXPECT_FILE are exclusive" + ) + endif() + include(${CMAKE_CURRENT_LIST_DIR}/beman-xfail-compile-expect.cmake) + beman_xfail_compile_read_expect("${BEMAN_XFAIL_EXPECT_FILE}" BEMAN_XFAIL_EXPECT) + if("${BEMAN_XFAIL_EXPECT}" STREQUAL "") + message( + FATAL_ERROR + "beman-xfail-compile-driver: no '// EXPECT: \"\"' line in ${BEMAN_XFAIL_EXPECT_FILE}" + ) + endif() +elseif(NOT DEFINED BEMAN_XFAIL_EXPECT OR "${BEMAN_XFAIL_EXPECT}" STREQUAL "") + message( + FATAL_ERROR + "beman-xfail-compile-driver: BEMAN_XFAIL_EXPECT or BEMAN_XFAIL_EXPECT_FILE is required" + ) +endif() + set(_build_args --build "${BEMAN_XFAIL_BUILD_DIR}" diff --git a/cmake/beman-xfail-compile-expect.cmake b/cmake/beman-xfail-compile-expect.cmake new file mode 100644 index 0000000..ddd0561 --- /dev/null +++ b/cmake/beman-xfail-compile-expect.cmake @@ -0,0 +1,49 @@ +# cmake/beman-xfail-compile-expect.cmake -*-CMake-*- +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +include_guard(GLOBAL) + +# beman_xfail_compile_read_expect +# =============================== +# +# beman_xfail_compile_read_expect( ) +# +# Reads the expectation lines of a negative compile test source, of the form +# +# // EXPECT: "" +# +# and stores them in joined with "|", or "" when there are none. +# It is shared by beman_add_xfail_compile_test(), which uses it to reject a +# source with no expectation at configure time, and by the test driver, +# which reads the file when the test runs so that editing an expectation +# needs neither a reconfigure nor a rebuild. +# +# The file is scanned with string operations rather than file(STRINGS) and a +# list: a regex is free to contain an unbalanced "[" (as in +# "\[with R = .+foo"), and CMake does not split a list at a ";" that follows +# an unmatched "[", which would merge that line with the next. + +function(beman_xfail_compile_read_expect source out_var) + file(READ "${source}" _rest) + set(_expect "") + set(_separator "") + while(TRUE) + string( + REGEX MATCH "(^|\n)// EXPECT: \"([^\"\n]*)\"[ \t]*(\n|$)" + _match + "${_rest}" + ) + if("${_match}" STREQUAL "") + break() + endif() + string(APPEND _expect "${_separator}${CMAKE_MATCH_2}") + set(_separator "|") + # Resume just before the newline that ended this match, so it can + # begin the next one. + string(FIND "${_rest}" "${_match}" _position) + string(LENGTH "${_match}" _length) + math(EXPR _next "${_position} + ${_length} - 1") + string(SUBSTRING "${_rest}" ${_next} -1 _rest) + endwhile() + set(${out_var} "${_expect}" PARENT_SCOPE) +endfunction() diff --git a/cmake/beman-xfail-compile.cmake b/cmake/beman-xfail-compile.cmake index 4cc4d65..119a354 100644 --- a/cmake/beman-xfail-compile.cmake +++ b/cmake/beman-xfail-compile.cmake @@ -44,8 +44,10 @@ include_guard(GLOBAL) # so the expectation lives next to the ill-formed code it describes. # Several lines are joined with "|", so a long alternation can be split # one compiler's wording per line and stay inside a formatter's column -# limit. Configuration re-runs when that file changes. Passing EXPECT -# overrides the comment, for sources that cannot carry one. +# limit. The driver reads those lines when the test runs, so editing them +# takes effect on the next `ctest` with neither a reconfigure nor a +# rebuild; configuration only checks that at least one is present. +# Passing EXPECT overrides the comment, for sources that cannot carry one. # # LINK_LIBRARIES (optional) # Targets whose usage requirements the source is compiled with, typically @@ -79,7 +81,9 @@ include_guard(GLOBAL) # under `ctest -j` the nested builds never run concurrently in one build # tree. Other tests still run alongside them. # -# EXPECT is a CMake regular expression and must not contain ";". +# EXPECT is a CMake regular expression. Passed as an argument it must not +# contain ";", since it travels to the driver as one element of the test +# command; read from SOURCE it may. function(beman_add_xfail_compile_test name) set(oneValueArgs SOURCE EXPECT TARGET_PREFIX) @@ -110,42 +114,32 @@ function(beman_add_xfail_compile_test name) _source_path BASE_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" ) - set_property( - DIRECTORY - APPEND - PROPERTY CMAKE_CONFIGURE_DEPENDS "${_source_path}" + include( + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/beman-xfail-compile-expect.cmake ) - file(STRINGS "${_source_path}" _expect_lines REGEX "^// EXPECT: \"") - if(NOT _expect_lines) + beman_xfail_compile_read_expect("${_source_path}" _expect) + if("${_expect}" STREQUAL "") message( FATAL_ERROR "beman_add_xfail_compile_test(${name}): no '// EXPECT: \"\"' line in ${_source_path}; add one or pass EXPECT" ) endif() - set(_alternatives) - foreach(_line IN LISTS _expect_lines) - string( - REGEX REPLACE "^// EXPECT: \"(.*)\"[ \t]*$" - "\\1" - _alternative - "${_line}" + set(_expect_arg "-DBEMAN_XFAIL_EXPECT_FILE=${_source_path}") + else() + if("${BEMAN_XFAIL_EXPECT}" STREQUAL "") + message( + FATAL_ERROR + "beman_add_xfail_compile_test(${name}): EXPECT must not be empty" ) - list(APPEND _alternatives "${_alternative}") - endforeach() - list(JOIN _alternatives "|" BEMAN_XFAIL_EXPECT) - endif() - if("${BEMAN_XFAIL_EXPECT}" STREQUAL "") - message( - FATAL_ERROR - "beman_add_xfail_compile_test(${name}): EXPECT must not be empty" - ) - endif() - string(FIND "${BEMAN_XFAIL_EXPECT}" ";" _semicolon) - if(NOT _semicolon EQUAL -1) - message( - FATAL_ERROR - "beman_add_xfail_compile_test(${name}): EXPECT must not contain ';'" - ) + endif() + string(FIND "${BEMAN_XFAIL_EXPECT}" ";" _semicolon) + if(NOT _semicolon EQUAL -1) + message( + FATAL_ERROR + "beman_add_xfail_compile_test(${name}): EXPECT must not contain ';'" + ) + endif() + set(_expect_arg "-DBEMAN_XFAIL_EXPECT=${BEMAN_XFAIL_EXPECT}") endif() if(NOT DEFINED BEMAN_XFAIL_TARGET_PREFIX) set(BEMAN_XFAIL_TARGET_PREFIX "${PROJECT_NAME}.tests.") @@ -174,7 +168,7 @@ function(beman_add_xfail_compile_test name) COMMAND ${CMAKE_COMMAND} -DBEMAN_XFAIL_BUILD_DIR=${CMAKE_BINARY_DIR} -DBEMAN_XFAIL_TARGET=${_target} -DBEMAN_XFAIL_CONFIG=$ - -DBEMAN_XFAIL_EXPECT=${BEMAN_XFAIL_EXPECT} -P + ${_expect_arg} -P ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/beman-xfail-compile-driver.cmake ) set_tests_properties(