Skip to content

Commit

Permalink
Bump minimum C++ version to C++17 after branch cut for v29.
Browse files Browse the repository at this point in the history
Branch cut for v29 is done on 2024-09-30:
https://github.com/protocolbuffers/protobuf/releases/tag/v29.0-rc1

The next version v30 will be a breaking release. The release date is scheduled
after the EOL of C++14 support on 2024-12-15 for Google open source projects
generally:
https://github.com/google/oss-policies-info/blob/main/foundational-cxx-support-matrix.md

This commit allows us to start taking advantage of C++17 features now.

Some issues I ran into while upgrading:

Two GCC 9.5 bugs related to -Wunused-but-set-parameter:
- https://godbolt.org/z/qo51cKe7b
- https://godbolt.org/z/65qW3vGhP

Another GCC warning related to -Wself-assign in a template.

There is a custom ASAN check that is not yet open sourced. I'll see if I can
open source them in a subsequent commit.

#test-continuous

PiperOrigin-RevId: 684540366
  • Loading branch information
tonyliaoss authored and copybara-github committed Oct 17, 2024
1 parent 3fe0c75 commit 8420ed1
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 32 deletions.
34 changes: 15 additions & 19 deletions .github/workflows/test_cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ jobs:
-c "set -ex;
sccache -z;
cmake . -DWITH_PROTOC=/workspace/${{ steps.cross-compile.outputs.protoc }}
-Dprotobuf_BUILD_LIBUPB=OFF -Dprotobuf_BUILD_CONFORMANCE=ON -DCMAKE_CXX_STANDARD=14
-Dprotobuf_BUILD_LIBUPB=OFF -Dprotobuf_BUILD_CONFORMANCE=ON -DCMAKE_CXX_STANDARD=17
-Dprotobuf_WITH_ZLIB=OFF ${{ env.SCCACHE_CMAKE_FLAGS }};
cmake --build . --parallel 20;
ctest --parallel 20;
Expand All @@ -141,15 +141,13 @@ jobs:
fail-fast: false # Don't cancel all jobs if one fails.
matrix:
include:
- flags: -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=14 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
- flags: -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=17 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
- name: Ninja
flags: -G Ninja -DCMAKE_CXX_STANDARD=14 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
flags: -G Ninja -DCMAKE_CXX_STANDARD=17 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
continuous-only: true
- name: Shared
flags: -Dprotobuf_BUILD_SHARED_LIBS=ON -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=14 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
flags: -Dprotobuf_BUILD_SHARED_LIBS=ON -Dprotobuf_BUILD_EXAMPLES=ON -DCMAKE_CXX_STANDARD=17 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
continuous-only: true
- name: C++17
flags: -DCMAKE_CXX_STANDARD=17 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
- name: C++20
flags: -DCMAKE_CXX_STANDARD=20 -Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
- name: Fetch
Expand Down Expand Up @@ -217,15 +215,15 @@ jobs:
image: us-docker.pkg.dev/protobuf-build/containers/test/linux/cmake:3.16.9-f39fc8b4e244fe5cd4c7138d0b6959a52b46ca48
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
command: >-
/install.sh -DCMAKE_CXX_STANDARD=14 ${{ env.SCCACHE_CMAKE_FLAGS }}
/install.sh -DCMAKE_CXX_STANDARD=17 ${{ env.SCCACHE_CMAKE_FLAGS }}
${{ matrix.flags }}
-Dprotobuf_BUILD_SHARED_LIBS=ON \&\&
/test.sh
${{ env.SCCACHE_CMAKE_FLAGS }}
-Dprotobuf_REMOVE_INSTALLED_HEADERS=ON
-Dprotobuf_BUILD_PROTOBUF_BINARIES=OFF
-Dprotobuf_BUILD_CONFORMANCE=ON
-DCMAKE_CXX_STANDARD=14
-DCMAKE_CXX_STANDARD=17
${{ matrix.flags }}
# This test should always be skipped on presubmit
Expand Down Expand Up @@ -253,21 +251,19 @@ jobs:
image: us-docker.pkg.dev/protobuf-build/containers/test/linux/cmake:3.16.9-f39fc8b4e244fe5cd4c7138d0b6959a52b46ca48
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
command: >-
/install.sh -DCMAKE_CXX_STANDARD=14 ${{ env.SCCACHE_CMAKE_FLAGS }}
/install.sh -DCMAKE_CXX_STANDARD=17 ${{ env.SCCACHE_CMAKE_FLAGS }}
-Dprotobuf_USE_EXTERNAL_GTEST=ON -Dprotobuf_FETCH_DEPENDENCIES=OFF
-Dprotobuf_BUILD_EXAMPLES=OFF \&\&
mkdir examples/build \&\&
cd examples/build \&\&
cmake .. -DCMAKE_CXX_STANDARD=14 \&\&
cmake .. -DCMAKE_CXX_STANDARD=17 \&\&
cmake --build .
linux-cmake-gcc:
strategy:
fail-fast: false # Don't cancel all jobs if one fails.
matrix:
include:
- name: C++14
flags: -DCMAKE_CXX_STANDARD=14
- name: C++17
flags: -DCMAKE_CXX_STANDARD=17
continuous-only: true
Expand Down Expand Up @@ -331,7 +327,7 @@ jobs:
/bin/bash -cex '
cd /workspace;
sccache -z;
cmake . -DCMAKE_CXX_STANDARD=14 ${{ env.SCCACHE_CMAKE_FLAGS }};
cmake . -DCMAKE_CXX_STANDARD=17 ${{ env.SCCACHE_CMAKE_FLAGS }};
cmake --build . --parallel 20;
ctest --verbose --parallel 20;
sccache -s'
Expand Down Expand Up @@ -391,22 +387,22 @@ jobs:
include:
- name: MacOS CMake
os: macos-13
flags: -DCMAKE_CXX_STANDARD=14
flags: -DCMAKE_CXX_STANDARD=17
cache-prefix: macos-cmake
continuous-only: true
- name: Windows CMake
os: windows-2022
flags: >-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
-Dprotobuf_BUILD_SHARED_LIBS=OFF
-Dprotobuf_BUILD_SHARED_LIBS=OFF -DCMAKE_CXX_STANDARD=17
-Dprotobuf_BUILD_EXAMPLES=ON
vsversion: '2022'
cache-prefix: windows-2022-cmake
- name: Windows CMake 2019
os: windows-2019
flags: >-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
-Dprotobuf_BUILD_SHARED_LIBS=OFF
-Dprotobuf_BUILD_SHARED_LIBS=OFF -DCMAKE_CXX_STANDARD=17
-Dprotobuf_BUILD_EXAMPLES=ON
vsversion: '2019'
cache-prefix: windows-2019-cmake
Expand All @@ -416,7 +412,7 @@ jobs:
- name: Windows CMake 32-bit
os: windows-2022
flags: >-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF -DCMAKE_CXX_STANDARD=17
vsversion: '2022'
windows-arch: 'win32'
cache-prefix: windows-2022-win32-cmake
Expand All @@ -425,15 +421,15 @@ jobs:
os: windows-2022
flags: >-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
-Dprotobuf_BUILD_SHARED_LIBS=ON
-Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_CXX_STANDARD=17
vsversion: '2022'
cache-prefix: windows-2022-cmake
- name: Windows CMake Install
os: windows-2022
install-flags: -G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF -Dprotobuf_BUILD_TESTS=OFF
flags: >-
-G Ninja -Dprotobuf_WITH_ZLIB=OFF -Dprotobuf_BUILD_CONFORMANCE=OFF
-Dprotobuf_REMOVE_INSTALLED_HEADERS=ON
-Dprotobuf_REMOVE_INSTALLED_HEADERS=ON -DCMAKE_CXX_STANDARD=17
-Dprotobuf_BUILD_PROTOBUF_BINARIES=OFF
vsversion: '2022'
cache-prefix: windows-2022-cmake
Expand Down
3 changes: 2 additions & 1 deletion ci/Linux.bazelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import common.bazelrc

