Details
-
Bug
-
Resolution: Fixed
-
P1: Critical
-
6.6.1
-
None
-
344f7fe15 (6.7.0), 13dc9800b (dev), 4de699f4a (6.7)
Description
When I try to compile the following .proto file with QtProtobuf (qtprotobufgen), I cannot build the project.
// bug.proto
message MyMessage {
required string name = 1;
required string template = 2;
}
The build fails with the following errors:
C:\PathToProject\CmakeBuildDir\bug.qpb.h(59,13): error C2059: syntax error: 'template' [C:\PathToProject\CmakeBuildDir\ProtobufsQt_protobuf_registration.vcxproj] (compiling source file 'bug_protobuftyperegistrations.cpp')
That is, because QtProtobuf (qtprotobufgen) generated the following C++ code:
class QPB_PROTOBUFSQT_EXPORT MyMessage : public QProtobufMessage { // ... QString name() const; QString template() const; void setName(const QString &name); void setTemplate(const QString &template); static void registerTypes(); // ... };
As you can see, the field's name "template" is used as a class member function name and parameter name. However, the keyword "template" is reserved by C++.
The original protobuf compiler (protoc) circumvents this issue by suffixing the function name with an underscore (e.g. `template_()`). See https://github.com/protocolbuffers/protobuf/blob/7ef5207d8145183f49b0e80c5b2f612a8f5065cb/upb_generator/keywords.cc#L125
Some Qt keywords cannot be used neither (e.g. slots).
I've also prepared a repository for reproduction: https://github.com/jdoubleu/QtProtobuf-Bug-123400