pw_build: add "-Wundef" to strict_warnings

Adds "-Wundef" to "strict_warnings" to catch use of accidentally
undefined macros which otherwise implicitly default to 0.

Updates macros across Pigweed to make it compile.

Drops support for PW_TEST_DONT_DEFINE_* in addition to
GTEST_DONT_DEFINE_*, instead only GTEST_DONT_DEFINE_* is used.

Change-Id: Id89f85f7cb8a52c1619bc0f7262349310203067b
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/21463
Reviewed-by: Alexei Frolov <frolv@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Ewout van Bekkum <ewout@google.com>
diff --git a/pw_allocator/block.cc b/pw_allocator/block.cc
index 67b0a7b..bbf1a34 100644
--- a/pw_allocator/block.cc
+++ b/pw_allocator/block.cc
@@ -43,7 +43,7 @@
 
   aliased.block->prev = nullptr;
   *block = aliased.block;
-#if PW_ALLOCATOR_POISON_ENABLE
+#if defined(PW_ALLOCATOR_POISON_ENABLE) && PW_ALLOCATOR_POISON_ENABLE
   (*block)->PoisonBlock();
 #endif  // PW_ALLOCATOR_POISON_ENABLE
   return Status::Ok();
@@ -113,7 +113,7 @@
 
   *new_block = next;
 
-#if PW_ALLOCATOR_POISON_ENABLE
+#if defined(PW_ALLOCATOR_POISON_ENABLE) && PW_ALLOCATOR_POISON_ENABLE
   PoisonBlock();
   (*new_block)->PoisonBlock();
 #endif  // PW_ALLOCATOR_POISON_ENABLE
@@ -208,7 +208,7 @@
     return BlockStatus::PREV_MISMATCHED;
   }
 
-#if PW_ALLOCATOR_POISON_ENABLE
+#if defined(PW_ALLOCATOR_POISON_ENABLE) && PW_ALLOCATOR_POISON_ENABLE
   if (!this->CheckPoisonBytes()) {
     return BlockStatus::POISON_CORRUPTED;
   }
@@ -219,7 +219,7 @@
 // Paint sizeof(void*) bytes before and after the usable space in Block as the
 // randomized function pattern.
 void Block::PoisonBlock() {
-#if PW_ALLOCATOR_POISON_ENABLE
+#if defined(PW_ALLOCATOR_POISON_ENABLE) && PW_ALLOCATOR_POISON_ENABLE
   std::byte* front_region = reinterpret_cast<std::byte*>(this) + sizeof(*this);
   memcpy(front_region, POISON_PATTERN, PW_ALLOCATOR_POISON_OFFSET);
 
@@ -230,7 +230,7 @@
 }
 
 bool Block::CheckPoisonBytes() const {
-#if PW_ALLOCATOR_POISON_ENABLE
+#if defined(PW_ALLOCATOR_POISON_ENABLE) && PW_ALLOCATOR_POISON_ENABLE
   std::byte* front_region = reinterpret_cast<std::byte*>(
       reinterpret_cast<intptr_t>(this) + sizeof(*this));
   if (std::memcmp(front_region, POISON_PATTERN, PW_ALLOCATOR_POISON_OFFSET)) {
diff --git a/pw_allocator/block_test.cc b/pw_allocator/block_test.cc
index 0fd5d39..0e09579 100644
--- a/pw_allocator/block_test.cc
+++ b/pw_allocator/block_test.cc
@@ -374,7 +374,7 @@
   EXPECT_EQ(third_block->IsValid(), true);
   EXPECT_EQ(fourth_block->IsValid(), true);
 
-#if PW_ALLOCATOR_POISON_ENABLE
+#if defined(PW_ALLOCATOR_POISON_ENABLE) && PW_ALLOCATOR_POISON_ENABLE
   std::byte fault_poison[PW_ALLOCATOR_POISON_OFFSET] = {std::byte(0)};
   std::byte* front_poison =
       reinterpret_cast<std::byte*>(third_block) + sizeof(*third_block);
@@ -389,7 +389,7 @@
 }
 
 TEST(Block, CanPoisonBlock) {
-#if PW_ALLOCATOR_POISON_ENABLE
+#if defined(PW_ALLOCATOR_POISON_ENABLE) && PW_ALLOCATOR_POISON_ENABLE
   constexpr size_t kN = 1024;
   byte bytes[kN];
 
diff --git a/pw_allocator/freelist_heap_test.cc b/pw_allocator/freelist_heap_test.cc
index 94a10ef..7746383 100644
--- a/pw_allocator/freelist_heap_test.cc
+++ b/pw_allocator/freelist_heap_test.cc
@@ -111,7 +111,7 @@
   EXPECT_EQ(ptr2_start % alignment, static_cast<size_t>(0));
 }
 
-#if CHECK_TEST_CRASHES
+#if defined(CHECK_TEST_CRASHES) && CHECK_TEST_CRASHES
 
 // TODO(amontanez): Ensure that this test triggers an assert.
 TEST(FreeListHeap, CannotFreeNonOwnedPointer) {
diff --git a/pw_allocator/public/pw_allocator/block.h b/pw_allocator/public/pw_allocator/block.h
index b0cfae4..b82dee9 100644
--- a/pw_allocator/public/pw_allocator/block.h
+++ b/pw_allocator/public/pw_allocator/block.h
@@ -23,7 +23,7 @@
 
 namespace pw::allocator {
 
-#if PW_ALLOCATOR_POISON_ENABLE
+#if defined(PW_ALLOCATOR_POISON_ENABLE) && PW_ALLOCATOR_POISON_ENABLE
 // Add poison offset of sizeof(void*) bytes before and after usable space in all
 // Blocks.
 #define PW_ALLOCATOR_POISON_OFFSET sizeof(void*)
diff --git a/pw_assert_basic/assert_basic.cc b/pw_assert_basic/assert_basic.cc
index 9af097c..e6a64f5 100644
--- a/pw_assert_basic/assert_basic.cc
+++ b/pw_assert_basic/assert_basic.cc
@@ -21,6 +21,7 @@
 
 #include <cstring>
 
+#include "pw_assert/options.h"
 #include "pw_preprocessor/util.h"
 #include "pw_string/string_builder.h"
 #include "pw_sys_io/sys_io.h"
@@ -157,9 +158,9 @@
 }
 
 extern "C" void pw_assert_HandleFailure(void) {
-#if PW_ASSERT_DEBUG_ENABLED
+#if PW_ASSERT_ENABLE_DEBUG
   pw_Crash("", 0, "", "Crash: PW_ASSERT() or PW_DASSERT() failure");
 #else
   pw_Crash("", 0, "", "Crash: PW_ASSERT() failure. Note: PW_DASSERT disabled");
-#endif  // PW_ASSERT_DEBUG_ENABLED
+#endif  // PW_ASSERT_ENABLE_DEBUG
 }
diff --git a/pw_assert_log/BUILD.gn b/pw_assert_log/BUILD.gn
index 2f70f67..083c5e8 100644
--- a/pw_assert_log/BUILD.gn
+++ b/pw_assert_log/BUILD.gn
@@ -38,7 +38,10 @@
 pw_source_set("core") {
   public_configs = [ ":default_config" ]
   public_deps = [ "$dir_pw_log" ]
-  deps = [ "$dir_pw_preprocessor" ]
+  deps = [
+    "$dir_pw_assert:facade",
+    "$dir_pw_preprocessor",
+  ]
   public = [ "public/pw_assert_log/assert_log.h" ]
   sources = [ "assert_log.cc" ]
 }
diff --git a/pw_assert_log/assert_log.cc b/pw_assert_log/assert_log.cc
index 2d835d9..9d8b9ba 100644
--- a/pw_assert_log/assert_log.cc
+++ b/pw_assert_log/assert_log.cc
@@ -14,8 +14,10 @@
 
 #include "pw_assert_log/assert_log.h"
 
+#include "pw_assert/options.h"
+
 extern "C" void pw_assert_HandleFailure(void) {
-#if PW_ASSERT_DEBUG_ENABLED
+#if PW_ASSERT_ENABLE_DEBUG
   PW_LOG(PW_LOG_LEVEL_CRITICAL,
          PW_LOG_ASSERT_FAILED_FLAG,
          "Crash: PW_ASSERT() or PW_DASSERT() failure");
@@ -23,6 +25,6 @@
   PW_LOG(PW_LOG_LEVEL_CRITICAL,
          PW_LOG_ASSERT_FAILED_FLAG,
          "Crash: PW_ASSERT() failure. Note: PW_DASSERT disabled");
-#endif  // PW_ASSERT_DEBUG_ENABLED
+#endif  // PW_ASSERT_ENABLE_DEBUG
   PW_UNREACHABLE;
 }
diff --git a/pw_build/BUILD.gn b/pw_build/BUILD.gn
index 26a7f50..339a031 100644
--- a/pw_build/BUILD.gn
+++ b/pw_build/BUILD.gn
@@ -72,6 +72,7 @@
     "-Wextra",
     "-Wimplicit-fallthrough",
     "-Wcast-qual",
+    "-Wundef",
 
     # Make all warnings errors, except for the exemptions below.
     "-Werror",
diff --git a/pw_containers/intrusive_list_test.cc b/pw_containers/intrusive_list_test.cc
index af1d433..9f56c1d 100644
--- a/pw_containers/intrusive_list_test.cc
+++ b/pw_containers/intrusive_list_test.cc
@@ -376,9 +376,10 @@
   }
 }
 
