From 00587c07b1e1a5f4a0ac6a0003d379ea8c1e2420 Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Sat, 1 Sep 2012 16:16:17 -0400 Subject: [PATCH] Bug 782647. Move the nullptr workaround macros to MFBT so that it can be shared. r=waldo This also changes the header to use compiler detection instead of a configure test. This makes the header more portable because it doesn't require the configure infrastructure. --- configure.in | 18 ---------------- gfx/2d/Types.h | 18 +--------------- mfbt/NullPtr.h | 46 ++++++++++++++++++++++++++++++++++++++++ mfbt/exported_headers.mk | 1 + xpcom/base/nscore.h | 18 ++-------------- 5 files changed, 50 insertions(+), 51 deletions(-) create mode 100644 mfbt/NullPtr.h diff --git a/configure.in b/configure.in index 5d5295f99d2..5981abbe787 100644 --- a/configure.in +++ b/configure.in @@ -754,9 +754,6 @@ if test -n "$_WIN32_MSVC"; then AC_DEFINE(HAVE_IO_H) AC_DEFINE(HAVE_SETBUF) AC_DEFINE(HAVE_ISATTY) - if test $_MSC_VER -ge 1600; then - AC_DEFINE(HAVE_NULLPTR) - fi fi fi # COMPILE_ENVIRONMENT @@ -2804,21 +2801,6 @@ dnl Note that we assume that mac & win32 have short wchar (see nscore.h) fi fi -dnl Check for nullptr (bug 626472) -AC_LANG_CPLUSPLUS -AC_MSG_CHECKING(for nullptr) -AC_CACHE_VAL(ac_cv_nullptr, - [AC_TRY_COMPILE([], - [int* foo = nullptr;], - [ac_cv_nullptr=true], - [ac_cv_nullptr=false])]) -if test "$ac_cv_nullptr" = true ; then - AC_DEFINE(HAVE_NULLPTR) - AC_MSG_RESULT(yes) -else - AC_MSG_RESULT(no) -fi - AC_LANG_C dnl Check for .hidden assembler directive and visibility attribute. diff --git a/gfx/2d/Types.h b/gfx/2d/Types.h index 675ada05f1d..f02d0af68a2 100644 --- a/gfx/2d/Types.h +++ b/gfx/2d/Types.h @@ -6,24 +6,8 @@ #ifndef MOZILLA_GFX_TYPES_H_ #define MOZILLA_GFX_TYPES_H_ -/** - * Use C++11 nullptr if available; otherwise use a C++ typesafe template; and - * for C, fall back to longs. See bugs 547964 and 626472. - * Copy and paste job from nscore.h, see bug 781943 - */ -#if defined(MOZ_GFX) && !defined(HAVE_NULLPTR) -#ifndef __cplusplus -# define nullptr ((void*)0) -#elif defined(__GNUC__) -# define nullptr __null -#elif defined(_WIN64) -# define nullptr 0LL -#else -# define nullptr 0L -#endif -#endif /* defined(MOZ_GFX) && !defined(HAVE_NULLPTR) */ - #include "mozilla/StandardInteger.h" +#include "mozilla/NullPtr.h" #include diff --git a/mfbt/NullPtr.h b/mfbt/NullPtr.h new file mode 100644 index 00000000000..e6fc892759f --- /dev/null +++ b/mfbt/NullPtr.h @@ -0,0 +1,46 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* + * Implements a workaround for compilers which do not support the C++11 nullptr + * constant. + */ + +#ifndef mozilla_NullPtr_h_ +#define mozilla_NullPtr_h_ + +#if defined(__clang__) +# ifndef __has_extension +# define __has_extension __has_feature +# endif +# if __has_extension(cxx_nullptr) +# define MOZ_HAVE_CXX11_NULLPTR +# endif +#elif defined(__GNUC__) +# if defined(_GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L +# if (__GNUC__ * 1000 + __GNU_MINOR__) >= 4006 +# define MOZ_HAVE_CXX11_NULLPTR +# endif +# endif +#elif _MSC_VER >= 1600 +# define MOZ_HAVE_CXX11_NULLPTR +#endif + +/** + * Use C++11 nullptr if available; otherwise use __null for gcc, or a 0 literal + * with the correct size to match the size of a pointer on a given platform. + */ + +#ifndef MOZ_HAVE_CXX11_NULLPTR +# if defined(__GNUC__) +# define nullptr __null +# elif defined(_WIN64) +# define nullptr 0LL +# else +# define nullptr 0L +# endif +#endif + +#endif /* mozilla_NullPtr_h_ */ diff --git a/mfbt/exported_headers.mk b/mfbt/exported_headers.mk index 40df12aa881..155704d9c46 100644 --- a/mfbt/exported_headers.mk +++ b/mfbt/exported_headers.mk @@ -21,6 +21,7 @@ EXPORTS_mozilla += \ LinkedList.h \ MathAlgorithms.h \ MSStdInt.h \ + NullPtr.h \ RangedPtr.h \ RefPtr.h \ Scoped.h \ diff --git a/xpcom/base/nscore.h b/xpcom/base/nscore.h index e4b3e1fcba8..e71614d9da7 100644 --- a/xpcom/base/nscore.h +++ b/xpcom/base/nscore.h @@ -26,6 +26,8 @@ #include "mozilla/StandardInteger.h" #include "stddef.h" +#include "mozilla/NullPtr.h" + /* * This is for functions that are like malloc_usable_size. Such functions are * used for measuring the size of data structures. @@ -324,22 +326,6 @@ typedef unsigned long nsrefcnt; typedef uint32_t nsrefcnt; #endif -/** - * Use C++11 nullptr if available; otherwise use a C++ typesafe template; and - * for C, fall back to longs. See bugs 547964 and 626472. - */ -#ifndef HAVE_NULLPTR -#ifndef __cplusplus -# define nullptr ((void*)0) -#elif defined(__GNUC__) -# define nullptr __null -#elif defined(_WIN64) -# define nullptr 0LL -#else -# define nullptr 0L -#endif -#endif /* defined(HAVE_NULLPTR) */ - #include "nsError.h" /* ------------------------------------------------------------------------ */