From eb83feefff14e6b805e46b795b133f2523d0d585 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 19 Jan 2021 18:48:22 -0500 Subject: [PATCH] style: avoid using unintialized variables (#2806) * style: avoid using unintialized variables Tested with cmake --warn-unintialized -S . -B build * refactor: use function for possibly uninit vars --- CMakeLists.txt | 2 ++ tests/CMakeLists.txt | 25 ++++++++++++++++++------- tests/test_cmake_build/CMakeLists.txt | 2 ++ tests/test_embed/CMakeLists.txt | 3 +++ tools/FindEigen3.cmake | 3 +++ tools/FindPythonLibsNew.cmake | 2 ++ tools/pybind11NewTools.cmake | 2 ++ tools/pybind11Tools.cmake | 11 +++++++++-- 8 files changed, 41 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c08ff0b9..cb1506e73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,8 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR) set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) endif() + + set(pybind11_system "") else() set(PYBIND11_MASTER_PROJECT OFF) set(pybind11_system SYSTEM) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index cf1a984f0..3bfd5f1ca 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -24,7 +24,7 @@ include(CMakeParseArguments) # Usage: # pybind11_filter_tests(LISTNAME file1.cpp file2.cpp ... MESSAGE "") # -macro(PYBIND11_FILTER_TESTS LISTNAME) +macro(pybind11_filter_tests LISTNAME) cmake_parse_arguments(ARG "" "MESSAGE" "" ${ARGN}) set(PYBIND11_FILTER_TESTS_FOUND OFF) foreach(filename IN LISTS ARG_UNPARSED_ARGUMENTS) @@ -39,6 +39,14 @@ macro(PYBIND11_FILTER_TESTS LISTNAME) endif() endmacro() +macro(possibly_uninitialized) + foreach(VARNAME ${ARGN}) + if(NOT DEFINED "${VARNAME}") + set("${VARNAME}" "") + endif() + endforeach() +endmacro() + # New Python support if(DEFINED Python_EXECUTABLE) set(PYTHON_EXECUTABLE "${Python_EXECUTABLE}") @@ -67,7 +75,7 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) find_package(pybind11 REQUIRED CONFIG) endif() -if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) +if(NOT CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES) message(STATUS "Setting tests build type to MinSizeRel as none was specified") set(CMAKE_BUILD_TYPE MinSizeRel @@ -345,11 +353,14 @@ foreach(target ${test_targets}) if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") - foreach(config ${CMAKE_CONFIGURATION_TYPES}) - string(TOUPPER ${config} config) - set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} - "${CMAKE_CURRENT_BINARY_DIR}") - endforeach() + + if(DEFINED CMAKE_CONFIGURATION_TYPES) + foreach(config ${CMAKE_CONFIGURATION_TYPES}) + string(TOUPPER ${config} config) + set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} + "${CMAKE_CURRENT_BINARY_DIR}") + endforeach() + endif() endif() endforeach() diff --git a/tests/test_cmake_build/CMakeLists.txt b/tests/test_cmake_build/CMakeLists.txt index 0c0578ad3..cd39b0b03 100644 --- a/tests/test_cmake_build/CMakeLists.txt +++ b/tests/test_cmake_build/CMakeLists.txt @@ -55,6 +55,8 @@ function(pybind11_add_build_test name) add_dependencies(test_cmake_build test_build_${name}) endfunction() +possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID) + pybind11_add_build_test(subdirectory_function) pybind11_add_build_test(subdirectory_target) if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy") diff --git a/tests/test_embed/CMakeLists.txt b/tests/test_embed/CMakeLists.txt index fabcb24ea..c960c877a 100644 --- a/tests/test_embed/CMakeLists.txt +++ b/tests/test_embed/CMakeLists.txt @@ -1,4 +1,7 @@ +possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID) + if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy") + message(STATUS "Skipping embed test on PyPy") add_custom_target(cpptest) # Dummy target on PyPy. Embedding is not supported. set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}") return() diff --git a/tools/FindEigen3.cmake b/tools/FindEigen3.cmake index 98ab43d9e..83625d92e 100644 --- a/tools/FindEigen3.cmake +++ b/tools/FindEigen3.cmake @@ -64,6 +64,9 @@ if(EIGEN3_INCLUDE_DIR) set(EIGEN3_FOUND ${EIGEN3_VERSION_OK}) else(EIGEN3_INCLUDE_DIR) + if(NOT DEFINED KDE4_INCLUDE_DIR) + set(KDE4_INCLUDE_DIR "") + endif() find_path( EIGEN3_INCLUDE_DIR diff --git a/tools/FindPythonLibsNew.cmake b/tools/FindPythonLibsNew.cmake index b7125912c..3605aebcf 100644 --- a/tools/FindPythonLibsNew.cmake +++ b/tools/FindPythonLibsNew.cmake @@ -57,6 +57,8 @@ endif() if(PythonLibsNew_FIND_QUIETLY) set(_pythonlibs_quiet QUIET) +else() + set(_pythonlibs_quiet "") endif() if(PythonLibsNew_FIND_REQUIRED) diff --git a/tools/pybind11NewTools.cmake b/tools/pybind11NewTools.cmake index 4dc91a191..18da8be17 100644 --- a/tools/pybind11NewTools.cmake +++ b/tools/pybind11NewTools.cmake @@ -12,6 +12,8 @@ get_property( if(pybind11_FIND_QUIETLY) set(_pybind11_quiet QUIET) +else() + set(_pybind11_quiet "") endif() if(CMAKE_VERSION VERSION_LESS 3.12) diff --git a/tools/pybind11Tools.cmake b/tools/pybind11Tools.cmake index 3fa452f97..323135399 100644 --- a/tools/pybind11Tools.cmake +++ b/tools/pybind11Tools.cmake @@ -10,6 +10,8 @@ include(CMakeParseArguments) if(pybind11_FIND_QUIETLY) set(_pybind11_quiet QUIET) +else() + set(_pybind11_quiet "") endif() # If this is the first run, PYTHON_VERSION can stand in for PYBIND11_PYTHON_VERSION @@ -22,11 +24,16 @@ if(NOT DEFINED PYBIND11_PYTHON_VERSION AND DEFINED PYTHON_VERSION) CACHE STRING "Python version to use for compiling modules") unset(PYTHON_VERSION) unset(PYTHON_VERSION CACHE) -else() - # If this is set as a normal variable, promote it, otherwise, make an empty cache variable. +elseif(DEFINED PYBIND11_PYTHON_VERSION) + # If this is set as a normal variable, promote it set(PYBIND11_PYTHON_VERSION "${PYBIND11_PYTHON_VERSION}" CACHE STRING "Python version to use for compiling modules") +else() + # Make an empty cache variable. + set(PYBIND11_PYTHON_VERSION + "" + CACHE STRING "Python version to use for compiling modules") endif() # A user can set versions manually too