|
@@ -23,42 +23,25 @@ namespace compiler {
|
|
namespace {
|
|
namespace {
|
|
|
|
|
|
string StripProto(string filename) {
|
|
string StripProto(string filename) {
|
|
- if (filename.substr(filename.size() - 11) == ".protodevel") {
|
|
|
|
- // .protodevel
|
|
|
|
- return filename.substr(0, filename.size() - 11);
|
|
|
|
- } else {
|
|
|
|
- // .proto
|
|
|
|
- return filename.substr(0, filename.size() - 6);
|
|
|
|
- }
|
|
|
|
|
|
+ return filename.substr(0, filename.rfind(".proto"));
|
|
}
|
|
}
|
|
|
|
|
|
-DescriptorPool new_pool_;
|
|
|
|
|
|
+DescriptorPool* GetPool() {
|
|
|
|
+ static DescriptorPool *pool = new DescriptorPool();
|
|
|
|
+ return pool;
|
|
|
|
+}
|
|
|
|
|
|
} // namespace
|
|
} // namespace
|
|
|
|
|
|
-class Proto2ToProto3Generator : public CodeGenerator {
|
|
|
|
|
|
+class Proto2ToProto3Generator final : public CodeGenerator {
|
|
public:
|
|
public:
|
|
- virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files,
|
|
|
|
|
|
+ bool GenerateAll(const std::vector<const FileDescriptor*>& files,
|
|
const string& parameter,
|
|
const string& parameter,
|
|
GeneratorContext* context,
|
|
GeneratorContext* context,
|
|
string* error) const {
|
|
string* error) const {
|
|
for (int i = 0; i < files.size(); i++) {
|
|
for (int i = 0; i < files.size(); i++) {
|
|
for (auto file : files) {
|
|
for (auto file : files) {
|
|
- bool can_generate =
|
|
|
|
- (new_pool_.FindFileByName(file->name()) == nullptr);
|
|
|
|
- for (int j = 0; j < file->dependency_count(); j++) {
|
|
|
|
- can_generate &= (new_pool_.FindFileByName(
|
|
|
|
- file->dependency(j)->name()) != nullptr);
|
|
|
|
- }
|
|
|
|
- for (int j = 0; j < file->public_dependency_count(); j++) {
|
|
|
|
- can_generate &= (new_pool_.FindFileByName(
|
|
|
|
- file->public_dependency(j)->name()) != nullptr);
|
|
|
|
- }
|
|
|
|
- for (int j = 0; j < file->weak_dependency_count(); j++) {
|
|
|
|
- can_generate &= (new_pool_.FindFileByName(
|
|
|
|
- file->weak_dependency(j)->name()) != nullptr);
|
|
|
|
- }
|
|
|
|
- if (can_generate) {
|
|
|
|
|
|
+ if (CanGenerate(file)) {
|
|
Generate(file, parameter, context, error);
|
|
Generate(file, parameter, context, error);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
@@ -68,7 +51,7 @@ class Proto2ToProto3Generator : public CodeGenerator {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- virtual bool Generate(const FileDescriptor* file,
|
|
|
|
|
|
+ bool Generate(const FileDescriptor* file,
|
|
const string& parameter,
|
|
const string& parameter,
|
|
GeneratorContext* context,
|
|
GeneratorContext* context,
|
|
string* error) const {
|
|
string* error) const {
|
|
@@ -90,10 +73,34 @@ class Proto2ToProto3Generator : public CodeGenerator {
|
|
|
|
|
|
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
|
|
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
|
|
context->Open(basename + ".proto"));
|
|
context->Open(basename + ".proto"));
|
|
- string content = new_pool_.BuildFile(new_file)->DebugString();
|
|
|
|
|
|
+ string content = GetPool()->BuildFile(new_file)->DebugString();
|
|
Printer printer(output.get(), '$');
|
|
Printer printer(output.get(), '$');
|
|
printer.WriteRaw(content.c_str(), content.size());
|
|
printer.WriteRaw(content.c_str(), content.size());
|
|
|
|
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ private:
|
|
|
|
+ bool CanGenerate(const FileDescriptor* file) const {
|
|
|
|
+ if (GetPool()->FindFileByName(file->name()) != nullptr) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ for (int j = 0; j < file->dependency_count(); j++) {
|
|
|
|
+ if (GetPool()->FindFileByName(file->dependency(j)->name()) == nullptr) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for (int j = 0; j < file->public_dependency_count(); j++) {
|
|
|
|
+ if (GetPool()->FindFileByName(
|
|
|
|
+ file->public_dependency(j)->name()) == nullptr) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ for (int j = 0; j < file->weak_dependency_count(); j++) {
|
|
|
|
+ if (GetPool()->FindFileByName(
|
|
|
|
+ file->weak_dependency(j)->name()) == nullptr) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
};
|
|
};
|