aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Vijay Pai <vpai@google.com>2017-12-19 09:09:52 -0800
committerGravatar Vijay Pai <vpai@google.com>2018-01-08 15:12:55 -0800
commit9809ce38e9f79b4e9a0b1ec1c076cce0beee1e98 (patch)
treecc5c3b950d119399570724fa705042a69092257e
parent9427eabf3bf9f1de14a3fabdc72e090dac7601e1 (diff)
Use appropriate preprocessor guards to allow building without exceptions
-rw-r--r--BUILD18
-rw-r--r--bazel/grpc_build_system.bzl4
-rw-r--r--include/grpc++/impl/codegen/method_handler_impl.h10
-rw-r--r--include/grpc/impl/codegen/port_platform.h15
-rw-r--r--test/cpp/end2end/exception_test.cc4
5 files changed, 44 insertions, 7 deletions
diff --git a/BUILD b/BUILD
index dba6592f17..ebd198275e 100644
--- a/BUILD
+++ b/BUILD
@@ -39,6 +39,16 @@ config_setting(
)
config_setting(
+ name = "grpc_allow_exceptions",
+ values = {"define": "GRPC_ALLOW_EXCEPTIONS=1"},
+)
+
+config_setting(
+ name = "grpc_disallow_exceptions",
+ values = {"define": "GRPC_ALLOW_EXCEPTIONS=0"},
+)
+
+config_setting(
name = "remote_execution",
values = {"define": "GRPC_PORT_ISOLATED_RUNTIME=1"},
)
@@ -544,24 +554,24 @@ grpc_cc_library(
grpc_cc_library(
name = "debug_location",
- public_hdrs = ["src/core/lib/support/debug_location.h"],
language = "c++",
+ public_hdrs = ["src/core/lib/support/debug_location.h"],
)
grpc_cc_library(
name = "ref_counted",
- public_hdrs = ["src/core/lib/support/ref_counted.h"],
language = "c++",
+ public_hdrs = ["src/core/lib/support/ref_counted.h"],
deps = [
- "grpc_trace",
"debug_location",
+ "grpc_trace",
],
)
grpc_cc_library(
name = "ref_counted_ptr",
- public_hdrs = ["src/core/lib/support/ref_counted_ptr.h"],
language = "c++",
+ public_hdrs = ["src/core/lib/support/ref_counted_ptr.h"],
)
grpc_cc_library(
diff --git a/bazel/grpc_build_system.bzl b/bazel/grpc_build_system.bzl
index d146ca9c2c..d61eced2d9 100644
--- a/bazel/grpc_build_system.bzl
+++ b/bazel/grpc_build_system.bzl
@@ -60,6 +60,10 @@ def grpc_cc_library(name, srcs = [], public_hdrs = [], hdrs = [],
defines = select({"//:grpc_no_ares": ["GRPC_ARES=0"],
"//conditions:default": [],}) +
select({"//:remote_execution": ["GRPC_PORT_ISOLATED_RUNTIME=1"],
+ "//conditions:default": [],} +
+ select({"//:grpc_allow_exceptions": ["GRPC_ALLOW_EXCEPTIONS=1"],
+ "//:grpc_disallow_exceptions":
+ ["GRPC_ALLOW_EXCEPTIONS=0"],
"//conditions:default": [],}),
hdrs = _maybe_update_cc_library_hdrs(hdrs + public_hdrs),
deps = deps + _get_external_deps(external_deps),
diff --git a/include/grpc++/impl/codegen/method_handler_impl.h b/include/grpc++/impl/codegen/method_handler_impl.h
index b72dceb1b4..41c287231f 100644
--- a/include/grpc++/impl/codegen/method_handler_impl.h
+++ b/include/grpc++/impl/codegen/method_handler_impl.h
@@ -35,15 +35,19 @@ namespace internal {
// so this process doesn't require additional overhead in the common case.
// Additionally, we don't need to return if we caught an exception or not;
// the handling is the same in either case.
-template <class F>
-Status CatchingFunctionHandler(F&& callable) {
+template <class Callable>
+Status CatchingFunctionHandler(Callable&& handler) {
+#if GRPC_ALLOW_EXCEPTIONS
try {
- return callable();
+ return handler();
} catch (const std::exception& e) {
return Status(StatusCode::UNKNOWN, e.what());
} catch (...) {
return Status(StatusCode::UNKNOWN, "Exception in method handler");
}
+#else
+ return handler();
+#endif
}
/// A wrapper class of an application provided rpc method handler.
diff --git a/include/grpc/impl/codegen/port_platform.h b/include/grpc/impl/codegen/port_platform.h
index e6bee73ef1..becb16b5b8 100644
--- a/include/grpc/impl/codegen/port_platform.h
+++ b/include/grpc/impl/codegen/port_platform.h
@@ -477,6 +477,21 @@ typedef unsigned __int64 uint64_t;
#endif /* GPR_ATTRIBUTE_NO_TSAN (2) */
#endif /* GPR_ATTRIBUTE_NO_TSAN (1) */
+/* GRPC_ALLOW_EXCEPTIONS should be 0 or 1 if exceptions are allowed or not */
+#ifndef GRPC_ALLOW_EXCEPTIONS
+/* If not already set, set to 1 on Windows (style guide standard) but to
+ * 0 on non-Windows platforms unless the compiler defines __EXCEPTIONS */
+#ifdef GPR_WINDOWS
+#define GRPC_ALLOW_EXCEPTIONS 1
+#else
+#ifdef __EXCEPTIONS
+#define GRPC_ALLOW_EXCEPTIONS 1
+#else
+#define GRPC_ALLOW_EXCEPTIONS 0
+#endif
+#endif
+#endif
+
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS
#endif
diff --git a/test/cpp/end2end/exception_test.cc b/test/cpp/end2end/exception_test.cc
index 6545ffa530..7e0d5c7951 100644
--- a/test/cpp/end2end/exception_test.cc
+++ b/test/cpp/end2end/exception_test.cc
@@ -24,6 +24,7 @@
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
+#include <grpc/impl/codegen/port_platform.h>
#include "src/proto/grpc/testing/echo.grpc.pb.h"
#include "test/core/util/test_config.h"
@@ -35,6 +36,7 @@ namespace testing {
const char* kErrorMessage = "This service caused an exception";
+#if GRPC_ALLOW_EXCEPTIONS
class ExceptingServiceImpl : public ::grpc::testing::EchoTestService::Service {
public:
Status Echo(ServerContext* server_context, const EchoRequest* request,
@@ -106,6 +108,8 @@ TEST_F(ExceptionTest, RequestStream) {
EXPECT_EQ(s.error_message(), kErrorMessage);
}
+#endif
+
} // namespace testing
} // namespace grpc