Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
4482f6f1
Commit
4482f6f1
authored
Dec 13, 2021
by
Alexey Ochapov
Committed by
Victor Zverovich
Dec 27, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrite compile-error-test to use non-header-only library
parent
796662a6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
122 additions
and
49 deletions
+122
-49
test/CMakeLists.txt
test/CMakeLists.txt
+2
-2
test/compile-error-test/CMakeLists.txt
test/compile-error-test/CMakeLists.txt
+120
-47
No files found.
test/CMakeLists.txt
View file @
4482f6f1
...
@@ -168,9 +168,9 @@ if (FMT_PEDANTIC)
...
@@ -168,9 +168,9 @@ if (FMT_PEDANTIC)
--build-makeprogram
${
CMAKE_MAKE_PROGRAM
}
--build-makeprogram
${
CMAKE_MAKE_PROGRAM
}
--build-options
--build-options
"-DCMAKE_CXX_COMPILER=
${
CMAKE_CXX_COMPILER
}
"
"-DCMAKE_CXX_COMPILER=
${
CMAKE_CXX_COMPILER
}
"
"-DCMAKE_CXX_FLAGS=
${
CMAKE_CXX_FLAGS
}
"
"-DCMAKE_CXX_STANDARD=
${
CMAKE_CXX_STANDARD
}
"
"-DCMAKE_CXX_STANDARD=
${
CMAKE_CXX_STANDARD
}
"
"-DCXX_STANDARD_FLAG=
${
CXX_STANDARD_FLAG
}
"
"-DFMT_DIR=
${
CMAKE_SOURCE_DIR
}
"
"-DPEDANTIC_COMPILE_FLAGS=
${
PEDANTIC_COMPILE_FLAGS
}
"
"-DSUPPORTS_USER_DEFINED_LITERALS=
${
SUPPORTS_USER_DEFINED_LITERALS
}
"
)
"-DSUPPORTS_USER_DEFINED_LITERALS=
${
SUPPORTS_USER_DEFINED_LITERALS
}
"
)
endif
()
endif
()
...
...
test/compile-error-test/CMakeLists.txt
View file @
4482f6f1
# Test if compile errors are produced where necessary.
# Test if compile errors are produced where necessary.
cmake_minimum_required
(
VERSION 3.1...3.18
)
cmake_minimum_required
(
VERSION 3.1...3.18
)
project
(
compile-error-test CXX
)
include
(
CheckCXXSourceCompiles
)
set
(
fmt_headers
"
include
(
CheckCXXCompilerFlag
)
#include <fmt/format.h>
#include <fmt/xchar.h>
"
)
set
(
CMAKE_REQUIRED_INCLUDES
${
CMAKE_CURRENT_SOURCE_DIR
}
/../../include
)
set
(
error_test_names
""
)
set
(
CMAKE_REQUIRED_FLAGS
${
CXX_STANDARD_FLAG
}
${
PEDANTIC_COMPILE_FLAGS
}
)
set
(
non_error_test_content
""
)
function
(
generate_source result fragment
)
# For error tests (we expect them to produce compilation error):
set
(
${
result
}
"
# * adds a name of test into `error_test_names` list
#define FMT_HEADER_ONLY 1
# * generates a single source file (with the same name) for each test
#include
\"
fmt/format.h
\"
# For non-error tests (we expect them to compile successfully):
int main() {
# * adds a code segment as separate function to `non_error_test_content`
${
fragment
}
function
(
expect_compile name code_fragment
)
}
cmake_parse_arguments
(
EXPECT_COMPILE
"ERROR"
""
""
${
ARGN
}
)
"
PARENT_SCOPE
)
string
(
MAKE_C_IDENTIFIER
"
${
name
}
"
test_name
)
endfunction
()
function
(
expect_compile code
)
if
(
EXPECT_COMPILE_ERROR
)
generate_source
(
source
"
${
code
}
"
)
file
(
WRITE
"
${
CMAKE_CURRENT_BINARY_DIR
}
/test/
${
test_name
}
.cc"
"
check_cxx_source_compiles
(
"
${
source
}
"
compiles
)
${
fmt_headers
}
if
(
NOT compiles
)
void
${
test_name
}
() {
set
(
error_msg
"Compile error for:
${
code
}
"
)
${
code_fragment
}
endif
()
}
# Unset the CMake cache variable compiles. Otherwise the compile test will
"
)
# just use cached information next time it runs.
set
(
error_test_names_copy
"
${
error_test_names
}
"
)
unset
(
compiles CACHE
)
list
(
APPEND error_test_names_copy
"
${
test_name
}
"
)
if
(
error_msg
)
set
(
error_test_names
"
${
error_test_names_copy
}
"
PARENT_SCOPE
)
message
(
FATAL_ERROR
${
error_msg
}
)
else
()
endif
()
set
(
non_error_test_content
"
${
non_error_test_content
}
void
${
test_name
}
() {
${
code_fragment
}
}"
PARENT_SCOPE
)
endif
()
endfunction
()
endfunction
()
function
(
expect_compile_error code
)
# Generates a source file for non-error test with `non_error_test_content` and
generate_source
(
source
"
${
code
}
"
)
# CMake project file with all error and single non-error test targets.
check_cxx_source_compiles
(
"
${
source
}
"
compiles
)
function
(
run_tests
)
if
(
compiles
)
set
(
cmake_targets
""
)
set
(
error_msg
"No compile error for:
${
code
}
"
)
foreach
(
test_name IN LISTS error_test_names
)
endif
()
set
(
cmake_targets
"
# Unset the CMake cache variable compiles. Otherwise the compile test will
${
cmake_targets
}
# just use cached information next time it runs.
add_library(test-
${
test_name
}
${
test_name
}
.cc)
unset
(
compiles CACHE
)
target_link_libraries(test-
${
test_name
}
PRIVATE fmt::fmt)
if
(
error_msg
)
"
)
message
(
FATAL_ERROR
${
error_msg
}
)
endforeach
()
file
(
WRITE
"
${
CMAKE_CURRENT_BINARY_DIR
}
/test/non_error_test.cc"
"
${
fmt_headers
}
${
non_error_test_content
}
"
)
set
(
cmake_targets
"
${
cmake_targets
}
add_library(non-error-test non_error_test.cc)
target_link_libraries(non-error-test PRIVATE fmt::fmt)
"
)
file
(
WRITE
"
${
CMAKE_CURRENT_BINARY_DIR
}
/test/CMakeLists.txt"
"
cmake_minimum_required(VERSION 3.1...3.18)
project(tests CXX)
add_subdirectory(
${
FMT_DIR
}
fmt)
${
cmake_targets
}
"
)
set
(
build_directory
"
${
CMAKE_CURRENT_BINARY_DIR
}
/test/build"
)
file
(
MAKE_DIRECTORY
"
${
build_directory
}
"
)
execute_process
(
COMMAND
"
${
CMAKE_COMMAND
}
"
"-DCMAKE_CXX_COMPILER=
${
CMAKE_CXX_COMPILER
}
"
"-DCMAKE_CXX_FLAGS=
${
CMAKE_CXX_FLAGS
}
"
"-DCMAKE_CXX_STANDARD=
${
CMAKE_CXX_STANDARD
}
"
"-DCMAKE_GENERATOR=
${
CMAKE_GENERATOR
}
"
"-DCMAKE_MAKE_PROGRAM=
${
CMAKE_MAKE_PROGRAM
}
"
"-DFMT_DIR=
${
FMT_DIR
}
"
"
${
CMAKE_CURRENT_BINARY_DIR
}
/test"
WORKING_DIRECTORY
"
${
build_directory
}
"
RESULT_VARIABLE result_var
OUTPUT_VARIABLE output_var
ERROR_VARIABLE output_var
)
if
(
NOT result_var EQUAL 0
)
message
(
FATAL_ERROR
"Unable to configure:
\n
${
output_var
}
"
)
endif
()
foreach
(
test_name IN LISTS error_test_names
)
execute_process
(
COMMAND
"
${
CMAKE_COMMAND
}
"
--build
"
${
build_directory
}
"
--target
"test-
${
test_name
}
"
WORKING_DIRECTORY
"
${
build_directory
}
"
RESULT_VARIABLE result_var
OUTPUT_VARIABLE output_var
ERROR_QUIET
)
if
(
result_var EQUAL 0
)
message
(
SEND_ERROR
"No compile error for
\"
${
test_name
}
\"
:
\n
${
output_var
}
"
)
endif
()
endforeach
()
execute_process
(
COMMAND
"
${
CMAKE_COMMAND
}
"
--build
"
${
build_directory
}
"
--target
"non-error-test"
WORKING_DIRECTORY
"
${
build_directory
}
"
RESULT_VARIABLE result_var
OUTPUT_VARIABLE output_var
ERROR_VARIABLE output_var
)
if
(
NOT result_var EQUAL 0
)
message
(
SEND_ERROR
"Compile error for combined non-error test:
\n
${
output_var
}
"
)
endif
()
endif
()
endfunction
()
endfunction
()
# check if the source file skeleton compiles
# check if the source file skeleton compiles
expect_compile
(
""
)
expect_compile
(
check
""
)
# Formatting a wide character with a narrow format string is forbidden.
# Formatting a wide character with a narrow format string is forbidden.
expect_compile
_error
(
"fmt::format(
\"
{}
\"
, L'a');"
)
expect_compile
(
wide-character-narrow-format-string
"fmt::format(
\"
{}
\"
, L'a');"
ERROR
)
# Formatting a wide string with a narrow format string is forbidden.
# Formatting a wide string with a narrow format string is forbidden.
expect_compile
_error
(
"fmt::format(
\"
{}
\"
, L
\"
foo
\"
);"
)
expect_compile
(
wide-string-narrow-format-string
"fmt::format(
\"
{}
\"
, L
\"
foo
\"
);"
ERROR
)
# Formatting a narrow string with a wide format string is forbidden because
# Formatting a narrow string with a wide format string is forbidden because
# mixing UTF-8 with UTF-16/32 can result in an invalid output.
# mixing UTF-8 with UTF-16/32 can result in an invalid output.
expect_compile
_error
(
"fmt::format(L
\"
{}
\"
,
\"
foo
\"
);"
)
expect_compile
(
narrow-string-wide-format-string
"fmt::format(L
\"
{}
\"
,
\"
foo
\"
);"
ERROR
)
# Formatting a wide string with a narrow format string is forbidden.
# Formatting a wide string with a narrow format string is forbidden.
expect_compile
_error
(
"
expect_compile
(
cast-to-string
"
struct S {
struct S {
operator std::string() const { return std::string(); }
operator std::string() const { return std::string(); }
};
};
fmt::format(
\"
{}
\"
, S());
fmt::format(
\"
{}
\"
, S());
"
)
"
ERROR
)
# Formatting a function
# Formatting a function
expect_compile
_error
(
"
expect_compile
(
format-function
"
void (*f)();
void (*f)();
fmt::format(
\"
{}
\"
, f);
fmt::format(
\"
{}
\"
, f);
"
)
"
ERROR
)
# Make sure that compiler features detected in the header
# Make sure that compiler features detected in the header
# match the features detected in CMake.
# match the features detected in CMake.
...
@@ -80,6 +148,11 @@ if (SUPPORTS_USER_DEFINED_LITERALS)
...
@@ -80,6 +148,11 @@ if (SUPPORTS_USER_DEFINED_LITERALS)
else
()
else
()
set
(
supports_udl 0
)
set
(
supports_udl 0
)
endif
()
endif
()
expect_compile
(
"#if FMT_USE_USER_DEFINED_LITERALS !=
${
supports_udl
}
expect_compile
(
udl-check
"
# error
#if FMT_USE_USER_DEFINED_LITERALS !=
${
supports_udl
}
#endif"
)
# error
#endif
"
)
# Run all tests
run_tests
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment