diff --git a/src/app/tests/TestEventLogging.cpp b/src/app/tests/TestEventLogging.cpp index 87f522baa55ca0..97f152eb4a28b8 100644 --- a/src/app/tests/TestEventLogging.cpp +++ b/src/app/tests/TestEventLogging.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include diff --git a/src/app/tests/TestMessageDef.cpp b/src/app/tests/TestMessageDef.cpp index 9f08ba6b31bc73..95c3cd52f075e2 100644 --- a/src/app/tests/TestMessageDef.cpp +++ b/src/app/tests/TestMessageDef.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include diff --git a/src/app/tests/integration/common.cpp b/src/app/tests/integration/common.cpp index 69bb8b903d923b..d898928cf2c5ae 100644 --- a/src/app/tests/integration/common.cpp +++ b/src/app/tests/integration/common.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include diff --git a/src/lib/core/CHIPTLV.h b/src/lib/core/CHIPTLV.h index 8509c8a417e866..9f3947c1df98bc 100644 --- a/src/lib/core/CHIPTLV.h +++ b/src/lib/core/CHIPTLV.h @@ -33,6 +33,7 @@ #include #include +#include #include #include #include diff --git a/src/lib/shell/streamer.cpp b/src/lib/shell/streamer.cpp index 5b8e2e5e91c46c..852688ebc33edb 100644 --- a/src/lib/shell/streamer.cpp +++ b/src/lib/shell/streamer.cpp @@ -23,6 +23,7 @@ #include "streamer.h" +#include #include #include #include diff --git a/src/lib/support/BUILD.gn b/src/lib/support/BUILD.gn index fe8c92dd4909e9..39b83c37cfffb4 100644 --- a/src/lib/support/BUILD.gn +++ b/src/lib/support/BUILD.gn @@ -40,6 +40,10 @@ source_set("logging_constants") { sources = [ "logging/Constants.h" ] } +source_set("enforce_format") { + sources = [ "EnforceFormat.h" ] +} + source_set("chip_version_header") { sources = get_target_outputs(":gen_chip_version") @@ -131,6 +135,7 @@ static_library("support") { public_deps = [ ":chip_version_header", + ":enforce_format", ":logging_constants", "${chip_root}/src/lib/core:chip_config_header", "${chip_root}/src/platform:platform_buildconfig", diff --git a/src/lib/support/CHIPArgParser.cpp b/src/lib/support/CHIPArgParser.cpp index 2af19e854b02c5..6fd0858d855e25 100644 --- a/src/lib/support/CHIPArgParser.cpp +++ b/src/lib/support/CHIPArgParser.cpp @@ -48,6 +48,7 @@ #include #include +#include #include /* diff --git a/src/lib/support/DefaultStorageKeyAllocator.h b/src/lib/support/DefaultStorageKeyAllocator.h index 8ac8d14670cfe7..c469f04ba94efc 100644 --- a/src/lib/support/DefaultStorageKeyAllocator.h +++ b/src/lib/support/DefaultStorageKeyAllocator.h @@ -17,6 +17,7 @@ #pragma once #include +#include #include #include diff --git a/src/lib/support/EnforceFormat.h b/src/lib/support/EnforceFormat.h new file mode 100644 index 00000000000000..7d8f681b7de777 --- /dev/null +++ b/src/lib/support/EnforceFormat.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 + * + * http://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. + */ + +#pragma once + +/** + * gcc and clang provide a way to warn for a custom formatter when formats don't + * match arguments. Use that so we catch mistakes. The "format" + * attribute takes the type of format, which arg is the format string, and which + * arg is the first variadic arg, with both arg numbers 1-based. + * + * The second arg should be set to 0 if the function takes a va_list instead of + * varargs. + */ + +#if defined(__GNUC__) +#define ENFORCE_FORMAT(n, m) __attribute__((format(printf, n, m))) +#else // __GNUC__ +#define ENFORCE_FORMAT(n, m) /* How to do with MSVC? */ +#endif // __GNUC__ diff --git a/src/lib/support/logging/CHIPLogging.h b/src/lib/support/logging/CHIPLogging.h index b23b47a31f665f..fa48aa487c2041 100644 --- a/src/lib/support/logging/CHIPLogging.h +++ b/src/lib/support/logging/CHIPLogging.h @@ -39,6 +39,7 @@ #include +#include #include #include diff --git a/src/lib/support/logging/Constants.h b/src/lib/support/logging/Constants.h index 1dffc2b6467404..0214fcaa2dbab8 100644 --- a/src/lib/support/logging/Constants.h +++ b/src/lib/support/logging/Constants.h @@ -2,22 +2,6 @@ #pragma once -/** - * gcc and clang provide a way to warn for a custom formatter when formats don't - * match arguments. Use that for Log() so we catch mistakes. The "format" - * attribute takes the type of format, which arg is the format string, and which - * arg is the first variadic arg, with both arg numbers 1-based. - * - * The second arg should be set to 0 if the function takes a va_list instead of - * varargs. - */ - -#if defined(__GNUC__) -#define ENFORCE_FORMAT(n, m) __attribute__((format(printf, n, m))) -#else // __GNUC__ -#define ENFORCE_FORMAT(n, m) /* How to do with MSVC? */ -#endif // __GNUC__ - namespace chip { namespace Logging { diff --git a/src/lib/support/tests/TestCHIPArgParser.cpp b/src/lib/support/tests/TestCHIPArgParser.cpp index 092194e33332df..4ba1f9fe8ccc79 100644 --- a/src/lib/support/tests/TestCHIPArgParser.cpp +++ b/src/lib/support/tests/TestCHIPArgParser.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include diff --git a/src/platform/Darwin/Logging.cpp b/src/platform/Darwin/Logging.cpp index 362977fbd0a3ec..8e25d6a8417542 100644 --- a/src/platform/Darwin/Logging.cpp +++ b/src/platform/Darwin/Logging.cpp @@ -1,5 +1,6 @@ /* See Project chip LICENSE file for licensing information. */ +#include #include #include diff --git a/src/platform/ESP32/Logging.cpp b/src/platform/ESP32/Logging.cpp index a6f7aca403dc09..ee22dc41fa0a53 100644 --- a/src/platform/ESP32/Logging.cpp +++ b/src/platform/ESP32/Logging.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -18,7 +19,7 @@ namespace chip { namespace Logging { namespace Platform { -void LogV(const char * module, uint8_t category, const char * msg, va_list v) +void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v) { char tag[11]; diff --git a/src/platform/Linux/Logging.cpp b/src/platform/Linux/Logging.cpp index ebf3e21b8cab50..5f8aeb57e73575 100644 --- a/src/platform/Linux/Logging.cpp +++ b/src/platform/Linux/Logging.cpp @@ -1,5 +1,6 @@ /* See Project CHIP LICENSE file for licensing information. */ +#include #include #include diff --git a/src/platform/Tizen/Logging.cpp b/src/platform/Tizen/Logging.cpp index 7c8865f1e09d73..4c5c8b8643cfa8 100644 --- a/src/platform/Tizen/Logging.cpp +++ b/src/platform/Tizen/Logging.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -30,7 +31,7 @@ namespace Platform { /** * CHIP log output functions. */ -void LogV(const char * module, uint8_t category, const char * msg, va_list v) +void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v) { constexpr const char * kLogTag = "CHIP"; char msgBuf[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE] = { diff --git a/src/platform/Zephyr/Logging.cpp b/src/platform/Zephyr/Logging.cpp index 017ed0f478313c..5fb3b98aea5c40 100644 --- a/src/platform/Zephyr/Logging.cpp +++ b/src/platform/Zephyr/Logging.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -40,7 +41,7 @@ namespace Platform { * CHIP log output function. */ -void LogV(const char * module, uint8_t category, const char * msg, va_list v) +void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v) { char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]; snprintfcb(formattedMsg, sizeof(formattedMsg), "[%s]", module); diff --git a/src/platform/logging/BUILD.gn b/src/platform/logging/BUILD.gn index bfece14c187760..038f9421d819b2 100644 --- a/src/platform/logging/BUILD.gn +++ b/src/platform/logging/BUILD.gn @@ -16,6 +16,7 @@ if (current_os == "android") { deps = [ ":headers", "${chip_root}/src/lib/core:chip_config_header", + "${chip_root}/src/lib/support:enforce_format", "${chip_root}/src/lib/support:logging_constants", "${chip_root}/src/platform:platform_buildconfig", ] @@ -30,6 +31,7 @@ static_library("stdio") { deps = [ ":headers", "${chip_root}/src/lib/core:chip_config_header", + "${chip_root}/src/lib/support:enforce_format", "${chip_root}/src/lib/support:logging_constants", "${chip_root}/src/platform:platform_buildconfig", ] diff --git a/src/platform/logging/LogV.h b/src/platform/logging/LogV.h index 5b961b7f864e61..5bff38f284969b 100644 --- a/src/platform/logging/LogV.h +++ b/src/platform/logging/LogV.h @@ -2,6 +2,7 @@ #pragma once +#include #include #include @@ -27,7 +28,7 @@ namespace Platform { * correspond to the format specifiers in @a msg. * */ -void LogV(const char * module, uint8_t category, const char * msg, va_list v); +void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v); } // namespace Platform } // namespace Logging diff --git a/src/platform/logging/impl/android/Logging.cpp b/src/platform/logging/impl/android/Logging.cpp index f89044b34d095a..fc6433113bd740 100644 --- a/src/platform/logging/impl/android/Logging.cpp +++ b/src/platform/logging/impl/android/Logging.cpp @@ -1,5 +1,6 @@ /* See Project chip LICENSE file for licensing information. */ +#include #include #include diff --git a/src/platform/logging/impl/stdio/Logging.cpp b/src/platform/logging/impl/stdio/Logging.cpp index 74beb7212b51b9..6eceebb2e961df 100644 --- a/src/platform/logging/impl/stdio/Logging.cpp +++ b/src/platform/logging/impl/stdio/Logging.cpp @@ -1,5 +1,6 @@ /* See Project CHIP LICENSE file for licensing information. */ +#include #include #include diff --git a/src/platform/mbed/Logging.cpp b/src/platform/mbed/Logging.cpp index 0da70e436d08be..30615852363f16 100644 --- a/src/platform/mbed/Logging.cpp +++ b/src/platform/mbed/Logging.cpp @@ -22,6 +22,7 @@ */ #include +#include #include #include #include @@ -65,7 +66,7 @@ char logMsgBuffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]; /** * CHIP log output functions. */ -void LogV(const char * module, uint8_t category, const char * msg, va_list v) +void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v) { size_t prefixLen = 0; snprintf(logMsgBuffer, sizeof(logMsgBuffer), "[%s]", module); diff --git a/src/platform/nxp/k32w/k32w0/Logging.cpp b/src/platform/nxp/k32w/k32w0/Logging.cpp index bf5bc981ee5cde..69db5817e664e9 100644 --- a/src/platform/nxp/k32w/k32w0/Logging.cpp +++ b/src/platform/nxp/k32w/k32w0/Logging.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -96,7 +97,7 @@ void __attribute__((weak)) OnLogOutput(void) {} } // namespace DeviceLayer } // namespace chip -void GenericLog(const char * format, va_list arg, const char * module, uint8_t category) +void ENFORCE_FORMAT(1, 0) GenericLog(const char * format, va_list arg, const char * module, uint8_t category) { #if K32W_LOG_ENABLED @@ -135,7 +136,7 @@ namespace Platform { /** * CHIP log output function. */ -void LogV(const char * module, uint8_t category, const char * msg, va_list v) +void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v) { (void) module; (void) category; diff --git a/src/platform/qpg/Logging.cpp b/src/platform/qpg/Logging.cpp index f5f346e65808d7..b9fb6b4ef0eaf5 100644 --- a/src/platform/qpg/Logging.cpp +++ b/src/platform/qpg/Logging.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -42,7 +43,7 @@ namespace Platform { * CHIP log output function. */ -void LogV(const char * module, uint8_t category, const char * msg, va_list v) +void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char * msg, va_list v) { char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE]; size_t prefixLen;