Debian Codegraph @

libdrgn/m4/my_c_switch_enum.m4

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 # Check whether our SWITCH_ENUM macro works with the current compiler. There
5 # are a few known limitations:
6 #
7 # - Before GCC 12.1, GCC can't parse it. This was fixed by GCC commit
8 # 1bf976a5de69 ("openmp: Actually ignore pragma_stmt pragmas for which
9 # c_parser_pragma returns false").
10 # - Before GCC 12.2, GCC doesn't actually apply the warnings. This was fixed by
11 # GCC commit 98e2676558f6 ("c: Fix location for _Pragma tokens [PR97498]").
12 # - Before Clang 18.1, Clang ignores -Wswitch-default. It was implemented in
13 # llvm-project commit c28178298513 ("[clang][Sema] Add -Wswitch-default
14 # warning option (#73077)").
15 #
16 # Keep this in sync with util.h.
17 AC_DEFUN([MY_C_SWITCH_ENUM],
18 [AC_CACHE_CHECK([whether SWITCH_ENUM compiles], [my_cv_c_switch_enum_compiles],
19 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
20 #define SWITCH_ENUM(expr) \
21 _Pragma("GCC diagnostic push") \
22 _Pragma("GCC diagnostic error \"-Wswitch-enum\"") \
23 _Pragma("GCC diagnostic error \"-Wswitch-default\"") \
24 switch (expr) \
25 _Pragma("GCC diagnostic pop")
26
27 int main(void)
28 {
29 enum { FOO, BAR } x;
30 SWITCH_ENUM(x) {
31 case FOO:
32 case BAR:
33 default:
34 break;
35 }
36 return 0;
37 }
38 ]])], [my_cv_c_switch_enum_compiles=yes], [my_cv_c_switch_enum_compiles=no])])
39
40 if test "x$my_cv_c_switch_enum_compiles" = xyes; then
41 dnl Now we know that the macro compiles. Check whether it actually
42 dnl works. We don't do anything with this beyond logging it.
43 AC_CACHE_CHECK([whether SWITCH_ENUM catches missing enumeration values],
44 [my_cv_c_switch_enum_works],
45 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
46 #define SWITCH_ENUM(expr) \
47 _Pragma("GCC diagnostic push") \
48 _Pragma("GCC diagnostic error \"-Wswitch-enum\"") \
49 _Pragma("GCC diagnostic error \"-Wswitch-default\"") \
50 switch (expr) \
51 _Pragma("GCC diagnostic pop")
52
53 int main(void)
54 {
55 enum { FOO, BAR } x;
56 SWITCH_ENUM(x) {
57 case FOO:
58 default:
59 break;
60 }
61 return 0;
62 }
63 ]])], [my_cv_c_switch_enum_works=no], [my_cv_c_switch_enum_works=yes])])
64 AC_CACHE_CHECK([whether SWITCH_ENUM catches missing default case],
65 [my_cv_c_switch_enum_default_works],
66 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
67 #define SWITCH_ENUM(expr) \
68 _Pragma("GCC diagnostic push") \
69 _Pragma("GCC diagnostic error \"-Wswitch-enum\"") \
70 _Pragma("GCC diagnostic error \"-Wswitch-default\"") \
71 switch (expr) \
72 _Pragma("GCC diagnostic pop")
73
74 int main(void)
75 {
76 enum { FOO, BAR } x;
77 SWITCH_ENUM(x) {
78 case FOO:
79 case BAR:
80 break;
81 }
82 return 0;
83 }
84 ]])], [my_cv_c_switch_enum_default_works=no], [my_cv_c_switch_enum_default_works=yes])])
85 else
86 AC_DEFINE([SWITCH_ENUM(expr)], [switch (expr)])
87 fi
88 ])