-#if NO_COMPILE_TESTS
 // TODO(pwbug/47): These tests should fail to compile, enable when no-compile
 // tests are set up in Pigweed.
+#define NO_COMPILE_TESTS 0
+#if NO_COMPILE_TESTS
 TEST(IntrusiveList, ConstIteratorModify) {
   TestItem item1(1);
   TestItem item2(99);
@@ -395,13 +396,12 @@
     it++;
   }
 }
-
 #endif  // NO_COMPILE_TESTS
 
 // TODO(pwbug/88): These tests should trigger a CHECK failure. This requires
 // using a testing version of pw_assert.
+#define TESTING_CHECK_FAILURES_IS_SUPPORTED 0
 #if TESTING_CHECK_FAILURES_IS_SUPPORTED
-
 TEST(IntrusiveList, Construct_DuplicateItems) {
   TestItem item(1);
   IntrusiveList<TestItem> list({&item, &item});
@@ -434,7 +434,6 @@
 
   list.push_front(item);
 }
-
 #endif  // TESTING_CHECK_FAILURES_IS_SUPPORTED
 
 TEST(IntrusiveList, EraseAfter_FirstItem) {
diff --git a/pw_kvs/flash_partition_test.cc b/pw_kvs/flash_partition_test.cc
index aa7a12d..b9a2299 100644
--- a/pw_kvs/flash_partition_test.cc
+++ b/pw_kvs/flash_partition_test.cc
@@ -193,8 +193,8 @@
   EXPECT_LE(sector_size_bytes % kMaxFlashAlignment, 0U);
 }
 
-#if CHECK_TEST_CRASHES
-
+#define TESTING_CHECK_FAILURES_IS_SUPPORTED 0
+#if TESTING_CHECK_FAILURES_IS_SUPPORTED
 // TODO: Ensure that this test triggers an assert.
 TEST(FlashPartitionTest, BadWriteAddressAlignment) {
   FlashPartition& test_partition = FlashTestPartition();
@@ -234,7 +234,7 @@
   test_partition.Erase(1, 1);
 }
 
