Merge pull request #2892 from Clcanny/charles-dev

[C++ runtime] Remove codecvt header (deprecated in C++17) in cpp runtime.
This commit is contained in:
Terence Parr 2020-09-13 09:01:40 -07:00 committed by GitHub
commit e01df6b6a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 114 additions and 29 deletions

View File

@ -255,4 +255,5 @@ YYYY/MM/DD, github id, Full name, email
2020/06/04, IohannRabeson, Iohann Rabeson, iotaka6@gmail.com
2020/07/01, sha-N, Shan M Mathews, admin@bluestarqatar.com
2020/08/22, stevenjohnstone, Steven Johnstone, steven.james.johnstone@gmail.com
2020/09/06, ArthurSonzogni, Sonzogni Arthur, arthursonzogni@gmail.com
2020/09/06, ArthurSonzogni, Sonzogni Arthur, arthursonzogni@gmail.com
2020/09/12, Clcanny, Charles Ruan, a837940593@gmail.com

View File

@ -71,6 +71,9 @@ else()
set(MY_CXX_WARNING_FLAGS " -Wall -pedantic -W")
endif()
# Define USE_UTF8_INSTEAD_OF_CODECVT macro.
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_UTF8_INSTEAD_OF_CODECVT")
# Initialize CXXFLAGS.
if("${CMAKE_VERSION}" VERSION_GREATER 3.1.0)
set(CMAKE_CXX_STANDARD 11)

View File

@ -4,9 +4,27 @@
rm -f -R antlr4-runtime build lib 2> /dev/null
rm antlr4-cpp-runtime-macos.zip 2> /dev/null
# Get utf8 dependency.
mkdir -p runtime/thirdparty 2> /dev/null
pushd runtime/thirdparty
if [ ! -d utfcpp ]
then
git clone https://github.com/nemtrif/utfcpp.git utfcpp
pushd utfcpp
git checkout tags/v3.1.1
popd
fi
popd
# Binaries
xcodebuild -project runtime/antlrcpp.xcodeproj -target antlr4 -configuration Release
xcodebuild -project runtime/antlrcpp.xcodeproj -target antlr4_static -configuration Release
xcodebuild -project runtime/antlrcpp.xcodeproj \
-target antlr4 \
# GCC_PREPROCESSOR_DEFINITIONS='$GCC_PREPROCESSOR_DEFINITIONS USE_UTF8_INSTEAD_OF_CODECVT' \
-configuration Release
xcodebuild -project runtime/antlrcpp.xcodeproj \
-target antlr4_static \
# GCC_PREPROCESSOR_DEFINITIONS='$GCC_PREPROCESSOR_DEFINITIONS USE_UTF8_INSTEAD_OF_CODECVT' \
-configuration Release
rm -f -R lib
mkdir lib
mv runtime/build/Release/libantlr4-runtime.a lib/
@ -17,6 +35,9 @@ rm -f -R antlr4-runtime
pushd runtime/src
find . -name '*.h' | cpio -pdm ../../antlr4-runtime
popd
pushd runtime/thirdparty/utfcpp/source
find . -name '*.h' | cpio -pdm ../../../../antlr4-runtime
popd
# Zip up and clean up
zip -r antlr4-cpp-runtime-macos.zip antlr4-runtime lib

View File

