Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for MSVC's string literal compiler limit. #472

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions src/google/protobuf/compiler/cpp/cpp_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -434,20 +434,53 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
string file_data;
file_proto.SerializeToString(&file_data);

printer->Print(
"::google::protobuf::DescriptorPool::InternalAddGeneratedFile(");

// Only write 40 bytes per line.
static const int kBytesPerLine = 40;
for (int i = 0; i < file_data.size(); i += kBytesPerLine) {
printer->Print("\n \"$data$\"",
"data",
EscapeTrigraphs(
CEscape(file_data.substr(i, kBytesPerLine))));
// Workaround for MSVC: "Error C1091: compiler limit: string exceeds 65535
// bytes in length". Declare a static array of characters rather than use a
// string literal.
size_t literal_size = EscapeTrigraphs(CEscape(file_data)).size();
if (literal_size > 65535) {
printer->Print(
"static const char descriptor[] = {\n");
printer->Indent();

// Only write 25 bytes per line.
static const int kBytesPerLine = 25;
for (int i = 0; i < file_data.size();) {
for (int j = 0; j < kBytesPerLine && i < file_data.size(); ++i, ++j) {
printer->Print(
"$char$, ",
"char", SimpleItoa(file_data[i]));
}
printer->Print(
"\n");
}

printer->Outdent();
printer->Print(
"};\n");

printer->Print(
"::google::protobuf::DescriptorPool::InternalAddGeneratedFile(descriptor, $size$);\n",
"size", SimpleItoa(file_data.size()));

} else {

printer->Print(
"::google::protobuf::DescriptorPool::InternalAddGeneratedFile(");

// Only write 40 bytes per line.
static const int kBytesPerLine = 40;
for (int i = 0; i < file_data.size(); i += kBytesPerLine) {
printer->Print("\n \"$data$\"",
"data",
EscapeTrigraphs(
CEscape(file_data.substr(i, kBytesPerLine))));
}
printer->Print(
", $size$);\n",
"size", SimpleItoa(file_data.size()));

}
printer->Print(
", $size$);\n",
"size", SimpleItoa(file_data.size()));

// Call MessageFactory::InternalRegisterGeneratedFile().
printer->Print(
Expand Down