Updates #if generation to allow for explicit define() checking

pull/14490/head
Tyson Roberts 7 years ago
parent 3554764963
commit e0b6f004f6
  1. 70
      src/compiler/objective_c_plugin.cc

@ -40,14 +40,40 @@ inline ::grpc::string SystemImport(const ::grpc::string &import) {
return ::grpc::string("#import <" + import + ">\n"); return ::grpc::string("#import <" + import + ">\n");
} }
inline ::grpc::string PreprocIf(const ::grpc::string& condition, // Preprocessor condition flags.
const ::grpc::string& if_true) { 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"); return ::grpc::string("#if " + condition + "\n" + if_true + "#endif\n");
} }
inline ::grpc::string PreprocIfElse(const ::grpc::string& condition, inline ::grpc::string PreprocIfElse(const ::grpc::string& symbol,
const ::grpc::string& if_true, const ::grpc::string& if_true,
const ::grpc::string& if_false) { const ::grpc::string& if_false,
PreprocConditionFlag flags = 0) {
::grpc::string condition = PreprocConditional(symbol, flags);
return ::grpc::string("#if " + condition + "\n" + return ::grpc::string("#if " + condition + "\n" +
if_true + "#else\n" + if_false + "#endif\n"); if_true + "#else\n" + if_false + "#endif\n");
} }
@ -71,7 +97,8 @@ inline ::grpc::string ImportProtoHeaders(
"GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS"; "GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS";
return PreprocIfElse(kFrameworkImportsCondition, return PreprocIfElse(kFrameworkImportsCondition,
indent + SystemImport(framework_header), indent + SystemImport(framework_header),
indent + LocalImport(header)); indent + LocalImport(header),
kCheckIfDefined);
} }
} // namespace } // namespace
@ -81,6 +108,13 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
ObjectiveCGrpcGenerator() {} ObjectiveCGrpcGenerator() {}
virtual ~ObjectiveCGrpcGenerator() {} virtual ~ObjectiveCGrpcGenerator() {}
private:
static const ::grpc::string kNonNullBegin = "NS_ASSUME_NONNULL_BEGIN\n";
static const ::grpc::string kNonNullEnd = "NS_ASSUME_NONNULL_END\n";
static const ::grpc::string kProtocolOnly = "GPB_GRPC_PROTOCOL_ONLY";
static const ::grpc::string kForwardDeclare =
"GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO";
public: public:
virtual bool Generate(const grpc::protobuf::FileDescriptor* file, virtual bool Generate(const grpc::protobuf::FileDescriptor* file,
const ::grpc::string& parameter, const ::grpc::string& parameter,
@ -91,15 +125,13 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
return true; return true;
} }
auto OmitIf = [](const ::grpc::string& s, const ::grpc::string& v) {
return PreprocIf(s, v, kInvertCondition | kCheckIfDefined);
};
::grpc::string file_name = ::grpc::string file_name =
google::protobuf::compiler::objectivec::FilePath(file); google::protobuf::compiler::objectivec::FilePath(file);
static const ::grpc::string kNonNullBegin = "NS_ASSUME_NONNULL_BEGIN\n";
static const ::grpc::string kNonNullEnd = "NS_ASSUME_NONNULL_END\n";
static const ::grpc::string kForwardDeclareCondition =
"GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO";
static const ::grpc::string kOmitInterfaceCondition =
"GPB_GRPC_OMIT_INTERFACE";
{ {
// Generate .pbrpc.h // Generate .pbrpc.h
@ -134,14 +166,14 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
} }
Write(context, file_name + ".pbrpc.h", Write(context, file_name + ".pbrpc.h",
PreprocIf("!" + kForwardDeclareCondition, imports) + "\n" + OmitIf(kForwardDeclare, imports) + "\n" +
PreprocIf("!" + kOmitInterfaceCondition, system_imports) + "\n" + OmitIf(kProtocolOnly, system_imports) + "\n" +
PreprocIfElse(kForwardDeclareCondition, PreprocIfElse(kForwardDeclare, class_declarations, class_imports,
class_declarations, class_imports) + "\n" + kCheckIfDefined) + "\n" +
forward_declarations + "\n" + forward_declarations + "\n" +
kNonNullBegin + "\n" + kNonNullBegin + "\n" +
protocols + "\n" + protocols + "\n" +
PreprocIf("!" + kOmitInterfaceCondition, interfaces) + "\n" + OmitIf(kProtocolOnly, interfaces) + "\n" +
kNonNullEnd + "\n"); kNonNullEnd + "\n");
} }
@ -166,10 +198,8 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
} }
Write(context, file_name + ".pbrpc.m", Write(context, file_name + ".pbrpc.m",
PreprocIf("!" + kOmitInterfaceCondition, OmitIf(kProtocolOnly,
imports + "\n" + imports + "\n" + class_imports + "\n" + definitions));
class_imports + "\n" +
definitions));
} }
return true; return true;

Loading…
Cancel
Save