Skip to content

Commit

Permalink
COMMON: Move XOREOS_FALLTHROUGH into its own file, fallthrough.h
Browse files Browse the repository at this point in the history
To be more in line with xoreos proper.
  • Loading branch information
DrMcCoy committed May 25, 2018
1 parent 789add1 commit b31ed4e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 35 deletions.
63 changes: 63 additions & 0 deletions src/common/fallthrough.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* xoreos-tools - Tools to help with xoreos development
*
* xoreos-tools is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos-tools is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos-tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos-tools. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Compiler-specific defines to mark an implicit switch-case fallthrough.
*/

#ifndef COMMON_FALLTHROUGH_H
#define COMMON_FALLTHROUGH_H

/* XOREOS_FALLTHROUGH is an annotation to suppress compiler warnings about switch
* cases that fall through without a break or return statement. XOREOS_FALLTHROUGH
* is only needed on cases that have code.
*
* Based on Mozilla's MOZ_FALLTHROUGH and Boost's BOOST_FALLTHROUGH.
*
* switch (foo) {
* case 1: // These cases have no code. No fallthrough annotations are needed.
* case 2:
* case 3: // This case has code, so a fallthrough annotation is needed!
* foo++;
* XOREOS_FALLTHROUGH;
* case 4:
* return foo;
* }
*/
#ifndef __has_cpp_attribute
#define __has_cpp_attribute(x) 0
#endif

#if __has_cpp_attribute(clang::fallthrough)
#define XOREOS_FALLTHROUGH [[clang::fallthrough]]
#elif __has_cpp_attribute(gnu::fallthrough)
#define XOREOS_FALLTHROUGH [[gnu::fallthrough]]
#elif defined(_MSC_VER)
/*
* MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
* https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
*/
#include <sal.h>
#define XOREOS_FALLTHROUGH __fallthrough
#else
#define XOREOS_FALLTHROUGH // Fallthrough
#endif

#endif // COMMON_FALLTHROUGH_H
1 change: 1 addition & 0 deletions src/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ src_common_libcommon_la_SOURCES =
src_common_libcommon_la_SOURCES += \
src/common/system.h \
src/common/noreturn.h \
src/common/fallthrough.h \
src/common/types.h \
src/common/endianness.h \
src/common/deallocator.h \
Expand Down
36 changes: 1 addition & 35 deletions src/common/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#endif

#include "src/common/noreturn.h"
#include "src/common/fallthrough.h"

#if defined(_MSC_VER)

Expand Down Expand Up @@ -176,41 +177,6 @@
#define IGNORE_UNUSED_VARIABLES _Pragma("GCC diagnostic ignored \"-Wunused-variable\"")
#endif

/** XOREOS_FALLTHROUGH is an annotation to suppress compiler warnings about switch
* cases that fall through without a break or return statement. XOREOS_FALLTHROUGH
* is only needed on cases that have code.
*
* Based on Mozilla's MOZ_FALLTHROUGH and Boost's BOOST_FALLTHROUGH.
*
* switch (foo) {
* case 1: // These cases have no code. No fallthrough annotations are needed.
* case 2:
* case 3: // This case has code, so a fallthrough annotation is needed!
* foo++;
* XOREOS_FALLTHROUGH;
* case 4:
* return foo;
* }
*/
#ifndef __has_cpp_attribute
#define __has_cpp_attribute(x) 0
#endif

#if __has_cpp_attribute(clang::fallthrough)
#define XOREOS_FALLTHROUGH [[clang::fallthrough]]
#elif __has_cpp_attribute(gnu::fallthrough)
#define XOREOS_FALLTHROUGH [[gnu::fallthrough]]
#elif defined(_MSC_VER)
/*
* MSVC's __fallthrough annotations are checked by /analyze (Code Analysis):
* https://msdn.microsoft.com/en-us/library/ms235402%28VS.80%29.aspx
*/
#include <sal.h>
#define XOREOS_FALLTHROUGH __fallthrough
#else
#define XOREOS_FALLTHROUGH // Fallthrough
#endif

//
// Fallbacks for various functions
//
Expand Down

0 comments on commit b31ed4e

Please sign in to comment.