fix constexpr, clean-up

This commit is contained in:
Neargye 2018-08-08 00:56:14 +05:00
parent 292ca9f563
commit d297993f52
5 changed files with 38 additions and 82 deletions

View file

@ -4,6 +4,7 @@ image:
- Visual Studio 2017 - Visual Studio 2017
platform: platform:
- Win32
- x64 - x64
configuration: configuration:
@ -17,16 +18,14 @@ matrix:
environment: environment:
matrix: matrix:
- GENERATOR: "Visual Studio 15 2017 Win64"
- GENERATOR: "Visual Studio 15 2017" - GENERATOR: "Visual Studio 15 2017"
- GENERATOR: "Visual Studio 14 2015 Win64"
- GENERATOR: "Visual Studio 14 2015" - GENERATOR: "Visual Studio 14 2015"
before_build: before_build:
- if exist build RMDIR /S /Q build - if exist build RMDIR /S /Q build
- if not exist build mkdir build - if not exist build mkdir build
- cd build - cd build
- cmake .. -G "%GENERATOR%" -DCMAKE_BUILD_TYPE=${CONFIGURATION} - cmake .. -G "%GENERATOR%" -A %PLATFORM% -DCMAKE_BUILD_TYPE=${CONFIGURATION}
build_script: build_script:
- cmake --build . --config %CONFIGURATION% - cmake --build . --config %CONFIGURATION%

View file

@ -77,18 +77,6 @@ matrix:
env: env:
- CXX_COMPILER=g++-8 CC_COMPILER=gcc-8 CONFIGURATION=Debug - CXX_COMPILER=g++-8 CC_COMPILER=gcc-8 CONFIGURATION=Debug
- os: linux
compiler: clang++
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.5
packages:
- clang-3.5
env:
- CXX_COMPILER=clang++-3.5 CC_COMPILER=clang-3.5 CONFIGURATION=Debug
- os: linux - os: linux
compiler: clang++ compiler: clang++
addons: addons:
@ -173,36 +161,6 @@ matrix:
env: env:
- CXX_COMPILER=clang++-6.0 CC_COMPILER=clang-6.0 CONFIGURATION=Debug - CXX_COMPILER=clang++-6.0 CC_COMPILER=clang-6.0 CONFIGURATION=Debug
- os: osx
compiler: clang++
osx_image: xcode6.0
env:
- CXX_COMPILER=clang++ CC_COMPILER=clang CONFIGURATION=Debug
- os: osx
compiler: clang++
osx_image: xcode6.1
env:
- CXX_COMPILER=clang++ CC_COMPILER=clang CONFIGURATION=Debug
- os: osx
compiler: clang++
osx_image: xcode6.2
env:
- CXX_COMPILER=clang++ CC_COMPILER=clang CONFIGURATION=Debug
- os: osx
compiler: clang++
osx_image: xcode6.3
env:
- CXX_COMPILER=clang++ CC_COMPILER=clang CONFIGURATION=Debug
- os: osx
compiler: clang++
osx_image: xcode6.4
env:
- CXX_COMPILER=clang++ CC_COMPILER=clang CONFIGURATION=Debug
- os: osx - os: osx
compiler: clang++ compiler: clang++
osx_image: xcode7.0 osx_image: xcode7.0
@ -289,7 +247,7 @@ install:
# If linux and clang install the right version of libc++. # If linux and clang install the right version of libc++.
if [[ "${TRAVIS_OS_NAME}" == "linux" && "${CXX%%+*}" == "clang" && -n "$(ls -A ${LLVM_INSTALL})" ]]; then if [[ "${TRAVIS_OS_NAME}" == "linux" && "${CXX%%+*}" == "clang" && -n "$(ls -A ${LLVM_INSTALL})" ]]; then
LLVM_INSTALL=${DEPS_DIR}/llvm/install LLVM_INSTALL=${DEPS_DIR}/llvm/install
if [[ "${CXX}" == "clang++-3.5" ]]; then LLVM_VERSION="3.6.2"; # libc++ bug uncaught_exception not yet implemented. if [[ "${CXX}" == "clang++-3.5" ]]; then LLVM_VERSION="3.5.2";
elif [[ "${CXX}" == "clang++-3.6" ]]; then LLVM_VERSION="3.6.2"; elif [[ "${CXX}" == "clang++-3.6" ]]; then LLVM_VERSION="3.6.2";
elif [[ "${CXX}" == "clang++-3.7" ]]; then LLVM_VERSION="3.7.1"; elif [[ "${CXX}" == "clang++-3.7" ]]; then LLVM_VERSION="3.7.1";
elif [[ "${CXX}" == "clang++-3.8" ]]; then LLVM_VERSION="3.8.1"; elif [[ "${CXX}" == "clang++-3.8" ]]; then LLVM_VERSION="3.8.1";
@ -306,8 +264,8 @@ install:
travis_retry wget -O - ${LIBCXX_URL} | tar --strip-components=1 -xJ -C llvm/projects/libcxx travis_retry wget -O - ${LIBCXX_URL} | tar --strip-components=1 -xJ -C llvm/projects/libcxx
travis_retry wget -O - ${LIBCXXABI_URL} | tar --strip-components=1 -xJ -C llvm/projects/libcxxabi travis_retry wget -O - ${LIBCXXABI_URL} | tar --strip-components=1 -xJ -C llvm/projects/libcxxabi
(cd llvm/build && cmake .. -DCMAKE_INSTALL_PREFIX=${LLVM_INSTALL}) (cd llvm/build && cmake .. -DCMAKE_INSTALL_PREFIX=${LLVM_INSTALL})
(cd llvm/build/projects/libcxx && sudo make install -j JOBS) (cd llvm/build/projects/libcxx && sudo make install -j${JOBS})
(cd llvm/build/projects/libcxxabi && sudo make install -j JOBS) (cd llvm/build/projects/libcxxabi && sudo make install -j${JOBS})
export CXXFLAGS="-isystem ${LLVM_INSTALL}/include/c++/v1" export CXXFLAGS="-isystem ${LLVM_INSTALL}/include/c++/v1"
export LDFLAGS="-L ${LLVM_INSTALL}/lib -l c++ -l c++abi" export LDFLAGS="-L ${LLVM_INSTALL}/lib -l c++ -l c++abi"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${LLVM_INSTALL}/lib" export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${LLVM_INSTALL}/lib"

