From 55d3a4c20b1589fe0a2d8dddc0e74802c717e5b5 Mon Sep 17 00:00:00 2001 From: Marcin Cieslak Date: Sat, 28 Mar 2015 02:55:59 +0000 Subject: [PATCH] Work around C++11 nullptr Provide CXX11_NULLPTR autoconf macro --- configure.ac | 2 ++ m4/cxx11_nullptr.m4 | 12 ++++++++++ m4/cxx11_std_try.m4 | 54 +++++++++++++++++++++++++++++++++++++++++++++ src/util.cpp | 14 +++++++++++- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 m4/cxx11_nullptr.m4 create mode 100644 m4/cxx11_std_try.m4 diff --git a/configure.ac b/configure.ac index 640fef39cb..79494cfb7c 100644 --- a/configure.ac +++ b/configure.ac @@ -42,6 +42,8 @@ AC_TYPE_SIZE_T AC_FUNC_MALLOC AC_CHECK_FUNCS([floor getcwd strtol]) +CXX11_NULLPTR + # Checks for testing. AC_ARG_ENABLE(tests, AS_HELP_STRING([--enable-tests], [enable testing the build]), [enable_tests="$enableval"], [enable_tests=no]) diff --git a/m4/cxx11_nullptr.m4 b/m4/cxx11_nullptr.m4 new file mode 100644 index 0000000000..0d1a06a713 --- /dev/null +++ b/m4/cxx11_nullptr.m4 @@ -0,0 +1,12 @@ +#serial 1 +AC_DEFUN([CXX11_NULLPTR], +[ + AC_REQUIRE([CXX11_STD_AVAILABLE]) + CXX11_STD_TRY([nullptr], [], [[char *s = nullptr;]], + [ $cxx11_cv_prog_cxx_cxx11 ], + [ + AC_DEFINE([HAVE_CXX11_NULLPTR],[1], + [Define to 1 C++11 nullptr is available]) + ], + []) +]) diff --git a/m4/cxx11_std_try.m4 b/m4/cxx11_std_try.m4 new file mode 100644 index 0000000000..f518b0809d --- /dev/null +++ b/m4/cxx11_std_try.m4 @@ -0,0 +1,54 @@ +#serial 1 +# CXX11_STD_TRY(STANDARD, TEST-PROLOGUE, TEST-BODY, OPTION-LIST, +# ACTION-IF-AVAILABLE, ACTION-IF-UNAVAILABLE) +# ---------------------------------------------------------------- +# Check whether the C++ compiler accepts features of STANDARD (e.g +# `cxx98', `cxx11') by trying to compile a program of TEST-PROLOGUE +# and TEST-BODY. If this fails, try again with each compiler option +# in the space-separated OPTION-LIST; if one helps, append it to CXX. +# If eventually successful, run ACTION-IF-AVAILABLE, else +# ACTION-IF-UNAVAILABLE. +AC_DEFUN([_CXX11_STD_TRY], +[ +AC_LANG_PUSH(C++)dnl +AC_CACHE_VAL(cxx11_cv_prog_cxx_$1, +[cxx11_cv_prog_cxx_$1=no +cxx11_save_CXX=$CXX +AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])]) +for cxx11_arg in $4 +do + CXX="$cxx11_save_CXX $cxx11_arg" + _AS_ECHO_N([$cxx11_arg ]) + AC_COMPILE_IFELSE([], [cxx11_cv_prog_cxx_$1=$cxx11_arg]) + test "x$cxx11_cv_prog_cxx_$1" != "xno" && break +done +rm -f conftest.$ac_ext +CXX=$cxx11_save_CXX +])# AC_CACHE_VAL +cxx11_prog_cxx_stdcxx_options= +case "x$cxx11_cv_prog_cxx_$1" in + x) + AC_MSG_RESULT([working as-is]) ;; + xno) + AC_MSG_RESULT([not working]) ;; + *) + cxx11_prog_cxx_stdcxx_options=" $cxx11_cv_prog_cxx_$1" + CXX=$CXX$cxx11_prog_cxx_stdcxx_options + AC_MSG_RESULT([working]) ;; +esac +AC_LANG_POP(C++)dnl +])# _CXX11_STD_TRY +AC_DEFUN([CXX11_STD_TRY], +[ + AC_MSG_CHECKING([for $CXX option to enable ]m4_translit($1, [x], [+])[ feature]) + _CXX11_STD_TRY($1, $2, $3, ["" m4_expand($4)]) + AS_IF([test "x$cxx11_cv_prog_cxx_$1" != xno], [$5], [$6]) +]) +AC_DEFUN([CXX11_STD_AVAILABLE], +[ + cxx11_save_CXX=$CXX + AC_MSG_CHECKING([for $CXX to have C++11 rvalue references]) + _CXX11_STD_TRY([cxx11], [], [[double&& d = 2]], + [[ -std=c++11 -std=c++0x -qlanglvl=extended0x -AA ]]) + CXX=$cxx11_save_CXX +]) diff --git a/src/util.cpp b/src/util.cpp index 38e0d84b91..106cc65abd 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -107,7 +107,13 @@ namespace Sass { // convert the extracted hex string to code point value // ToDo: Maybe we could do this without creating a substring +#ifndef HAVE_CXX11_NULLPTR +#define nullptr (char **)0 +#endif uint32_t cp = strtol(s.substr (i + 1, len - 1).c_str(), nullptr, 16); +#ifndef HAVE_CXX11_NULLPTR +#undef nullptr +#endif if (cp == 0) cp = 0xFFFD; @@ -401,7 +407,13 @@ namespace Sass { // convert the extracted hex string to code point value // ToDo: Maybe we could do this without creating a substring - uint32_t cp = strtol(s.substr (i + 1, len - 1).c_str(), nullptr, 16); +#ifndef HAVE_CXX11_NULLPTR +#define nullptr (char **)0 +#endif + uint32_t cp = strtol(s.substr (i + 1, len - 1).c_str(), nullptr, 16); +#ifndef HAVE_CXX11_NULLPTR +#undef nullptr +#endif if (s[i + len] == ' ') ++ len;