build --cxxopt=-std=c++14 --host_cxxopt=-std=c++14
build --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
build --cxxopt="-Woverloaded-virtual"
build --copt="-Werror" --copt="-Wno-sign-compare" --copt="-Wno-sign-conversion" --copt="-Wno-error=sign-conversion" --copt="-Wno-deprecated-declarations"

4 changes: 3 additions & 1 deletion ci/Windows.bazelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import common.bazelrc

# Workaround for maximum path length issues
build --cxxopt=/std:c++17 --host_cxxopt=/std:c++17
startup --output_user_root=C:/tmp --windows_enable_symlinks
common --enable_runfiles
common --enable_runfiles

3 changes: 2 additions & 1 deletion ci/macOS.bazelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import common.bazelrc

build --cxxopt=-std=c++14 --host_cxxopt=-std=c++14
build --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
build --cxxopt="-Woverloaded-virtual"
build --copt="-Werror" --copt="-Wno-sign-compare" --copt="-Wno-sign-conversion" --copt="-Wno-error=sign-conversion" --copt="-Wno-deprecated-declarations"
common --repo_env=BAZEL_NO_APPLE_CPP_TOOLCHAIN=1
common --xcode_version_config=@com_google_protobuf//.github:host_xcodes

6 changes: 3 additions & 3 deletions examples/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ common --enable_platform_specific_config
# TODO: Once the provider is ported to Starlark the flag may be removed.
common --experimental_google_legacy_api

