pull/21504/head
Richard Belleville 5 years ago
parent 87b50e08b9
commit e9746f03d6
  1. 113
      tools/distrib/python/grpcio_tools/grpc_tools/main.cc
  2. 33
      tools/distrib/python/grpcio_tools/grpc_tools/main.h
  3. 2
      tools/dockerfile/grpc_clang_format/clang_format_all_the_things.sh

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/compiler/command_line_interface.h>
#include <google/protobuf/compiler/python/python_generator.h>
@ -21,17 +20,17 @@
#include "grpc_tools/main.h"
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
// TODO: Clang format.
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
int protoc_main(int argc, char* argv[]) {
google::protobuf::compiler::CommandLineInterface cli;
@ -57,20 +56,25 @@ namespace detail {
// TODO: Consider deduping between this and command_line_interface.cc.
// TODO: Separate declarations and definitions.
class GeneratorContextImpl : public ::google::protobuf::compiler::GeneratorContext {
public:
GeneratorContextImpl(const std::vector<const ::google::protobuf::FileDescriptor*>& parsed_files,
std::vector<std::pair<std::string, std::string>>* files_out) :
files_(files_out),
parsed_files_(parsed_files) {}
::google::protobuf::io::ZeroCopyOutputStream* Open(const std::string& filename) {
class GeneratorContextImpl
: public ::google::protobuf::compiler::GeneratorContext {
public:
GeneratorContextImpl(
const std::vector<const ::google::protobuf::FileDescriptor*>&
parsed_files,
std::vector<std::pair<std::string, std::string>>* files_out)
: files_(files_out), parsed_files_(parsed_files) {}
::google::protobuf::io::ZeroCopyOutputStream* Open(
const std::string& filename) {
files_->emplace_back(filename, "");
return new ::google::protobuf::io::StringOutputStream(&(files_->back().second));
return new ::google::protobuf::io::StringOutputStream(
&(files_->back().second));
}
// NOTE: Equivalent to Open, since all files start out empty.
::google::protobuf::io::ZeroCopyOutputStream* OpenForAppend(const std::string& filename) {
::google::protobuf::io::ZeroCopyOutputStream* OpenForAppend(
const std::string& filename) {
return Open(filename);
}
@ -80,22 +84,23 @@ public:
return Open(filename);
}
void ListParsedFiles(std::vector<const ::google::protobuf::FileDescriptor*>* output) {
void ListParsedFiles(
std::vector<const ::google::protobuf::FileDescriptor*>* output) {
*output = parsed_files_;
}
private:
private:
std::vector<std::pair<std::string, std::string>>* files_;
const std::vector<const ::google::protobuf::FileDescriptor*>& parsed_files_;
};
// TODO: Write a bunch of tests for exception propagation.
class ErrorCollectorImpl : public ::google::protobuf::compiler::MultiFileErrorCollector {
class ErrorCollectorImpl
: public ::google::protobuf::compiler::MultiFileErrorCollector {
public:
ErrorCollectorImpl(std::vector<ProtocError>* errors,
std::vector<ProtocWarning>* warnings) :
errors_(errors),
warnings_(warnings) {}
std::vector<ProtocWarning>* warnings)
: errors_(errors), warnings_(warnings) {}
void AddError(const std::string& filename, int line, int column,
const std::string& message) {
@ -107,17 +112,18 @@ class ErrorCollectorImpl : public ::google::protobuf::compiler::MultiFileErrorCo
warnings_->emplace_back(filename, line, column, message);
}
private:
private:
std::vector<ProtocError>* errors_;
std::vector<ProtocWarning>* warnings_;
};
static void calculate_transitive_closure(const ::google::protobuf::FileDescriptor* descriptor,
std::vector<const ::google::protobuf::FileDescriptor*>* transitive_closure,
std::unordered_set<const ::google::protobuf::FileDescriptor*>* visited)
{
static void calculate_transitive_closure(
const ::google::protobuf::FileDescriptor* descriptor,
std::vector<const ::google::protobuf::FileDescriptor*>* transitive_closure,
std::unordered_set<const ::google::protobuf::FileDescriptor*>* visited) {
for (int i = 0; i < descriptor->dependency_count(); ++i) {
const ::google::protobuf::FileDescriptor* dependency = descriptor->dependency(i);
const ::google::protobuf::FileDescriptor* dependency =
descriptor->dependency(i);
if (visited->find(dependency) == visited->end()) {
calculate_transitive_closure(dependency, transitive_closure, visited);
}
@ -126,28 +132,31 @@ static void calculate_transitive_closure(const ::google::protobuf::FileDescripto
visited->insert(descriptor);
}
} // end namespace detail
static int generate_code(::google::protobuf::compiler::CodeGenerator* code_generator,
char* protobuf_path,
const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors,
std::vector<ProtocWarning>* warnings)
{
std::unique_ptr<detail::ErrorCollectorImpl> error_collector(new detail::ErrorCollectorImpl(errors, warnings));
std::unique_ptr<::google::protobuf::compiler::DiskSourceTree> source_tree(new ::google::protobuf::compiler::DiskSourceTree());
} // end namespace detail
static int generate_code(
::google::protobuf::compiler::CodeGenerator* code_generator,
char* protobuf_path, const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors, std::vector<ProtocWarning>* warnings) {
std::unique_ptr<detail::ErrorCollectorImpl> error_collector(
new detail::ErrorCollectorImpl(errors, warnings));
std::unique_ptr<::google::protobuf::compiler::DiskSourceTree> source_tree(
new ::google::protobuf::compiler::DiskSourceTree());
for (const auto& include_path : *include_paths) {
source_tree->MapPath("", include_path);
}
::google::protobuf::compiler::Importer importer(source_tree.get(), error_collector.get());
const ::google::protobuf::FileDescriptor* parsed_file = importer.Import(protobuf_path);
::google::protobuf::compiler::Importer importer(source_tree.get(),
error_collector.get());
const ::google::protobuf::FileDescriptor* parsed_file =
importer.Import(protobuf_path);
if (parsed_file == nullptr) {
return 1;
}
std::vector<const ::google::protobuf::FileDescriptor*> transitive_closure;
std::unordered_set<const ::google::protobuf::FileDescriptor*> visited;
::detail::calculate_transitive_closure(parsed_file, &transitive_closure, &visited);
::detail::calculate_transitive_closure(parsed_file, &transitive_closure,
&visited);
detail::GeneratorContextImpl generator_context(transitive_closure, files_out);
std::string error;
for (const auto descriptor : transitive_closure) {
@ -156,23 +165,21 @@ static int generate_code(::google::protobuf::compiler::CodeGenerator* code_gener
return 0;
}
int protoc_get_protos(char* protobuf_path,
const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors,
std::vector<ProtocWarning>* warnings)
{
int protoc_get_protos(
char* protobuf_path, const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors, std::vector<ProtocWarning>* warnings) {
::google::protobuf::compiler::python::Generator python_generator;
return generate_code(&python_generator, protobuf_path, include_paths, files_out, errors, warnings);
return generate_code(&python_generator, protobuf_path, include_paths,
files_out, errors, warnings);
}
int protoc_get_services(char* protobuf_path,
const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors,
std::vector<ProtocWarning>* warnings)
{
int protoc_get_services(
char* protobuf_path, const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors, std::vector<ProtocWarning>* warnings) {
grpc_python_generator::GeneratorConfiguration grpc_py_config;
grpc_python_generator::PythonGrpcGenerator grpc_py_generator(grpc_py_config);
return generate_code(&grpc_py_generator, protobuf_path, include_paths, files_out, errors, warnings);
return generate_code(&grpc_py_generator, protobuf_path, include_paths,
files_out, errors, warnings);
}

@ -12,15 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <vector>
#include <string>
#include <utility>
#include <vector>
// We declare `protoc_main` here since we want access to it from Cython as an
// extern but *without* triggering a dllimport declspec when on Windows.
int protoc_main(int argc, char *argv[]);
int protoc_main(int argc, char* argv[]);
struct ProtocError {
std::string filename;
@ -29,24 +27,19 @@ struct ProtocError {
std::string message;
ProtocError() {}
ProtocError(std::string filename, int line, int column, std::string message) :
filename(filename),
line(line),
column(column),
message(message) {}
ProtocError(std::string filename, int line, int column, std::string message)
: filename(filename), line(line), column(column), message(message) {}
};
typedef ProtocError ProtocWarning;
// TODO: Create Alias for files_out type?
int protoc_get_protos(char* protobuf_path,
const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors,
std::vector<ProtocWarning>* warnings);
int protoc_get_services(char* protobuf_path,
const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors,
std::vector<ProtocWarning>* warnings);
int protoc_get_protos(
char* protobuf_path, const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors, std::vector<ProtocWarning>* warnings);
int protoc_get_services(
char* protobuf_path, const std::vector<std::string>* include_paths,
std::vector<std::pair<std::string, std::string>>* files_out,
std::vector<ProtocError>* errors, std::vector<ProtocWarning>* warnings);

@ -16,7 +16,7 @@
set -e
# directories to run against
DIRS="src/core/lib src/core/tsi src/core/ext src/cpp test/core test/cpp include src/compiler src/csharp src/ruby third_party/address_sorting src/objective-c"
DIRS="src/core/lib src/core/tsi src/core/ext src/cpp test/core test/cpp include src/compiler src/csharp src/ruby third_party/address_sorting src/objective-c tools/distrib/python"
# file matching patterns to check
GLOB="*.h *.c *.cc *.m *.mm"

Loading…
Cancel
Save