|
|
|
@ -95,10 +95,29 @@ class SimpleLineCollector : public LineConsumer { |
|
|
|
|
std::unordered_set<std::string>* set_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class ExpectedPrefixesCollector : public LineConsumer { |
|
|
|
|
public: |
|
|
|
|
ExpectedPrefixesCollector(std::map<std::string, std::string>* inout_package_to_prefix_map) |
|
|
|
|
: prefix_map_(inout_package_to_prefix_map) {} |
|
|
|
|
|
|
|
|
|
virtual bool ConsumeLine(const StringPiece& line, std::string* out_error) override; |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
std::map<std::string, std::string>* prefix_map_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
class PrefixModeStorage { |
|
|
|
|
public: |
|
|
|
|
PrefixModeStorage(); |
|
|
|
|
|
|
|
|
|
const std::string prefix_to_proto_package_mappings_path() const { return prefix_to_proto_package_mappings_path_; } |
|
|
|
|
void set_prefix_to_proto_package_mappings_path(const std::string& path) { |
|
|
|
|
prefix_to_proto_package_mappings_path_ = path; |
|
|
|
|
prefix_to_proto_package_map_.clear(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string prefix_from_proto_package_mappings(const FileDescriptor* file); |
|
|
|
|
|
|
|
|
|
bool use_package_name() const { return use_package_name_; } |
|
|
|
|
void set_use_package_name(bool on_or_off) { use_package_name_ = on_or_off; } |
|
|
|
|
|
|
|
|
@ -116,6 +135,8 @@ class PrefixModeStorage { |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
bool use_package_name_; |
|
|
|
|
std::map<std::string, std::string> prefix_to_proto_package_map_; |
|
|
|
|
std::string prefix_to_proto_package_mappings_path_; |
|
|
|
|
std::string exception_path_; |
|
|
|
|
std::string forced_prefix_; |
|
|
|
|
std::unordered_set<std::string> exceptions_; |
|
|
|
@ -140,6 +161,44 @@ PrefixModeStorage::PrefixModeStorage() { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::string PrefixModeStorage::prefix_from_proto_package_mappings(const FileDescriptor* file) { |
|
|
|
|
if (!file) { |
|
|
|
|
return ""; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (prefix_to_proto_package_map_.empty() && !prefix_to_proto_package_mappings_path_.empty()) { |
|
|
|
|
std::string error_str; |
|
|
|
|
// Re use the same collector as we use for expected_prefixes_path since the file
|
|
|
|
|
// format is the same.
|
|
|
|
|
ExpectedPrefixesCollector collector(&prefix_to_proto_package_map_); |
|
|
|
|
if (!ParseSimpleFile(prefix_to_proto_package_mappings_path_, &collector, &error_str)) { |
|
|
|
|
if (error_str.empty()) { |
|
|
|
|
error_str = std::string("protoc:0: warning: Failed to parse") |
|
|
|
|
+ std::string(" prefix to proto package mappings file: ") |
|
|
|
|
+ prefix_to_proto_package_mappings_path_; |
|
|
|
|
} |
|
|
|
|
std::cerr << error_str << std::endl; |
|
|
|
|
std::cerr.flush(); |
|
|
|
|
prefix_to_proto_package_map_.clear();
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const std::string package = file->package(); |
|
|
|
|
// For files without packages, the can be registered as "no_package:PATH",
|
|
|
|
|
// allowing the expected prefixes file.
|
|
|
|
|
static const std::string no_package_prefix("no_package:"); |
|
|
|
|
const std::string lookup_key = package.empty() ? no_package_prefix + file->name() : package; |
|
|
|
|
|
|
|
|
|
std::map<std::string, std::string>::const_iterator prefix_lookup = |
|
|
|
|
prefix_to_proto_package_map_.find(lookup_key); |
|
|
|
|
|
|
|
|
|
if (prefix_lookup != prefix_to_proto_package_map_.end()) { |
|
|
|
|
return prefix_lookup->second; |
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ""; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool PrefixModeStorage::is_package_exempted(const std::string& package) { |
|
|
|
|
if (exceptions_.empty() && !exception_path_.empty()) { |
|
|
|
|
std::string error_str; |
|
|
|
@ -169,6 +228,14 @@ PrefixModeStorage g_prefix_mode; |
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
std::string GetPrefixToProtoPackageMappingsPath() { |
|
|
|
|
return g_prefix_mode.prefix_to_proto_package_mappings_path(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void SetPrefixToProtoPackageMappingsPath(const std::string& file_path) { |
|
|
|
|
g_prefix_mode.set_prefix_to_proto_package_mappings_path(file_path); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool UseProtoPackageAsDefaultPrefix() { |
|
|
|
|
return g_prefix_mode.use_package_name(); |
|
|
|
|
} |
|
|
|
@ -531,6 +598,12 @@ std::string FileClassPrefix(const FileDescriptor* file) { |
|
|
|
|
return file->options().objc_class_prefix(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// If package prefix is specified in an prefix to proto mappings file then use that.
|
|
|
|
|
std::string objc_class_prefix = g_prefix_mode.prefix_from_proto_package_mappings(file); |
|
|
|
|
if (!objc_class_prefix.empty()) { |
|
|
|
|
return objc_class_prefix; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// If package prefix isn't enabled, done.
|
|
|
|
|
if (!g_prefix_mode.use_package_name()) { |
|
|
|
|
return ""; |
|
|
|
@ -1205,17 +1278,6 @@ void RemoveComment(StringPiece* input) { |
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
class ExpectedPrefixesCollector : public LineConsumer { |
|
|
|
|
public: |
|
|
|
|
ExpectedPrefixesCollector(std::map<std::string, std::string>* inout_package_to_prefix_map) |
|
|
|
|
: prefix_map_(inout_package_to_prefix_map) {} |
|
|
|
|
|
|
|
|
|
virtual bool ConsumeLine(const StringPiece& line, std::string* out_error) override; |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
std::map<std::string, std::string>* prefix_map_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
bool ExpectedPrefixesCollector::ConsumeLine( |
|
|
|
|
const StringPiece& line, std::string* out_error) { |
|
|
|
|
int offset = line.find('='); |
|
|
|
|