View file

@ -79,12 +79,11 @@ enum class Color { RED, GREEN, BLUE };
SomeStruct somevar; SomeStruct somevar;
Long othervar; Long othervar;
SomeStruct& refvar = somevar;
SomeStruct* ptrvar = &somevar; SomeStruct* ptrvar = &somevar;
int main() { int main() {
#if (__cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSC_VER >= 1910 && _MSVC_LANG >= 201402L)) #if NAMEOF_HAS_CONSTEXPR
// Compile-time supported by C++14. // Compile-time nameof supported by C++14.
constexpr auto constexpr_work_fine = NAMEOF(somevar); constexpr auto constexpr_work_fine = NAMEOF(somevar);
static_assert("somevar" == constexpr_work_fine, ""); static_assert("somevar" == constexpr_work_fine, "");
#endif #endif
@ -111,12 +110,7 @@ int main() {
// Type name. // Type name.
std::cout << NAMEOF_TYPE(somevar) << std::endl; // SomeStruct std::cout << NAMEOF_TYPE(somevar) << std::endl; // SomeStruct
std::cout << NAMEOF_TYPE(refvar) << std::endl; // SomeStruct
std::cout << NAMEOF_TYPE(ptrvar) << std::endl; // SomeStruct
std::cout << NAMEOF_TYPE(othervar.ll) << std::endl; // LL std::cout << NAMEOF_TYPE(othervar.ll) << std::endl; // LL
std::cout << NAMEOF_TYPE(othervar.ll.field) << std::endl; // int
std::cout << NAMEOF_TYPE(Color::RED) << std::endl; // Color
std::cout << NAMEOF_TYPE(SomeClass<int>{}) << std::endl; // SomeClass std::cout << NAMEOF_TYPE(SomeClass<int>{}) << std::endl; // SomeClass
// Type full name. // Type full name.

View file

@ -35,10 +35,12 @@
#include <string> #include <string>
#include <ostream> #include <ostream>
#if (__cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSC_VER >= 1910 && _MSVC_LANG >= 201402L)) #if (defined(__cpp_constexpr) && __cpp_constexpr >= 201304L) || (defined(_MSC_VER) && _MSC_VER >= 1910)
# define NAMEOF_CONSTEXPR14 constexpr # define NAMEOF_CONSTEXPR constexpr
# define NAMEOF_HAS_CONSTEXPR 1
#else #else
# define NAMEOF_CONSTEXPR14 # define NAMEOF_CONSTEXPR
# define NAMEOF_HAS_CONSTEXPR 0
#endif #endif
namespace nameof { namespace nameof {
@ -70,16 +72,16 @@ inline constexpr std::size_t StrLen(const char* str, std::size_t size = 0) {
// STD like compile-time const char* string. // STD like compile-time const char* string.
class cstring final { class cstring final {
const char* str_; const char* str_;
std::size_t size_; const std::size_t size_;
public: public:
constexpr cstring(const char* str, std::size_t length, std::size_t prefix = 0, std::size_t suffix = 0) noexcept inline constexpr cstring(const char* str, std::size_t length, std::size_t prefix = 0, std::size_t suffix = 0) noexcept
: str_{str + prefix}, : str_{str + prefix},
size_{length - prefix - suffix} {} size_{length - prefix - suffix} {}
constexpr cstring() noexcept : cstring{nullptr, 0, 0, 0} {} inline constexpr cstring() noexcept : cstring{nullptr, 0, 0, 0} {}
constexpr cstring(const char* str) noexcept : cstring{str, StrLen(str), 0, 0} {} inline constexpr cstring(const char* str) noexcept : cstring{str, StrLen(str), 0, 0} {}
cstring(const cstring&) = default; cstring(const cstring&) = default;
@ -145,7 +147,7 @@ inline constexpr bool IsLexeme(char s) noexcept {
return !((s >= '0' && s <= '9') || (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || s == '_'); return !((s >= '0' && s <= '9') || (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || s == '_');
} }
inline NAMEOF_CONSTEXPR14 cstring NameofBase(const char* name, std::size_t length, bool with_suffix) noexcept { inline NAMEOF_CONSTEXPR cstring NameofBase(const char* name, std::size_t length, bool with_suffix) noexcept {
std::size_t p = 0; std::size_t p = 0;
if(IsLexeme(name[length - 1])) { if(IsLexeme(name[length - 1])) {
for (std::size_t i = length, h = 0; i > 0; --i) { for (std::size_t i = length, h = 0; i > 0; --i) {
@ -195,7 +197,7 @@ inline constexpr cstring NameofRaw(const char* name, std::size_t length) noexcep
template <typename T, template <typename T,
typename = typename std::enable_if<!std::is_reference<T>::value && typename = typename std::enable_if<!std::is_reference<T>::value &&
!std::is_void<T>::value>::type> !std::is_void<T>::value>::type>
inline NAMEOF_CONSTEXPR14 detail::cstring Nameof(const T&, const char* name, std::size_t length, bool with_suffix = false) noexcept { inline NAMEOF_CONSTEXPR detail::cstring Nameof(const T&, const char* name, std::size_t length, bool with_suffix = false) noexcept {
return detail::NameofBase(name, length, with_suffix); return detail::NameofBase(name, length, with_suffix);
} }
@ -203,19 +205,23 @@ template <typename T,
typename = typename std::enable_if<!std::is_enum<T>::value && typename = typename std::enable_if<!std::is_enum<T>::value &&
!std::is_function<T>::value && !std::is_function<T>::value &&
!std::is_member_function_pointer<T>::value>::type> !std::is_member_function_pointer<T>::value>::type>
inline NAMEOF_CONSTEXPR14 detail::cstring Nameof(T&&, const char*, std::size_t, bool) = delete; inline NAMEOF_CONSTEXPR detail::cstring Nameof(T&&, const char*, std::size_t, bool) = delete;
template <typename T> template <typename T>
inline constexpr detail::cstring NameofTypeRaw() noexcept { inline NAMEOF_CONSTEXPR detail::cstring NameofTypeRaw() noexcept {
#if defined(__clang__) #if defined(__clang__)
return {__PRETTY_FUNCTION__, return {__PRETTY_FUNCTION__,
sizeof(__PRETTY_FUNCTION__) - 1, sizeof(__PRETTY_FUNCTION__) - 1,
sizeof("detail::cstring nameof::NameofTypeRaw() [T = ") - 1, sizeof("detail::cstring nameof::NameofTypeRaw() [T = ") - 1,
sizeof("]") - 1}; sizeof("]") - 1};
#elif defined(__GNUC__) #elif defined(__GNUC__)
return {__PRETTY_FUNCTION__, return {__PRETTY_FUNCTION__,
sizeof(__PRETTY_FUNCTION__) - 1, sizeof(__PRETTY_FUNCTION__) - 1,
# if NAMEOF_HAS_CONSTEXPR
sizeof("constexpr nameof::detail::cstring nameof::NameofTypeRaw() [with T = ") - 1, sizeof("constexpr nameof::detail::cstring nameof::NameofTypeRaw() [with T = ") - 1,
# else
sizeof("nameof::detail::cstring nameof::NameofTypeRaw() [with T = ") - 1,
# endif
sizeof("]") - 1}; sizeof("]") - 1};
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
return {__FUNCSIG__, return {__FUNCSIG__,
@ -228,14 +234,14 @@ inline constexpr detail::cstring NameofTypeRaw() noexcept {
} }
template <typename T, typename D = typename detail::Decay<T>::type> template <typename T, typename D = typename detail::Decay<T>::type>
inline constexpr detail::cstring NameofType() noexcept { inline NAMEOF_CONSTEXPR detail::cstring NameofType() noexcept {
const auto raw_type_name = NameofTypeRaw<D>(); const auto raw_type_name = NameofTypeRaw<D>();
return detail::NameofBase(raw_type_name.begin(), raw_type_name.length(), false); return detail::NameofBase(raw_type_name.begin(), raw_type_name.length(), false);
} }
} // namespace nameof } // namespace nameof
#if defined(__GNUC__) || defined(__clang__) #if defined(__GNUC__) || defined(__clang__)
// Used to obtain the raw string name of a variable, type, member, function, macros. // Used to obtain the raw string name of a variable, type, member, function, macros.
# define NAMEOF_RAW(name) ::nameof::detail::NameofRaw(#name, ((sizeof(#name) / sizeof(char)) - 1) + (0 * sizeof(void (*)(__typeof__(name))))) # define NAMEOF_RAW(name) ::nameof::detail::NameofRaw(#name, ((sizeof(#name) / sizeof(char)) - 1) + (0 * sizeof(void (*)(__typeof__(name)))))
#elif defined(_MSC_VER) #elif defined(_MSC_VER)

View file

@ -75,8 +75,8 @@ Long othervar;
SomeStruct& refvar = somevar; SomeStruct& refvar = somevar;
SomeStruct* ptrvar = &somevar; SomeStruct* ptrvar = &somevar;
#if (__cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSC_VER >= 1910 && _MSVC_LANG >= 201402L)) #if NAMEOF_HAS_CONSTEXPR
// Compile-time supported by C++14. // Compile-time nameof supported by C++14.
TEST_CASE("constexpr") { TEST_CASE("constexpr") {
SECTION("NAMEOF") { SECTION("NAMEOF") {
// variable // variable
@ -114,20 +114,20 @@ TEST_CASE("constexpr") {
static_assert(cx6 == "__cplusplus", ""); static_assert(cx6 == "__cplusplus", "");
} }
//TODO: constexpr NameofType gcc. # if defined(_MSC_VER) || defined(__clang__)
#if (defined(__clang__) || (defined(_MSC_VER) && _MSC_VER >= 1910))
SECTION("NAMEOF_TYPE") { SECTION("NAMEOF_TYPE") {
SomeClass<int> a; SomeClass<int> a;
constexpr auto cx1 = NAMEOF_TYPE(a); constexpr auto cx1 = NAMEOF_TYPE(a);
static_assert(cx1 == "SomeClass", "");
constexpr auto cx2 = nameof::NameofType<SomeClass<int>>(); constexpr auto cx2 = nameof::NameofType<SomeClass<int>>();
static_assert(cx2 == "SomeClass", "");
constexpr auto cx3 = nameof::NameofType<decltype(a)>(); constexpr auto cx3 = nameof::NameofType<decltype(a)>();
static_assert(cx1 == "SomeClass", "");
static_assert(cx2 == "SomeClass", "");
static_assert(cx3 == "SomeClass", ""); static_assert(cx3 == "SomeClass", "");
} }
# endif
SECTION("NAMEOF_TYPE_RAW") { SECTION("NAMEOF_TYPE_RAW") {
SomeClass<int> a; SomeClass<int> a;
@ -136,17 +136,16 @@ TEST_CASE("constexpr") {
constexpr auto cx2 = nameof::NameofTypeRaw<SomeClass<int>>(); constexpr auto cx2 = nameof::NameofTypeRaw<SomeClass<int>>();
constexpr auto cx3 = nameof::NameofTypeRaw<decltype(a)>(); constexpr auto cx3 = nameof::NameofTypeRaw<decltype(a)>();
#if defined(_MSC_VER) # if defined(_MSC_VER)
static_assert(cx1 == "class SomeClass<int>", ""); static_assert(cx1 == "class SomeClass<int>", "");
static_assert(cx2 == "class SomeClass<int>", ""); static_assert(cx2 == "class SomeClass<int>", "");
static_assert(cx3 == "class SomeClass<int>", ""); static_assert(cx3 == "class SomeClass<int>", "");
#elif defined(__GNUC__) || defined(__clang__) # elif defined(__clang__)
static_assert(cx1 == "SomeClass<int>", ""); static_assert(cx1 == "SomeClass<int>", "");
static_assert(cx2 == "SomeClass<int>", ""); static_assert(cx2 == "SomeClass<int>", "");
static_assert(cx3 == "SomeClass<int>", ""); static_assert(cx3 == "SomeClass<int>", "");
#endif # endif
} }
#endif
} }
#endif #endif