Skip to content

Commit

Permalink
COMMON: Add a PHAETHON_FALLTHROUGH macro
Browse files Browse the repository at this point in the history
While gcc can parse the "// Fallthrough" comments we use to mark
switch case fallthroughs, clang can not. Instead, clang wants a
[[clang::fallthrough]] attribute.

So we use the PHAETHON_FALLTHROUGH macro to select the appropriate
way to mark a fallthrough.
  • Loading branch information
DrMcCoy committed May 25, 2018
1 parent 58d98b3 commit 814cdb1
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/common/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,41 @@
#define IGNORE_UNUSED_VARIABLES _Pragma("GCC diagnostic ignored \"-Wunused-variable\"")
#endif

/** PHAETHON_FALLTHROUGH is an annotation to suppress compiler warnings about switch
* cases that fall through without a break or return statement. PHAETHON_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++;
* PHAETHON_FALLTHROUGH;
* case 4:
* return foo;
* }
*/
#ifndef __has_cpp_attribute
#define __has_cpp_attribute(x) 0
#endif

#if __has_cpp_attribute(clang::fallthrough)
#define PHAETHON_FALLTHROUGH [[clang::fallthrough]]
#elif __has_cpp_attribute(gnu::fallthrough)
#define PHAETHON_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 PHAETHON_FALLTHROUGH __fallthrough
#else
#define PHAETHON_FALLTHROUGH // Fallthrough
#endif

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

0 comments on commit 814cdb1

Please sign in to comment.