-#endif  // CHECK_TEST_CRASHES
+#endif  // TESTING_CHECK_FAILURES_IS_SUPPORTED
 
 TEST(FlashPartitionTest, IsErased) {
   FlashPartition& test_partition = FlashTestPartition();
diff --git a/pw_polyfill/public/pw_polyfill/standard.h b/pw_polyfill/public/pw_polyfill/standard.h
index 8e8feeb..d5e1b75 100644
--- a/pw_polyfill/public/pw_polyfill/standard.h
+++ b/pw_polyfill/public/pw_polyfill/standard.h
@@ -13,8 +13,12 @@
 // the License.
 #pragma once
 
+#ifdef __cplusplus
 #define PW_CXX_STANDARD_IS_SUPPORTED(std) \
   (__cplusplus >= _PW_CXX_STANDARD_##std())
+#else
+#define PW_CXX_STANDARD_IS_SUPPORTED(std) (0 >= _PW_CXX_STANDARD_##std())
+#endif
 
 #define _PW_CXX_STANDARD_98() 199711L
 #define _PW_CXX_STANDARD_11() 201103L
diff --git a/pw_preprocessor/public/pw_preprocessor/compiler.h b/pw_preprocessor/public/pw_preprocessor/compiler.h
index 9d7d03d..8d002f9 100644
--- a/pw_preprocessor/public/pw_preprocessor/compiler.h
+++ b/pw_preprocessor/public/pw_preprocessor/compiler.h
@@ -49,7 +49,7 @@
 
 // When compiling for host using MinGW, use gnu_printf() rather than printf()
 // to support %z format specifiers.
-#if __USE_MINGW_ANSI_STDIO
+#ifdef __USE_MINGW_ANSI_STDIO
 #define _PW_PRINTF_FORMAT_TYPE gnu_printf
 #else
 #define _PW_PRINTF_FORMAT_TYPE printf
@@ -62,7 +62,7 @@
 // to keep the variable, even if it is not used. Depending on the linker
 // options, the linker may still remove this section if it is not declared in
 // the linker script and marked KEEP.
-#if __APPLE__
+#ifdef __APPLE__
 #define PW_KEEP_IN_SECTION(name) __attribute__((section("__DATA," name), used))
 #else
 #define PW_KEEP_IN_SECTION(name) __attribute__((section(name), used))
@@ -101,7 +101,7 @@
 //     }
 //     return hash;
 //   }
-#if __clang__
+#ifdef __clang__
 #define PW_NO_SANITIZE(check) __attribute__((no_sanitize(check)))
 #else
 #define PW_NO_SANITIZE(check)
diff --git a/pw_protobuf_compiler/proto.gni b/pw_protobuf_compiler/proto.gni
index 7ba0128..899b561 100644
--- a/pw_protobuf_compiler/proto.gni
+++ b/pw_protobuf_compiler/proto.gni
@@ -239,6 +239,9 @@
       "$_proto_gen_dir",
       "$_proto_gen_dir/$_include_root",
     ]
+
+    # Nanopb uses __cplusplus with the implicit default of 0.
+    cflags = [ "-Wno-undef" ]
   }
 
   # Create a library with the generated source files.
diff --git a/pw_stream/memory_stream_test.cc b/pw_stream/memory_stream_test.cc
index 0a0241e..51c9a38 100644
--- a/pw_stream/memory_stream_test.cc
+++ b/pw_stream/memory_stream_test.cc
@@ -131,7 +131,8 @@
   EXPECT_EQ(memory_writer.data()[1], std::byte{0x7E});
 }
 
-#if CHECK_TEST_CRASHES
+#define TESTING_CHECK_FAILURES_IS_SUPPORTED 0
+#if TESTING_CHECK_FAILURES_IS_SUPPORTED
 
 // TODO(amontanez): Ensure that this test triggers an assert.
 TEST(MemoryWriter, NullPointer) {
@@ -152,7 +153,7 @@
   memory_reader.Read(nullptr, 21);
 }
 
-#endif  // CHECK_TEST_CRASHES
+#endif  // TESTING_CHECK_FAILURES_IS_SUPPORTED
 
 TEST(MemoryReader, SingleFullRead) {
   constexpr size_t kTempBufferSize = 32;
diff --git a/pw_tokenizer/BUILD.gn b/pw_tokenizer/BUILD.gn
index 41430c3..4090546 100644
--- a/pw_tokenizer/BUILD.gn
+++ b/pw_tokenizer/BUILD.gn
@@ -130,10 +130,7 @@
     ":pw_tokenizer",
     dir_pw_varint,
   ]
-  sources = [
-    "generate_decoding_test_data.cc",
-    "tokenize_test_fakes.cc",
-  ]
+  sources = [ "generate_decoding_test_data.cc" ]
 }
 
 # Executable for generating a test ELF file for elf_reader_test.py. A host
