[Mlir-commits] [mlir] [mlir][cmake] Enable -Wundef. (PR #84011)

Ingo Müller llvmlistbot at llvm.org
Wed Mar 6 01:25:11 PST 2024


https://github.com/ingomueller-net updated https://github.com/llvm/llvm-project/pull/84011

>From ed2dfcfa5bd1b69b50a84cbb90fe98880394ab7e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ingo=20M=C3=BCller?= <ingomueller at google.com>
Date: Tue, 5 Mar 2024 13:06:28 +0000
Subject: [PATCH 1/2] [mlir][cmake] Enable -Wundef.

This is another follow-up of #83004, which fixed a bug due to some
macros not being defined in some situations. By raising warnings on
undefined macros, this kind of bug is less likely to be introduced.
Similar to #83004, the fix is probably adding an include to
`mlir-config.h` (and potentially defining the macro there).
---
 mlir/CMakeLists.txt | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt
index e37cb465b9196f..5c4301af040b47 100644
--- a/mlir/CMakeLists.txt
+++ b/mlir/CMakeLists.txt
@@ -70,6 +70,12 @@ endif()
 check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)
 append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_FLAGS)
 
+# Warn on undefined macros. This is often an indication that an include to
+# `mlir-config.h` or similar is missing.
+check_c_compiler_flag("-Wundef" C_SUPPORTS_WUNDEF)
+append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_C_FLAGS)
+append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_CXX_FLAGS)
+
 # Forbid mismatch between declaration and definition for class vs struct. This is
 # harmless on Unix systems, but it'll be a ticking bomb for MSVC/Windows systems
 # where it creeps into the ABI.

>From 916e4ba3da2680fff728ad3ddbe2faa35e0838ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ingo=20M=C3=BCller?= <ingomueller at google.com>
Date: Wed, 6 Mar 2024 09:24:46 +0000
Subject: [PATCH 2/2] Add rationale to cmake-config.h.cmake.

---
 mlir/include/mlir/Config/mlir-config.h.cmake | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/mlir/include/mlir/Config/mlir-config.h.cmake b/mlir/include/mlir/Config/mlir-config.h.cmake
index a0176063b47404..b38708186f87cd 100644
--- a/mlir/include/mlir/Config/mlir-config.h.cmake
+++ b/mlir/include/mlir/Config/mlir-config.h.cmake
@@ -8,6 +8,13 @@
 
 /* This file enumerates variables from the MLIR configuration so that they
    can be in exported headers and won't override package specific directives.
+   Defining the variables here is preferable over specifying them in CMake files
+   via `target_compile_definitions` because it is easier to ensure that they are
+   defined consistently across all targets: They are guaranteed to be 0/1
+   variables thanks to #cmakedefine01, so we can test with `#if` and find
+   missing definitions or includes with `-Wundef`. With `#ifdef`, these mistakes
+   can go unnoticed.
+
    This is a C header that can be included in the mlir-c headers. */
 
 #ifndef MLIR_CONFIG_H



More information about the Mlir-commits mailing list