FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
warnings.h
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
12 
13  */
14 
15 #ifndef __included_warnings_h__
16 #define __included_warnings_h__
17 
18 /* Macros to check compiler version */
19 #if defined(__GNUC__)
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
28 #endif
29 
30 /* '#' needs to preceed a macro parameter */
31 #define WARN_TOSTR(x) #x
32 
33 /*
34  * Macros to toggle off/on warnings
35  *
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.
39  *
40  * gcc corresponding warning is "Wpargma"
41  * clang corresponding warning is "Wunknown-warning-option"
42  *
43  * For example, Wtautological-compare is introduced in gcc-6 and this would
44  * trigger a Wpargma warning on gcc-5.
45  *
46  * Example usage to disable -Wtautological-compare warning:
47  * WARN_OFF(tautological-compare)
48  * if (...) {
49  * WARN_ON(tautological-compare)
50  * ; // conditional code
51  * }
52  */
53 #if defined(__GNUC__) && COMPILER_VERSION_GTE(4, 6)
54 /*
55  * GCC option to locally ignore warning was introduced in gcc-4.6
56  * gcc.gnu.org/gcc-4.6/changes.html
57  */
58 #define WARN_PRAGMA(x) _Pragma (WARN_TOSTR (GCC diagnostic x))
59 #define WARN_OFF(x) \
60  WARN_PRAGMA (push) WARN_PRAGMA (ignored "-Wpragmas") \
61  WARN_PRAGMA (push) WARN_PRAGMA (ignored WARN_TOSTR (-W##x))
62 #define WARN_ON(x) \
63  WARN_PRAGMA (pop) \
64  WARN_PRAGMA (pop)
65 
66 #elif defined(__clang__) && COMPILER_VERSION_GTE(3, 3)
67 /*
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
70  */
71 #define WARN_PRAGMA(x) _Pragma (WARN_TOSTR (clang diagnostic x))
72 #define WARN_OFF(x) \
73  WARN_PRAGMA (push) WARN_PRAGMA (ignored "-Wunknown-warning-option") \
74  WARN_PRAGMA (push) WARN_PRAGMA (ignored WARN_TOSTR (-W##x))
75 #define WARN_ON(x) \
76  WARN_PRAGMA (pop) \
77  WARN_PRAGMA (pop)
78 #else
79 /* Ignore WARN_* instruction for all other compilers */
80 #define WARN_OFF(x)
81 #define WARN_ON(x)
82 #endif
83 
84 /*
85  * Clang supports a wider range of warnings than gcc.
86  * Use those specific macros for the warnings that are only supported by clang
87  */
88 #ifdef __clang__
89 #define WARN_OFF_CLANG(x) WARN_OFF (x)
90 #define WARN_ON_CLANG(x) WARN_ON (x)
91 #else
92 #define WARN_OFF_CLANG(x)
93 #define WARN_ON_CLANG(x)
94 #endif
95 
96 /*
97  * fd.io coding-style-patch-verification: ON
98  *
99  * Local Variables:
100  * eval: (c-set-style "gnu")
101  * End:
102  */
103 #endif /* __included_warnings_h__ */