@@ -174,7 +171,6 @@
     "argument_types_test.cc",
     "argument_types_test_c.c",
     "pw_tokenizer_private/argument_types_test.h",
-    "tokenize_test_fakes.cc",
   ]
   deps = [ ":pw_tokenizer" ]
 
@@ -224,7 +220,6 @@
   sources = [
     "hash_test.cc",
     "pw_tokenizer_private/generated_hash_test_cases.h",
-    "tokenize_test_fakes.cc",
   ]
   deps = [ ":pw_tokenizer" ]
 }
diff --git a/pw_tokenizer/encode_args.cc b/pw_tokenizer/encode_args.cc
index d18e1eb..af537eb 100644
--- a/pw_tokenizer/encode_args.cc
+++ b/pw_tokenizer/encode_args.cc
@@ -45,7 +45,7 @@
 // Store tokenization metadata in its own section. Mach-O files are not
 // supported by pw_tokenizer, but a short, Mach-O compatible section name is
 // used on macOS so that this file can at least compile.
-#if __APPLE__
+#ifdef __APPLE__
 #define PW_TOKENIZER_INFO_SECTION PW_KEEP_IN_SECTION(".pw_info")
 #else
 #define PW_TOKENIZER_INFO_SECTION PW_KEEP_IN_SECTION(".pw_tokenzier_info")
