Skip to content

Commit eb83fee

Browse files
authored
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
1 parent 87f5aff commit eb83fee

File tree

8 files changed

+41
-9
lines changed

8 files changed

+41
-9
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
7373
set(CMAKE_CXX_EXTENSIONS OFF)
7474
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7575
endif()
76+
77+
set(pybind11_system "")
7678
else()
7779
set(PYBIND11_MASTER_PROJECT OFF)
7880
set(pybind11_system SYSTEM)

tests/CMakeLists.txt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ include(CMakeParseArguments)
2424
# Usage:
2525
# pybind11_filter_tests(LISTNAME file1.cpp file2.cpp ... MESSAGE "")
2626
#
27-
macro(PYBIND11_FILTER_TESTS LISTNAME)
27+
macro(pybind11_filter_tests LISTNAME)
2828
cmake_parse_arguments(ARG "" "MESSAGE" "" ${ARGN})
2929
set(PYBIND11_FILTER_TESTS_FOUND OFF)
3030
foreach(filename IN LISTS ARG_UNPARSED_ARGUMENTS)
@@ -39,6 +39,14 @@ macro(PYBIND11_FILTER_TESTS LISTNAME)
3939
endif()
4040
endmacro()
4141

42+
macro(possibly_uninitialized)
43+
foreach(VARNAME ${ARGN})
44+
if(NOT DEFINED "${VARNAME}")
45+
set("${VARNAME}" "")
46+
endif()
47+
endforeach()
48+
endmacro()
49+
4250
# New Python support
4351
if(DEFINED Python_EXECUTABLE)
4452
set(PYTHON_EXECUTABLE "${Python_EXECUTABLE}")
@@ -67,7 +75,7 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
6775
find_package(pybind11 REQUIRED CONFIG)
6876
endif()
6977

70-
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
78+
if(NOT CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
7179
message(STATUS "Setting tests build type to MinSizeRel as none was specified")
7280
set(CMAKE_BUILD_TYPE
7381
MinSizeRel
@@ -345,11 +353,14 @@ foreach(target ${test_targets})
345353
if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
346354
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
347355
"${CMAKE_CURRENT_BINARY_DIR}")
348-
foreach(config ${CMAKE_CONFIGURATION_TYPES})
349-
string(TOUPPER ${config} config)
350-
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
351-
"${CMAKE_CURRENT_BINARY_DIR}")
352-
endforeach()
356+
357+
if(DEFINED CMAKE_CONFIGURATION_TYPES)
358+
foreach(config ${CMAKE_CONFIGURATION_TYPES})
359+
string(TOUPPER ${config} config)
360+
set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config}
361+
"${CMAKE_CURRENT_BINARY_DIR}")
362+
endforeach()
363+
endif()
353364
endif()
354365
endforeach()
355366

tests/test_cmake_build/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ function(pybind11_add_build_test name)
5555
add_dependencies(test_cmake_build test_build_${name})
5656
endfunction()
5757

58+
possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID)
59+
5860
pybind11_add_build_test(subdirectory_function)
5961
pybind11_add_build_test(subdirectory_target)
6062
if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy")

tests/test_embed/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
possibly_uninitialized(PYTHON_MODULE_EXTENSION Python_INTERPRETER_ID)
2+
13
if("${PYTHON_MODULE_EXTENSION}" MATCHES "pypy" OR "${Python_INTERPRETER_ID}" STREQUAL "PyPy")
4+
message(STATUS "Skipping embed test on PyPy")
25
add_custom_target(cpptest) # Dummy target on PyPy. Embedding is not supported.
36
set(_suppress_unused_variable_warning "${DOWNLOAD_CATCH}")
47
return()

tools/FindEigen3.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ if(EIGEN3_INCLUDE_DIR)
6464
set(EIGEN3_FOUND ${EIGEN3_VERSION_OK})
6565

6666
else(EIGEN3_INCLUDE_DIR)
67+
if(NOT DEFINED KDE4_INCLUDE_DIR)
68+
set(KDE4_INCLUDE_DIR "")
69+
endif()
6770

6871
find_path(
6972
EIGEN3_INCLUDE_DIR

tools/FindPythonLibsNew.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ endif()
5757

5858
if(PythonLibsNew_FIND_QUIETLY)
5959
set(_pythonlibs_quiet QUIET)
60+
else()
61+
set(_pythonlibs_quiet "")
6062
endif()
6163

6264
if(PythonLibsNew_FIND_REQUIRED)

tools/pybind11NewTools.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ get_property(
1212

1313
if(pybind11_FIND_QUIETLY)
1414
set(_pybind11_quiet QUIET)
15+
else()
16+
set(_pybind11_quiet "")
1517
endif()
1618

1719
if(CMAKE_VERSION VERSION_LESS 3.12)

tools/pybind11Tools.cmake

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ include(CMakeParseArguments)
1010

1111
if(pybind11_FIND_QUIETLY)
1212
set(_pybind11_quiet QUIET)
13+
else()
14+
set(_pybind11_quiet "")
1315
endif()
1416

1517
# 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)
2224
CACHE STRING "Python version to use for compiling modules")
2325
unset(PYTHON_VERSION)
2426
unset(PYTHON_VERSION CACHE)
25-
else()
26-
# If this is set as a normal variable, promote it, otherwise, make an empty cache variable.
27+
elseif(DEFINED PYBIND11_PYTHON_VERSION)
28+
# If this is set as a normal variable, promote it
2729
set(PYBIND11_PYTHON_VERSION
2830
"${PYBIND11_PYTHON_VERSION}"
2931
CACHE STRING "Python version to use for compiling modules")
32+
else()
33+
# Make an empty cache variable.
34+
set(PYBIND11_PYTHON_VERSION
35+
""
36+
CACHE STRING "Python version to use for compiling modules")
3037
endif()
3138

3239
# A user can set versions manually too

0 commit comments

Comments
 (0)