@ -1,4 +1,19 @@
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
set(THIRDPARTY_DIR ${CMAKE_BINARY_DIR}/runtime/thirdparty)
set(UTFCPP_DIR ${THIRDPARTY_DIR}/utfcpp)
ExternalProject_Add(
utfcpp
GIT_REPOSITORY "git://github.com/nemtrif/utfcpp"
GIT_TAG "v3.1.1"
SOURCE_DIR ${UTFCPP_DIR}
UPDATE_DISCONNECTED 1
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${UTFCPP_DIR}/install
TEST_AFTER_INSTALL 1
STEP_TARGETS build)
include_directories(
${PROJECT_SOURCE_DIR}/runtime/src
${PROJECT_SOURCE_DIR}/runtime/src/atn
@ -8,6 +23,8 @@ include_directories(
${PROJECT_SOURCE_DIR}/runtime/src/tree
${PROJECT_SOURCE_DIR}/runtime/src/tree/pattern
${PROJECT_SOURCE_DIR}/runtime/src/tree/xpath
${UTFCPP_DIR}/install/include/utf8cpp
${UTFCPP_DIR}/install/include/utf8cpp/utf8
)
@ -33,8 +50,8 @@ add_custom_target(make_lib_output_dir ALL
COMMAND ${CMAKE_COMMAND} -E make_directory ${LIB_OUTPUT_DIR}
)
add_dependencies(antlr4_shared make_lib_output_dir)
add_dependencies(antlr4_static make_lib_output_dir)
add_dependencies(antlr4_shared make_lib_output_dir utfcpp)
add_dependencies(antlr4_static make_lib_output_dir utfcpp)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
target_link_libraries(antlr4_shared ${UUID_LIBRARIES})
@ -102,15 +119,23 @@ set_target_properties(antlr4_static
COMPILE_FLAGS "${disabled_compile_warnings} ${extra_static_compile_flags}")
install(TARGETS antlr4_shared
DESTINATION lib
DESTINATION lib
EXPORT antlr4-targets)
install(TARGETS antlr4_static
DESTINATION lib
EXPORT antlr4-targets)
install(DIRECTORY "${PROJECT_SOURCE_DIR}/runtime/src/"
install(DIRECTORY "${PROJECT_SOURCE_DIR}/runtime/src/"
DESTINATION "include/antlr4-runtime"
COMPONENT dev
COMPONENT dev
FILES_MATCHING PATTERN "*.h"
)
install(FILES "${UTFCPP_DIR}/source/utf8.h"
DESTINATION "include/antlr4-runtime")
install(DIRECTORY "${UTFCPP_DIR}/source/utf8"
DESTINATION "include/antlr4-runtime"
COMPONENT dev
FILES_MATCHING PATTERN "*.h"
)

View File

@ -2889,7 +2889,7 @@
GCC_WARN_UNUSED_LABEL = YES;
GCC_WARN_UNUSED_PARAMETER = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = src/;
HEADER_SEARCH_PATHS = "src/ thirdparty/utfcpp/source/ thirdparty/utfcpp/source/utf8/";
MACOSX_DEPLOYMENT_TARGET = 10.9;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
@ -2945,7 +2945,7 @@
GCC_WARN_UNUSED_LABEL = YES;
GCC_WARN_UNUSED_PARAMETER = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = src/;
HEADER_SEARCH_PATHS = "src/ thirdparty/utfcpp/source/ thirdparty/utfcpp/source/utf8/";
MACOSX_DEPLOYMENT_TARGET = 10.9;
SDKROOT = macosx;
};

View File

@ -8,7 +8,6 @@
#include <algorithm>
#include <assert.h>
#include <atomic>
#include <codecvt>
#include <chrono>
#include <fstream>
#include <iostream>
@ -37,6 +36,10 @@
#include <condition_variable>
#include <functional>
#ifndef USE_UTF8_INSTEAD_OF_CODECVT
#include <codecvt>
#endif
// Defines for the Guid class and other platform dependent stuff.
#ifdef _WIN32
#ifdef _MSC_VER

View File

@ -20,16 +20,26 @@ void replaceAll(std::string& str, std::string const& from, std::string const& to
}
std::string ws2s(std::wstring const& wstr) {
#ifndef USE_UTF8_INSTEAD_OF_CODECVT
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string narrow = converter.to_bytes(wstr);
#else
std::string narrow;
utf8::utf32to8(wstr.begin(), wstr.end(), std::back_inserter(narrow));
#endif
return narrow;
}
std::wstring s2ws(const std::string &str) {
#ifndef USE_UTF8_INSTEAD_OF_CODECVT
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wide = converter.from_bytes(str);
#else
std::wstring wide;
utf8::utf8to32(str.begin(), str.end(), std::back_inserter(wide));
#endif
return wide;
}

View File

@ -7,43 +7,65 @@
#include "antlr4-common.h"
#ifdef USE_UTF8_INSTEAD_OF_CODECVT
#include "utf8.h"
#endif
namespace antlrcpp {
// For all conversions utf8 <-> utf32.
// I wouldn't prefer wstring_convert because: according to
// https://en.cppreference.com/w/cpp/locale/wstring_convert,
// wstring_convert is deprecated in C++17.
// utfcpp (https://github.com/nemtrif/utfcpp) is a substitution.
#ifndef USE_UTF8_INSTEAD_OF_CODECVT
// VS 2015 and VS 2017 have different bugs in std::codecvt_utf8<char32_t> (VS 2013 works fine).
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
typedef std::wstring_convert<std::codecvt_utf8<__int32>, __int32> UTF32Converter;
#else
typedef std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> UTF32Converter;
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
typedef std::wstring_convert<std::codecvt_utf8<__int32>, __int32> UTF32Converter;
#else
typedef std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> UTF32Converter;
#endif
#endif
// The conversion functions fails in VS2017, so we explicitly use a workaround.
template<typename T>
inline std::string utf32_to_utf8(T const& data)
{
// Don't make the converter static or we have to serialize access to it.
thread_local UTF32Converter converter;
#ifndef USE_UTF8_INSTEAD_OF_CODECVT
// Don't make the converter static or we have to serialize access to it.
thread_local UTF32Converter converter;
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
const auto p = reinterpret_cast<const int32_t *>(data.data());
return converter.to_bytes(p, p + data.size());
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
const auto p = reinterpret_cast<const int32_t *>(data.data());
return converter.to_bytes(p, p + data.size());
#else
return converter.to_bytes(data);
#endif
#else
return converter.to_bytes(data);
std::string narrow;
utf8::utf32to8(data.begin(), data.end(), std::back_inserter(narrow));
return narrow;
#endif
}
inline UTF32String utf8_to_utf32(const char* first, const char* last)
{
thread_local UTF32Converter converter;
#ifndef USE_UTF8_INSTEAD_OF_CODECVT
thread_local UTF32Converter converter;
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
auto r = converter.from_bytes(first, last);
i32string s = reinterpret_cast<const int32_t *>(r.data());
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000
auto r = converter.from_bytes(first, last);
i32string s = reinterpret_cast<const int32_t *>(r.data());
return s;
#else
std::u32string s = converter.from_bytes(first, last);
return s;
#endif
#else
std::u32string s = converter.from_bytes(first, last);
UTF32String wide;
utf8::utf8to32(first, last, std::back_inserter(wide));
return wide;
#endif
return s;
}
void replaceAll(std::string &str, std::string const& from, std::string const& to);