| 1 |
#ifndef RBIMPL_ATTR_NOEXCEPT_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_ATTR_NOEXCEPT_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Defines #RBIMPL_ATTR_NOEXCEPT. |
| 22 |
* |
| 23 |
* This isn't actually an attribute in C++ but who cares... |
| 24 |
* |
| 25 |
* Mainly due to aesthetic reasons, this one is rarely used in the project. |
| 26 |
* But can be handy on occasions, especially when a function's noexcept-ness |
| 27 |
* depends on its calling functions. |
| 28 |
* |
| 29 |
* ### Q&A ### |
| 30 |
* |
| 31 |
* - Q: Can a function that raises Ruby exceptions be attributed `noexcept`? |
| 32 |
* |
| 33 |
* - A: Yes. `noexcept` is about C++ exceptions, not Ruby's. They don't |
| 34 |
* interface each other. You can safely attribute a function that raises |
| 35 |
* Ruby exceptions as `noexcept`. |
| 36 |
* |
| 37 |
* - Q: How, then, can I assert that a function I wrote doesn't raise any Ruby |
| 38 |
* exceptions? |
| 39 |
* |
| 40 |
* - A: `__attribute__((__leaf__))` is for that purpose. A function attributed |
| 41 |
* as leaf can still throw C++ exceptions, but not Ruby's. Note however, |
| 42 |
* that it's extremely difficult -- if not impossible -- to assert that a |
| 43 |
* function doesn't raise any Ruby exceptions at all. Use of that |
| 44 |
* attribute is not recommended; mere mortals can't properly use that by |
| 45 |
* hand. |
| 46 |
* |
| 47 |
* - Q: Does it make sense to attribute an inline function `noexcept`? |
| 48 |
* |
| 49 |
* - A: I thought so before. But no, I don't think they are useful any longer. |
| 50 |
* |
| 51 |
* - When an inline function attributed `noexcept` actually doesn't throw |
| 52 |
* any exceptions at all: these days I don't see any difference in |
| 53 |
* generated assembly by adding/removing this attribute. C++ compilers |
| 54 |
* get smarter and smarter. Today they can infer if it actually throws |
| 55 |
* or not without any annotations by humans (correct me if I'm wrong). |
| 56 |
* |
| 57 |
* - When an inline function attributed `noexcept` actually _does_ throw an |
| 58 |
* exception: they have to call `std::terminate` then (C++ standard |
| 59 |
* mandates so). This means exception handling routines are actually |
| 60 |
* enforced, not omitted. This doesn't impact runtime performance (The |
| 61 |
* Itanium C++ ABI has zero-cost exception handling), but does impact on |
| 62 |
* generated binary size. This is bad. |
| 63 |
*/ |
| 64 |
#include "ruby/internal/compiler_since.h" |
| 65 |
#include "ruby/internal/has/feature.h" |
| 66 |
|
| 67 |
/** Wraps (or simulates) C++11 `noexcept` */ |
| 68 |
#if ! defined(__cplusplus) |
| 69 |
# /* Doesn't make sense. */ |
| 70 |
# define RBIMPL_ATTR_NOEXCEPT(_) /* void */ |
| 71 |
|
| 72 |
#elif RBIMPL_HAS_FEATURE(cxx_noexcept) |
| 73 |
# define RBIMPL_ATTR_NOEXCEPT(_) noexcept(noexcept(_)) |
| 74 |
|
| 75 |
#elif defined(__GXX_EXPERIMENTAL_CXX0X__) && __GXX_EXPERIMENTAL_CXX0X__ |
| 76 |
# define RBIMPL_ATTR_NOEXCEPT(_) noexcept(noexcept(_)) |
| 77 |
|
| 78 |
#elif defined(__INTEL_CXX11_MODE__) |
| 79 |
# define RBIMPL_ATTR_NOEXCEPT(_) noexcept(noexcept(_)) |
| 80 |
|
| 81 |
#elif RBIMPL_COMPILER_SINCE(MSVC, 19, 0, 0) |
| 82 |
# define RBIMPL_ATTR_NOEXCEPT(_) noexcept(noexcept(_)) |
| 83 |
|
| 84 |
#elif __cplusplus >= 201103L |
| 85 |
# define RBIMPL_ATTR_NOEXCEPT(_) noexcept(noexcept(_)) |
| 86 |
|
| 87 |
#else |
| 88 |
# define RBIMPL_ATTR_NOEXCEPT(_) /* void */ |
| 89 |
#endif |
| 90 |
|
| 91 |
#endif /* RBIMPL_ATTR_NOEXCEPT_H */ |