Skip to content

Commit

Permalink
Don't wrap feature detection macros with QT_HAS_FOO() variants
Browse files Browse the repository at this point in the history
Using wrappers for these macros is problematic when for example passing the
-frewrite-includes flag to preprocess sources before shipping off to distcc
or Icecream. It will also start producing warnings when compilers implement
http://eel.is/c++draft/cpp.cond#7.sentence-2. See for example
https://reviews.llvm.org/D49091

Both https://clang.llvm.org/docs/LanguageExtensions.html and the SD-6 document at
https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
recommend defining '__has_foo(x) 0' as a fallback for compilers without the
macros, so that's what we go for.

Change-Id: I0298cd3b4a6ff6618821e34642a5ddd6728be767
Reviewed-by: Alex Richardson <arichardson.kde@gmail.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
  • Loading branch information
torarnv committed Dec 9, 2019
1 parent 191ac31 commit c3bd5ff
Show file tree
Hide file tree
Showing 34 changed files with 102 additions and 105 deletions.
77 changes: 37 additions & 40 deletions src/corelib/global/qcompilerdetection.h
Expand Up @@ -505,6 +505,39 @@
# error "Qt has not been tested with this compiler - see http://www.qt-project.org/"
#endif

/*
* SG10's SD-6 feature detection and some useful extensions from Clang and GCC
* https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
* http://clang.llvm.org/docs/LanguageExtensions.html#feature-checking-macros
* Not using wrapper macros, per http://eel.is/c++draft/cpp.cond#7.sentence-2
*/
#ifndef __has_builtin
# define __has_builtin(x) 0
#endif
#ifndef __has_feature
# define __has_feature(x) 0
#endif
#ifndef __has_attribute
# define __has_attribute(x) 0
#endif
#ifndef __has_cpp_attribute
# define __has_cpp_attribute(x) 0
#endif
#ifndef __has_include
# define __has_include(x) 0
#endif
#ifndef __has_include_next
# define __has_include_next(x) 0
#endif

// Kept around until all submodules have transitioned
#define QT_HAS_BUILTIN(x) __has_builtin(x)
#define QT_HAS_FEATURE(x) __has_feature(x)
#define QT_HAS_ATTRIBUTE(x) __has_attribute(x)
#define QT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#define QT_HAS_INCLUDE(x) __has_include(x)
#define QT_HAS_INCLUDE_NEXT(x) __has_include_next(x)

/*
* C++11 support
*
Expand Down Expand Up @@ -1031,37 +1064,6 @@
# endif
#endif

/*
* SG10's SD-6 feature detection and some useful extensions from Clang and GCC
* https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
* http://clang.llvm.org/docs/LanguageExtensions.html#feature-checking-macros
*/
#ifdef __has_builtin
# define QT_HAS_BUILTIN(x) __has_builtin(x)
#else
# define QT_HAS_BUILTIN(x) 0
#endif
#ifdef __has_attribute
# define QT_HAS_ATTRIBUTE(x) __has_attribute(x)
#else
# define QT_HAS_ATTRIBUTE(x) 0
#endif
#ifdef __has_cpp_attribute
# define QT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#else
# define QT_HAS_CPP_ATTRIBUTE(x) 0
#endif
#ifdef __has_include
# define QT_HAS_INCLUDE(x) __has_include(x)
#else
# define QT_HAS_INCLUDE(x) 0
#endif
#ifdef __has_include_next
# define QT_HAS_INCLUDE_NEXT(x) __has_include_next(x)
#else
# define QT_HAS_INCLUDE_NEXT(x) 0
#endif

/*
* C++11 keywords and expressions
*/
Expand Down Expand Up @@ -1138,7 +1140,7 @@
# define Q_DECL_ALIGN(n) alignas(n)
#endif

