diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2024-04-18 17:13:09 +0200 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2024-04-18 17:13:09 +0200 |
| commit | bb45198413ef8efe236afbd665a4623239ac0da0 (patch) | |
| tree | 9b769652a1eec4c0b0a26a1334e72f7b65de5633 /test/CMakeLists.txt | |
| parent | 2c21e520f4859904739deb7d058b084f314ed47d (diff) | |
Test public headers for shadowed variables (STR 2728)
This program is built only when using CMake and a GNU or Clang
compiler to detect shadowed variables in header files.
Developers should fix such warnings whenever they see them during the
build to avoid user reports.
Diffstat (limited to 'test/CMakeLists.txt')
| -rw-r--r-- | test/CMakeLists.txt | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5ff5cff78..8f965adf2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -216,6 +216,48 @@ if(OPENGL_FOUND) fl_create_example(shape shape.cxx "${GLDEMO_LIBS}") endif(OPENGL_FOUND) +# Test FLTK headers for variable shadowing [-Wshadow] (STR 2728) +# +# The following test program is added only for GNU or Clang compilers which +# are known to support the '-Wshadow' compiler flag. We could also implement +# a compile test but using GNU and Clang appears to be sufficient. +# +# Note 1: usage of `file(GLOB ...)` is discouraged by CMake documentation because +# it doesn't trigger rebuilding when new header files are added. However, this +# is better than nothing. +# +# Note 2: a better way to generate the header file might be with a custom command +# to be able to generate proper dependencies but this is left for later... +# +# Note 3: this test program is only included in CMake builds and was inspired +# by STR 2728: "Add test program to test for shadowed variables (-Wshadow)". +# Its sole purpose is to issue compilation warnings during build time if +# variables are shadowed in public headers. + +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + + set(include_all "${CMAKE_CURRENT_BINARY_DIR}/include_all.h") + file(WRITE ${include_all} "/* DO NOT EDIT - this file is created by CMake */\n") + + file(GLOB all_headers "${FLTK_SOURCE_DIR}/FL/*.[hH]") + foreach(hd ${all_headers}) + get_filename_component(hd "${hd}" NAME) + # skip headers that must not be included directly and fl_config.h if it exists + string(REGEX MATCH "^(mac|win32|wayland|fl_config)\.[hH]$" skip "${hd}") + if(skip STREQUAL "") + file(APPEND ${include_all} "#include <FL/${hd}>\n") + endif() + endforeach() + file(APPEND ${include_all} "/* End of generated file */\n") + + # now add the 'shadow_variables' target and set the compiler flag + + fl_create_example(shadow_variables shadow_variables.cxx fltk::fltk) + set_target_properties(shadow_variables PROPERTIES COMPILE_FLAGS -Wshadow) + +endif() # GNU or Clang (-Wshadow test) + + # *** EXPERIMENTAL *** # Build some of the demo programs linked against the shared FLTK lib(s). # This is currently pretty complicated and should be improved. |
