2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #ifndef __included_warnings_h__
16 #define __included_warnings_h__
18 /* Macros to check compiler version */
20 #define COMPILER_VERSION_GTE(major, minor) \
21 (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
22 #elif defined(__clang__)
23 #define COMPILER_VERSION_GTE(major, minor) \
24 (__clang_major__ > (major) || \
25 (__clang_major__ == (major) && __clang_minor__ >= (minor)))
26 #else /* disable all warning customization for all other compilers */
27 #define COMPILER_VERSION_GTE(maj, min) 0
30 /* '#' needs to preceed a macro parameter */
31 #define WARN_TOSTR(x) #x
34 * Macros to toggle off/on warnings
36 * Start by silencing pragma warnings so that we can explicitly silence
37 * a warning introduced on some compiler version and not get a warning on older
38 * versions of that same compiler.
40 * gcc corresponding warning is "Wpargma"
41 * clang corresponding warning is "Wunknown-warning-option"
43 * For example, Wtautological-compare is introduced in gcc-6 and this would
44 * trigger a Wpargma warning on gcc-5.
46 * Example usage to disable -Wtautological-compare warning:
47 * WARN_OFF(tautological-compare)
49 * WARN_ON(tautological-compare)
50 * ; // conditional code
53 #if defined(__GNUC__) && COMPILER_VERSION_GTE(4, 6)
55 * GCC option to locally ignore warning was introduced in gcc-4.6
56 * gcc.gnu.org/gcc-4.6/changes.html
58 #define WARN_PRAGMA(x) _Pragma (WARN_TOSTR (GCC diagnostic x))
60 WARN_PRAGMA (push) WARN_PRAGMA (ignored "-Wpragmas") \
61 WARN_PRAGMA (push) WARN_PRAGMA (ignored WARN_TOSTR (-W##x))
66 #elif defined(__clang__) && COMPILER_VERSION_GTE(3, 3)
68 * clang option to locally ignore warning was introduced in clang-3.3
69 * releases.llvm.org/3.3/tools/clang/docs/UsersManual.html#controlling-diagnostics-via-pragmas
71 #define WARN_PRAGMA(x) _Pragma (WARN_TOSTR (clang diagnostic x))
73 WARN_PRAGMA (push) WARN_PRAGMA (ignored "-Wunknown-warning-option") \
74 WARN_PRAGMA (push) WARN_PRAGMA (ignored WARN_TOSTR (-W##x))
79 /* Ignore WARN_* instruction for all other compilers */
85 * Clang supports a wider range of warnings than gcc.
86 * Use those specific macros for the warnings that are only supported by clang
89 #define WARN_OFF_CLANG(x) WARN_OFF (x)
90 #define WARN_ON_CLANG(x) WARN_ON (x)
92 #define WARN_OFF_CLANG(x)
93 #define WARN_ON_CLANG(x)
97 * fd.io coding-style-patch-verification: ON
100 * eval: (c-set-style "gnu")
103 #endif /* __included_warnings_h__ */