aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/compiler/cpp/cpp_file.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/compiler/cpp/cpp_file.cc')
-rw-r--r--src/google/protobuf/compiler/cpp/cpp_file.cc168
1 files changed, 91 insertions, 77 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_file.cc b/src/google/protobuf/compiler/cpp/cpp_file.cc
index e0542ae8..c56e26f7 100644
--- a/src/google/protobuf/compiler/cpp/cpp_file.cc
+++ b/src/google/protobuf/compiler/cpp/cpp_file.cc
@@ -126,12 +126,20 @@ FileGenerator::FileGenerator(const FileDescriptor* file, const Options& options)
new EnumGenerator(file->enum_type(i), options));
enum_generators_.push_back(enum_generators_owner_[i].get());
}
+ for (int i = 0; i < enum_generators_.size(); i++) {
+ enum_generators_[i]->index_in_metadata_ = i;
+ }
for (int i = 0; i < file->service_count(); i++) {
service_generators_owner_[i].reset(
new ServiceGenerator(file->service(i), options));
service_generators_.push_back(service_generators_owner_[i].get());
}
+ if (HasGenericServices(file_, options_)) {
+ for (int i = 0; i < service_generators_.size(); i++) {
+ service_generators_[i]->index_in_metadata_ = i;
+ }
+ }
for (int i = 0; i < file->extension_count(); i++) {
extension_generators_owner_[i].reset(
@@ -526,11 +534,10 @@ class FileGenerator::ForwardDeclarations {
void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
// AddDescriptors() is a file-level procedure which adds the encoded
// FileDescriptorProto for this .proto file to the global DescriptorPool for
- // generated files (DescriptorPool::generated_pool()). It either runs at
- // static initialization time (by default) or when default_instance() is
- // called for the first time (in LITE_RUNTIME mode with
- // GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER flag enabled). This procedure also
- // constructs default instances and registers extensions.
+ // generated files (DescriptorPool::generated_pool()). It ordinarily runs at
+ // static initialization time, but is not used at all in LITE_RUNTIME mode
+ // except when extensions are used. This procedure also constructs default
+ // instances and registers extensions.
//
// Its sibling, AssignDescriptors(), actually pulls the compiled
// FileDescriptor from the DescriptorPool and uses it to populate all of
@@ -546,7 +553,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
// table-driven parsing.
printer->Print("PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTableField\n"
- " const TableStruct::entries[] = {\n");
+ " const TableStruct::entries[] "
+ "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
printer->Indent();
std::vector<size_t> entries;
@@ -567,7 +575,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
"};\n"
"\n"
"PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::AuxillaryParseTableField\n"
- " const TableStruct::aux[] = {\n");
+ " const TableStruct::aux[] "
+ "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
printer->Indent();
std::vector<size_t> aux_entries;
@@ -586,7 +595,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
printer->Print(
"};\n"
"PROTOBUF_CONSTEXPR_VAR ::google::protobuf::internal::ParseTable const\n"
- " TableStruct::schema[] = {\n");
+ " TableStruct::schema[] "
+ "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
printer->Indent();
size_t offset = 0;
@@ -606,9 +616,47 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
"};\n"
"\n");
+ if (!message_generators_.empty() && options_.table_driven_serialization) {
+ printer->Print(
+ "const ::google::protobuf::internal::FieldMetadata TableStruct::field_metadata[] "
+ "= {\n");
+ printer->Indent();
+ std::vector<int> field_metadata_offsets;
+ int idx = 0;
+ for (int i = 0; i < message_generators_.size(); i++) {
+ field_metadata_offsets.push_back(idx);
+ idx += message_generators_[i]->GenerateFieldMetadata(printer);
+ }
+ field_metadata_offsets.push_back(idx);
+ printer->Outdent();
+ printer->Print(
+ "};\n"
+ "const ::google::protobuf::internal::SerializationTable "
+ "TableStruct::serialization_table[] = {\n");
+ printer->Indent();
+ // We rely on the order we layout the tables to match the order we
+ // calculate them with FlattenMessagesInFile, so we check here that
+ // these match exactly.
+ std::vector<const Descriptor*> calculated_order =
+ FlattenMessagesInFile(file_);
+ GOOGLE_CHECK_EQ(calculated_order.size(), message_generators_.size());
+ for (int i = 0; i < message_generators_.size(); i++) {
+ GOOGLE_CHECK_EQ(calculated_order[i], message_generators_[i]->descriptor_);
+ printer->Print(
+ "{$num_fields$, TableStruct::field_metadata + $index$},\n",
+ "classname", message_generators_[i]->classname_, "num_fields",
+ SimpleItoa(field_metadata_offsets[i + 1] - field_metadata_offsets[i]),
+ "index", SimpleItoa(field_metadata_offsets[i]));
+ }
+ printer->Outdent();
+ printer->Print(
+ "};\n"
+ "\n");
+ }
if (HasDescriptorMethods(file_, options_)) {
if (!message_generators_.empty()) {
- printer->Print("const ::google::protobuf::uint32 TableStruct::offsets[] = {\n");
+ printer->Print("const ::google::protobuf::uint32 TableStruct::offsets[] "
+ "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
printer->Indent();
std::vector<std::pair<size_t, size_t> > pairs;
for (int i = 0; i < message_generators_.size(); i++) {
@@ -617,8 +665,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
printer->Outdent();
printer->Print(
"};\n"
- "\n"
- "static const ::google::protobuf::internal::MigrationSchema schemas[] = {\n");
+ "static const ::google::protobuf::internal::MigrationSchema schemas[] "
+ "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
printer->Indent();
{
int offset = 0;
@@ -649,7 +697,7 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
// we still need these symbols to exist
printer->Print(
// MSVC doesn't like empty arrays, so we add a dummy.
- "const ::google::protobuf::uint32 TableStruct::offsets[] = { ~0u };\n"
+ "const ::google::protobuf::uint32 TableStruct::offsets[1] = {};\n"
"static const ::google::protobuf::internal::MigrationSchema* schemas = NULL;\n"
"static const ::google::protobuf::Message* const* "
"file_default_instances = NULL;\n");
@@ -739,33 +787,6 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
"} // namespace\n");
}
- // -----------------------------------------------------------------
-
- // ShutdownFile(): Deletes descriptors, default instances, etc. on shutdown.
- printer->Print(
- "\n"
- "void TableStruct::Shutdown() {\n");
- printer->Indent();
-
- for (int i = 0; i < message_generators_.size(); i++) {
- message_generators_[i]->GenerateShutdownCode(printer);
- }
-
- if (HasDescriptorMethods(file_, options_)) {
- for (int i = 0; i < message_generators_.size(); i++) {
- if (!IsMapEntryMessage(message_generators_[i]->descriptor_)) continue;
- printer->Print(
- "delete file_level_metadata[$index$].reflection;\n",
- "index", SimpleItoa(i));
- }
- }
-
- printer->Outdent();
- printer->Print(
- "}\n\n");
-
- // -----------------------------------------------------------------
-
// Now generate the InitDefaultsImpl() function.
printer->Print(
"void TableStruct::InitDefaultsImpl() {\n"
@@ -826,7 +847,8 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
string file_data;
file_proto.SerializeToString(&file_data);
- printer->Print("static const char descriptor[] = {\n");
+ printer->Print("static const char descriptor[] "
+ "GOOGLE_ATTRIBUTE_SECTION_VARIABLE(protodesc_cold) = {\n");
printer->Indent();
if (file_data.size() > 66535) {
@@ -877,9 +899,6 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
file_namespace);
}
- printer->Print(
- "::google::protobuf::internal::OnShutdown(&TableStruct::Shutdown);\n");
-
printer->Outdent();
printer->Print(
"}\n"
@@ -889,19 +908,15 @@ void FileGenerator::GenerateBuildDescriptors(io::Printer* printer) {
" ::google::protobuf::GoogleOnceInit(&once, &AddDescriptorsImpl);\n"
"}\n");
- if (!StaticInitializersForced(file_, options_)) {
- printer->Print("#ifdef GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n");
- }
- printer->Print(
- // With static initializers.
- "// Force AddDescriptors() to be called at static initialization time.\n"
- "struct StaticDescriptorInitializer {\n"
- " StaticDescriptorInitializer() {\n"
- " AddDescriptors();\n"
- " }\n"
- "} static_descriptor_initializer;\n");
- if (!StaticInitializersForced(file_, options_)) {
- printer->Print("#endif // GOOGLE_PROTOBUF_NO_STATIC_INITIALIZER\n");
+ if (StaticInitializersForced(file_, options_)) {
+ printer->Print(
+ "// Force AddDescriptors() to be called at dynamic initialization "
+ "time.\n"
+ "struct StaticDescriptorInitializer {\n"
+ " StaticDescriptorInitializer() {\n"
+ " AddDescriptors();\n"
+ " }\n"
+ "} static_descriptor_initializer;\n");
}
}
@@ -925,19 +940,11 @@ void FileGenerator::GenerateNamespaceClosers(io::Printer* printer) {
void FileGenerator::GenerateForwardDeclarations(io::Printer* printer) {
ForwardDeclarations decls;
- for (int i = 0; i < file_->dependency_count(); i++) {
- FileGenerator dependency(file_->dependency(i), options_);
- dependency.FillForwardDeclarations(&decls);
- }
FillForwardDeclarations(&decls);
decls.Print(printer, options_);
}
void FileGenerator::FillForwardDeclarations(ForwardDeclarations* decls) {
- for (int i = 0; i < file_->public_dependency_count(); i++) {
- FileGenerator dependency(file_->public_dependency(i), options_);
- dependency.FillForwardDeclarations(decls);
- }
for (int i = 0; i < package_parts_.size(); i++) {
decls = decls->AddOrGetNamespace(package_parts_[i]);
}
@@ -1033,11 +1040,11 @@ void FileGenerator::GenerateLibraryIncludes(io::Printer* printer) {
"#include <google/protobuf/map.h>"
" // IWYU pragma: export\n");
if (HasDescriptorMethods(file_, options_)) {
- printer->Print(
- "#include <google/protobuf/map_field_inl.h>\n");
+ printer->Print("#include <google/protobuf/map_entry.h>\n");
+ printer->Print("#include <google/protobuf/map_field_inl.h>\n");
} else {
- printer->Print(
- "#include <google/protobuf/map_field_lite.h>\n");
+ printer->Print("#include <google/protobuf/map_entry_lite.h>\n");
+ printer->Print("#include <google/protobuf/map_field_lite.h>\n");
}
}
@@ -1104,7 +1111,7 @@ void FileGenerator::GenerateDependencyIncludes(io::Printer* printer) {
void FileGenerator::GenerateGlobalStateFunctionDeclarations(
io::Printer* printer) {
- // Forward-declare the AddDescriptors, AssignDescriptors, and ShutdownFile
+ // Forward-declare the AddDescriptors, AssignDescriptors
// functions, so that we can declare them to be friends of each class.
printer->Print(
"\n"
@@ -1115,12 +1122,14 @@ void FileGenerator::GenerateGlobalStateFunctionDeclarations(
" static const ::google::protobuf::internal::AuxillaryParseTableField aux[];\n"
" static const ::google::protobuf::internal::ParseTable schema[];\n"
" static const ::google::protobuf::uint32 offsets[];\n"
+ " static const ::google::protobuf::internal::FieldMetadata field_metadata[];\n"
+ " static const ::google::protobuf::internal::SerializationTable "
+ "serialization_table[];\n"
// The following function(s) need to be able to access private members of
// the messages defined in the file. So we make them static members.
// This is the internal implementation of InitDefaults. It should only
// be called by InitDefaults which makes sure it will be called only once.
" static void InitDefaultsImpl();\n"
- " static void Shutdown();\n"
"};\n"
"void $dllexport_decl$AddDescriptors();\n"
"void $dllexport_decl$InitDefaults();\n"
@@ -1210,6 +1219,13 @@ void FileGenerator::GenerateInlineFunctionDefinitions(io::Printer* printer) {
// dependent.
printer->Print("#if !PROTOBUF_INLINE_NOT_IN_HEADERS\n");
+ // TODO(gerbens) remove pragmas when gcc is no longer used. Current version
+ // of gcc fires a bogus error when compiled with strict-aliasing.
+ printer->Print(
+ "#ifdef __GNUC__\n"
+ " #pragma GCC diagnostic push\n"
+ " #pragma GCC diagnostic ignored \"-Wstrict-aliasing\"\n"
+ "#endif // __GNUC__\n");
// Generate class inline methods.
for (int i = 0; i < message_generators_.size(); i++) {
if (i > 0) {
@@ -1219,6 +1235,10 @@ void FileGenerator::GenerateInlineFunctionDefinitions(io::Printer* printer) {
message_generators_[i]->GenerateInlineMethods(printer,
/* is_inline = */ true);
}
+ printer->Print(
+ "#ifdef __GNUC__\n"
+ " #pragma GCC diagnostic pop\n"
+ "#endif // __GNUC__\n");
printer->Print("#endif // !PROTOBUF_INLINE_NOT_IN_HEADERS\n");
for (int i = 0; i < message_generators_.size(); i++) {
@@ -1235,13 +1255,8 @@ void FileGenerator::GenerateProto2NamespaceEnumSpecializations(
io::Printer* printer) {
// Emit GetEnumDescriptor specializations into google::protobuf namespace:
if (HasEnumDefinitions(file_)) {
- // The SWIG conditional is to avoid a null-pointer dereference
- // (bug 1984964) in swig-1.3.21 resulting from the following syntax:
- // namespace X { void Y<Z::W>(); }
- // which appears in GetEnumDescriptor() specializations.
printer->Print(
"\n"
- "#ifndef SWIG\n"
"namespace google {\nnamespace protobuf {\n"
"\n");
for (int i = 0; i < enum_generators_.size(); i++) {
@@ -1249,8 +1264,7 @@ void FileGenerator::GenerateProto2NamespaceEnumSpecializations(
}
printer->Print(
"\n"
- "} // namespace protobuf\n} // namespace google\n"
- "#endif // SWIG\n");
+ "} // namespace protobuf\n} // namespace google\n");
}
}