From 9895ee085d445ae3ae6becdddfb9c8f9d8f6c1f5 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Wed, 23 Mar 2016 12:19:45 -0700 Subject: [PATCH] Use C++'s standardized [[noreturn]] attribute Summary:Use C++'s standardized `[[noreturn]]` attribute. All supported compilers (and some unsupported compilers) also support the standardized syntax. GCC >= 4.8, Clang >= 3.0, and MSVC >= 2015 have direct support for the C++'s standardized way of writing the attribute. Clang - http://goo.gl/ftJGVM GCC 4.8.2 - http://goo.gl/ORCVOD ICC 13.0.1 - http://goo.gl/I5tn5I MSVC 2015 - https://msdn.microsoft.com/en-us/library/hh567368.aspx (Regardling Clang, earlier versions may support it. 3.0 was the earliest Clang version listed at godbolt.com, so that's as far back as I went.) Therefore, we no longer need to use the compiler-specific syntaxes, or use preprocessor macros with per-compiler definitions: __attribute__((__noreturn__)) __attribute__((noreturn)) __declspec(noreturn) Reviewed By: Orvid Differential Revision: D3073621 fb-gh-sync-id: 32d4771d1bf1974693b8574fa2d39c9559872945 shipit-source-id: 32d4771d1bf1974693b8574fa2d39c9559872945 --- folly/Exception.h | 11 +++-------- folly/FormatArg.h | 2 +- folly/Indestructible.h | 2 +- folly/Portability.h | 9 --------- folly/SafeAssert.h | 10 ++++++---- folly/Subprocess.cpp | 3 +-- folly/detail/FunctionalExcept.h | 8 ++++---- folly/experimental/bser/Load.cpp | 2 +- .../exception_tracer/ExceptionTracerLib.cpp | 9 +++++---- folly/experimental/io/HugePageUtil.cpp | 4 +--- .../experimental/test/NestedCommandLineAppExample.cpp | 4 ++-- 11 files changed, 25 insertions(+), 39 deletions(-) diff --git a/folly/Exception.h b/folly/Exception.h index d15dcca1..30444b2b 100644 --- a/folly/Exception.h +++ b/folly/Exception.h @@ -37,24 +37,19 @@ namespace folly { // The *Explicit functions take an explicit value for errno. // Helper to throw std::system_error -FOLLY_NORETURN void throwSystemErrorExplicit(int err, const char*); -inline void throwSystemErrorExplicit(int err, const char* msg) { +[[noreturn]] inline void throwSystemErrorExplicit(int err, const char* msg) { throw std::system_error(err, std::system_category(), msg); } template -FOLLY_NORETURN void throwSystemErrorExplicit(int, Args&&... args); -template -void throwSystemErrorExplicit(int err, Args&&... args) { +[[noreturn]] void throwSystemErrorExplicit(int err, Args&&... args) { throwSystemErrorExplicit( err, to(std::forward(args)...).c_str()); } // Helper to throw std::system_error from errno and components of a string template -FOLLY_NORETURN void throwSystemError(Args&&... args); -template -void throwSystemError(Args&&... args) { +[[noreturn]] void throwSystemError(Args&&... args) { throwSystemErrorExplicit(errno, std::forward(args)...); } diff --git a/folly/FormatArg.h b/folly/FormatArg.h index 8ea863be..127745c4 100644 --- a/folly/FormatArg.h +++ b/folly/FormatArg.h @@ -82,7 +82,7 @@ struct FormatArg { template std::string errorStr(Args&&... args) const; template - FOLLY_NORETURN void error(Args&&... args) const; + [[noreturn]] void error(Args&&... args) const; /** * Full argument string, as passed in to the constructor. diff --git a/folly/Indestructible.h b/folly/Indestructible.h index 4e01ab86..39c8f151 100644 --- a/folly/Indestructible.h +++ b/folly/Indestructible.h @@ -101,7 +101,7 @@ class Indestructible final { } } - FOLLY_NORETURN FOLLY_NOINLINE static void fail() { + [[noreturn]] FOLLY_NOINLINE static void fail() { LOG(FATAL) << "Indestructible is not initialized"; } diff --git a/folly/Portability.h b/folly/Portability.h index 1da837e8..efef0184 100644 --- a/folly/Portability.h +++ b/folly/Portability.h @@ -79,15 +79,6 @@ constexpr bool kHasUnalignedAccess = false; # define FOLLY_DEPRECATED(msg) #endif -// noreturn -#if defined(_MSC_VER) -# define FOLLY_NORETURN __declspec(noreturn) -#elif defined(__clang__) || defined(__GNUC__) -# define FOLLY_NORETURN __attribute__((__noreturn__)) -#else -# define FOLLY_NORETURN -#endif - // noinline #ifdef _MSC_VER # define FOLLY_NOINLINE __declspec(noinline) diff --git a/folly/SafeAssert.h b/folly/SafeAssert.h index 804c2c9d..65d2e9f9 100644 --- a/folly/SafeAssert.h +++ b/folly/SafeAssert.h @@ -43,10 +43,12 @@ namespace folly { namespace detail { -FOLLY_NORETURN void assertionFailure(const char* expr, const char* msg, - const char* file, unsigned int line, - const char* function); - +[[noreturn]] void assertionFailure( + const char* expr, + const char* msg, + const char* file, + unsigned int line, + const char* function); }} // namespace folly #endif /* FOLLY_SAFEASSERT_H_ */ diff --git a/folly/Subprocess.cpp b/folly/Subprocess.cpp index c40630a5..769cbebb 100644 --- a/folly/Subprocess.cpp +++ b/folly/Subprocess.cpp @@ -210,8 +210,7 @@ struct ChildErrorInfo { int errnoValue; }; -FOLLY_NORETURN void childError(int errFd, int errCode, int errnoValue); -void childError(int errFd, int errCode, int errnoValue) { +[[noreturn]] void childError(int errFd, int errCode, int errnoValue) { ChildErrorInfo info = {errCode, errnoValue}; // Write the error information over the pipe to our parent process. // We can't really do anything else if this write call fails. diff --git a/folly/detail/FunctionalExcept.h b/folly/detail/FunctionalExcept.h index b0018779..575da6f3 100644 --- a/folly/detail/FunctionalExcept.h +++ b/folly/detail/FunctionalExcept.h @@ -23,12 +23,12 @@ FOLLY_NAMESPACE_STD_BEGIN -FOLLY_NORETURN void __throw_length_error(const char* msg); -FOLLY_NORETURN void __throw_logic_error(const char* msg); -FOLLY_NORETURN void __throw_out_of_range(const char* msg); +[[noreturn]] void __throw_length_error(const char* msg); +[[noreturn]] void __throw_logic_error(const char* msg); +[[noreturn]] void __throw_out_of_range(const char* msg); #ifdef _MSC_VER -FOLLY_NORETURN void __throw_bad_alloc(); +[[noreturn]] void __throw_bad_alloc(); #endif FOLLY_NAMESPACE_STD_END diff --git a/folly/experimental/bser/Load.cpp b/folly/experimental/bser/Load.cpp index 874037b4..9898de8a 100644 --- a/folly/experimental/bser/Load.cpp +++ b/folly/experimental/bser/Load.cpp @@ -25,7 +25,7 @@ namespace bser { static dynamic parseBser(Cursor& curs); template -static FOLLY_NORETURN void throwDecodeError(Cursor& curs, ARGS&&... args) { +[[noreturn]] static void throwDecodeError(Cursor& curs, ARGS&&... args) { throw BserDecodeError(folly::to(std::forward(args)..., " with ", curs.length(), diff --git a/folly/experimental/exception_tracer/ExceptionTracerLib.cpp b/folly/experimental/exception_tracer/ExceptionTracerLib.cpp index d0ac4baa..2d2c0133 100644 --- a/folly/experimental/exception_tracer/ExceptionTracerLib.cpp +++ b/folly/experimental/exception_tracer/ExceptionTracerLib.cpp @@ -27,11 +27,12 @@ namespace __cxxabiv1 { extern "C" { -FOLLY_NORETURN void __cxa_throw(void* thrownException, - std::type_info* type, - void (*destructor)(void*)); +void __cxa_throw( + void* thrownException, + std::type_info* type, + void (*destructor)(void*)) __attribute__((__noreturn__)); void* __cxa_begin_catch(void* excObj) throw(); -FOLLY_NORETURN void __cxa_rethrow(void); +void __cxa_rethrow(void) __attribute__((__noreturn__)); void __cxa_rethrow(void); void __cxa_end_catch(void); } diff --git a/folly/experimental/io/HugePageUtil.cpp b/folly/experimental/io/HugePageUtil.cpp index 8f58ad39..d3ba4b52 100644 --- a/folly/experimental/io/HugePageUtil.cpp +++ b/folly/experimental/io/HugePageUtil.cpp @@ -41,9 +41,7 @@ using namespace folly; namespace { -FOLLY_NORETURN void usage(const char* name); - -void usage(const char* name) { +[[noreturn]] void usage(const char* name) { std::cerr << folly::format( "Usage: {0}\n" " list all huge page sizes and their mount points\n" diff --git a/folly/experimental/test/NestedCommandLineAppExample.cpp b/folly/experimental/test/NestedCommandLineAppExample.cpp index 6dfa2c2b..a07dfa9d 100644 --- a/folly/experimental/test/NestedCommandLineAppExample.cpp +++ b/folly/experimental/test/NestedCommandLineAppExample.cpp @@ -55,11 +55,11 @@ class Concatenator { size_t lineNumber_ = 0; }; -FOLLY_NORETURN void throwOutputError() { +[[noreturn]] void throwOutputError() { throw OutputError(folly::errnoStr(errno).toStdString()); } -FOLLY_NORETURN void throwInputError() { +[[noreturn]] void throwInputError() { throw InputError(folly::errnoStr(errno).toStdString()); } -- 2.34.1