Skip to content

Commit 8406f80

Browse files
[mlir][cmake] Enable -Wundef. (#84011)
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).
1 parent 099045a commit 8406f80

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

mlir/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ endif()
7070
check_c_compiler_flag("-Werror=implicit-function-declaration" C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION)
7171
append_if(C_SUPPORTS_WERROR_IMPLICIT_FUNCTION_DECLARATION "-Werror=implicit-function-declaration" CMAKE_C_FLAGS)
7272

73+
# Warn on undefined macros. This is often an indication that an include to
74+
# `mlir-config.h` or similar is missing.
75+
check_c_compiler_flag("-Wundef" C_SUPPORTS_WUNDEF)
76+
append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_C_FLAGS)
77+
append_if(C_SUPPORTS_WUNDEF "-Wundef" CMAKE_CXX_FLAGS)
78+
7379
# Forbid mismatch between declaration and definition for class vs struct. This is
7480
# harmless on Unix systems, but it'll be a ticking bomb for MSVC/Windows systems
7581
# where it creeps into the ABI.

mlir/include/mlir/Config/mlir-config.h.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
/* This file enumerates variables from the MLIR configuration so that they
1010
can be in exported headers and won't override package specific directives.
11+
Defining the variables here is preferable over specifying them in CMake files
12+
via `target_compile_definitions` because it is easier to ensure that they are
13+
defined consistently across all targets: They are guaranteed to be 0/1
14+
variables thanks to #cmakedefine01, so we can test with `#if` and find
15+
missing definitions or includes with `-Wundef`. With `#ifdef`, these mistakes
16+
can go unnoticed.
17+
1118
This is a C header that can be included in the mlir-c headers. */
1219

1320
#ifndef MLIR_CONFIG_H

0 commit comments

Comments
 (0)