Don't generate imports for the WKTs unless generating the WKTs.

Since the generated header import GPBProtocolBuffers.h, there is no need
to generate imports for the WKTs as they will have already been imported.
This commit is contained in:
Thomas Van Lenten 2018-03-30 18:09:58 -04:00
parent e998b8ff66
commit bd941d5d69
4 changed files with 21 additions and 8 deletions

View File

@ -188,6 +188,7 @@ bool IsDirectDependency(const FileDescriptor* dep, const FileDescriptor* file) {
FileGenerator::FileGenerator(const FileDescriptor *file, const Options& options)
: file_(file),
root_class_name_(FileClassName(file)),
is_bundled_proto_(IsProtobufLibraryBundledProtoFile(file)),
options_(options) {
for (int i = 0; i < file_->enum_type_count(); i++) {
EnumGenerator *generator = new EnumGenerator(file_->enum_type(i));
@ -217,7 +218,7 @@ void FileGenerator::GenerateHeader(io::Printer *printer) {
std::set<string> headers;
// Generated files bundled with the library get minimal imports, everything
// else gets the wrapper so everything is usable.
if (IsProtobufLibraryBundledProtoFile(file_)) {
if (is_bundled_proto_) {
headers.insert("GPBRootObject.h");
headers.insert("GPBMessage.h");
headers.insert("GPBDescriptor.h");
@ -246,7 +247,8 @@ void FileGenerator::GenerateHeader(io::Printer *printer) {
{
ImportWriter import_writer(
options_.generate_for_named_framework,
options_.named_framework_to_proto_path_mappings_path);
options_.named_framework_to_proto_path_mappings_path,
is_bundled_proto_);
const string header_extension(kHeaderExtension);
for (int i = 0; i < file_->public_dependency_count(); i++) {
import_writer.AddFile(file_->public_dependency(i), header_extension);
@ -364,7 +366,8 @@ void FileGenerator::GenerateSource(io::Printer *printer) {
{
ImportWriter import_writer(
options_.generate_for_named_framework,
options_.named_framework_to_proto_path_mappings_path);
options_.named_framework_to_proto_path_mappings_path,
is_bundled_proto_);
const string header_extension(kHeaderExtension);
// #import the header for this proto file.

View File

@ -66,6 +66,7 @@ class FileGenerator {
private:
const FileDescriptor* file_;
string root_class_name_;
bool is_bundled_proto_;
std::vector<EnumGenerator*> enum_generators_;
std::vector<MessageGenerator*> message_generators_;

View File

@ -1504,10 +1504,12 @@ bool ParseSimpleFile(
ImportWriter::ImportWriter(
const string& generate_for_named_framework,
const string& named_framework_to_proto_path_mappings_path)
const string& named_framework_to_proto_path_mappings_path,
bool include_wkt_imports)
: generate_for_named_framework_(generate_for_named_framework),
named_framework_to_proto_path_mappings_path_(
named_framework_to_proto_path_mappings_path),
include_wkt_imports_(include_wkt_imports),
need_to_parse_mapping_file_(true) {
}
@ -1518,9 +1520,14 @@ void ImportWriter::AddFile(const FileDescriptor* file,
const string file_path(FilePath(file));
if (IsProtobufLibraryBundledProtoFile(file)) {
protobuf_framework_imports_.push_back(
FilePathBasename(file) + header_extension);
protobuf_non_framework_imports_.push_back(file_path + header_extension);
// The imports of the WKTs are only needed within the library itself,
// in other cases, they get skipped because the generated code already
// import GPBProtocolBuffers.h and hence proves them.
if (include_wkt_imports_) {
protobuf_framework_imports_.push_back(
FilePathBasename(file) + header_extension);
protobuf_non_framework_imports_.push_back(file_path + header_extension);
}
return;
}

View File

@ -253,7 +253,8 @@ bool LIBPROTOC_EXPORT ParseSimpleFile(
class LIBPROTOC_EXPORT ImportWriter {
public:
ImportWriter(const string& generate_for_named_framework,
const string& named_framework_to_proto_path_mappings_path);
const string& named_framework_to_proto_path_mappings_path,
bool include_wkt_imports);
~ImportWriter();
void AddFile(const FileDescriptor* file, const string& header_extension);
@ -275,6 +276,7 @@ class LIBPROTOC_EXPORT ImportWriter {
const string generate_for_named_framework_;
const string named_framework_to_proto_path_mappings_path_;
const bool include_wkt_imports_;
std::map<string, string> proto_file_to_framework_name_;
bool need_to_parse_mapping_file_;