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.
diff --git a/build/config/compiler/BUILD.gn b/build/config/compiler/BUILD.gn
index c508d65..9f34a73 100644
--- a/build/config/compiler/BUILD.gn
+++ b/build/config/compiler/BUILD.gn
@@ -253,6 +253,20 @@
     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 3e120c5..58e0f6b 100644
--- a/examples/darwin-framework-tool/commands/common/MTRLogging.h
+++ b/examples/darwin-framework-tool/commands/common/MTRLogging.h
@@ -21,7 +21,7 @@
 
 #import <os/log.h>
 
-#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 44a96b1..46229c5 100644
--- a/examples/platform/linux/BUILD.gn
+++ b/examples/platform/linux/BUILD.gn
@@ -94,9 +94,7 @@
     ]
   }
 
-  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 d980811..8522f1f 100644
--- a/examples/tv-app/tv-common/src/ZCLCallbacks.cpp
+++ b/examples/tv-app/tv-common/src/ZCLCallbacks.cpp
@@ -24,6 +24,7 @@
 #include <app-common/zap-generated/ids/Clusters.h>
 #include <app/ConcreteAttributePath.h>
 #include <lib/support/logging/CHIPLogging.h>
+#include <platform/CHIPDeviceConfig.h>
 
 #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 f555a62..9d4a3d3 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 @@
   ]
 
   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 7a42c09..c981aba 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 <lib/core/Optional.h>
 #include <lib/support/Span.h>
+#include <setup_payload/CHIPAdditionalDataPayloadBuildConfig.h>
 
 /**
  * @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 06cf6a1..d94caed 100644
--- a/src/app/tests/TestDataModelSerialization.cpp
+++ b/src/app/tests/TestDataModelSerialization.cpp
@@ -105,7 +105,7 @@
     //
     // 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 fd8937d..e29c001 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 e1c14dc..4d30309 100644
--- a/src/inet/InetInterface.cpp
+++ b/src/inet/InetInterface.cpp
@@ -502,7 +502,7 @@
     }
 }
 
-#if __ANDROID__
+#ifdef __ANDROID__
 
 static struct if_nameindex * backport_if_nameindex(void);
 static void backport_if_freenameindex(struct if_nameindex *);
@@ -648,7 +648,7 @@
 {
     if (mIntfArray != nullptr)
     {
-#if __ANDROID__
+#ifdef __ANDROID__
         backport_if_freenameindex(mIntfArray);
 #else
         if_freenameindex(mIntfArray);
@@ -666,7 +666,7 @@
 {
     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 b681a56..f5e89e6 100644
--- a/src/inet/UDPEndPointImplSockets.cpp
+++ b/src/inet/UDPEndPointImplSockets.cpp
@@ -696,7 +696,7 @@
     }
 }
 
-#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<unsigned int>(aLoopback);
@@ -707,7 +707,7 @@
 
     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 @@
         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 f047f6d..e85000d 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 <lib/core/CHIPConfig.h>
 
 #include <inet/IPAddress.h>
 
@@ -1205,7 +1206,7 @@
 #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<const struct TestContext *>(inContext);
     IPAddressExpandedContextIterator lCurrent = lContext->mIPAddressExpandedContextRange.mBegin;
     IPAddressExpandedContextIterator lEnd     = lContext->mIPAddressExpandedContextRange.mEnd;
@@ -1260,7 +1261,7 @@
 
         ++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 eb1d174..47e8331 100644
--- a/src/inet/tests/TestInetLayer.cpp
+++ b/src/inet/tests/TestInetLayer.cpp
@@ -205,7 +205,7 @@
 {
     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 @@
 
         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 fad393a..8681f7b 100644
--- a/src/inet/tests/TestLwIPDNS.cpp
+++ b/src/inet/tests/TestLwIPDNS.cpp
@@ -25,16 +25,17 @@
  */
 
 #include <inet/InetConfig.h>
+#include <lib/core/CHIPConfig.h>
 
 #include <stdint.h>
 #include <string.h>
 
 #include <sys/time.h>
 
-#if INET_LWIP
+#if CHIP_SYSTEM_CONFIG_USE_LWIP
 #include <lwip/dns.h>
 #include <lwip/ip_addr.h>
-#endif // INET_LWIP
+#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
 
 #include <CHIPVersion.h>
 
@@ -54,10 +55,10 @@
 
 // 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 @@
 };
 // 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 @@
         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 @@
 
     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 2c16a03..57c68e3 100644
--- a/src/lib/support/CHIPArgParser.hpp
+++ b/src/lib/support/CHIPArgParser.hpp
@@ -32,6 +32,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#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 72d1a18..c2486f0 100644
--- a/src/lib/support/Pool.h
+++ b/src/lib/support/Pool.h
@@ -324,9 +324,13 @@
 #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 e706bbc..93ea135 100644
--- a/src/lib/support/UnitTestRegistration.cpp
+++ b/src/lib/support/UnitTestRegistration.cpp
@@ -32,7 +32,7 @@
 
 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 @@
         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 2c97e84..b0f4f59 100644
--- a/src/platform/tests/TestCHIPoBLEStackMgr.h
+++ b/src/platform/tests/TestCHIPoBLEStackMgr.h
@@ -23,6 +23,8 @@
 
 #pragma once
 
+#include <platform/CHIPDeviceConfig.h>
+
 #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 e70fccd..1d85fdc 100644
--- a/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp
+++ b/src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp
@@ -16,6 +16,7 @@
  */
 
 #include "TestCHIPoBLEStackMgr.h"
+#include <platform/CHIPDeviceConfig.h>
 #include <stdlib.h>
 
 int main(int argc, char * argv[])
diff --git a/src/system/SystemClock.cpp b/src/system/SystemClock.cpp
index c52a1ec..3ef6379 100644
--- a/src/system/SystemClock.cpp
+++ b/src/system/SystemClock.cpp
@@ -73,7 +73,7 @@
 
 #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 b0000f4..e6acb76 100644
--- a/src/system/SystemPacketBufferInternal.h
+++ b/src/system/SystemPacketBufferInternal.h
@@ -27,6 +27,10 @@
 #include <lib/core/CHIPConfig.h>
 #include <system/SystemConfig.h>
 
+#if CHIP_SYSTEM_CONFIG_USE_LWIP
+#include <lwip/opt.h>
+#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
+
 /**
  * CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_HEAP
  *