Skip to content

Commit

Permalink
Add a workaround for GCC constexpr bug
Browse files Browse the repository at this point in the history
This turns the constexpr constructors into templates to silence errors when constexpr isn't valid.  We are also switching to 12.2 for GCC/cmake tests to prevent regressions (9.5 and 13.1 are already tested by GCC/bazel tests).

Fixes #12807

PiperOrigin-RevId: 532258101
  • Loading branch information
mkruskal-google committed May 15, 2023
1 parent 521c04e commit 67ecdde
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test_cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ jobs:
- name: Run tests
uses: protocolbuffers/protobuf-ci/docker@v1
with:
image: us-docker.pkg.dev/protobuf-build/containers/test/linux/gcc:13.1-5.4.0-307caa02808127e49720f3e77d6a9f3b3ef5a915
image: us-docker.pkg.dev/protobuf-build/containers/test/linux/gcc:12.2-5.4.0-307caa02808127e49720f3e77d6a9f3b3ef5a915
credentials: ${{ secrets.GAR_SERVICE_ACCOUNT }}
entrypoint: bash
command: >-
Expand Down
12 changes: 12 additions & 0 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,9 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
" ::$proto_ns$::internal::WireFormatLite::$val_wire_type$> "
"SuperType;\n"
" $classname$();\n"
// Templatize constexpr constructor as a workaround for a bug in gcc 12
// (warning in gcc 13).
" template <typename = void>\n"
" explicit PROTOBUF_CONSTEXPR $classname$(\n"
" ::$proto_ns$::internal::ConstantInitialized);\n"
" explicit $classname$(::$proto_ns$::Arena* arena);\n"
Expand Down Expand Up @@ -1248,6 +1251,9 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
format("~$classname$() override;\n");
}
format(
// Templatize constexpr constructor as a workaround for a bug in gcc 12
// (warning in gcc 13).
"template<typename = void>\n"
"explicit PROTOBUF_CONSTEXPR "
"$classname$(::$proto_ns$::internal::ConstantInitialized);\n"
"\n"
Expand Down Expand Up @@ -2449,13 +2455,19 @@ void MessageGenerator::GenerateConstexprConstructor(io::Printer* p) {
Formatter format(p);

if (IsMapEntryMessage(descriptor_) || !HasImplData(descriptor_, options_)) {
// Templatize constexpr constructor as a workaround for a bug in gcc 12
// (warning in gcc 13).
format(
"template <typename>\n"
"PROTOBUF_CONSTEXPR $classname$::$classname$(\n"
" ::_pbi::ConstantInitialized) {}\n");
return;
}

// Templatize constexpr constructor as a workaround for a bug in gcc 12
// (warning in gcc 13).
format(
"template <typename>\n"
"PROTOBUF_CONSTEXPR $classname$::$classname$(\n"
" ::_pbi::ConstantInitialized)");

Expand Down
4 changes: 4 additions & 0 deletions src/google/protobuf/compiler/plugin.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/google/protobuf/compiler/plugin.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/google/protobuf/descriptor.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

1 comment on commit 67ecdde

@MichaelBiegluk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer proto

Please sign in to comment.