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
This commit is contained in:
Henry Schreiner 2021-01-19 18:48:22 -05:00 committed by GitHub
parent 87f5aff4a7
commit eb83feefff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 9 deletions

View File

@ -73,6 +73,8 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif() endif()
set(pybind11_system "")
else() else()
set(PYBIND11_MASTER_PROJECT OFF) set(PYBIND11_MASTER_PROJECT OFF)
set(pybind11_system SYSTEM) set(pybind11_system SYSTEM)

View File

@ -24,7 +24,7 @@ include(CMakeParseArguments)
# Usage: # Usage:
# pybind11_filter_tests(LISTNAME file1.cpp file2.cpp ... MESSAGE "") # 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}) cmake_parse_arguments(ARG "" "MESSAGE" "" ${ARGN})
set(PYBIND11_FILTER_TESTS_FOUND OFF) set(PYBIND11_FILTER_TESTS_FOUND OFF)
foreach(filename IN LISTS ARG_UNPARSED_ARGUMENTS) foreach(filename IN LISTS ARG_UNPARSED_ARGUMENTS)
@ -39,6 +39,14 @@ macro(PYBIND11_FILTER_TESTS LISTNAME)
endif() endif()
endmacro() endmacro()
macro(possibly_uninitialized)
foreach(VARNAME ${ARGN})
if(NOT DEFINED "${VARNAME}")
set("${VARNAME}" "")
endif()
endforeach()
endmacro()
# New Python support # New Python support
if(DEFINED Python_EXECUTABLE) if(DEFINED Python_EXECUTABLE)
set(PYTHON_EXECUTABLE "${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) find_package(pybind11 REQUIRED CONFIG)
endif() 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") message(STATUS "Setting tests build type to MinSizeRel as none was specified")
set(CMAKE_BUILD_TYPE set(CMAKE_BUILD_TYPE
MinSizeRel MinSizeRel
@ -345,12 +353,15 @@ foreach(target ${test_targets})
if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
"${CMAKE_CURRENT_BINARY_DIR}") "${CMAKE_CURRENT_BINARY_DIR}")
if(DEFINED CMAKE_CONFIGURATION_TYPES)
foreach(config ${CMAKE_CONFIGURATION_TYPES}) foreach(config ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${config} config) string(TOUPPER ${config} config)
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
"${CMAKE_CURRENT_BINARY_DIR}") "${CMAKE_CURRENT_BINARY_DIR}")
endforeach() endforeach()
endif() endif()
endif()
endforeach() endforeach()
# Make sure pytest is found or produce a warning # Make sure pytest is found or produce a warning

View File

@ -55,6 +55,8 @@ function(pybind11_add_build_test name)
add_dependencies(test_cmake_build test_build_${name}) add_dependencies(test_cmake_build test_build_${name})
endfunction() endfunction()
possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID)
pybind11_add_build_test(subdirectory_function) pybind11_add_build_test(subdirectory_function)
pybind11_add_build_test(subdirectory_target) pybind11_add_build_test(subdirectory_target)
if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy") if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy")

View File

@ -1,4 +1,7 @@
possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID)
if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy") 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. add_custom_target(cpptest) # Dummy target on PyPy. Embedding is not supported.
set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}") set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}")
return() return()

View File

@ -64,6 +64,9 @@ if(EIGEN3_INCLUDE_DIR)
set(EIGEN3_FOUND ${EIGEN3_VERSION_OK}) set(EIGEN3_FOUND ${EIGEN3_VERSION_OK})
else(EIGEN3_INCLUDE_DIR) else(EIGEN3_INCLUDE_DIR)
if(NOT DEFINED KDE4_INCLUDE_DIR)
set(KDE4_INCLUDE_DIR "")
endif()
find_path( find_path(
EIGEN3_INCLUDE_DIR EIGEN3_INCLUDE_DIR

View File

@ -57,6 +57,8 @@ endif()
if(PythonLibsNew_FIND_QUIETLY) if(PythonLibsNew_FIND_QUIETLY)
set(_pythonlibs_quiet QUIET) set(_pythonlibs_quiet QUIET)
else()
set(_pythonlibs_quiet "")
endif() endif()
if(PythonLibsNew_FIND_REQUIRED) if(PythonLibsNew_FIND_REQUIRED)

View File

@ -12,6 +12,8 @@ get_property(
if(pybind11_FIND_QUIETLY) if(pybind11_FIND_QUIETLY)
set(_pybind11_quiet QUIET) set(_pybind11_quiet QUIET)
else()
set(_pybind11_quiet "")
endif() endif()
if(CMAKE_VERSION VERSION_LESS 3.12) if(CMAKE_VERSION VERSION_LESS 3.12)

View File

@ -10,6 +10,8 @@ include(CMakeParseArguments)
if(pybind11_FIND_QUIETLY) if(pybind11_FIND_QUIETLY)
set(_pybind11_quiet QUIET) set(_pybind11_quiet QUIET)
else()
set(_pybind11_quiet "")
endif() endif()
# If this is the first run, PYTHON_VERSION can stand in for PYBIND11_PYTHON_VERSION # 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") CACHE STRING "Python version to use for compiling modules")
unset(PYTHON_VERSION) unset(PYTHON_VERSION)
unset(PYTHON_VERSION CACHE) unset(PYTHON_VERSION CACHE)
else() elseif(DEFINED PYBIND11_PYTHON_VERSION)
# If this is set as a normal variable, promote it, otherwise, make an empty cache variable. # If this is set as a normal variable, promote it
set(PYBIND11_PYTHON_VERSION set(PYBIND11_PYTHON_VERSION
"${PYBIND11_PYTHON_VERSION}" "${PYBIND11_PYTHON_VERSION}"
CACHE STRING "Python version to use for compiling modules") 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() endif()
# A user can set versions manually too # A user can set versions manually too