aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compiler
diff options
context:
space:
mode:
authorGravatar Tyson Roberts <nall@google.com>2018-02-23 15:58:31 +0900
committerGravatar Tyson Roberts <nall@google.com>2018-02-23 16:33:28 +0900
commite0b6f004f6d0a5f5a042f9fdc3b93aaae146f975 (patch)
treea2f8d143eb1e5b497a6f83a7bf6c09e4383f3720 /src/compiler
parent3554764963f5e8d3cc82d5a33e6c234a1b96b55f (diff)
Updates #if generation to allow for explicit define() checking
Diffstat (limited to 'src/compiler')
-rw-r--r--src/compiler/objective_c_plugin.cc70
1 files changed, 50 insertions, 20 deletions
diff --git a/src/compiler/objective_c_plugin.cc b/src/compiler/objective_c_plugin.cc
index 398bbc25a7..16eba9a104 100644
--- a/src/compiler/objective_c_plugin.cc
+++ b/src/compiler/objective_c_plugin.cc
@@ -40,14 +40,40 @@ inline ::grpc::string SystemImport(const ::grpc::string &import) {
return ::grpc::string("#import <" + import + ">\n");
}
-inline ::grpc::string PreprocIf(const ::grpc::string& condition,
- const ::grpc::string& if_true) {
+// 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& condition,
+inline ::grpc::string PreprocIfElse(const ::grpc::string& symbol,
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" +
if_true + "#else\n" + if_false + "#endif\n");
}
@@ -71,7 +97,8 @@ inline ::grpc::string ImportProtoHeaders(
"GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS";
return PreprocIfElse(kFrameworkImportsCondition,
indent + SystemImport(framework_header),
- indent + LocalImport(header));
+ indent + LocalImport(header),
+ kCheckIfDefined);
}
} // namespace
@@ -81,6 +108,13 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
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:
virtual bool Generate(const grpc::protobuf::FileDescriptor* file,
const ::grpc::string& parameter,
@@ -91,15 +125,13 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
return true;
}
+ 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);
- 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
@@ -134,14 +166,14 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
}
Write(context, file_name + ".pbrpc.h",
- PreprocIf("!" + kForwardDeclareCondition, imports) + "\n" +
- PreprocIf("!" + kOmitInterfaceCondition, system_imports) + "\n" +
- PreprocIfElse(kForwardDeclareCondition,
- class_declarations, class_imports) + "\n" +
+ OmitIf(kForwardDeclare, imports) + "\n" +
+ OmitIf(kProtocolOnly, system_imports) + "\n" +
+ PreprocIfElse(kForwardDeclare, class_declarations, class_imports,
+ kCheckIfDefined) + "\n" +
forward_declarations + "\n" +
kNonNullBegin + "\n" +
protocols + "\n" +
- PreprocIf("!" + kOmitInterfaceCondition, interfaces) + "\n" +
+ OmitIf(kProtocolOnly, interfaces) + "\n" +
kNonNullEnd + "\n");
}
@@ -166,10 +198,8 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
}
Write(context, file_name + ".pbrpc.m",
- PreprocIf("!" + kOmitInterfaceCondition,
- imports + "\n" +
- class_imports + "\n" +
- definitions));
+ OmitIf(kProtocolOnly,
+ imports + "\n" + class_imports + "\n" + definitions));
}
return true;