From b063c87596f4e05e4aaac9f69541e01278920451 Mon Sep 17 00:00:00 2001 From: yang-g Date: Wed, 7 Oct 2015 11:40:13 -0700 Subject: [PATCH 1/2] mark unreachable code with a macro --- include/grpc/support/port_platform.h | 12 ++++++++++++ src/core/channel/client_channel.c | 4 +--- src/core/client_config/uri_parser.c | 4 ++-- src/core/httpcli/parser.c | 11 +++-------- src/core/iomgr/tcp_client_posix.c | 2 +- src/core/iomgr/tcp_server_posix.c | 2 +- src/core/surface/byte_buffer.c | 7 ++----- src/core/surface/call.c | 3 +-- src/core/surface/channel_connectivity.c | 8 ++------ src/core/surface/completion_queue.c | 3 +-- src/core/transport/chttp2/frame_data.c | 4 +--- src/core/transport/chttp2/frame_goaway.c | 4 +--- src/core/transport/chttp2/hpack_parser.c | 8 ++------ src/core/transport/chttp2/parsing.c | 16 ++++------------ src/core/transport/chttp2/stream_encoder.c | 6 +++--- src/cpp/server/server.cc | 3 +-- 16 files changed, 38 insertions(+), 59 deletions(-) diff --git a/include/grpc/support/port_platform.h b/include/grpc/support/port_platform.h index 434dd6de874..f98038da520 100644 --- a/include/grpc/support/port_platform.h +++ b/include/grpc/support/port_platform.h @@ -181,6 +181,7 @@ #ifndef _BSD_SOURCE #define _BSD_SOURCE #endif +#define GPR_FORBID_UNREACHABLE_CODE #define GPR_MSG_IOVLEN_TYPE int #if TARGET_OS_IPHONE #define GPR_PLATFORM_STRING "ios" @@ -336,4 +337,15 @@ typedef uintptr_t gpr_uintptr; #endif #endif +#ifdef GPR_FORBID_UNREACHABLE_CODE +#define GPR_UNREACHABLE_CODE(STATEMENT) +#else +#define GPR_UNREACHABLE_CODE(STATEMENT) \ + do { \ + gpr_log(GPR_ERROR, "Should never reach here."); \ + abort(); \ + STATEMENT; \ + } while (0) +#endif /* GPR_FORBID_UNREACHABLE_CODE */ + #endif /* GRPC_SUPPORT_PORT_PLATFORM_H */ diff --git a/src/core/channel/client_channel.c b/src/core/channel/client_channel.c index b59b62a6aa7..4b5cd04dc11 100644 --- a/src/core/channel/client_channel.c +++ b/src/core/channel/client_channel.c @@ -645,9 +645,7 @@ static void destroy_call_elem(grpc_exec_ctx *exec_ctx, case CALL_WAITING_FOR_CONFIG: case CALL_WAITING_FOR_CALL: case CALL_WAITING_FOR_SEND: - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - break; + GPR_UNREACHABLE_CODE(return ); } } diff --git a/src/core/client_config/uri_parser.c b/src/core/client_config/uri_parser.c index df9f32d403c..cbdfffcf8e8 100644 --- a/src/core/client_config/uri_parser.c +++ b/src/core/client_config/uri_parser.c @@ -37,6 +37,7 @@ #include #include +#include #include /** a size_t default value... maps to all 1's */ @@ -120,8 +121,7 @@ static int parse_fragment_or_query(const char *uri_text, size_t *i) { } else { return 1; } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); + GPR_UNREACHABLE_CODE(return 0); default: (*i) += advance; break; diff --git a/src/core/httpcli/parser.c b/src/core/httpcli/parser.c index 404906d5ae2..046770c0944 100644 --- a/src/core/httpcli/parser.c +++ b/src/core/httpcli/parser.c @@ -139,8 +139,7 @@ static int finish_line(grpc_httpcli_parser *parser) { } break; case GRPC_HTTPCLI_BODY: - gpr_log(GPR_ERROR, "should never reach here"); - abort(); + GPR_UNREACHABLE_CODE(return 0); } parser->cur_line_length = 0; @@ -165,8 +164,7 @@ static int addbyte(grpc_httpcli_parser *parser, gpr_uint8 byte) { } else { return 1; } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); + GPR_UNREACHABLE_CODE(return 0); case GRPC_HTTPCLI_BODY: if (parser->r.body_length == parser->body_capacity) { parser->body_capacity = GPR_MAX(8, parser->body_capacity * 3 / 2); @@ -177,10 +175,7 @@ static int addbyte(grpc_httpcli_parser *parser, gpr_uint8 byte) { parser->r.body_length++; return 1; } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - - return 0; + GPR_UNREACHABLE_CODE(return 0); } void grpc_httpcli_parser_init(grpc_httpcli_parser *parser) { diff --git a/src/core/iomgr/tcp_client_posix.c b/src/core/iomgr/tcp_client_posix.c index aca2691c416..fe200392641 100644 --- a/src/core/iomgr/tcp_client_posix.c +++ b/src/core/iomgr/tcp_client_posix.c @@ -191,7 +191,7 @@ static void on_writable(grpc_exec_ctx *exec_ctx, void *acp, int success) { goto finish; } - abort(); + GPR_UNREACHABLE_CODE(return ); finish: if (fd != NULL) { diff --git a/src/core/iomgr/tcp_server_posix.c b/src/core/iomgr/tcp_server_posix.c index 13bd67576f5..99c76dcbe9a 100644 --- a/src/core/iomgr/tcp_server_posix.c +++ b/src/core/iomgr/tcp_server_posix.c @@ -352,7 +352,7 @@ static void on_read(grpc_exec_ctx *exec_ctx, void *arg, int success) { gpr_free(addr_str); } - abort(); + GPR_UNREACHABLE_CODE(return ); error: gpr_mu_lock(&sp->server->mu); diff --git a/src/core/surface/byte_buffer.c b/src/core/surface/byte_buffer.c index a930949f2d4..fb39c4531d9 100644 --- a/src/core/surface/byte_buffer.c +++ b/src/core/surface/byte_buffer.c @@ -75,9 +75,7 @@ grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) { return grpc_raw_byte_buffer_create(bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count); } - gpr_log(GPR_INFO, "should never get here"); - abort(); - return NULL; + GPR_UNREACHABLE_CODE(return NULL); } void grpc_byte_buffer_destroy(grpc_byte_buffer *bb) { @@ -95,6 +93,5 @@ size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) { case GRPC_BB_RAW: return bb->data.raw.slice_buffer.length; } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); + GPR_UNREACHABLE_CODE(return 0); } diff --git a/src/core/surface/call.c b/src/core/surface/call.c index d15a3bcbade..bc1b95a305d 100644 --- a/src/core/surface/call.c +++ b/src/core/surface/call.c @@ -429,8 +429,7 @@ static grpc_cq_completion *allocate_completion(grpc_call *call) { gpr_mu_unlock(&call->completion_mu); return &call->completions[i]; } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); + GPR_UNREACHABLE_CODE(return NULL); } static void done_completion(grpc_exec_ctx *exec_ctx, void *call, diff --git a/src/core/surface/channel_connectivity.c b/src/core/surface/channel_connectivity.c index f72fb041426..ca3c02c536a 100644 --- a/src/core/surface/channel_connectivity.c +++ b/src/core/surface/channel_connectivity.c @@ -100,9 +100,7 @@ static void finished_completion(grpc_exec_ctx *exec_ctx, void *pw, switch (w->phase) { case WAITING: case CALLED_BACK: - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - break; + GPR_UNREACHABLE_CODE(return ); case CALLING_BACK: w->phase = CALLED_BACK; break; @@ -149,9 +147,7 @@ static void partly_done(grpc_exec_ctx *exec_ctx, state_watcher *w, w->phase = CALLING_BACK_AND_FINISHED; break; case CALLING_BACK_AND_FINISHED: - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - break; + GPR_UNREACHABLE_CODE(return ); case CALLED_BACK: delete = 1; break; diff --git a/src/core/surface/completion_queue.c b/src/core/surface/completion_queue.c index e818ccba488..9e552c2cdf8 100644 --- a/src/core/surface/completion_queue.c +++ b/src/core/surface/completion_queue.c @@ -254,8 +254,7 @@ static void del_plucker(grpc_completion_queue *cc, void *tag, return; } } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); + GPR_UNREACHABLE_CODE(return ); } grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cc, void *tag, diff --git a/src/core/transport/chttp2/frame_data.c b/src/core/transport/chttp2/frame_data.c index acfa7c002eb..07179a4571e 100644 --- a/src/core/transport/chttp2/frame_data.c +++ b/src/core/transport/chttp2/frame_data.c @@ -168,7 +168,5 @@ grpc_chttp2_parse_error grpc_chttp2_data_parser_parse( } } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - return GRPC_CHTTP2_CONNECTION_ERROR; + GPR_UNREACHABLE_CODE(return GRPC_CHTTP2_CONNECTION_ERROR); } diff --git a/src/core/transport/chttp2/frame_goaway.c b/src/core/transport/chttp2/frame_goaway.c index 2ff1eda89b6..c5758bcb714 100644 --- a/src/core/transport/chttp2/frame_goaway.c +++ b/src/core/transport/chttp2/frame_goaway.c @@ -152,9 +152,7 @@ grpc_chttp2_parse_error grpc_chttp2_goaway_parser_parse( } return GRPC_CHTTP2_PARSE_OK; } - gpr_log(GPR_ERROR, "Should never end up here"); - abort(); - return GRPC_CHTTP2_CONNECTION_ERROR; + GPR_UNREACHABLE_CODE(return GRPC_CHTTP2_CONNECTION_ERROR); } void grpc_chttp2_goaway_append(gpr_uint32 last_stream_id, gpr_uint32 error_code, diff --git a/src/core/transport/chttp2/hpack_parser.c b/src/core/transport/chttp2/hpack_parser.c index 20ea5133751..20d8312d547 100644 --- a/src/core/transport/chttp2/hpack_parser.c +++ b/src/core/transport/chttp2/hpack_parser.c @@ -1166,9 +1166,7 @@ static int append_string(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur, append_bytes(str, decoded, 3); goto b64_byte0; } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - return 1; + GPR_UNREACHABLE_CODE(return 1); } /* append a null terminator to a string */ @@ -1313,9 +1311,7 @@ static int parse_value_string(grpc_chttp2_hpack_parser *p, const gpr_uint8 *cur, return 0; } /* Add code to prevent return without value error */ - gpr_log(GPR_ERROR, "Should never reach beyond switch in parse_value_string"); - abort(); - return 0; + GPR_UNREACHABLE_CODE(return 0); } static int parse_value_string_with_indexed_key(grpc_chttp2_hpack_parser *p, diff --git a/src/core/transport/chttp2/parsing.c b/src/core/transport/chttp2/parsing.c index f7a0a10581b..a0977ccaf6c 100644 --- a/src/core/transport/chttp2/parsing.c +++ b/src/core/transport/chttp2/parsing.c @@ -417,14 +417,10 @@ int grpc_chttp2_perform_read(grpc_exec_ctx *exec_ctx, transport_parsing->incoming_frame_size -= (gpr_uint32)(end - cur); return 1; } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); + GPR_UNREACHABLE_CODE(return 0); } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - - return 0; + GPR_UNREACHABLE_CODE(return 0); } static int init_frame_parser(grpc_chttp2_transport_parsing *transport_parsing) { @@ -580,9 +576,7 @@ static int init_data_frame_parser( case GRPC_CHTTP2_CONNECTION_ERROR: return 0; } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - return 0; + GPR_UNREACHABLE_CODE(return 0); } static void free_timeout(void *p) { gpr_free(p); } @@ -820,7 +814,5 @@ static int parse_frame_slice(grpc_exec_ctx *exec_ctx, case GRPC_CHTTP2_CONNECTION_ERROR: return 0; } - gpr_log(GPR_ERROR, "should never reach here"); - abort(); - return 0; + GPR_UNREACHABLE_CODE(return 0); } diff --git a/src/core/transport/chttp2/stream_encoder.c b/src/core/transport/chttp2/stream_encoder.c index 83227e677db..6c7f7a9ea7f 100644 --- a/src/core/transport/chttp2/stream_encoder.c +++ b/src/core/transport/chttp2/stream_encoder.c @@ -428,7 +428,7 @@ static grpc_mdelem *hpack_enc(grpc_chttp2_hpack_compressor *c, emit_lithdr_noidx(c, dynidx(c, indices_key), elem, st); return elem; } - abort(); + GPR_UNREACHABLE_CODE(return NULL); } indices_key = c->indices_keys[HASH_FRAGMENT_3(key_hash)]; @@ -442,7 +442,7 @@ static grpc_mdelem *hpack_enc(grpc_chttp2_hpack_compressor *c, emit_lithdr_noidx(c, dynidx(c, indices_key), elem, st); return elem; } - abort(); + GPR_UNREACHABLE_CODE(return NULL); } /* no elem, key in the table... fall back to literal emission */ @@ -454,7 +454,7 @@ static grpc_mdelem *hpack_enc(grpc_chttp2_hpack_compressor *c, emit_lithdr_noidx_v(c, elem, st); return elem; } - abort(); + GPR_UNREACHABLE_CODE(return NULL); } #define STRLEN_LIT(x) (sizeof(x) - 1) diff --git a/src/cpp/server/server.cc b/src/cpp/server/server.cc index a44e1d20250..e09744b842b 100644 --- a/src/cpp/server/server.cc +++ b/src/cpp/server/server.cc @@ -153,8 +153,7 @@ class Server::SyncRequest GRPC_FINAL : public CompletionQueueTag { GPR_ASSERT((*req)->in_flight_); return true; } - gpr_log(GPR_ERROR, "Should never reach here"); - abort(); + GPR_UNREACHABLE_CODE(return false); } void SetupRequest() { cq_ = grpc_completion_queue_create(nullptr); } From 90540b4338943f388434ccfade4bb20cace4c777 Mon Sep 17 00:00:00 2001 From: yang-g Date: Thu, 8 Oct 2015 15:13:35 -0700 Subject: [PATCH 2/2] add another return in case the macro is not defined and the compiler complains about no return statement --- src/core/surface/call.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/surface/call.c b/src/core/surface/call.c index c8152b6eae9..f9e6a596741 100644 --- a/src/core/surface/call.c +++ b/src/core/surface/call.c @@ -435,6 +435,7 @@ static grpc_cq_completion *allocate_completion(grpc_call *call) { return &call->completions[i]; } GPR_UNREACHABLE_CODE(return NULL); + return NULL; } static void done_completion(grpc_exec_ctx *exec_ctx, void *call,