From 4286ac7ce8ffce0b61f13b672b9f1f27e16fb8bb Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 5 Oct 2023 19:05:57 -0400 Subject: [PATCH] Enable -Wundef by default. (#29582) * Enable -Wundef by default on some platforms. * Fixes https://github.com/project-chip/connectedhomeip/issues/29216 * Fix a bunch more errors. * Restrict -Wundef to where it actually passes CI. --- build/config/compiler/BUILD.gn | 14 ++++++++++++++ .../commands/common/MTRLogging.h | 2 +- examples/platform/linux/BUILD.gn | 4 +--- .../tv-app/tv-common/src/ZCLCallbacks.cpp | 1 + .../tv-casting-app/tv-casting-common/BUILD.gn | 2 ++ .../tv-casting-common/include/AppParams.h | 1 + src/app/tests/TestDataModelSerialization.cpp | 2 +- src/include/platform/CHIPDeviceConfig.h | 8 ++++++++ src/inet/InetInterface.cpp | 6 +++--- src/inet/UDPEndPointImplSockets.cpp | 8 ++++---- src/inet/tests/TestInetAddress.cpp | 5 +++-- src/inet/tests/TestInetLayer.cpp | 4 ++-- src/inet/tests/TestLwIPDNS.cpp | 19 ++++++++++--------- src/lib/support/CHIPArgParser.hpp | 4 ++++ src/lib/support/Pool.h | 10 +++++++--- src/lib/support/UnitTestRegistration.cpp | 4 ++-- src/platform/tests/TestCHIPoBLEStackMgr.h | 2 ++ .../tests/TestCHIPoBLEStackMgrDriver.cpp | 1 + src/system/SystemClock.cpp | 2 +- src/system/SystemPacketBufferInternal.h | 4 ++++ 20 files changed, 72 insertions(+), 31 deletions(-) diff --git a/build/config/compiler/BUILD.gn b/build/config/compiler/BUILD.gn index c508d657b17ab2..9f34a730563d4a 100644 --- a/build/config/compiler/BUILD.gn +++ b/build/config/compiler/BUILD.gn @@ -253,6 +253,20 @@ config("strict_warnings") { cflags += [ "-Wconversion" ] } + # For now we can't enable -Wundef across the board. Enable it where + # we can. Ideally this would be checking chip_device_platform or so + # to be more fine-grained than current_os, but it's not clear that + # we can access that here. + if (current_os != "android" && current_os != "freertos" && + current_os != "linux" && current_os != "mbed" && current_os != "tizen" && + current_os != "zephyr" && + # cmsis-rtos is OpenIOT + current_os != "cmsis-rtos" && + # cyw30739 is one of the Infineon builds + current_os != "cyw30739") { + cflags += [ "-Wundef" ] + } + if (matter_enable_java_compilation) { cflags -= [ "-Wshadow" ] } diff --git a/examples/darwin-framework-tool/commands/common/MTRLogging.h b/examples/darwin-framework-tool/commands/common/MTRLogging.h index 3e120c552a68b5..58e0f6b88b5a67 100644 --- a/examples/darwin-framework-tool/commands/common/MTRLogging.h +++ b/examples/darwin-framework-tool/commands/common/MTRLogging.h @@ -21,7 +21,7 @@ #import -#if DEBUG +#ifdef DEBUG #define MTR_LOG_DEBUG(format, ...) os_log(OS_LOG_DEFAULT, format, ##__VA_ARGS__) #define MTR_LOG_ERROR(format, ...) os_log(OS_LOG_DEFAULT, format, ##__VA_ARGS__) #define MTR_LOG_METHOD_ENTRY() \ diff --git a/examples/platform/linux/BUILD.gn b/examples/platform/linux/BUILD.gn index 44a96b12c00604..46229c5f7ed42c 100644 --- a/examples/platform/linux/BUILD.gn +++ b/examples/platform/linux/BUILD.gn @@ -94,9 +94,7 @@ source_set("app-main") { ] } - if (chip_enable_smoke_co_trigger) { - defines += [ "CHIP_DEVICE_CONFIG_ENABLE_SMOKE_CO_TRIGGER=1" ] - } + defines += [ "CHIP_DEVICE_CONFIG_ENABLE_SMOKE_CO_TRIGGER=${chip_enable_smoke_co_trigger}" ] public_configs = [ ":app-main-config" ] } diff --git a/examples/tv-app/tv-common/src/ZCLCallbacks.cpp b/examples/tv-app/tv-common/src/ZCLCallbacks.cpp index d980811a6c09ad..8522f1f3d71f5a 100644 --- a/examples/tv-app/tv-common/src/ZCLCallbacks.cpp +++ b/examples/tv-app/tv-common/src/ZCLCallbacks.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "account-login/AccountLoginManager.h" #include "application-basic/ApplicationBasicManager.h" diff --git a/examples/tv-casting-app/tv-casting-common/BUILD.gn b/examples/tv-casting-app/tv-casting-common/BUILD.gn index f555a62a181a41..9d4a3d3b4faea3 100644 --- a/examples/tv-casting-app/tv-casting-common/BUILD.gn +++ b/examples/tv-casting-app/tv-casting-common/BUILD.gn @@ -27,6 +27,8 @@ config("config") { ] cflags = [ "-Wconversion" ] + + defines = [ "CONFIG_USE_SEPARATE_EVENTLOOP=0" ] } chip_data_model("tv-casting-common") { diff --git a/examples/tv-casting-app/tv-casting-common/include/AppParams.h b/examples/tv-casting-app/tv-casting-common/include/AppParams.h index 7a42c09b501dda..c981aba8fa0d46 100644 --- a/examples/tv-casting-app/tv-casting-common/include/AppParams.h +++ b/examples/tv-casting-app/tv-casting-common/include/AppParams.h @@ -20,6 +20,7 @@ #include #include +#include /** * @brief Parameters passed to the CastingServer at the time of startup (i.e. init call) diff --git a/src/app/tests/TestDataModelSerialization.cpp b/src/app/tests/TestDataModelSerialization.cpp index 06cf6a17609fe4..d94caed6b6c2d3 100644 --- a/src/app/tests/TestDataModelSerialization.cpp +++ b/src/app/tests/TestDataModelSerialization.cpp @@ -105,7 +105,7 @@ void TestDataModelSerialization::DumpBuf() // // Enable this once the TLV pretty printer has been checked in. // -#if ENABLE_TLV_PRINT_OUT +#if defined(ENABLE_TLV_PRINT_OUT) && ENABLE_TLV_PRINT_OUT TLV::Debug::Print(reader); #endif } diff --git a/src/include/platform/CHIPDeviceConfig.h b/src/include/platform/CHIPDeviceConfig.h index fd8937dcc2302c..e29c00161337db 100644 --- a/src/include/platform/CHIPDeviceConfig.h +++ b/src/include/platform/CHIPDeviceConfig.h @@ -1419,3 +1419,11 @@ #ifndef CHIP_DEVICE_CONFIG_ENABLE_NFC #define CHIP_DEVICE_CONFIG_ENABLE_NFC 0 #endif + +/** + * CHIP_DEVICE_ENABLE_PORT_PARAMS enables command-line parameters to set the + * port to use for POSIX example applications. + */ +#ifndef CHIP_DEVICE_ENABLE_PORT_PARAMS +#define CHIP_DEVICE_ENABLE_PORT_PARAMS 0 +#endif // CHIP_DEVICE_ENABLE_PORT_PARAMS diff --git a/src/inet/InetInterface.cpp b/src/inet/InetInterface.cpp index e1c14dcfde449f..4d30309752408e 100644 --- a/src/inet/InetInterface.cpp +++ b/src/inet/InetInterface.cpp @@ -502,7 +502,7 @@ void CloseIOCTLSocket() } } -#if __ANDROID__ +#ifdef __ANDROID__ static struct if_nameindex * backport_if_nameindex(void); static void backport_if_freenameindex(struct if_nameindex *); @@ -648,7 +648,7 @@ InterfaceIterator::~InterfaceIterator() { if (mIntfArray != nullptr) { -#if __ANDROID__ +#ifdef __ANDROID__ backport_if_freenameindex(mIntfArray); #else if_freenameindex(mIntfArray); @@ -666,7 +666,7 @@ bool InterfaceIterator::Next() { if (mIntfArray == nullptr) { -#if __ANDROID__ +#ifdef __ANDROID__ mIntfArray = backport_if_nameindex(); #else mIntfArray = if_nameindex(); diff --git a/src/inet/UDPEndPointImplSockets.cpp b/src/inet/UDPEndPointImplSockets.cpp index b681a56fabe92f..f5e89e69fc10fe 100644 --- a/src/inet/UDPEndPointImplSockets.cpp +++ b/src/inet/UDPEndPointImplSockets.cpp @@ -696,7 +696,7 @@ void UDPEndPointImplSockets::HandlePendingIO(System::SocketEvents events) } } -#if IP_MULTICAST_LOOP || IPV6_MULTICAST_LOOP +#ifdef IPV6_MULTICAST_LOOP static CHIP_ERROR SocketsSetMulticastLoopback(int aSocket, bool aLoopback, int aProtocol, int aOption) { const unsigned int lValue = static_cast(aLoopback); @@ -707,7 +707,7 @@ static CHIP_ERROR SocketsSetMulticastLoopback(int aSocket, bool aLoopback, int a return CHIP_NO_ERROR; } -#endif // IP_MULTICAST_LOOP || IPV6_MULTICAST_LOOP +#endif // IPV6_MULTICAST_LOOP static CHIP_ERROR SocketsSetMulticastLoopback(int aSocket, IPVersion aIPVersion, bool aLoopback) { @@ -721,11 +721,11 @@ static CHIP_ERROR SocketsSetMulticastLoopback(int aSocket, IPVersion aIPVersion, lRetval = SocketsSetMulticastLoopback(aSocket, aLoopback, IPPROTO_IPV6, IPV6_MULTICAST_LOOP); break; -#if INET_CONFIG_ENABLE_IPV4 +#if INET_CONFIG_ENABLE_IPV4 && defined(IP_MULTICAST_LOOP) case kIPVersion_4: lRetval = SocketsSetMulticastLoopback(aSocket, aLoopback, IPPROTO_IP, IP_MULTICAST_LOOP); break; -#endif // INET_CONFIG_ENABLE_IPV4 +#endif // INET_CONFIG_ENABLE_IPV4 && defined(IP_MULTICAST_LOOP) default: lRetval = INET_ERROR_WRONG_ADDRESS_TYPE; diff --git a/src/inet/tests/TestInetAddress.cpp b/src/inet/tests/TestInetAddress.cpp index f047f6dc751c12..e85000d4008487 100644 --- a/src/inet/tests/TestInetAddress.cpp +++ b/src/inet/tests/TestInetAddress.cpp @@ -23,6 +23,7 @@ * a class to store and format IPV4 and IPV6 Internet Protocol addresses. * */ +#include #include @@ -1205,7 +1206,7 @@ void CheckFromSocket(nlTestSuite * inSuite, void * inContext) #if CHIP_SYSTEM_CONFIG_USE_LWIP (void) inSuite; // This test is only supported for non LWIP stack. -#else // INET_LWIP +#else // CHIP_SYSTEM_CONFIG_USE_LWIP const struct TestContext * lContext = static_cast(inContext); IPAddressExpandedContextIterator lCurrent = lContext->mIPAddressExpandedContextRange.mBegin; IPAddressExpandedContextIterator lEnd = lContext->mIPAddressExpandedContextRange.mEnd; @@ -1260,7 +1261,7 @@ void CheckFromSocket(nlTestSuite * inSuite, void * inContext) ++lCurrent; } -#endif // INET_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP } /** diff --git a/src/inet/tests/TestInetLayer.cpp b/src/inet/tests/TestInetLayer.cpp index eb1d174af3611f..47e83316228b5f 100644 --- a/src/inet/tests/TestInetLayer.cpp +++ b/src/inet/tests/TestInetLayer.cpp @@ -205,7 +205,7 @@ static void CheckSucceededOrFailed(TestState & aTestState, bool & aOutSucceeded, { const TransferStats & lStats = aTestState.mStats; -#if DEBUG +#ifdef DEBUG_TCP_TEST printf("%u/%u sent, %u/%u received\n", lStats.mTransmit.mActual, lStats.mTransmit.mExpected, lStats.mReceive.mActual, lStats.mReceive.mExpected); #endif @@ -298,7 +298,7 @@ int main(int argc, char * argv[]) CheckSucceededOrFailed(sTestState, lSucceeded, lFailed); -#if DEBUG +#ifdef DEBUG_TCP_TEST // clang-format off printf("%s %s number of expected bytes\n", ((lSucceeded) ? "successfully" : diff --git a/src/inet/tests/TestLwIPDNS.cpp b/src/inet/tests/TestLwIPDNS.cpp index fad393aee3e54e..8681f7b2e54328 100644 --- a/src/inet/tests/TestLwIPDNS.cpp +++ b/src/inet/tests/TestLwIPDNS.cpp @@ -25,16 +25,17 @@ */ #include +#include #include #include #include -#if INET_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP #include #include -#endif // INET_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP #include @@ -54,10 +55,10 @@ static bool HandleNonOptionArgs(const char * progName, int argc, char * const ar // Globals -#if INET_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP static uint8_t sNumIpAddrs = DNS_MAX_ADDRS_PER_NAME; static ip_addr_t sIpAddrs[DNS_MAX_ADDRS_PER_NAME]; -#endif // INET_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP static const char * sHostname = nullptr; static const char * sDNSServerAddr = nullptr; @@ -76,7 +77,7 @@ static ArgParser::OptionSet * gToolOptionSets[] = }; // clang-format on -#if INET_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP static void found_multi(const char * aName, ip_addr_t * aIpAddrs, uint8_t aNumIpAddrs, void * callback_arg) { printf("\tfound_multi response\n"); @@ -205,7 +206,7 @@ static void TestLwIPDNS(void) printf("\tdns_gethostbyname_multi: %d (expected : ERR_OK)\n", res); } } -#endif // INET_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP int main(int argc, char * argv[]) { @@ -226,11 +227,11 @@ int main(int argc, char * argv[]) InitNetwork(); -#if INET_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP TestLwIPDNS(); #else - fprintf(stderr, "Please assert INET_LWIP to use this test.\n"); -#endif // INET_LWIP + fprintf(stderr, "Please assert CHIP_SYSTEM_CONFIG_USE_LWIP to use this test.\n"); +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP ShutdownNetwork(); diff --git a/src/lib/support/CHIPArgParser.hpp b/src/lib/support/CHIPArgParser.hpp index 2c16a03f3a9239..57c68e353f58c6 100644 --- a/src/lib/support/CHIPArgParser.hpp +++ b/src/lib/support/CHIPArgParser.hpp @@ -32,6 +32,10 @@ #include #include +#ifndef CHIP_CONFIG_NON_POSIX_LONG_OPT +#define CHIP_CONFIG_NON_POSIX_LONG_OPT 0 +#endif + namespace chip { namespace ArgParser { diff --git a/src/lib/support/Pool.h b/src/lib/support/Pool.h index 72d1a1842b0e36..c2486f0c0e20b3 100644 --- a/src/lib/support/Pool.h +++ b/src/lib/support/Pool.h @@ -324,9 +324,13 @@ class HeapObjectPool : public internal::Statistics, public internal::PoolCommon< #ifdef __clang__ #if __has_feature(address_sanitizer) #define __SANITIZE_ADDRESS__ 1 -#endif -#endif -#endif +#else +#define __SANITIZE_ADDRESS__ 0 +#endif // __has_feature(address_sanitizer) +#else +#define __SANITIZE_ADDRESS__ 0 +#endif // __clang__ +#endif // __SANITIZE_ADDRESS__ #if __SANITIZE_ADDRESS__ // Free all remaining objects so that ASAN can catch specific use-after-free cases. ReleaseAll(); diff --git a/src/lib/support/UnitTestRegistration.cpp b/src/lib/support/UnitTestRegistration.cpp index e706bbc891d6a5..93ea135b85a9ca 100644 --- a/src/lib/support/UnitTestRegistration.cpp +++ b/src/lib/support/UnitTestRegistration.cpp @@ -32,7 +32,7 @@ typedef struct static test_suites_t gs_test_suites; -#if __ZEPHYR__ +#ifdef __ZEPHYR__ inline static bool AlreadyExists(UnitTestTriggerFunction tests) { for (uint32_t i = 0; i < gs_test_suites.num_test_suites; ++i) @@ -50,7 +50,7 @@ CHIP_ERROR RegisterUnitTests(UnitTestTriggerFunction tests) return CHIP_ERROR_NO_MEMORY; } -#if __ZEPHYR__ +#ifdef __ZEPHYR__ // Not sure yet if it's a Zephyr bug or misconfiguration, but global constructors are called // twice on native_posix platform - by libc and by Zephyr's main thread initialization code. // This makes sure tests are not run twice for that reason. diff --git a/src/platform/tests/TestCHIPoBLEStackMgr.h b/src/platform/tests/TestCHIPoBLEStackMgr.h index 2c97e8450f4b55..b0f4f59516f2b1 100644 --- a/src/platform/tests/TestCHIPoBLEStackMgr.h +++ b/src/platform/tests/TestCHIPoBLEStackMgr.h @@ -23,6 +23,8 @@ #pragma once +#include + #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE int TestCHIPoBLEStackManager(); #endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE diff --git a/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp b/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp index e70fccd7e798e6..1d85fdcca4d1b2 100644 --- a/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp +++ b/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp @@ -16,6 +16,7 @@ */ #include "TestCHIPoBLEStackMgr.h" +#include #include int main(int argc, char * argv[]) diff --git a/src/system/SystemClock.cpp b/src/system/SystemClock.cpp index c52a1ecec0ed03..3ef63797598f1f 100644 --- a/src/system/SystemClock.cpp +++ b/src/system/SystemClock.cpp @@ -73,7 +73,7 @@ ClockBase * gClockBase = &gClockImpl; #if HAVE_CLOCK_GETTIME -#if HAVE_DECL_CLOCK_BOOTTIME +#if defined(HAVE_DECL_CLOCK_BOOTTIME) && HAVE_DECL_CLOCK_BOOTTIME // CLOCK_BOOTTIME is a Linux-specific option to clock_gettime for a clock which compensates for system sleep. #define MONOTONIC_CLOCK_ID CLOCK_BOOTTIME #define MONOTONIC_RAW_CLOCK_ID CLOCK_MONOTONIC_RAW diff --git a/src/system/SystemPacketBufferInternal.h b/src/system/SystemPacketBufferInternal.h index b0000f4e665df9..e6acb76f6b6e80 100644 --- a/src/system/SystemPacketBufferInternal.h +++ b/src/system/SystemPacketBufferInternal.h @@ -27,6 +27,10 @@ #include #include +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#include +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + /** * CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_HEAP *