#if QT_HAS_CPP_ATTRIBUTE(nodiscard) && !defined(Q_CC_CLANG) // P0188R1
#if __has_cpp_attribute(nodiscard) && !defined(Q_CC_CLANG) // P0188R1
// Can't use [[nodiscard]] with Clang, see https://bugs.llvm.org/show_bug.cgi?id=33518
# undef Q_REQUIRED_RESULT
# define Q_REQUIRED_RESULT [[nodiscard]]
Expand Down Expand Up @@ -1240,11 +1242,6 @@
#ifndef QT_MAKE_CHECKED_ARRAY_ITERATOR
# define QT_MAKE_CHECKED_ARRAY_ITERATOR(x, N) (x)
#endif
#ifdef __has_feature
# define QT_HAS_FEATURE(x) __has_feature(x)
#else
# define QT_HAS_FEATURE(x) 0
#endif

/*
* Warning/diagnostic handling
Expand Down Expand Up @@ -1335,11 +1332,11 @@
} while (false)

#if defined(__cplusplus)
#if QT_HAS_CPP_ATTRIBUTE(clang::fallthrough)
#if __has_cpp_attribute(clang::fallthrough)
# define Q_FALLTHROUGH() [[clang::fallthrough]]
#elif QT_HAS_CPP_ATTRIBUTE(gnu::fallthrough)
#elif __has_cpp_attribute(gnu::fallthrough)
# define Q_FALLTHROUGH() [[gnu::fallthrough]]
#elif QT_HAS_CPP_ATTRIBUTE(fallthrough)
#elif __has_cpp_attribute(fallthrough)
# define Q_FALLTHROUGH() [[fallthrough]]
#endif
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/corelib/global/qconfig-bootstrapped.h
Expand Up @@ -75,13 +75,13 @@
# define QT_FEATURE_alloca_malloc_h -1
#endif
#define QT_CRYPTOGRAPHICHASH_ONLY_SHA1
#define QT_FEATURE_cxx11_random (QT_HAS_INCLUDE(<random>) ? 1 : -1)
#define QT_FEATURE_cxx11_random (__has_include(<random>) ? 1 : -1)
#define QT_NO_DATASTREAM
#define QT_FEATURE_datestring 1
#define QT_FEATURE_datetimeparser -1
#define QT_FEATURE_easingcurve -1
#define QT_FEATURE_etw -1
#define QT_FEATURE_getauxval (QT_HAS_INCLUDE(<sys/auxv.h>) ? 1 : -1)
#define QT_FEATURE_getauxval (__has_include(<sys/auxv.h>) ? 1 : -1)
#define QT_FEATURE_getentropy -1
#define QT_NO_GEOM_VARIANT
#define QT_FEATURE_hijricalendar -1
Expand Down
4 changes: 2 additions & 2 deletions src/corelib/global/qendian.h
Expand Up @@ -66,7 +66,7 @@ template <typename T> Q_ALWAYS_INLINE void qToUnaligned(const T src, void *dest)
// Using sizeof(T) inside memcpy function produces internal compiler error with
// MSVC2008/ARM in tst_endian -> use extra indirection to resolve size of T.
const size_t size = sizeof(T);
#if QT_HAS_BUILTIN(__builtin_memcpy)
#if __has_builtin(__builtin_memcpy)
__builtin_memcpy
#else
memcpy
Expand All @@ -78,7 +78,7 @@ template <typename T> Q_ALWAYS_INLINE T qFromUnaligned(const void *src)
{
T dest;
const size_t size = sizeof(T);
#if QT_HAS_BUILTIN(__builtin_memcpy)
#if __has_builtin(__builtin_memcpy)
__builtin_memcpy
#else
memcpy
Expand Down
4 changes: 2 additions & 2 deletions src/corelib/global/qglobal.cpp
Expand Up @@ -92,7 +92,7 @@
# include <sys/systeminfo.h>
#endif

#if defined(Q_OS_DARWIN) && QT_HAS_INCLUDE(<IOKit/IOKitLib.h>)
#if defined(Q_OS_DARWIN) && __has_include(<IOKit/IOKitLib.h>)
# include <IOKit/IOKitLib.h>
# include <private/qcore_mac_p.h>
#endif
Expand Down Expand Up @@ -3041,7 +3041,7 @@ enum {
*/
QByteArray QSysInfo::machineUniqueId()
{
#if defined(Q_OS_DARWIN) && QT_HAS_INCLUDE(<IOKit/IOKitLib.h>)
#if defined(Q_OS_DARWIN) && __has_include(<IOKit/IOKitLib.h>)
char uuid[UuidStringLen + 1];
io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
QCFString stringRef = (CFStringRef)IORegistryEntryCreateCFProperty(service, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/corelib/global/qglobal.h
Expand Up @@ -769,7 +769,7 @@ inline void qt_noop(void) {}

#if !defined(QT_NO_EXCEPTIONS)
# if !defined(Q_MOC_RUN)
# if (defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !QT_HAS_FEATURE(cxx_exceptions)) || \
# if (defined(Q_CC_CLANG) && !defined(Q_CC_INTEL) && !__has_feature(cxx_exceptions)) || \
(defined(Q_CC_GNU) && !defined(__EXCEPTIONS))
# define QT_NO_EXCEPTIONS
# endif
Expand Down
4 changes: 2 additions & 2 deletions src/corelib/global/qglobal_p.h
Expand Up @@ -74,7 +74,7 @@ Q_CORE_EXPORT time_t qMkTime(struct tm *when);

QT_END_NAMESPACE

#if !QT_HAS_BUILTIN(__builtin_available)
#if !__has_builtin(__builtin_available)
#include <initializer_list>
#include <QtCore/qoperatingsystemversion.h>
#include <QtCore/qversionnumber.h>
Expand Down Expand Up @@ -142,7 +142,7 @@ QT_END_NAMESPACE
QT_BUILTIN_AVAILABLE1, \
QT_BUILTIN_AVAILABLE0, )
#define __builtin_available(...) QT_BUILTIN_AVAILABLE_CHOOSER(__VA_ARGS__)(__VA_ARGS__)
#endif // !QT_HAS_BUILTIN(__builtin_available)
#endif // !__has_builtin(__builtin_available)
#endif // defined(__cplusplus)

#endif // QGLOBAL_P_H
Expand Down
8 changes: 4 additions & 4 deletions src/corelib/global/qlogging.cpp
Expand Up @@ -70,7 +70,7 @@
#if QT_CONFIG(slog2)
#include <sys/slog2.h>
#endif
#if QT_HAS_INCLUDE(<paths.h>)
#if __has_include(<paths.h>)
#include <paths.h>
#endif

Expand Down Expand Up @@ -106,7 +106,7 @@
# if __UCLIBC_HAS_BACKTRACE__
# define QLOGGING_HAVE_BACKTRACE
# endif
# elif (defined(__GLIBC__) && defined(__GLIBCXX__)) || (QT_HAS_INCLUDE(<cxxabi.h>) && QT_HAS_INCLUDE(<execinfo.h>))
# elif (defined(__GLIBC__) && defined(__GLIBCXX__)) || (__has_include(<cxxabi.h>) && __has_include(<execinfo.h>))
# define QLOGGING_HAVE_BACKTRACE
# endif
#endif
Expand All @@ -116,7 +116,7 @@ extern char *__progname;
#endif

#ifndef QT_BOOTSTRAPPED
#if defined(Q_OS_LINUX) && (defined(__GLIBC__) || QT_HAS_INCLUDE(<sys/syscall.h>))
#if defined(Q_OS_LINUX) && (defined(__GLIBC__) || __has_include(<sys/syscall.h>))
# include <sys/syscall.h>

# if defined(Q_OS_ANDROID) && !defined(SYS_gettid)
Expand Down Expand Up @@ -1276,7 +1276,7 @@ void QMessagePattern::setPattern(const QString &pattern)
#if defined(QLOGGING_HAVE_BACKTRACE) && !defined(QT_BOOTSTRAPPED)
// make sure the function has "Message" in the name so the function is removed

#if ((defined(Q_CC_GNU) && defined(QT_COMPILER_SUPPORTS_SIMD_ALWAYS)) || QT_HAS_ATTRIBUTE(optimize)) \
#if ((defined(Q_CC_GNU) && defined(QT_COMPILER_SUPPORTS_SIMD_ALWAYS)) || __has_attribute(optimize)) \
&& !defined(Q_CC_INTEL) && !defined(Q_CC_CLANG)
// force skipping the frame pointer, to save the backtrace() function some work
__attribute__((optimize("omit-frame-pointer")))
Expand Down
2 changes: 1 addition & 1 deletion src/corelib/global/qnumeric_p.h
Expand Up @@ -249,7 +249,7 @@ QT_WARNING_POP
// size_t. Implementations for 8- and 16-bit types will work but may not be as
// efficient. Implementations for 64-bit may be missing on 32-bit platforms.

#if (defined(Q_CC_GNU) && (Q_CC_GNU >= 500) || (defined(Q_CC_INTEL) && !defined(Q_OS_WIN))) || QT_HAS_BUILTIN(__builtin_add_overflow)
#if (defined(Q_CC_GNU) && (Q_CC_GNU >= 500) || (defined(Q_CC_INTEL) && !defined(Q_OS_WIN))) || __has_builtin(__builtin_add_overflow)
// GCC 5, ICC 18, and Clang 3.8 have builtins to detect overflows

template <typename T> inline
Expand Down
2 changes: 1 addition & 1 deletion src/corelib/io/qfilesystemengine_unix.cpp
Expand Up @@ -55,7 +55,7 @@
#include <stdio.h>
#include <errno.h>

#if QT_HAS_INCLUDE(<paths.h>)
#if __has_include(<paths.h>)
# include <paths.h>
#endif
#ifndef _PATH_TMP // from <paths.h>
Expand Down
2 changes: 1 addition & 1 deletion src/corelib/io/qprocess.cpp
Expand Up @@ -100,7 +100,7 @@ QT_END_NAMESPACE
#include <private/qcore_unix_p.h>
#endif

#if QT_HAS_INCLUDE(<paths.h>)
#if __has_include(<paths.h>)
#include <paths.h>
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/corelib/io/qstandardpaths.cpp
Expand Up @@ -48,7 +48,7 @@
#include <qcoreapplication.h>
#endif

#if QT_HAS_INCLUDE(<paths.h>)
#if __has_include(<paths.h>)
#include <paths.h>
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/corelib/io/qstorageinfo_unix.cpp
Expand Up @@ -108,7 +108,7 @@
# endif // QT_LARGEFILE_SUPPORT
#endif // Q_OS_BSD4

#if QT_HAS_INCLUDE(<paths.h>)
#if __has_include(<paths.h>)
# include <paths.h>
#endif
#ifndef _PATH_MOUNTED
Expand Down
4 changes: 2 additions & 2 deletions src/corelib/kernel/qdeadlinetimer.h
Expand Up @@ -52,7 +52,7 @@

#include <limits>

#if QT_HAS_INCLUDE(<chrono>)
#if __has_include(<chrono>)
# include <chrono>
#endif

Expand Down Expand Up @@ -120,7 +120,7 @@ class Q_CORE_EXPORT QDeadlineTimer
QDeadlineTimer &operator-=(qint64 msecs)
{ *this = *this + (-msecs); return *this; }

#if QT_HAS_INCLUDE(<chrono>) || defined(Q_CLANG_QDOC)
#if __has_include(<chrono>) || defined(Q_CLANG_QDOC)
template <class Clock, class Duration>
QDeadlineTimer(std::chrono::time_point<Clock, Duration> deadline_,
Qt::TimerType type_ = Qt::CoarseTimer) : t2(0)
Expand Down
4 changes: 2 additions & 2 deletions src/corelib/kernel/qobject.h
Expand Up @@ -55,7 +55,7 @@

#include <QtCore/qobject_impl.h>

#if QT_HAS_INCLUDE(<chrono>)
#if __has_include(<chrono>)
# include <chrono>
#endif

Expand Down Expand Up @@ -160,7 +160,7 @@ class Q_CORE_EXPORT QObject
void moveToThread(QThread *thread);

int startTimer(int interval, Qt::TimerType timerType = Qt::CoarseTimer);
#if QT_HAS_INCLUDE(<chrono>)
#if __has_include(<chrono>)
Q_ALWAYS_INLINE
int startTimer(std::chrono::milliseconds time, Qt::TimerType timerType = Qt::CoarseTimer)
{
Expand Down
6 changes: 3 additions & 3 deletions src/corelib/kernel/qtimer.h
Expand Up @@ -47,7 +47,7 @@
#include <QtCore/qbasictimer.h> // conceptual inheritance
#include <QtCore/qobject.h>

#if QT_HAS_INCLUDE(<chrono>)
#if __has_include(<chrono>)
# include <chrono>
#endif

Expand Down Expand Up @@ -177,7 +177,7 @@ public Q_SLOTS:
void timeout(QPrivateSignal);

public:
#if QT_HAS_INCLUDE(<chrono>) || defined(Q_QDOC)
#if __has_include(<chrono>) || defined(Q_QDOC)
void setInterval(std::chrono::milliseconds value)
{
setInterval(int(value.count()));
Expand Down Expand Up @@ -223,7 +223,7 @@ public Q_SLOTS:
static void singleShotImpl(int msec, Qt::TimerType timerType,
const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj);

#if QT_HAS_INCLUDE(<chrono>)
#if __has_include(<chrono>)
static Qt::TimerType defaultTypeFor(std::chrono::milliseconds interval)
{ return defaultTypeFor(int(interval.count())); }

Expand Down
6 changes: 3 additions & 3 deletions src/corelib/kernel/qvariant.h
Expand Up @@ -53,7 +53,7 @@
#include <QtCore/qbytearraylist.h>
#endif

#if QT_HAS_INCLUDE(<variant>) && __cplusplus >= 201703L
#if __has_include(<variant>) && __cplusplus >= 201703L
#include <variant>
#elif defined(Q_CLANG_QDOC)
namespace std { template<typename...> struct variant; }
Expand Down Expand Up @@ -370,7 +370,7 @@ class Q_CORE_EXPORT QVariant
static inline QVariant fromValue(const T &value)
{ return QVariant(qMetaTypeId<T>(), &value, QTypeInfo<T>::isPointer); }

#if (QT_HAS_INCLUDE(<variant>) && __cplusplus >= 201703L) || defined(Q_CLANG_QDOC)
#if (__has_include(<variant>) && __cplusplus >= 201703L) || defined(Q_CLANG_QDOC)
template<typename... Types>
static inline QVariant fromStdVariant(const std::variant<Types...> &value)
{
Expand Down Expand Up @@ -544,7 +544,7 @@ inline QVariant QVariant::fromValue(const QVariant &value)
return value;
}

#if QT_HAS_INCLUDE(<variant>) && __cplusplus >= 201703L
#if __has_include(<variant>) && __cplusplus >= 201703L
template<>
inline QVariant QVariant::fromValue(const std::monostate &)
{
Expand Down
2 changes: 1 addition & 1 deletion src/corelib/serialization/qcborarray.h
Expand Up @@ -214,7 +214,7 @@ class Q_CORE_EXPORT QCborArray
bool contains(const QCborValue &value) const;

int compare(const QCborArray &other) const noexcept Q_DECL_PURE_FUNCTION;
#if 0 && QT_HAS_INCLUDE(<compare>)
#if 0 && __has_include(<compare>)
std::strong_ordering operator<=>(const QCborArray &other) const
{
int c = compare(other);
Expand Down
2 changes: 1 addition & 1 deletion src/corelib/serialization/qcbormap.h
Expand Up @@ -245,7 +245,7 @@ class Q_CORE_EXPORT QCborMap
{ const_iterator it = find(key); return it != end(); }

int compare(const QCborMap &other) const noexcept Q_DECL_PURE_FUNCTION;
#if 0 && QT_HAS_INCLUDE(<compare>)
#if 0 && __has_include(<compare>)
std::strong_ordering operator<=>(const QCborMap &other) const
{
int c = compare(other);
Expand Down

0 comments on commit c3bd5ff

Please sign in to comment.