From 24c9fc30456d0b1ee30a987623747332dd5642ad Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Thu, 23 Dec 2021 23:58:16 +0300 Subject: [PATCH] add test for format string compile-time check --- test/compile-error-test/CMakeLists.txt | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test/compile-error-test/CMakeLists.txt b/test/compile-error-test/CMakeLists.txt index 5940601c7ab8..5636d6e6c41f 100644 --- a/test/compile-error-test/CMakeLists.txt +++ b/test/compile-error-test/CMakeLists.txt @@ -8,7 +8,18 @@ set(test_names "") # Adds a name of test into `test_names` list # Sets `test_{}_error_expected` (where {} is test name) to 0 or 1 depending on test result function (expect_compile name code_fragment) - cmake_parse_arguments(EXPECT_COMPILE "ERROR;FORMAT;XCHAR" "" "" ${ARGN}) + cmake_parse_arguments(EXPECT_COMPILE "ERROR;FORMAT;XCHAR;FORMAT_STRING_CHECK" "" "" ${ARGN}) + if (EXPECT_COMPILE_FORMAT_STRING_CHECK) + if (CMAKE_CXX_STANDARD LESS 20) + return() + else () + set(else_branch "") + if (EXPECT_COMPILE_ERROR) + set(else_branch "#error") + endif () + set(code_fragment "#ifdef FMT_HAS_CONSTEVAL\n${code_fragment}\n#else\n${else_branch}\n#endif") + endif () + endif () set(test_names_copy "${test_names}") list(APPEND test_names_copy "${name}") set(test_names "${test_names_copy}" PARENT_SCOPE) @@ -131,5 +142,13 @@ expect_compile(udl-check " #endif " FORMAT) +# Compile-time argument type check +expect_compile(format-string-number-spec " + fmt::format(\"{:d}\", 42); +" FORMAT_STRING_CHECK) +expect_compile(format-string-number-spec-error " + fmt::format(\"{:d}\", \"I am not a number\"); +" FORMAT_STRING_CHECK ERROR) + # Run all tests run_tests()