Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions cmake/beman-xfail-compile-driver.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,51 @@
# cmake -DBEMAN_XFAIL_BUILD_DIR=<build dir>
# -DBEMAN_XFAIL_TARGET=<object library>
# -DBEMAN_XFAIL_CONFIG=<config or empty>
# -DBEMAN_XFAIL_EXPECT=<regex>
# -DBEMAN_XFAIL_EXPECT=<regex> | -DBEMAN_XFAIL_EXPECT_FILE=<source>
# -P beman-xfail-compile-driver.cmake
#
# The expectation is either a regex given outright, or read from the
# `// EXPECT: "<regex>"` 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
# expected diagnostic; or it failed with the expected diagnostic.

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: \"<regex>\"' 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}"
Expand Down
49 changes: 49 additions & 0 deletions cmake/beman-xfail-compile-expect.cmake
Original file line number Diff line number Diff line change
@@ -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(<source> <out-var>)
#
# Reads the expectation lines of a negative compile test source, of the form
#
# // EXPECT: "<regex>"
#
# and stores them in <out-var> 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()
60 changes: 27 additions & 33 deletions cmake/beman-xfail-compile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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: \"<regex>\"' 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.")
Expand Down Expand Up @@ -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=$<CONFIG>
-DBEMAN_XFAIL_EXPECT=${BEMAN_XFAIL_EXPECT} -P
${_expect_arg} -P
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/beman-xfail-compile-driver.cmake
)
set_tests_properties(
Expand Down
Loading