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 authored and copybara-github committed May 15, 2023
1 parent 59a996b commit e15326b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
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
14 changes: 13 additions & 1 deletion src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ std::vector<const FieldDescriptor*> SortFieldsByNumber(
struct ExtensionRangeSorter {
bool operator()(const Descriptor::ExtensionRange* left,
const Descriptor::ExtensionRange* right) const {
return left->start_number() < right->start_number();
return left->start < right->start;
}
};

Expand Down Expand Up @@ -1176,6 +1176,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 @@ -1269,6 +1272,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 @@ -2526,6 +2532,9 @@ void MessageGenerator::GenerateConstexprConstructor(io::Printer* p) {

if (IsMapEntryMessage(descriptor_) || !HasImplData(descriptor_, options_)) {
p->Emit(R"cc(
//~ Templatize constexpr constructor as a workaround for a bug in gcc 12
//~ (warning in gcc 13).
template <typename>
$constexpr$ $classname$::$classname$(::_pbi::ConstantInitialized) {}
)cc");
return;
Expand Down Expand Up @@ -2604,6 +2613,9 @@ void MessageGenerator::GenerateConstexprConstructor(io::Printer* p) {
}},
},
R"cc(
//~ Templatize constexpr constructor as a workaround for a bug in gcc 12
//~ (warning in gcc 13).
template <typename>
$constexpr$ $classname$::$classname$(::_pbi::ConstantInitialized)
: _impl_{$init_body$} {}
)cc");
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

0 comments on commit e15326b

Please sign in to comment.