diff --git a/pw_tokenizer/public/pw_tokenizer/internal/argument_types.h b/pw_tokenizer/public/pw_tokenizer/internal/argument_types.h
index 21f5b4c..1f431e4 100644
--- a/pw_tokenizer/public/pw_tokenizer/internal/argument_types.h
+++ b/pw_tokenizer/public/pw_tokenizer/internal/argument_types.h
@@ -85,7 +85,7 @@
 namespace pw {
 namespace tokenizer {
 
-#if __cpp_if_constexpr  // C++17 version
+#ifdef __cpp_if_constexpr  // C++17 version
 
 // This function selects the matching type enum for supported argument types.
 template <typename T>
diff --git a/pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h b/pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h
index 73d2739..f616bbb 100644
--- a/pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h
+++ b/pw_tokenizer/public/pw_tokenizer/internal/tokenize_string.h
@@ -28,7 +28,8 @@
 #include <stdint.h>
 
 // In C++17, use a constexpr function to calculate the hash.
-#if __cpp_constexpr >= 201304L && defined(__cpp_inline_variables)
+#if defined(__cpp_constexpr) && __cpp_constexpr >= 201304L && \
+    defined(__cpp_inline_variables)
 
 #include "pw_tokenizer/pw_tokenizer_65599_fixed_length_hash.h"
 
diff --git a/pw_tokenizer/public/pw_tokenizer/tokenize.h b/pw_tokenizer/public/pw_tokenizer/tokenize.h
index fd776b4..9a93055 100644
--- a/pw_tokenizer/public/pw_tokenizer/tokenize.h
+++ b/pw_tokenizer/public/pw_tokenizer/tokenize.h
@@ -269,7 +269,7 @@
 // pw_tokenizer is intended for use with ELF files only. Mach-O files (macOS
 // executables) do not support section names longer than 16 characters, so a
 // short, dummy section name is used on macOS.
-#if __APPLE__
+#ifdef __APPLE__
 #define _PW_TOKENIZER_SECTION(unused_domain) \
   PW_KEEP_IN_SECTION(".pw." PW_STRINGIFY(__LINE__))
 #else
diff --git a/pw_tokenizer/tokenize_test_fakes.cc b/pw_tokenizer/tokenize_test_fakes.cc
deleted file mode 100644
index 4b9655a..0000000
--- a/pw_tokenizer/tokenize_test_fakes.cc
+++ /dev/null
@@ -1,40 +0,0 @@
-// Copyright 2020 The Pigweed Authors
-//
-// Licensed under the Apache License, Version 2.0 (the "License"); you may not
-// use this file except in compliance with the License. You may obtain a copy of
-// the License at
-//
-//     https://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-// License for the specific language governing permissions and limitations under
-// the License.
-
-// This file provide stub implementations for the function projects are expected
-// to provide when PW_TOKENIZER_CFG_ENABLE_TOKENIZE_TO_GLOBAL_HANDLER is set.
-
-#include <cstddef>
-#include <cstdint>
-
-#include "pw_tokenizer/tokenize.h"
-
-#if PW_TOKENIZER_CFG_ENABLE_TOKENIZE_TO_GLOBAL_HANDLER
-
-PW_EXTERN_C void pw_tokenizer_HandleEncodedMessage(
-    const uint8_t encoded_message[], size_t size_bytes) {
-  PW_UNUSED(encoded_message[0]);
-  PW_UNUSED(size_bytes);
-}
-
-PW_EXTERN_C void pw_tokenizer_HandleEncodedMessageWithPayload(
-    pw_tokenizer_Payload payload,
-    const uint8_t encoded_message[],
-    size_t size_bytes) {
-  PW_UNUSED(payload);
-  PW_UNUSED(encoded_message[0]);
-  PW_UNUSED(size_bytes);
-}
-
-#endif  // PW_TOKENIZER_CFG_ENABLE_TOKENIZE_TO_GLOBAL_HANDLER
diff --git a/pw_unit_test/public/pw_unit_test/framework.h b/pw_unit_test/public/pw_unit_test/framework.h
index ce1cea5..22ff635 100644
--- a/pw_unit_test/public/pw_unit_test/framework.h
+++ b/pw_unit_test/public/pw_unit_test/framework.h
@@ -36,11 +36,10 @@
   _PW_TEST(test_suite_name, test_name, ::pw::unit_test::Test)
 
 // TEST() is a pretty generic macro name which could conflict with other code.
-// If PW_TEST_DONT_DEFINE_TEST is set, don't alias PW_TEST to TEST.
-// GTEST_DONT_DEFINE_TEST is also accepted for compatibility.
-#if !PW_TEST_DONT_DEFINE_TEST && !GTEST_DONT_DEFINE_TEST
+// If GTEST_DONT_DEFINE_TEST is set, don't alias PW_TEST to TEST.
+#if !(defined(GTEST_DONT_DEFINE_TEST) && GTEST_DONT_DEFINE_TEST)
 #define TEST PW_TEST
-#endif  // !PW_TEST_DONT_DEFINE_TEST && !GTEST_DONT_DEFINE_TEST
+#endif  // !GTEST_DONT_DEFINE_TEST
 
 #define TEST_F(test_fixture, test_name) \
   _PW_TEST(test_fixture, test_name, test_fixture)
@@ -76,18 +75,18 @@
 
 // Define either macro to 1 to omit the definition of FAIL(), which is a
 // generic name and clashes with some other libraries.
-#if !PW_TEST_DONT_DEFINE_FAIL && !GTEST_DONT_DEFINE_FAIL
+#if !(defined(GTEST_DONT_DEFINE_FAIL) && GTEST_DONT_DEFINE_FAIL)
 #define FAIL() GTEST_FAIL()
-#endif  // !PW_TEST_DONT_DEFINE_FAIL && !GTEST_DONT_DEFINE_FAIL
+#endif  // !GTEST_DONT_DEFINE_FAIL
 
 // Generates a success with a generic message.
 #define GTEST_SUCCEED() _PW_TEST_MESSAGE("(success)", "(success)", true)
 
 // Define either macro to 1 to omit the definition of SUCCEED(), which
 // is a generic name and clashes with some other libraries.
-#if !PW_TEST_DONT_DEFINE_SUCCEED && !GTEST_DONT_DEFINE_SUCCEED
+#if !(defined(GTEST_DONT_DEFINE_SUCCEED) && GTEST_DONT_DEFINE_SUCCEED)
 #define SUCCEED() GTEST_SUCCEED()
-#endif  // !PW_TEST_DONT_DEFINE_SUCCEED && !GTEST_DONT_DEFINE_SUCCEED
+#endif  // !GTEST_DONT_DEFINE_SUCCEED
 
 // pw_unit_test framework entry point. Runs every registered test case and
 // dispatches the results through the event handler. Returns a status of zero