xoreos  0.0.5
fallthrough.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef COMMON_FALLTHROUGH_H
26 #define COMMON_FALLTHROUGH_H
27 
28 /* XOREOS_FALLTHROUGH is an annotation to suppress compiler warnings about switch
29  * cases that fall through without a break or return statement. XOREOS_FALLTHROUGH
30  * is only needed on cases that have code.
31  *
32  * Based on Mozilla's MOZ_FALLTHROUGH and Boost's BOOST_FALLTHROUGH.
33  *
34  * switch (foo) {
35  * case 1: // These cases have no code. No fallthrough annotations are needed.
36  * case 2:
37  * case 3: // This case has code, so a fallthrough annotation is needed!
38  * foo++;
39  * XOREOS_FALLTHROUGH;
40  * case 4:
41  * return foo;
42  * }
43  */
44 #ifndef __has_cpp_attribute
45  #define __has_cpp_attribute(x) 0
46 #endif
47 
48 #if __has_cpp_attribute(clang::fallthrough)
49  #define XOREOS_FALLTHROUGH [[clang::fallthrough]]
50 #elif __has_cpp_attribute(gnu::fallthrough)
51  #define XOREOS_FALLTHROUGH [[gnu::fallthrough]]
52 #elif defined(_MSC_VER)
53  /*
54  * MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
55  * https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
56  */
57  #include <sal.h>
58  #define XOREOS_FALLTHROUGH __fallthrough
59 #else
60  #define XOREOS_FALLTHROUGH // Fallthrough
61 #endif
62 
63 #if defined(__clang__)
64  /* Macros to make clang ignore implicit fallthroughs in certain includes.
65  *
66  * This is needed for the SDL2 headers. Because sdl2-config puts the SDL2
67  * path into -I instead of -isystem, and SDL2 marks implicit fallthroughs
68  * with a comment instead of an attribute -- something which clang doesn't
69  * understand -- clang spits out a warning for the SDL2 headers.
70  *
71  * We don't want this, so we disable this warning for the SDL2 headers.
72  *
73  * Usage: wrap the include in these two macros.
74  */
75  #define START_IGNORE_IMPLICIT_FALLTHROUGH _Pragma("clang diagnostic push") \
76  _Pragma("clang diagnostic ignored \"-Wimplicit-fallthrough\"")
77  #define STOP_IGNORE_IMPLICIT_FALLTHROUGH _Pragma("clang diagnostic pop")
78 #else
79  #define START_IGNORE_IMPLICIT_FALLTHROUGH
80  #define STOP_IGNORE_IMPLICIT_FALLTHROUGH
81 #endif
82 
83 #endif // COMMON_FALLTHROUGH_H