build:linux --cxxopt=-std=c++14 --host_cxxopt=-std=c++14
build:macos --cxxopt=-std=c++14 --host_cxxopt=-std=c++14
build:linux --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
build:macos --cxxopt=-std=c++17 --host_cxxopt=-std=c++17

common:windows --enable_runfiles
common:windows --cxxopt=/std:c++17 --host_cxxopt=/std:c++17 --enable_runfiles

build --experimental_remote_cache_eviction_retries=5
build --remote_download_outputs=all
2 changes: 1 addition & 1 deletion src/google/protobuf/arena_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,7 @@ TEST(ArenaTest, ClearOneofMessageOnArena) {
#ifndef PROTOBUF_ASAN
EXPECT_NE(child->moo_int(), 100);
#else
#if GTEST_HAS_DEATH_TEST && defined(__cpp_if_constexpr)
#if GTEST_HAS_DEATH_TEST && defined(PROTO2_OPENSOURCE)
EXPECT_DEATH(EXPECT_EQ(child->moo_int(), 0), "use-after-poison");
#endif
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/google/protobuf/port_def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ static_assert(PROTOBUF_GNUC_MIN(7, 3), "Protobuf only supports GCC 7.3 and newer
#elif defined(_MSVC_LANG)
static_assert(PROTOBUF_MSC_VER_MIN(1910), "Protobuf only supports MSVC 2017 and newer.");
#endif
static_assert(PROTOBUF_CPLUSPLUS_MIN(201402L), "Protobuf only supports C++14 and newer.");
static_assert(PROTOBUF_CPLUSPLUS_MIN(201703L),
"Protobuf only supports C++17 and newer.");

// Check minimum Abseil version.
#if defined(ABSL_LTS_RELEASE_VERSION) && defined(ABSL_LTS_RELEASE_PATCH_LEVEL)
Expand Down Expand Up @@ -371,7 +372,7 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
#ifdef PROTOBUF_NODISCARD
#error PROTOBUF_NODISCARD was previously defined
#endif
#if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) && PROTOBUF_CPLUSPLUS_MIN(201703L)
#if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard)
#define PROTOBUF_NODISCARD [[nodiscard]]
#elif ABSL_HAVE_ATTRIBUTE(warn_unused_result) || defined(__GNUC__)
#define PROTOBUF_NODISCARD __attribute__((warn_unused_result))
Expand Down Expand Up @@ -615,8 +616,7 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3),
#ifdef PROTOBUF_UNUSED
#error PROTOBUF_UNUSED was previously defined
#endif
#if ABSL_HAVE_CPP_ATTRIBUTE(maybe_unused) || \
(PROTOBUF_MSC_VER_MIN(1911) && PROTOBUF_CPLUSPLUS_MIN(201703L))
#if ABSL_HAVE_CPP_ATTRIBUTE(maybe_unused) || (PROTOBUF_MSC_VER_MIN(1911))
#define PROTOBUF_UNUSED [[maybe_unused]]
#elif ABSL_HAVE_ATTRIBUTE(unused) || defined(__GNUC__)
#define PROTOBUF_UNUSED __attribute__((__unused__))
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/proto3_arena_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ TEST(Proto3ArenaTest, CheckOneofMessageFieldIsCleared) {
#ifndef PROTOBUF_ASAN
EXPECT_EQ(child->bb(), 0);
#else
#if GTEST_HAS_DEATH_TEST && defined(__cpp_if_constexpr)
#if GTEST_HAS_DEATH_TEST && defined(PROTO2_OPENSOURCE)
EXPECT_DEATH(EXPECT_EQ(child->bb(), 100), "use-after-poison");
#endif
#endif
Expand Down
7 changes: 7 additions & 0 deletions src/google/protobuf/reflection_visit_field_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,13 @@ struct RepeatedGroupDynamicExtensionInfo
// users from a similar dispatch without creating KeyInfo or ValueInfo per type.
template <FieldDescriptor::CppType cpp_type, typename T>
inline size_t MapPrimitiveFieldByteSize(FieldDescriptor::Type type, T value) {
// There is a bug in GCC 9.5 where if-constexpr arguments are not understood
// if encased in a switch statement. A reproduction of the bug can be found
// at: https://godbolt.org/z/qo51cKe7b
// This is fixed in GCC 10.1+.
(void)type; // Suppress -Wunused-but-set-parameter
(void)value; // Suppress -Wunused-but-set-parameter

if constexpr (cpp_type == FieldDescriptor::CPPTYPE_INT32) {
static_assert(std::is_same_v<T, int32_t>, "type mismatch");
switch (type) {
Expand Down
14 changes: 13 additions & 1 deletion src/google/protobuf/reflection_visit_fields_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void MutateNothingByVisit(Message& message) {
}
} else {
for (auto& it : info.Mutable()) {
it = it;
it = *&it; // Avoid -Wself-assign.
}
}
} else {
Expand Down Expand Up @@ -207,6 +207,12 @@ TEST_P(VisitFieldsTest, MutateNothingByVisitIdempotent) {

template <typename InfoT>
inline size_t MapKeyByteSizeLong(FieldDescriptor::Type type, InfoT info) {
// There is a bug in GCC 9.5 where if-constexpr arguments are not understood
// if passed into a helper function. A reproduction of the bug can be found
// at: https://godbolt.org/z/65qW3vGhP
// This is fixed in GCC 10.1+.
(void)type; // Suppress -Wunused-but-set-parameter

if constexpr (info.cpp_type == FieldDescriptor::CPPTYPE_STRING) {
return WireFormatLite::StringSize(info.Get());
} else {
Expand All @@ -216,6 +222,12 @@ inline size_t MapKeyByteSizeLong(FieldDescriptor::Type type, InfoT info) {

template <typename InfoT>
inline size_t MapValueByteSizeLong(FieldDescriptor::Type type, InfoT info) {
// There is a bug in GCC 9.5 where if-constexpr arguments are not understood
// if passed into a helper function. A reproduction of the bug can be found
// at: https://godbolt.org/z/65qW3vGhP
// This is fixed in GCC 10.1+.
(void)type; // Suppress -Wunused-but-set-parameter

if constexpr (info.cpp_type == FieldDescriptor::CPPTYPE_STRING) {
return WireFormatLite::StringSize(info.Get());
} else if constexpr (info.cpp_type == FieldDescriptor::CPPTYPE_MESSAGE) {
Expand Down

0 comments on commit 8420ed1

Please sign in to comment.