From 4ff3f205408ff8bb413d69151105d301858136ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= Date: Fri, 10 Apr 2020 14:11:40 +0900 Subject: add #include guard hack According to MSVC manual (*1), cl.exe can skip including a header file when that: - contains #pragma once, or - starts with #ifndef, or - starts with #if ! defined. GCC has a similar trick (*2), but it acts more stricter (e. g. there must be _no tokens_ outside of #ifndef...#endif). Sun C lacked #pragma once for a looong time. Oracle Developer Studio 12.5 finally implemented it, but we cannot assume such recent version. This changeset modifies header files so that each of them include strictly one #ifndef...#endif. I believe this is the most portable way to trigger compiler optimizations. [Bug #16770] *1: https://docs.microsoft.com/en-us/cpp/preprocessor/once *2: https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html --- include/ruby/3/has/attribute.h | 11 ++++++----- include/ruby/3/has/builtin.h | 11 ++++++----- include/ruby/3/has/c_attribute.h | 11 ++++++----- include/ruby/3/has/cpp_attribute.h | 17 +++++++---------- include/ruby/3/has/declspec_attribute.h | 12 ++++++------ include/ruby/3/has/extension.h | 12 ++++++------ include/ruby/3/has/feature.h | 12 ++++++------ include/ruby/3/has/warning.h | 12 ++++++------ 8 files changed, 49 insertions(+), 49 deletions(-) (limited to 'include/ruby/3/has') diff --git a/include/ruby/3/has/attribute.h b/include/ruby/3/has/attribute.h index 36a1a1229c..c8ad7da9cf 100644 --- a/include/ruby/3/has/attribute.h +++ b/include/ruby/3/has/attribute.h @@ -1,4 +1,6 @@ -/** \noop-*-C++-*-vi:ft=cpp +#ifndef RUBY3_HAS_ATTRIBUTE_H /*-*-C++-*-vi:se ft=cpp:*/ +#define RUBY3_HAS_ATTRIBUTE_H +/** * @file * @author Ruby developers * @copyright This file is a part of the programming language Ruby. @@ -21,10 +23,7 @@ #include "ruby/3/config.h" /** Wraps (or simulates) `__has_attribute`. */ -#if defined(RUBY3_HAS_ATTRIBUTE) -# /* Take that. */ - -#elif defined(__has_attribute) +#if defined(__has_attribute) # define RUBY3_HAS_ATTRIBUTE(_) __has_attribute(_) #elif RUBY3_COMPILER_IS(GCC) @@ -150,3 +149,5 @@ # define RUBY3_HAS_ATTRIBUTE_weak 1 # endif #endif + +#endif /* RUBY3_HAS_ATTRIBUTE_H */ diff --git a/include/ruby/3/has/builtin.h b/include/ruby/3/has/builtin.h index 54dc2a5672..f1524061ae 100644 --- a/include/ruby/3/has/builtin.h +++ b/include/ruby/3/has/builtin.h @@ -1,4 +1,6 @@ -/** \noop-*-C++-*-vi:ft=cpp +#ifndef RUBY3_HAS_BUILTIN_H /*-*-C++-*-vi:se ft=cpp:*/ +#define RUBY3_HAS_BUILTIN_H +/** * @file * @author Ruby developers * @copyright This file is a part of the programming language Ruby. @@ -21,10 +23,7 @@ #include "ruby/3/config.h" /** Wraps (or simulates) `__has_builtin`. */ -#if defined(RUBY3_HAS_BUILTIN) -# /* Take that. */ - -#elif defined(__has_builtin) && ! RUBY3_COMPILER_IS(Intel) +#if defined(__has_builtin) && ! RUBY3_COMPILER_IS(Intel) # /* :TODO: Intel C Compiler has __has_builtin (since 19.1 maybe?), and is # * reportedly broken. We have to skip them. However the situation can # * change. They might improve someday. We need to revisit here later. */ @@ -89,3 +88,5 @@ # define RUBY3_HAS_BUILTIN___builtin_unreachable 1 # endif #endif + +#endif /* RUBY3_HAS_BUILTIN_H */ diff --git a/include/ruby/3/has/c_attribute.h b/include/ruby/3/has/c_attribute.h index 89186217d3..e883eb71e6 100644 --- a/include/ruby/3/has/c_attribute.h +++ b/include/ruby/3/has/c_attribute.h @@ -1,4 +1,6 @@ -/** \noop-*-C++-*-vi:ft=cpp +#ifndef RUBY3_HAS_C_ATTRIBUTE_H /*-*-C++-*-vi:se ft=cpp:*/ +#define RUBY3_HAS_C_ATTRIBUTE_H +/** * @file * @author Ruby developers * @copyright This file is a part of the programming language Ruby. @@ -20,10 +22,7 @@ */ /** Wraps (or simulates) `__has_c_attribute`. */ -#if defined(RUBY3_HAS_C_ATTRIBUTE) -# /* Take that. */ - -#elif defined(__cplusplus) +#if defined(__cplusplus) # /* Makes no sense. */ # define RUBY3_HAS_C_ATTRIBUTE(_) 0 @@ -35,3 +34,5 @@ # * lacks C2x attributes as well. Might change in future? */ # define RUBY3_HAS_C_ATTRIBUTE(_) 0 #endif + +#endif /* RUBY3_HAS_C_ATTRIBUTE_H */ diff --git a/include/ruby/3/has/cpp_attribute.h b/include/ruby/3/has/cpp_attribute.h index 70bec4a267..42209c0955 100644 --- a/include/ruby/3/has/cpp_attribute.h +++ b/include/ruby/3/has/cpp_attribute.h @@ -1,4 +1,6 @@ -/** \noop-*-C++-*-vi:ft=cpp +#ifndef RUBY3_HAS_CPP_ATTRIBUTE_H /*-*-C++-*-vi:se ft=cpp:*/ +#define RUBY3_HAS_CPP_ATTRIBUTE_H +/** * @file * @author Ruby developers * @copyright This file is a part of the programming language Ruby. @@ -21,10 +23,7 @@ #include "ruby/3/compiler_is.h" /** @cond INTERNAL_MACRO */ -#if defined(RUBY3_HAS_CPP_ATTRIBUTE0) -# /* Take that. */ - -#elif defined(__has_cpp_attribute) +#if defined(__has_cpp_attribute) # define RUBY3_HAS_CPP_ATTRIBUTE0(_) __has_cpp_attribute(_) #elif RUBY3_COMPILER_IS(MSVC) @@ -66,15 +65,13 @@ /** @endcond */ /** Wraps (or simulates) `__has_cpp_attribute`. */ -#ifdef RUBY3_HAS_CPP_ATTRIBUTE -# /* Take that. */ - -#elif ! defined(__cplusplus) +#if ! defined(__cplusplus) # /* Makes no sense. */ # define RUBY3_HAS_CPP_ATTRIBUTE(_) 0 - #else # /* GCC needs workarounds. See https://gcc.godbolt.org/z/jdz3pa */ # define RUBY3_HAS_CPP_ATTRIBUTE(_) \ ((RUBY3_HAS_CPP_ATTRIBUTE0(_) <= __cplusplus) ? RUBY3_HAS_CPP_ATTRIBUTE0(_) : 0) #endif + +#endif /* RUBY3_HAS_CPP_ATTRIBUTE_H */ diff --git a/include/ruby/3/has/declspec_attribute.h b/include/ruby/3/has/declspec_attribute.h index a7d97b400a..4774eb88f2 100644 --- a/include/ruby/3/has/declspec_attribute.h +++ b/include/ruby/3/has/declspec_attribute.h @@ -1,4 +1,6 @@ -/** \noop-*-C++-*-vi:ft=cpp +#ifndef RUBY3_HAS_DECLSPEC_ATTRIBUTE_H /*-*-C++-*-vi:se ft=cpp:*/ +#define RUBY3_HAS_DECLSPEC_ATTRIBUTE_H +/** * @file * @author Ruby developers * @copyright This file is a part of the programming language Ruby. @@ -20,12 +22,8 @@ */ /** Wraps (or simulates) `__has_declspec_attribute`. */ -#if defined(RUBY3_HAS_DECLSPEC_ATTRIBUTE) -# /* Take that. */ - -#elif defined(__has_declspec_attribute) +#if defined(__has_declspec_attribute) # define RUBY3_HAS_DECLSPEC_ATTRIBUTE(_) __has_declspec_attribute(_) - #else # define RUBY3_HAS_DECLSPEC_ATTRIBUTE(_) RUBY3_TOKEN_PASTE(RUBY3_HAS_DECLSPEC_ATTRIBUTE_, _) # define RUBY3_HAS_DECLSPEC_ATTRIBUTE_align RUBY3_COMPILER_SINCE(MSVC, 8, 0, 0) @@ -44,3 +42,5 @@ # undef RUBY3_HAS_DECLSPEC_ATTRIBUTE_nothrow # endif #endif + +#endif /* RUBY3_HAS_DECLSPEC_ATTRIBUTE_H */ diff --git a/include/ruby/3/has/extension.h b/include/ruby/3/has/extension.h index ca3392a090..ed6f6d4d52 100644 --- a/include/ruby/3/has/extension.h +++ b/include/ruby/3/has/extension.h @@ -1,4 +1,6 @@ -/** \noop-*-C++-*-vi:ft=cpp +#ifndef RUBY3_HAS_EXTENSION_H /*-*-C++-*-vi:se ft=cpp:*/ +#define RUBY3_HAS_EXTENSION_H +/** * @file * @author Ruby developers * @copyright This file is a part of the programming language Ruby. @@ -21,13 +23,11 @@ #include "ruby/3/has/feature.h" /** Wraps (or simulates) `__has_extension`. */ -#if defined(RUBY3_HAS_EXTENSION) -# /* Take that. */ - -#elif defined(__has_extension) +#if defined(__has_extension) # define RUBY3_HAS_EXTENSION(_) __has_extension(_) - #else # /* Pre-3.0 clang had __has_feature but not __has_extension. */ # define RUBY3_HAS_EXTENSION(_) RUBY3_HAS_FEATURE(_) #endif + +#endif /* RUBY3_HAS_EXTENSION_H */ diff --git a/include/ruby/3/has/feature.h b/include/ruby/3/has/feature.h index 977b8150bb..f64c756817 100644 --- a/include/ruby/3/has/feature.h +++ b/include/ruby/3/has/feature.h @@ -1,4 +1,6 @@ -/** \noop-*-C++-*-vi:ft=cpp +#ifndef RUBY3_HAS_FEATURE_H /*-*-C++-*-vi:se ft=cpp:*/ +#define RUBY3_HAS_FEATURE_H +/** * @file * @author Ruby developers * @copyright This file is a part of the programming language Ruby. @@ -20,12 +22,10 @@ */ /** Wraps (or simulates) `__has_feature`. */ -#if defined(RUBY3_HAS_FEATURE) -# /* Take that. */ - -#elif defined(__has_feature) +#if defined(__has_feature) # define RUBY3_HAS_FEATURE(_) __has_feature(_) - #else # define RUBY3_HAS_FEATURE(_) 0 #endif + +#endif /* RUBY3_HAS_FEATURE_H */ diff --git a/include/ruby/3/has/warning.h b/include/ruby/3/has/warning.h index a66d76dfa7..3ec8e8f79e 100644 --- a/include/ruby/3/has/warning.h +++ b/include/ruby/3/has/warning.h @@ -1,4 +1,6 @@ -/** \noop-*-C++-*-vi:ft=cpp +#ifndef RUBY3_HAS_WARNING_H /*-*-C++-*-vi:se ft=cpp:*/ +#define RUBY3_HAS_WARNING_H +/** * @file * @author Ruby developers * @copyright This file is a part of the programming language Ruby. @@ -20,12 +22,10 @@ */ /** Wraps (or simulates) `__has_warning`. */ -#if defined(RUBY3_HAS_WARNING) -# /* Take that. */ - -#elif defined(__has_warning) +#if defined(__has_warning) # define RUBY3_HAS_WARNING(_) __has_warning(_) - #else # define RUBY3_HAS_WARNING(_) 0 #endif + +#endif /* RUBY3_HAS_WARNING_H */ -- cgit v1.2.3