From 5c752cb2a5965110234a3e132b40bc24d6ec4dd9 Mon Sep 17 00:00:00 2001 From: HÃ¥vard Pettersen Date: Mon, 22 Mar 2021 12:12:14 +0000 Subject: added UNWIND_DO macro --- .../tests/unwind_message/unwind_message_test.cpp | 35 ++++++++++++++++++---- .../src/vespa/vespalib/util/unwind_message.cpp | 1 - vespalib/src/vespa/vespalib/util/unwind_message.h | 6 +++- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/vespalib/src/tests/unwind_message/unwind_message_test.cpp b/vespalib/src/tests/unwind_message/unwind_message_test.cpp index 079d0235eb2..84245189842 100644 --- a/vespalib/src/tests/unwind_message/unwind_message_test.cpp +++ b/vespalib/src/tests/unwind_message/unwind_message_test.cpp @@ -6,6 +6,7 @@ using vespalib::unwind_msg; using vespalib::UnwindMessage; +using E = std::invalid_argument; //----------------------------------------------------------------------------- @@ -21,7 +22,6 @@ struct MyObj { UnwindMessage msg3 = UnwindMessage("this SHOULD be printed (2/4)"); ~MyObj() { EXPECT_EQ(std::uncaught_exceptions(), 1); - using E = std::invalid_argument; auto not_printed_1 = std::move(msg2); try { MyCheck my_check; @@ -32,7 +32,6 @@ struct MyObj { }; TEST(UnwindMessageTest, unwind_messages_are_printed_when_appropriate) { - using E = std::invalid_argument; auto not_printed_5 = unwind_msg("this should NOT be printed (%d)", 5); UNWIND_MSG("this should NOT be printed (%d)", 4); EXPECT_THROW( @@ -51,11 +50,7 @@ TEST(UnwindMessageTest, unwind_messages_are_printed_when_appropriate) { //----------------------------------------------------------------------------- -// need make_string for VESPA_STRLOC macro -#include - TEST(UnwindMessageTest, unwind_message_with_location) { - using E = std::invalid_argument; EXPECT_THROW( { UNWIND_MSG("%s message with location information", VESPA_STRLOC.c_str()); @@ -65,4 +60,32 @@ TEST(UnwindMessageTest, unwind_message_with_location) { //----------------------------------------------------------------------------- +void my_bad_call() { + throw E("just testing"); +} + +TEST(UnwindMessageTest, unwind_message_from_UNWIND_DO_macro_calling_a_function) { + EXPECT_THROW( + { + UNWIND_DO(my_bad_call()); + }, E); +} + +//----------------------------------------------------------------------------- + +TEST(UnwindMessageTest, unwind_message_from_UNWIND_DO_macro_with_inline_code) { + EXPECT_THROW( + { + UNWIND_DO( + int a = 1; + int b = 2; + int c = a + b; + (void) c; + throw E("oops"); + ); + }, E); +} + +//----------------------------------------------------------------------------- + GTEST_MAIN_RUN_ALL_TESTS() diff --git a/vespalib/src/vespa/vespalib/util/unwind_message.cpp b/vespalib/src/vespa/vespalib/util/unwind_message.cpp index 8e96aa2110d..5598f0068a5 100644 --- a/vespalib/src/vespa/vespalib/util/unwind_message.cpp +++ b/vespalib/src/vespa/vespalib/util/unwind_message.cpp @@ -1,7 +1,6 @@ // Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "unwind_message.h" -#include "stringfmt.h" #include namespace vespalib { diff --git a/vespalib/src/vespa/vespalib/util/unwind_message.h b/vespalib/src/vespa/vespalib/util/unwind_message.h index 56eb13bcf4f..43300ab830a 100644 --- a/vespalib/src/vespa/vespalib/util/unwind_message.h +++ b/vespalib/src/vespa/vespalib/util/unwind_message.h @@ -3,7 +3,7 @@ #pragma once #include "macro.h" -#include +#include "stringfmt.h" namespace vespalib { @@ -31,6 +31,10 @@ extern UnwindMessage unwind_msg(const char *fmt, ...) #endif ; +// make an unwind message with a hopefully unique name on the stack #define UNWIND_MSG(...) auto VESPA_CAT(unwindMessageOnLine, __LINE__) = unwind_msg(__VA_ARGS__) +// make an unwind message quoting a piece of code and then perform that code +#define UNWIND_DO(...) do { UNWIND_MSG("%s:%d: %s", __FILE__, __LINE__, VESPA_STRINGIZE(__VA_ARGS__)); __VA_ARGS__; } while(false) + } // namespace -- cgit v1.2.3