summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Hughes <tomhughes@chromium.org>2022-08-29 16:00:24 -0700
committerChromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-10-13 19:57:39 +0000
commitdc6a5f010be58345289bfb9d2f86153ef35b2223 (patch)
tree133ba24d0d177079285c7a43ee70c66ca89c9990
parentb6e08b63154cd9370f5206b7a3f928b44c31478d (diff)
downloadchrome-ec-dc6a5f010be58345289bfb9d2f86153ef35b2223.tar.gz
include/compiler.h: Add generic macro for disabling compiler warnings
These macros are easier to read and use than the underlying pragmas. BRANCH=none BUG=b:172020503 TEST=make buildall Signed-off-by: Tom Hughes <tomhughes@chromium.org> Change-Id: I2f67cbb3f83230b7e1a6a4512471bbd2bec0498f Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3872552 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Code-Coverage: Zoss <zoss-cl-coverage@prod.google.com>
-rw-r--r--chip/stm32/usb_endpoints.c10
-rw-r--r--common/acpi.c9
-rw-r--r--common/panic_output.c18
-rw-r--r--core/cortex-m/vecttable.c17
-rw-r--r--core/cortex-m0/vecttable.c10
-rw-r--r--include/compiler.h51
-rw-r--r--test/printf.c8
-rw-r--r--test/stdlib.c8
8 files changed, 78 insertions, 53 deletions
diff --git a/chip/stm32/usb_endpoints.c b/chip/stm32/usb_endpoints.c
index b435a88846..cf8cb17869 100644
--- a/chip/stm32/usb_endpoints.c
+++ b/chip/stm32/usb_endpoints.c
@@ -7,6 +7,7 @@
#include <stdint.h>
#include <stddef.h>
+#include "compiler.h"
#include "config.h"
#include "common.h"
#include "usb_hw.h"
@@ -73,10 +74,7 @@ int iface_undefined(iface_arguments)
* subobject", since we are explicitly doing this to handle the unused
* endpoints.
*/
-#ifdef __clang__
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Winitializer-overrides"
-#endif /* __clang__ */
+DISABLE_CLANG_WARNING("-Winitializer-overrides")
/* align function pointers on a 32-bit boundary */
#define table(type, name, x) \
@@ -138,9 +136,7 @@ table(xfer_func, usb_ep_tx,
#endif
#if PASS == 2
-#ifdef __clang__
-#pragma clang diagnostic pop
-#endif /* __clang__ */
+ ENABLE_CLANG_WARNING("-Winitializer-overrides")
#endif
#if PASS == 1
diff --git a/common/acpi.c b/common/acpi.c
index 86217ab1ba..c6fcfce417 100644
--- a/common/acpi.c
+++ b/common/acpi.c
@@ -134,19 +134,14 @@ static int acpi_read(uint8_t addr)
uint8_t *memmap_addr = (uint8_t *)(lpc_get_memmap_range() + addr -
EC_ACPI_MEM_MAPPED_BEGIN);
-#ifdef __clang__
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
-#endif /* __clang__ */
+ DISABLE_CLANG_WARNING("-Wtautological-constant-out-of-range-compare");
/* Check for out-of-range read. */
if (addr < EC_ACPI_MEM_MAPPED_BEGIN ||
addr >= EC_ACPI_MEM_MAPPED_BEGIN + EC_ACPI_MEM_MAPPED_SIZE) {
CPRINTS("ACPI read 0x%02x (ignored)", acpi_addr);
return 0xff;
}
-#ifdef __clang__
-#pragma clang diagnostic pop
-#endif /* __clang__ */
+ ENABLE_CLANG_WARNING("-Wtautological-constant-out-of-range-compare");
/* Read from cache if enabled (burst mode). */
if (acpi_read_cache.enabled) {
diff --git a/common/panic_output.c b/common/panic_output.c
index c08e895ecf..b1fab3bebe 100644
--- a/common/panic_output.c
+++ b/common/panic_output.c
@@ -304,14 +304,10 @@ DECLARE_HOOK(HOOK_CHIPSET_RESET, panic_init, HOOK_PRIO_LAST);
* Disable infinite recursion warning, since we're intentionally doing that
* here.
*/
-#ifdef __clang__
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Winfinite-recursion"
-#endif /* __clang__ */
+DISABLE_CLANG_WARNING("-Winfinite-recursion")
#if __GNUC__ >= 12
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Winfinite-recursion"
-#endif /* __GNUC__ >= 12 */
+DISABLE_GCC_WARNING("-Winfinite-recursion")
+#endif
static void stack_overflow_recurse(int n)
{
ccprintf("+%d", n);
@@ -330,12 +326,10 @@ static void stack_overflow_recurse(int n)
*/
ccprintf("-%d", n);
}
+ENABLE_CLANG_WARNING("-Winfinite-recursion")
#if __GNUC__ >= 12
-#pragma GCC diagnostic pop
-#endif /* __GNUC__ >= 12 */
-#ifdef __clang__
-#pragma clang diagnostic pop
-#endif /* __clang__ */
+ENABLE_GCC_WARNING("-Winfinite-recursion")
+#endif
#endif /* CONFIG_CMD_STACKOVERFLOW */
/*****************************************************************************/
diff --git a/core/cortex-m/vecttable.c b/core/cortex-m/vecttable.c
index 433898c00a..037bc28c36 100644
--- a/core/cortex-m/vecttable.c
+++ b/core/cortex-m/vecttable.c
@@ -7,6 +7,7 @@
#ifndef ___INIT
#define ___INIT
+#include "compiler.h"
#include "config.h"
#include <task.h>
#endif
@@ -39,13 +40,10 @@ void default_handler(void)
extern void stack_end(void); /* not technically correct, it's just a pointer */
extern void reset(void);
-#pragma GCC diagnostic push
-#if __GNUC__ >= 8
-#pragma GCC diagnostic ignored "-Wattribute-alias"
-#endif
+DISABLE_GCC_WARNING("-Wattribute-alias")
/* Call default_handler if svc_handler is not found (task.c is not built) */
void weak_with_default svc_handler(int desched, task_id_t resched);
-#pragma GCC diagnostic pop
+ENABLE_GCC_WARNING("-Wattribute-alias")
/*
* SVC handler helper
@@ -91,10 +89,7 @@ void svc_helper_handler(void)
/* Disable warning that "initializer overrides prior initialization of this
* subobject", since we are explicitly doing this to handle the unused IRQs.
*/
-#ifdef __clang__
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Winitializer-overrides"
-#endif /* __clang__ */
+DISABLE_CLANG_WARNING("-Winitializer-overrides")
#define table(x) \
const func vectors[] __attribute__((section( \
@@ -156,9 +151,7 @@ table(item(stack_end) item(reset) vec(nmi) vec(hard_fault) vec(mpu_fault) vec(
irq(239))
#if PASS == 2
-#ifdef __clang__
-#pragma clang diagnostic pop
-#endif /* __clang__ */
+ ENABLE_CLANG_WARNING("-Winitializer-overrides")
#endif
#if PASS == 1
diff --git a/core/cortex-m0/vecttable.c b/core/cortex-m0/vecttable.c
index 3871a30055..7fd5c7fb8f 100644
--- a/core/cortex-m0/vecttable.c
+++ b/core/cortex-m0/vecttable.c
@@ -10,6 +10,7 @@
#include <stddef.h>
#include <stdint.h>
+#include "compiler.h"
#include "config.h"
#include "panic-internal.h"
#include "task.h"
@@ -66,10 +67,7 @@ extern void reset(void);
/* Disable warning that "initializer overrides prior initialization of this
* subobject", since we are explicitly doing this to handle the unused IRQs.
*/
-#ifdef __clang__
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Winitializer-overrides"
-#endif /* __clang__ */
+DISABLE_CLANG_WARNING("-Winitializer-overrides")
#define table(x) \
const func vectors[] __attribute__(( \
@@ -95,9 +93,7 @@ table(item(stack_end) item(reset) vec(nmi) vec(hard_fault) vec(mpu_fault) vec(
irq(31));
#if PASS == 2
-#ifdef __clang__
-#pragma clang diagnostic pop
-#endif /* __clang__ */
+ENABLE_CLANG_WARNING("-Winitializer-overrides")
#endif
#if PASS == 1
diff --git a/include/compiler.h b/include/compiler.h
index d684308c95..736fac4850 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -42,4 +42,55 @@
#define FORWARD_DECLARE_ENUM(x) enum x
#endif /* __cplusplus */
+#define _DO_PRAGMA(x) _Pragma(#x)
+
+#define _DISABLE_COMPILER_WARNING(compiler, warning) \
+ _DO_PRAGMA(compiler diagnostic push) \
+ _DO_PRAGMA(compiler diagnostic ignored warning)
+
+#define _ENABLE_COMPILER_WARNING(compiler, warning) \
+ _DO_PRAGMA(compiler diagnostic pop)
+
+/**
+ * Disable the specified compiler warning for both clang and gcc.
+ */
+#define DISABLE_COMPILER_WARNING(warning) \
+ _DISABLE_COMPILER_WARNING(GCC, warning)
+
+/**
+ * Re-enable the specified compiler warning for both clang and gcc. Can only be
+ * used after a call to DISABLE_COMPILER_WARNING.
+ */
+#define ENABLE_COMPILER_WARNING(warning) _ENABLE_COMPILER_WARNING(GCC, warning)
+
+#ifdef __clang__
+/**
+ * Disable the specified compiler warning for clang.
+ */
+#define DISABLE_CLANG_WARNING(warning) _DISABLE_COMPILER_WARNING(clang, warning)
+/**
+ * Re-enable the specified compiler warning for clang. Can only be used after
+ * a call to DISABLE_CLANG_WARNING.
+ */
+#define ENABLE_CLANG_WARNING(warning) _ENABLE_COMPILER_WARNING(clang, warning)
+#else
+#define DISABLE_CLANG_WARNING(warning)
+#define ENABLE_CLANG_WARNING(warning)
+#endif
+
+#if defined(__GNUC__) && !defined(__clang__)
+/**
+ * Disable the specified compiler warning for gcc.
+ */
+#define DISABLE_GCC_WARNING(warning) _DISABLE_COMPILER_WARNING(GCC, warning)
+/**
+ * Re-enable the specified compiler warning for clang. Can only be used after
+ * a call to DISABLE_GCC_WARNING.
+ */
+#define ENABLE_GCC_WARNING(warning) _ENABLE_COMPILER_WARNING(GCC, warning)
+#else
+#define DISABLE_GCC_WARNING(warning)
+#define ENABLE_GCC_WARNING(warning)
+#endif
+
#endif /* __CROS_EC_COMPILER_H */
diff --git a/test/printf.c b/test/printf.c
index 728aa9b4a8..29ffdfcea4 100644
--- a/test/printf.c
+++ b/test/printf.c
@@ -8,6 +8,7 @@
#include <stddef.h>
#include "common.h"
+#include "compiler.h"
#include "printf.h"
#include "test_util.h"
#include "util.h"
@@ -178,8 +179,8 @@ test_static int test_vsnprintf_int(void)
* space had been available. Thus, a return value of size or
* more means that the output was truncated.
*/
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wformat-truncation"
+ DISABLE_COMPILER_WARNING("-Wformat-truncation");
+
ret = SNPRINTF(output, 4, "%5d", 123);
TEST_ASSERT_ARRAY_EQ(output, " 1", 4);
TEST_EQ(ret, 5, "%d");
@@ -195,7 +196,8 @@ test_static int test_vsnprintf_int(void)
ret = SNPRINTF(output, 4, "%.10d", 123);
TEST_ASSERT_ARRAY_EQ(output, "000", 4);
TEST_EQ(ret, 10, "%d");
-#pragma GCC diagnostic pop
+
+ ENABLE_COMPILER_WARNING("-Wformat-truncation");
}
if (use_builtin_stdlib) {
diff --git a/test/stdlib.c b/test/stdlib.c
index efff64829d..909502aee8 100644
--- a/test/stdlib.c
+++ b/test/stdlib.c
@@ -6,6 +6,7 @@
*/
#include "common.h"
+#include "compiler.h"
#include "console.h"
#include "system.h"
#include "printf.h"
@@ -237,12 +238,9 @@ static int test_strncpy(void)
* error: ‘__builtin_strncpy’ output truncated copying 10 bytes from a
* string of length 12 [-Werror=stringop-truncation]
*/
-#pragma GCC diagnostic push
-#if defined(__GNUC__) && __GNUC__ >= 8
-#pragma GCC diagnostic ignored "-Wstringop-truncation"
-#endif
+ DISABLE_GCC_WARNING("-Wstringop-truncation");
strncpy(dest, "testtesttest", 10);
-#pragma GCC diagnostic pop
+ ENABLE_GCC_WARNING("-Wstringop-truncation");
TEST_ASSERT_ARRAY_EQ("testtestte", dest, 10);
return EC_SUCCESS;