diff --git a/src/compiler/objective_c_generator_helpers.h b/src/compiler/objective_c_generator_helpers.h index 4004e6aef8f..3dffe6bd53e 100644 --- a/src/compiler/objective_c_generator_helpers.h +++ b/src/compiler/objective_c_generator_helpers.h @@ -40,5 +40,46 @@ inline string ServiceClassName(const ServiceDescriptor* service) { string prefix = file->options().objc_class_prefix(); return prefix + service->name(); } + +inline ::grpc::string LocalImport(const ::grpc::string &import) { + return ::grpc::string("#import \"" + import + "\"\n"); +} + +inline ::grpc::string SystemImport(const ::grpc::string &import) { + return ::grpc::string("#import <" + import + ">\n"); +} + +inline ::grpc::string PreprocConditional(::grpc::string symbol, bool invert) { + return invert + ? "!defined(" + symbol + ") || !" + symbol + : "defined(" + symbol + ") && " + symbol; +} + +inline ::grpc::string PreprocIf(const ::grpc::string& symbol, + const ::grpc::string& if_true) { + return ::grpc::string("#if " + PreprocConditional(symbol, false) + "\n" + + if_true + "#endif\n"); +} + +inline ::grpc::string PreprocIfNot(const ::grpc::string& symbol, + const ::grpc::string& if_true) { + return ::grpc::string("#if " + PreprocConditional(symbol, true) + "\n" + + if_true + "#endif\n"); +} + +inline ::grpc::string PreprocIfElse(const ::grpc::string& symbol, + const ::grpc::string& if_true, + const ::grpc::string& if_false) { + return ::grpc::string("#if " + PreprocConditional(symbol, false) + "\n" + + if_true + "#else\n" + if_false + "#endif\n"); +} + +inline ::grpc::string PreprocIfNotElse(const ::grpc::string& symbol, + const ::grpc::string& if_true, + const ::grpc::string& if_false) { + return ::grpc::string("#if " + PreprocConditional(symbol, false) + "\n" + + if_true + "#else\n" + if_false + "#endif\n"); +} + } // namespace grpc_objective_c_generator #endif // GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H diff --git a/src/compiler/objective_c_plugin.cc b/src/compiler/objective_c_plugin.cc index 57aad8b3919..46191229502 100644 --- a/src/compiler/objective_c_plugin.cc +++ b/src/compiler/objective_c_plugin.cc @@ -29,55 +29,13 @@ using ::google::protobuf::compiler::objectivec:: IsProtobufLibraryBundledProtoFile; using ::google::protobuf::compiler::objectivec::ProtobufLibraryFrameworkName; +using ::grpc_objective_c_generator::LocalImport; +using ::grpc_objective_c_generator::PreprocIfElse; +using ::grpc_objective_c_generator::PreprocIfNot; +using ::grpc_objective_c_generator::SystemImport; namespace { -inline ::grpc::string LocalImport(const ::grpc::string &import) { - return ::grpc::string("#import \"" + import + "\"\n"); -} - -inline ::grpc::string SystemImport(const ::grpc::string &import) { - return ::grpc::string("#import <" + import + ">\n"); -} - -// Preprocessor condition flags. -using PreprocConditionFlag = uint32_t; -constexpr PreprocConditionFlag kInvertCondition = 0b0001; -constexpr PreprocConditionFlag kCheckIfDefined = 0b0010; - -// Convenience flag set. -constexpr PreprocConditionFlag kIfNotOrNotDefined = - kInvertCondition | kCheckIfDefined; - -inline ::grpc::string PreprocConditional(::grpc::string symbol, - PreprocConditionFlag flags) { - if (flags & kCheckIfDefined) { - return (flags & kInvertCondition) - ? "!defined(" + symbol + ") || !" + symbol - : "defined(" + symbol + ") && " + symbol; - } else { - return (flags & kInvertCondition) - ? "!" + symbol - : symbol; - } -} - -inline ::grpc::string PreprocIf(const ::grpc::string& symbol, - const ::grpc::string& if_true, - PreprocConditionFlag flags = 0) { - ::grpc::string condition = PreprocConditional(symbol, flags); - return ::grpc::string("#if " + condition + "\n" + if_true + "#endif\n"); -} - -inline ::grpc::string PreprocIfElse(const ::grpc::string& symbol, - const ::grpc::string& if_true, - const ::grpc::string& if_false, - PreprocConditionFlag flags = 0) { - ::grpc::string condition = PreprocConditional(symbol, flags); - return ::grpc::string("#if " + condition + "\n" + - if_true + "#else\n" + if_false + "#endif\n"); -} - inline ::grpc::string ImportProtoHeaders( const grpc::protobuf::FileDescriptor* dep, const char *indent) { @@ -97,10 +55,10 @@ inline ::grpc::string ImportProtoHeaders( "GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS"; return PreprocIfElse(kFrameworkImportsCondition, indent + SystemImport(framework_header), - indent + LocalImport(header), - kCheckIfDefined); + indent + LocalImport(header)); } + } // namespace class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { @@ -123,10 +81,6 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { static const ::grpc::string kForwardDeclare = "GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO"; - auto OmitIf = [](const ::grpc::string& s, const ::grpc::string& v) { - return PreprocIf(s, v, kInvertCondition | kCheckIfDefined); - }; - ::grpc::string file_name = google::protobuf::compiler::objectivec::FilePath(file); @@ -164,14 +118,14 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { } Write(context, file_name + ".pbrpc.h", - OmitIf(kForwardDeclare, imports) + "\n" + - OmitIf(kProtocolOnly, system_imports) + "\n" + - PreprocIfElse(kForwardDeclare, class_declarations, class_imports, - kCheckIfDefined) + "\n" + + PreprocIfNot(kForwardDeclare, imports) + "\n" + + PreprocIfNot(kProtocolOnly, system_imports) + "\n" + + PreprocIfElse(kForwardDeclare, class_declarations, + class_imports) + "\n" + forward_declarations + "\n" + kNonNullBegin + "\n" + protocols + "\n" + - OmitIf(kProtocolOnly, interfaces) + "\n" + + PreprocIfNot(kProtocolOnly, interfaces) + "\n" + kNonNullEnd + "\n"); } @@ -196,7 +150,7 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { } Write(context, file_name + ".pbrpc.m", - OmitIf(kProtocolOnly, + PreprocIfNot(kProtocolOnly, imports + "\n" + class_imports + "\n" + definitions)); }