From e38fc6de8ace95299595ff7963a02f9dfa712cd2 Mon Sep 17 00:00:00 2001 From: Daniel Leung Date: Fri, 11 Aug 2023 13:40:05 -0700 Subject: [PATCH] cmake: enable -Wshadow partially for in-tree code This enables -Wshadow to warn about shadow variables on in tree code under arch/, boards/, drivers/, kernel/, lib/, soc/, and subsys/. Note that this does not enable it globally because out-of-tree modules will probably take some time to fix (or not at all depending on the project), and it would be great to avoid introduction of any new shadow variables in the meantime. Also note that this tries to be done in a minimally invasive way so it is easy to revert when we enable -Wshadow globally. Source files under modules/, samples/ and tests/ are currently excluded because there does not seem to be a trivial way to add -Wshadow there without going through all CMakeLists.txt to add the option (as there are 1000+ files to change). Signed-off-by: Daniel Leung --- CMakeLists.txt | 7 +------ arch/CMakeLists.txt | 3 +++ boards/CMakeLists.txt | 5 +++++ cmake/compiler/arcmwdt/compiler_flags.cmake | 3 +++ cmake/compiler/armclang/compiler_flags.cmake | 3 +++ cmake/compiler/compiler_flags_template.cmake | 3 +++ cmake/compiler/gcc/compiler_flags.cmake | 2 ++ cmake/compiler/icx/compiler_flags.cmake | 3 +++ cmake/compiler/xcc/compiler_flags.cmake | 3 +++ cmake/compiler/xt-clang/compiler_flags.cmake | 3 +++ drivers/CMakeLists.txt | 3 +++ kernel/CMakeLists.txt | 3 +++ lib/CMakeLists.txt | 3 +++ soc/CMakeLists.txt | 16 ++++++++++++++++ subsys/CMakeLists.txt | 12 +++++++++++- 15 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 soc/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e2ede86fd9..5f6f13f426c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -603,12 +603,7 @@ add_subdirectory(lib) # property which is set implicitly for custom command outputs include(misc/generated/CMakeLists.txt) -if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt) - add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH}) -else() - add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH}) -endif() - +add_subdirectory(soc) add_subdirectory(boards) add_subdirectory(subsys) add_subdirectory(drivers) diff --git a/arch/CMakeLists.txt b/arch/CMakeLists.txt index ef30d760bb8..e93b810fb05 100644 --- a/arch/CMakeLists.txt +++ b/arch/CMakeLists.txt @@ -1,5 +1,8 @@ # SPDX-License-Identifier: Apache-2.0 +# FIXME: SHADOW_VARS: Remove this once we have enabled -Wshadow globally. +add_compile_options($) + add_definitions(-D__ZEPHYR_SUPERVISOR__) include_directories( diff --git a/boards/CMakeLists.txt b/boards/CMakeLists.txt index 943e52667e2..02e9e158bb8 100644 --- a/boards/CMakeLists.txt +++ b/boards/CMakeLists.txt @@ -8,6 +8,11 @@ if(EXISTS ${BOARD_DIR}/CMakeLists.txt) set(build_dir boards/${ARCH}/${BOARD}) else() unset(build_dir) + + # FIXME: SHADOW_VARS: Remove this once we have enabled -Wshadow globally. + # + # For now, only enable warning for shadow variables for in-tree boards. + add_compile_options($) endif() add_subdirectory(${BOARD_DIR} ${build_dir}) diff --git a/cmake/compiler/arcmwdt/compiler_flags.cmake b/cmake/compiler/arcmwdt/compiler_flags.cmake index 2b85dadf5a7..8eeaf2fa37a 100644 --- a/cmake/compiler/arcmwdt/compiler_flags.cmake +++ b/cmake/compiler/arcmwdt/compiler_flags.cmake @@ -203,3 +203,6 @@ if(CONFIG_ARCMWDT_LIBC) # to ASM builds (which may use 'stdbool.h'). set_property(TARGET asm APPEND PROPERTY required "-I${NOSTDINC}") endif() + +# Remove after testing that -Wshadow works +set_compiler_property(PROPERTY warning_shadow_variables) diff --git a/cmake/compiler/armclang/compiler_flags.cmake b/cmake/compiler/armclang/compiler_flags.cmake index 3d0f713da4f..8cb3c46ae0e 100644 --- a/cmake/compiler/armclang/compiler_flags.cmake +++ b/cmake/compiler/armclang/compiler_flags.cmake @@ -7,3 +7,6 @@ set_property(TARGET asm APPEND PROPERTY required "--target=${triple}") # Only the ARM Compiler C library is currently supported. set_compiler_property(PROPERTY nostdinc) + +# Remove after testing that -Wshadow works +set_compiler_property(PROPERTY warning_shadow_variables) diff --git a/cmake/compiler/compiler_flags_template.cmake b/cmake/compiler/compiler_flags_template.cmake index 5f15d1b8007..1476c45e851 100644 --- a/cmake/compiler/compiler_flags_template.cmake +++ b/cmake/compiler/compiler_flags_template.cmake @@ -130,3 +130,6 @@ set_compiler_property(PROPERTY no_position_independent) # gen_kobject_list.py is does not understand it and end up identifying objects as if # they had the same address. set_compiler_property(PROPERTY no_global_merge) + +# Compiler flag for warning about shadow variables +set_compiler_property(PROPERTY warning_shadow_variables) diff --git a/cmake/compiler/gcc/compiler_flags.cmake b/cmake/compiler/gcc/compiler_flags.cmake index 741d49074f6..b77dfa4123f 100644 --- a/cmake/compiler/gcc/compiler_flags.cmake +++ b/cmake/compiler/gcc/compiler_flags.cmake @@ -225,3 +225,5 @@ set_compiler_property(PROPERTY no_position_independent ) set_compiler_property(PROPERTY no_global_merge "") + +set_compiler_property(PROPERTY warning_shadow_variables -Wshadow) diff --git a/cmake/compiler/icx/compiler_flags.cmake b/cmake/compiler/icx/compiler_flags.cmake index 5476c6603fc..1e038caaf93 100644 --- a/cmake/compiler/icx/compiler_flags.cmake +++ b/cmake/compiler/icx/compiler_flags.cmake @@ -1,2 +1,5 @@ include(${ZEPHYR_BASE}/cmake/compiler/clang/compiler_flags.cmake) + +# Remove after testing that -Wshadow works +set_compiler_property(PROPERTY warning_shadow_variables) diff --git a/cmake/compiler/xcc/compiler_flags.cmake b/cmake/compiler/xcc/compiler_flags.cmake index d6affa48ad4..28f76d5d809 100644 --- a/cmake/compiler/xcc/compiler_flags.cmake +++ b/cmake/compiler/xcc/compiler_flags.cmake @@ -12,3 +12,6 @@ set_compiler_property(PROPERTY warning_error_misra_sane) # XCC does not support -fno-pic and -fno-pie set_compiler_property(PROPERTY no_position_independent "") + +# Remove after testing that -Wshadow works +set_compiler_property(PROPERTY warning_shadow_variables) diff --git a/cmake/compiler/xt-clang/compiler_flags.cmake b/cmake/compiler/xt-clang/compiler_flags.cmake index 62c50316b06..593e3842843 100644 --- a/cmake/compiler/xt-clang/compiler_flags.cmake +++ b/cmake/compiler/xt-clang/compiler_flags.cmake @@ -31,3 +31,6 @@ endif() # Clang version used by Xtensa does not support -fno-pic and -fno-pie set_compiler_property(PROPERTY no_position_independent "") + +# Remove after testing that -Wshadow works +set_compiler_property(PROPERTY warning_shadow_variables) diff --git a/drivers/CMakeLists.txt b/drivers/CMakeLists.txt index a9caf9f0320..749285d29a3 100644 --- a/drivers/CMakeLists.txt +++ b/drivers/CMakeLists.txt @@ -1,5 +1,8 @@ # SPDX-License-Identifier: Apache-2.0 +# FIXME: SHADOW_VARS: Remove this once we have enabled -Wshadow globally. +add_compile_options($) + add_definitions(-D__ZEPHYR_SUPERVISOR__) add_subdirectory(disk) diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 9e7602bfbd8..087e96b0812 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -38,6 +38,9 @@ target_link_libraries(kernel INTERFACE ${libkernel}) else() +# FIXME: SHADOW_VARS: Remove this once we have enabled -Wshadow globally. +add_compile_options($) + list(APPEND kernel_files main_weak.c banner.c diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index dec4150c9ce..6fe18f63992 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,5 +1,8 @@ # SPDX-License-Identifier: Apache-2.0 +# FIXME: SHADOW_VARS: Remove this once we have enabled -Wshadow globally. +add_compile_options($) + add_subdirectory(crc) if(NOT CONFIG_EXTERNAL_LIBC) add_subdirectory(libc) diff --git a/soc/CMakeLists.txt b/soc/CMakeLists.txt new file mode 100644 index 00000000000..6706168281e --- /dev/null +++ b/soc/CMakeLists.txt @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: Apache-2.0 + +# FIXME: SHADOW_VARS: Remove this once we have enabled -Wshadow globally. +# +# Limit warning of shadow variables to in-tree SoC files for now. +cmake_path(IS_PREFIX ZEPHYR_BASE "${SOC_DIR}" NORMALIZE _SOC_IS_IN_TREE) +if(_SOC_IS_IN_TREE) + add_compile_options($) +endif() +unset(_SOC_IS_IN_TREE) + +if(EXISTS ${SOC_DIR}/${ARCH}/CMakeLists.txt) + add_subdirectory(${SOC_DIR}/${ARCH} soc/${ARCH}) +else() + add_subdirectory(${SOC_DIR}/${ARCH}/${SOC_PATH} soc/${ARCH}/${SOC_PATH}) +endif() diff --git a/subsys/CMakeLists.txt b/subsys/CMakeLists.txt index de5c85f51bb..3c508d6e8e1 100644 --- a/subsys/CMakeLists.txt +++ b/subsys/CMakeLists.txt @@ -1,5 +1,16 @@ # SPDX-License-Identifier: Apache-2.0 +# FIXME: SHADOW_VARS: move this before adding shadow variable warning below. +# This is because, in some build configurations, the external lorawan module +# is pulled in as though the source files are in main repo. This results in +# shadow variable warnings being active on these files. Until the module has +# fixed those shadow variables, keep this here before add_compile_options() +# below. +add_subdirectory_ifdef(CONFIG_LORAWAN lorawan) + +# FIXME: SHADOW_VARS: Remove this once we have enabled -Wshadow globally. +add_compile_options($) + add_subdirectory(canbus) add_subdirectory(debug) add_subdirectory(fb) @@ -29,7 +40,6 @@ add_subdirectory_ifdef(CONFIG_EMUL emul) add_subdirectory_ifdef(CONFIG_IMG_MANAGER dfu) add_subdirectory_ifdef(CONFIG_INPUT input) add_subdirectory_ifdef(CONFIG_JWT jwt) -add_subdirectory_ifdef(CONFIG_LORAWAN lorawan) add_subdirectory_ifdef(CONFIG_NET_BUF net) add_subdirectory_ifdef(CONFIG_RETENTION retention) add_subdirectory_ifdef(CONFIG_SENSING sensing)