From nobody Sun Apr 28 03:35:54 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8889C433F5 for ; Sat, 1 Oct 2022 00:27:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231646AbiJAA13 (ORCPT ); Fri, 30 Sep 2022 20:27:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232422AbiJAA1A (ORCPT ); Fri, 30 Sep 2022 20:27:00 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B04EC147A35 for ; Fri, 30 Sep 2022 17:26:48 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id j3-20020a256e03000000b006bc0294164dso5182265ybc.20 for ; Fri, 30 Sep 2022 17:26:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=NEg2UNzhugdAp054uzB1fjyZlTKV+IDmlPKtVI1Rou4=; b=DC9XtIzCxpTKCO6GS4PJLU8yLC6nZUZ81Xq/XkgrjTOFfSEnxSueP+PxaqX6MRpQNo wJuFToy4coT1kB9n/j7i+aJikBGzzYcYz7O/Nhbjv0objPyqomYpaPBHIEz6CdQtWQtU m16zhORNmSoiMauyBaRZEy0HeeI8zdCryXHACZagr+tjhvoy4apexvOtD/PPIHAdy8dM f3kdzSeFaJQGfwQpr/7Ddzp//lsQbyXoCu90tz5YWmzob2UV0sB6IqU2iH38/Lh0kwr3 SngsM3HaZPWrJh8fhYu8Bzlv2abb+f22GcJ7luH6yIST9jy6ZisZgYZO/CtVjSkgbyHz lgmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=NEg2UNzhugdAp054uzB1fjyZlTKV+IDmlPKtVI1Rou4=; b=Q8ZHHyLsBxM+kIAai77ls94juLxuXpvG9qFFKd22/fl4PNsRVwZeq7jMJDgsdeZlF4 Czkzuz0lPtQqq2g3ubJUxY+4D9kl6jfQndRiL62sX8VgfZ47hOE2fFd5NPUq0YWpMFR7 wCPjpX4Mranabv5HPj5bkEen9OB6dZFCNZOyLBqQ+i+THDSCq/Eesc56agLVLUAL3LFH k7YV5NGOLh6YCGHvJEb1GOc4UikjaJ5seRvdbvX35R0cQC/wqPmF5ZbcDQp3Az0LbSC8 crojx3gLauwsQua/YWBAalzmn97GCsn1oZsO1NPTm2PMR59gURDggL07/InZIenbzfUx uNnA== X-Gm-Message-State: ACrzQf1Pk/McCtYrSUYmFWbTgDddFTTAysWgzju4wBrDG8Lnln7ubjD3 t9gs4jRP3P95ts6ylylqNcO811jTRmBIag== X-Google-Smtp-Source: AMsMyM5+EXF3DUU8+VvZr1/7b/bGHnSKI8SVwNHpkpimiQlJaZuEPPsQs+WThQ/7g7BoiOCU/RnkEyiL43I38A== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a25:af48:0:b0:6a9:455a:ff10 with SMTP id c8-20020a25af48000000b006a9455aff10mr10736229ybj.84.1664584008001; Fri, 30 Sep 2022 17:26:48 -0700 (PDT) Date: Fri, 30 Sep 2022 17:26:35 -0700 In-Reply-To: <20221001002638.2881842-1-dlatypov@google.com> Mime-Version: 1.0 References: <20221001002638.2881842-1-dlatypov@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20221001002638.2881842-2-dlatypov@google.com> Subject: [PATCH 1/4] kunit: remove format func from struct kunit_assert, get it to 0 bytes From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, miguel.ojeda.sandonis@gmail.com, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Each calll to a KUNIT_EXPECT_*() macro creates a local variable which contains a struct kunit_assert. Normally, we'd hope the compiler would be able to optimize this away, but we've seen cases where it hasn't, see https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/GbrMNej2BAAJ. In changes like commit 21957f90b28f ("kunit: split out part of kunit_assert into a static const"), we've moved more and more parts out of struct kunit_assert and its children types (kunit_binary_assert). This patch removes the final field and gets us to: sizeof(struct kunit_assert) =3D=3D 0 sizeof(struct kunit_binary_assert) =3D=3D 24 (on UML x86_64). This also reduces the amount of macro plumbing going on at the cost of passing in one more arg to the base KUNIT_ASSERTION macro and kunit_do_failed_assertion(). Signed-off-by: Daniel Latypov Reviewed-by: David Gow --- include/kunit/assert.h | 28 ++++++---------------------- include/kunit/test.h | 17 +++++++++++------ lib/kunit/test.c | 7 ++++--- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/include/kunit/assert.h b/include/kunit/assert.h index 4b52e12c2ae8..ace3de8d1ee7 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -42,16 +42,15 @@ struct kunit_loc { =20 /** * struct kunit_assert - Data for printing a failed assertion or expectati= on. - * @format: a function which formats the data in this kunit_assert to a st= ring. * * Represents a failed expectation/assertion. Contains all the data necess= ary to * format a string to a user reporting the failure. */ -struct kunit_assert { - void (*format)(const struct kunit_assert *assert, - const struct va_format *message, - struct string_stream *stream); -}; +struct kunit_assert {}; + +typedef void (*assert_format_t)(const struct kunit_assert *assert, + const struct va_format *message, + struct string_stream *stream); =20 void kunit_assert_prologue(const struct kunit_loc *loc, enum kunit_assert_type type, @@ -71,16 +70,6 @@ void kunit_fail_assert_format(const struct kunit_assert = *assert, const struct va_format *message, struct string_stream *stream); =20 -/** - * KUNIT_INIT_FAIL_ASSERT_STRUCT - Initializer for &struct kunit_fail_asse= rt. - * - * Initializes a &struct kunit_fail_assert. Intended to be used in - * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. - */ -#define KUNIT_INIT_FAIL_ASSERT_STRUCT { \ - .assert =3D { .format =3D kunit_fail_assert_format }, \ -} - /** * struct kunit_unary_assert - Represents a KUNIT_{EXPECT|ASSERT}_{TRUE|FA= LSE} * @assert: The parent of this type. @@ -110,7 +99,6 @@ void kunit_unary_assert_format(const struct kunit_assert= *assert, * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ #define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \ - .assert =3D { .format =3D kunit_unary_assert_format }, \ .condition =3D cond, \ .expected_true =3D expect_true \ } @@ -145,7 +133,6 @@ void kunit_ptr_not_err_assert_format(const struct kunit= _assert *assert, * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. */ #define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \ - .assert =3D { .format =3D kunit_ptr_not_err_assert_format }, \ .text =3D txt, \ .value =3D val \ } @@ -190,7 +177,6 @@ void kunit_binary_assert_format(const struct kunit_asse= rt *assert, * KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like * kunit_binary_assert, kunit_binary_ptr_assert, etc. * - * @format_func: a function which formats the assert to a string. * @text_: Pointer to a kunit_binary_assert_text. * @left_val: The actual evaluated value of the expression in the left slo= t. * @right_val: The actual evaluated value of the expression in the right s= lot. @@ -200,11 +186,9 @@ void kunit_binary_assert_format(const struct kunit_ass= ert *assert, * fields but with different types for left_val/right_val. * This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ= , etc. */ -#define KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \ - text_, \ +#define KUNIT_INIT_BINARY_ASSERT_STRUCT(text_, \ left_val, \ right_val) { \ - .assert =3D { .format =3D format_func }, \ .text =3D text_, \ .left_value =3D left_val, \ .right_value =3D right_val \ diff --git a/include/kunit/test.h b/include/kunit/test.h index 840a2c375065..3476549106f7 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -472,9 +472,10 @@ void kunit_do_failed_assertion(struct kunit *test, const struct kunit_loc *loc, enum kunit_assert_type type, const struct kunit_assert *assert, + assert_format_t assert_format, const char *fmt, ...); =20 -#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, INITIALIZER= , fmt, ...) do { \ +#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, assert_form= at, INITIALIZER, fmt, ...) do { \ if (unlikely(!(pass))) { \ static const struct kunit_loc __loc =3D KUNIT_CURRENT_LOC; \ struct assert_class __assertion =3D INITIALIZER; \ @@ -482,6 +483,7 @@ void kunit_do_failed_assertion(struct kunit *test, &__loc, \ assert_type, \ &__assertion.assert, \ + assert_format, \ fmt, \ ##__VA_ARGS__); \ } \ @@ -493,7 +495,8 @@ void kunit_do_failed_assertion(struct kunit *test, assert_type, \ false, \ kunit_fail_assert, \ - KUNIT_INIT_FAIL_ASSERT_STRUCT, \ + kunit_fail_assert_format, \ + {}, \ fmt, \ ##__VA_ARGS__) =20 @@ -524,6 +527,7 @@ void kunit_do_failed_assertion(struct kunit *test, assert_type, \ !!(condition) =3D=3D !!expected_true, \ kunit_unary_assert, \ + kunit_unary_assert_format, \ KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ expected_true), \ fmt, \ @@ -581,8 +585,8 @@ do { \ assert_type, \ __left op __right, \ assert_class, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(format_func, \ - &__text, \ + format_func, \ + KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ __left, \ __right), \ fmt, \ @@ -639,8 +643,8 @@ do { \ assert_type, \ strcmp(__left, __right) op 0, \ kunit_binary_str_assert, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(kunit_binary_str_assert_format,\ - &__text, \ + kunit_binary_str_assert_format, \ + KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ __left, \ __right), \ fmt, \ @@ -659,6 +663,7 @@ do { \ assert_type, \ !IS_ERR_OR_NULL(__ptr), \ kunit_ptr_not_err_assert, \ + kunit_ptr_not_err_assert_format, \ KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \ __ptr), \ fmt, \ diff --git a/lib/kunit/test.c b/lib/kunit/test.c index b73d5bb5c473..53bf1793f915 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -247,7 +247,7 @@ static void kunit_print_string_stream(struct kunit *tes= t, =20 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc, enum kunit_assert_type type, const struct kunit_assert *assert, - const struct va_format *message) + assert_format_t assert_format, const struct va_format *message) { struct string_stream *stream; =20 @@ -263,7 +263,7 @@ static void kunit_fail(struct kunit *test, const struct= kunit_loc *loc, } =20 kunit_assert_prologue(loc, type, stream); - assert->format(assert, message, stream); + assert_format(assert, message, stream); =20 kunit_print_string_stream(test, stream); =20 @@ -287,6 +287,7 @@ void kunit_do_failed_assertion(struct kunit *test, const struct kunit_loc *loc, enum kunit_assert_type type, const struct kunit_assert *assert, + assert_format_t assert_format, const char *fmt, ...) { va_list args; @@ -296,7 +297,7 @@ void kunit_do_failed_assertion(struct kunit *test, message.fmt =3D fmt; message.va =3D &args; =20 - kunit_fail(test, loc, type, assert, &message); + kunit_fail(test, loc, type, assert, assert_format, &message); =20 va_end(args); =20 --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Sun Apr 28 03:35:54 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9154BC433F5 for ; Sat, 1 Oct 2022 00:27:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232435AbiJAA1h (ORCPT ); Fri, 30 Sep 2022 20:27:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232659AbiJAA1A (ORCPT ); Fri, 30 Sep 2022 20:27:00 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CFB71397E3 for ; Fri, 30 Sep 2022 17:26:50 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 88-20020a17090a09e100b00208c35d9452so3036001pjo.6 for ; Fri, 30 Sep 2022 17:26:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=g7IKu7kh+TXflaUbseOlbPiX53qX2o5Vb+tfLsI4XGE=; b=TT8XyI1j+MuboHcIEBy8S+9qTS/bL6xxy7o2ViUntEzgt3WGf883Lk7OvJyY7/Iz8w B+ZcRhFBcNQHlLbwX4dE/ZdFBJxLOoEfGJe1RNTSJ8WNP7W4IDkqlxG/7h26zv5slJsF CGbZuPy6QbD6vHxK0FXU28bRegwXt8oOh/qBLDGIeQSbXVwAGkLoFi6LvMtsudzHMLlX qGbe7IMI59y6lv+iA3H3VILoHPLGKJ5CBJHBcBQIJ65sIjfm5B8LIcKDhr24//taG87/ AwbtG4fj80zL+oepGgRJaM8K+8XQT3NoXVIU6FbpScI/g/w2UXrBF7igEnSZoRR/oBUq Pb2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=g7IKu7kh+TXflaUbseOlbPiX53qX2o5Vb+tfLsI4XGE=; b=C5uFxXF0aELWKStWUclOfDs1srrcgDAeLMtG4Fj1F63TVCNDw5qJgr0p2JcvI/Ja8F aB7TEiJInESFjqzd2KHwmoiOzUtRXFaNR9A0dV9Y/rlddwHxW70xR/lOUB232URJ23aB uRs/ouRmZJH0NxG6KEvTyTdb1GCbIuGuUwPGtIEx9u3SV/RgH7ygAhJCrN6dYdJuLNEN RbcNGnZ0sS92mVP1Y+TxexGrPWDEnLS9zfNzCVnE47Php9X+QZffgeF4z2ho4lp1P95l sQYImHF4tQRWwYfQz0yR8WAzrMbXTCY8akYo0SwZGSmlR3bUOJ479PDeKqSTNQ+YvgQ7 O2Ew== X-Gm-Message-State: ACrzQf2WbBHRA52ufbCqHxMpYIzIwoi18RoQNId5HtoVTbLz5dlrZIVw 33QywMeGbwZ68/O7P60NDxKPyNWSn8T3Pw== X-Google-Smtp-Source: AMsMyM5gCj7Mz0UQ8se1CKRxCJ9h08KUcoTB1jj8AdzDGbzgN1Iq1BCDVICqXx9lq/PnPRnKRTO98K/Z/fl/pw== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a17:90b:4a12:b0:203:3482:d39e with SMTP id kk18-20020a17090b4a1200b002033482d39emr877855pjb.145.1664584009747; Fri, 30 Sep 2022 17:26:49 -0700 (PDT) Date: Fri, 30 Sep 2022 17:26:36 -0700 In-Reply-To: <20221001002638.2881842-1-dlatypov@google.com> Mime-Version: 1.0 References: <20221001002638.2881842-1-dlatypov@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20221001002638.2881842-3-dlatypov@google.com> Subject: [PATCH 2/4] kunit: rename base KUNIT_ASSERTION macro to _KUNIT_FAILED From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, miguel.ojeda.sandonis@gmail.com, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Context: Currently this macro's name, KUNIT_ASSERTION conflicts with the name of an enum whose values are {KUNIT_EXPECTATION, KUNIT_ASSERTION}. It's hard to think of a better name for the enum, so rename this macro. It's also a bit strange that the macro might do nothing depending on the boolean argument `pass`. Why not have callers check themselves? This patch: Moves the pass/fail checking into the callers of KUNIT_ASSERTION, so now we only call it when the check has failed. Then we rename the macro the _KUNIT_FAILED() to reflect the new semantics. Signed-off-by: Daniel Latypov Reviewed-by: David Gow --- include/kunit/test.h | 123 +++++++++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 58 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 3476549106f7..fec437c8a2b7 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -475,30 +475,27 @@ void kunit_do_failed_assertion(struct kunit *test, assert_format_t assert_format, const char *fmt, ...); =20 -#define KUNIT_ASSERTION(test, assert_type, pass, assert_class, assert_form= at, INITIALIZER, fmt, ...) do { \ - if (unlikely(!(pass))) { \ - static const struct kunit_loc __loc =3D KUNIT_CURRENT_LOC; \ - struct assert_class __assertion =3D INITIALIZER; \ - kunit_do_failed_assertion(test, \ - &__loc, \ - assert_type, \ - &__assertion.assert, \ - assert_format, \ - fmt, \ - ##__VA_ARGS__); \ - } \ +#define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INIT= IALIZER, fmt, ...) do { \ + static const struct kunit_loc __loc =3D KUNIT_CURRENT_LOC; \ + struct assert_class __assertion =3D INITIALIZER; \ + kunit_do_failed_assertion(test, \ + &__loc, \ + assert_type, \ + &__assertion.assert, \ + assert_format, \ + fmt, \ + ##__VA_ARGS__); \ } while (0) =20 =20 #define KUNIT_FAIL_ASSERTION(test, assert_type, fmt, ...) \ - KUNIT_ASSERTION(test, \ - assert_type, \ - false, \ - kunit_fail_assert, \ - kunit_fail_assert_format, \ - {}, \ - fmt, \ - ##__VA_ARGS__) + _KUNIT_FAILED(test, \ + assert_type, \ + kunit_fail_assert, \ + kunit_fail_assert_format, \ + {}, \ + fmt, \ + ##__VA_ARGS__) =20 /** * KUNIT_FAIL() - Always causes a test to fail when evaluated. @@ -523,15 +520,19 @@ void kunit_do_failed_assertion(struct kunit *test, expected_true, \ fmt, \ ...) \ - KUNIT_ASSERTION(test, \ - assert_type, \ - !!(condition) =3D=3D !!expected_true, \ - kunit_unary_assert, \ - kunit_unary_assert_format, \ - KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ - expected_true), \ - fmt, \ - ##__VA_ARGS__) +do { \ + if (likely(!!(condition) =3D=3D !!expected_true)) \ + break; \ + \ + _KUNIT_FAILED(test, \ + assert_type, \ + kunit_unary_assert, \ + kunit_unary_assert_format, \ + KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ + expected_true), \ + fmt, \ + ##__VA_ARGS__); \ +} while (0) =20 #define KUNIT_TRUE_MSG_ASSERTION(test, assert_type, condition, fmt, ...) = \ KUNIT_UNARY_ASSERTION(test, \ @@ -581,16 +582,18 @@ do { \ .right_text =3D #right, \ }; \ \ - KUNIT_ASSERTION(test, \ - assert_type, \ - __left op __right, \ - assert_class, \ - format_func, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ - __left, \ - __right), \ - fmt, \ - ##__VA_ARGS__); \ + if (likely(__left op __right)) \ + break; \ + \ + _KUNIT_FAILED(test, \ + assert_type, \ + assert_class, \ + format_func, \ + KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ + __left, \ + __right), \ + fmt, \ + ##__VA_ARGS__); \ } while (0) =20 #define KUNIT_BINARY_INT_ASSERTION(test, \ @@ -639,16 +642,19 @@ do { \ .right_text =3D #right, \ }; \ \ - KUNIT_ASSERTION(test, \ - assert_type, \ - strcmp(__left, __right) op 0, \ - kunit_binary_str_assert, \ - kunit_binary_str_assert_format, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ - __left, \ - __right), \ - fmt, \ - ##__VA_ARGS__); \ + if (likely(strcmp(__left, __right) op 0)) \ + break; \ + \ + \ + _KUNIT_FAILED(test, \ + assert_type, \ + kunit_binary_str_assert, \ + kunit_binary_str_assert_format, \ + KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ + __left, \ + __right), \ + fmt, \ + ##__VA_ARGS__); \ } while (0) =20 #define KUNIT_PTR_NOT_ERR_OR_NULL_MSG_ASSERTION(test, \ @@ -659,15 +665,16 @@ do { \ do { \ const typeof(ptr) __ptr =3D (ptr); \ \ - KUNIT_ASSERTION(test, \ - assert_type, \ - !IS_ERR_OR_NULL(__ptr), \ - kunit_ptr_not_err_assert, \ - kunit_ptr_not_err_assert_format, \ - KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, \ - __ptr), \ - fmt, \ - ##__VA_ARGS__); \ + if (!IS_ERR_OR_NULL(__ptr)) \ + break; \ + \ + _KUNIT_FAILED(test, \ + assert_type, \ + kunit_ptr_not_err_assert, \ + kunit_ptr_not_err_assert_format, \ + KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \ + fmt, \ + ##__VA_ARGS__); \ } while (0) =20 /** --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Sun Apr 28 03:35:54 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1C9EC433F5 for ; Sat, 1 Oct 2022 00:27:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232674AbiJAA1p (ORCPT ); Fri, 30 Sep 2022 20:27:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33164 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232437AbiJAA1C (ORCPT ); Fri, 30 Sep 2022 20:27:02 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEC801DF35 for ; Fri, 30 Sep 2022 17:26:51 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id w14-20020a170902e88e00b00177ab7a12f6so4141577plg.16 for ; Fri, 30 Sep 2022 17:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=VtHbDGUyBMlmUU7N3DxfY8TUG6hQduH5eTgQ3i+7K4w=; b=dqKmZOYwrmLA7ihchLDdTFZu8Lku9ziAfkQaHH5XF3iYrQhGAxlAwW8OUlR76A2ivi 4tEwxYMmmnP3iB2VGC2Ngm4/tRoVTspfAPibOJUoA3rIdWx/Ii/hz5GeXBttisxIP1Gf DbGBcn8GD1rmcJd3Yyfv0usZNO6OJaRj32QQHrVJVz3YxbwBKPYhFrUyC2tmXDGICqrg xXtpqsBd5mbX5KwyPai8i5DXWZm3jhRKGck9w4j2+HS/GlVk4h+hQ+Xp2uC22kr776dO 1mLxojybJsSrLBRMmb0YJ7neMafnFfonmA0YIRfM2Fah8uOlZ7pH+hQQ1mbDtJT1Zlg6 xjbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=VtHbDGUyBMlmUU7N3DxfY8TUG6hQduH5eTgQ3i+7K4w=; b=ti2lLUBxWSiJYFnnviUCBQAJnpYfhkbmSxbmbjLdbyYVJO/585CM+S5JQPg5M2Fmhw ZuEj/fHJSVx0ySg1l3kLLEYA+KJheoum9aXgv0EzBjuKGyd4oZyMl8KAif8ETwIX2fHJ k9QIhOLt3JyShO/qULjgxOkpPEAhI0gjh2KAEP940Y3SRkOk1QPEiU+VqR4ixAhR+g4B PaMgvTMFwFpvALSIzlLpQitWI3qU0i9tKWZhZE9piGhrLVoljPPmWCGf9v+a+IqHogHa BR9OR03m1nAdJFn5TddxILiV5ZDwwSgpK+RH9zNcWmhY0dBszbBh9depoRjkRzX7i93n q8hw== X-Gm-Message-State: ACrzQf2OD+zjr/X6ft2l54d4FWjTLlS8XKWe7Gc1El6KT9jRrdZbIg6b 1rJkBypMv/JAZbYmNRq8nDUKGDsA+jV4TQ== X-Google-Smtp-Source: AMsMyM7+ieKmpuNfGNK3lVj+ymHsmU/Lgv/olEOsTFS9fjZPCLSyK2lX8thNmdwPjbaie+lBICU3N7ZgKfCAgQ== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a17:902:d509:b0:179:ee34:faaf with SMTP id b9-20020a170902d50900b00179ee34faafmr11510191plg.150.1664584011307; Fri, 30 Sep 2022 17:26:51 -0700 (PDT) Date: Fri, 30 Sep 2022 17:26:37 -0700 In-Reply-To: <20221001002638.2881842-1-dlatypov@google.com> Mime-Version: 1.0 References: <20221001002638.2881842-1-dlatypov@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20221001002638.2881842-4-dlatypov@google.com> Subject: [PATCH 3/4] kunit: eliminate KUNIT_INIT_*_ASSERT_STRUCT macros From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, miguel.ojeda.sandonis@gmail.com, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" These macros exist because passing an initializer list to other macros is hard. The goal of these macros is to generate a line like struct $ASSERT_TYPE __assertion =3D $APPROPRIATE_INITIALIZER; e.g. struct kunit_unary_assertion __assertion =3D { .condition =3D "foo()", .expected_true =3D true }; But the challenge is you can't pass `{.condition=3D..., .expect_true=3D...}` as a macro argument, since the comma means you're actually passing two arguments, `{.condition=3D...` and `.expect_true=3D....}`. So we'd made custom macros for each different initializer-list shape. But we can work around this with the following generic macro #define KUNIT_INIT_ASSERT(initializers...) { initializers } Note: this has the downside that we have to rename some macros arguments to not conflict with the struct field names (e.g. `expected_true`). It's a bit gross, but probably worth reducing the # of macros. Signed-off-by: Daniel Latypov Reviewed-by: David Gow --- include/kunit/assert.h | 48 ------------------------------------------ include/kunit/test.h | 27 +++++++++++++----------- 2 files changed, 15 insertions(+), 60 deletions(-) diff --git a/include/kunit/assert.h b/include/kunit/assert.h index ace3de8d1ee7..01b191fa17c3 100644 --- a/include/kunit/assert.h +++ b/include/kunit/assert.h @@ -90,19 +90,6 @@ void kunit_unary_assert_format(const struct kunit_assert= *assert, const struct va_format *message, struct string_stream *stream); =20 -/** - * KUNIT_INIT_UNARY_ASSERT_STRUCT() - Initializes &struct kunit_unary_asse= rt. - * @cond: A string representation of the expression asserted true or false. - * @expect_true: True if of type KUNIT_{EXPECT|ASSERT}_TRUE, false otherwi= se. - * - * Initializes a &struct kunit_unary_assert. Intended to be used in - * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. - */ -#define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \ - .condition =3D cond, \ - .expected_true =3D expect_true \ -} - /** * struct kunit_ptr_not_err_assert - An expectation/assertion that a point= er is * not NULL and not a -errno. @@ -123,20 +110,6 @@ void kunit_ptr_not_err_assert_format(const struct kuni= t_assert *assert, const struct va_format *message, struct string_stream *stream); =20 -/** - * KUNIT_INIT_PTR_NOT_ERR_ASSERT_STRUCT() - Initializes a - * &struct kunit_ptr_not_err_assert. - * @txt: A string representation of the expression passed to the expectati= on. - * @val: The actual evaluated pointer value of the expression. - * - * Initializes a &struct kunit_ptr_not_err_assert. Intended to be used in - * KUNIT_EXPECT_* and KUNIT_ASSERT_* macros. - */ -#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \ - .text =3D txt, \ - .value =3D val \ -} - /** * struct kunit_binary_assert_text - holds strings for &struct * kunit_binary_assert and friends to try and make the structs smaller. @@ -173,27 +146,6 @@ void kunit_binary_assert_format(const struct kunit_ass= ert *assert, const struct va_format *message, struct string_stream *stream); =20 -/** - * KUNIT_INIT_BINARY_ASSERT_STRUCT() - Initializes a binary assert like - * kunit_binary_assert, kunit_binary_ptr_assert, etc. - * - * @text_: Pointer to a kunit_binary_assert_text. - * @left_val: The actual evaluated value of the expression in the left slo= t. - * @right_val: The actual evaluated value of the expression in the right s= lot. - * - * Initializes a binary assert like kunit_binary_assert, - * kunit_binary_ptr_assert, etc. This relies on these structs having the s= ame - * fields but with different types for left_val/right_val. - * This is ultimately used by binary assertion macros like KUNIT_EXPECT_EQ= , etc. - */ -#define KUNIT_INIT_BINARY_ASSERT_STRUCT(text_, \ - left_val, \ - right_val) { \ - .text =3D text_, \ - .left_value =3D left_val, \ - .right_value =3D right_val \ -} - /** * struct kunit_binary_ptr_assert - An expectation/assertion that compares= two * pointer values (for example, KUNIT_EXPECT_PTR_EQ(test, foo, bar)). diff --git a/include/kunit/test.h b/include/kunit/test.h index fec437c8a2b7..e49348bbc6ee 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -514,22 +514,25 @@ void kunit_do_failed_assertion(struct kunit *test, fmt, \ ##__VA_ARGS__) =20 +/* Helper to safely pass around an initializer list to other macros. */ +#define KUNIT_INIT_ASSERT(initializers...) { initializers } + #define KUNIT_UNARY_ASSERTION(test, \ assert_type, \ - condition, \ - expected_true, \ + condition_, \ + expected_true_, \ fmt, \ ...) \ do { \ - if (likely(!!(condition) =3D=3D !!expected_true)) \ + if (likely(!!(condition_) =3D=3D !!expected_true_)) \ break; \ \ _KUNIT_FAILED(test, \ assert_type, \ kunit_unary_assert, \ kunit_unary_assert_format, \ - KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ - expected_true), \ + KUNIT_INIT_ASSERT(.condition =3D #condition_, \ + .expected_true =3D expected_true_), \ fmt, \ ##__VA_ARGS__); \ } while (0) @@ -589,9 +592,9 @@ do { \ assert_type, \ assert_class, \ format_func, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ - __left, \ - __right), \ + KUNIT_INIT_ASSERT(.text =3D &__text, \ + .left_value =3D __left, \ + .right_value =3D __right), \ fmt, \ ##__VA_ARGS__); \ } while (0) @@ -650,9 +653,9 @@ do { \ assert_type, \ kunit_binary_str_assert, \ kunit_binary_str_assert_format, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ - __left, \ - __right), \ + KUNIT_INIT_ASSERT(.text =3D &__text, \ + .left_value =3D __left, \ + .right_value =3D __right), \ fmt, \ ##__VA_ARGS__); \ } while (0) @@ -672,7 +675,7 @@ do { \ assert_type, \ kunit_ptr_not_err_assert, \ kunit_ptr_not_err_assert_format, \ - KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \ + KUNIT_INIT_ASSERT(.text =3D #ptr, .value =3D __ptr), \ fmt, \ ##__VA_ARGS__); \ } while (0) --=20 2.38.0.rc1.362.ged0d419d3c-goog From nobody Sun Apr 28 03:35:54 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BDE06C433FE for ; Sat, 1 Oct 2022 00:27:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231704AbiJAA1k (ORCPT ); Fri, 30 Sep 2022 20:27:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232662AbiJAA1C (ORCPT ); Fri, 30 Sep 2022 20:27:02 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BC865F82 for ; Fri, 30 Sep 2022 17:26:53 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id u6-20020a17090a1f0600b002039826d478so3374742pja.4 for ; Fri, 30 Sep 2022 17:26:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=DrdzDq6t5Szhsu/Sk6P5GuM2F1JxchQr+0dbQd4xjqA=; b=GnykWZuUusySEmZRDRq89nbwQwUSS2BZ3mERFnQ4lw308/JrzIFUt3G3r3OyQyCjfY xAGBEhZoOjtYOPP8uKLKyaeuOwyTC5+Ne5KdeSKX1oOdmB7M82beLgNPk+c8TS/FODPr jdMKA28NUTHDe1EM7NYZEZ+ZFBuycXKzuRj0CGqja9/DOu2jNxbXBg5SmocYCMh1SZXU ZCNc4wgHNfJ5CS7hmVhiauHHfxSm0LB0ad444Mr3DMD2ttT4FviZTfx6IOcsxF6372ME yszZfuUwgArHETLd59mL3L4rLuGOmlZJnOjm+9F6SqME9JE5Dlb8r0bpg5tpT6xSp2Eh KsUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=DrdzDq6t5Szhsu/Sk6P5GuM2F1JxchQr+0dbQd4xjqA=; b=I2nKVqa41a5BCAy0pwGUiFjoQ64vDT4/UQa2I3WwVIBl7/GBg/C9RvbTZxguOONgok lxfWcfZDpN589EZra2rVv+lrbENpR9ax32HWwUOb77t2/xa1k8fXmOZYHeUaAclQ23DR XVuVvc/Ga/Jiu59PcY6D0VNSUL9QnZVGlDrsnHnu+y63IFeH1eNR68MQWi9D+38vhyQa h8j4w16TfmzJS5aEb1CBlDwL5+scuxwZBHjVmREg/xed0mdzz8WqoDjMwJNZu2p027ah MXLpC6Oj6y4PyiG08EP5mlNSPyUiW/SqOlZeuCUjbdhp+D/8q0bUndyLYdFp0PmBnN0P REyA== X-Gm-Message-State: ACrzQf2B32zCOv27/xbQ0vrPbA+lmcYexjkNT+3HqtvUnU8jjXjNSxW9 JUXjYg784vlrVwHJVb00CvyGQqOqwqO+lQ== X-Google-Smtp-Source: AMsMyM7nL2AzNmgA6b7OLqOJyYnw86E/zsba9xXmPiPKN7886pdizJ5Lnh2FzZ9kEeNFeAPtVmNTcPPf1r1gVw== X-Received: from dlatypov-spec.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:3f35]) (user=dlatypov job=sendgmr) by 2002:a17:90b:3c49:b0:209:7137:4b7c with SMTP id pm9-20020a17090b3c4900b0020971374b7cmr876988pjb.115.1664584012978; Fri, 30 Sep 2022 17:26:52 -0700 (PDT) Date: Fri, 30 Sep 2022 17:26:38 -0700 In-Reply-To: <20221001002638.2881842-1-dlatypov@google.com> Mime-Version: 1.0 References: <20221001002638.2881842-1-dlatypov@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20221001002638.2881842-5-dlatypov@google.com> Subject: [PATCH 4/4] kunit: declare kunit_assert structs as const From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, skhan@linuxfoundation.org, miguel.ojeda.sandonis@gmail.com, Daniel Latypov Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Everywhere we use the assert structs now takes them via const*, as of commit 7466886b400b ("kunit: take `kunit_assert` as `const`"). So now let's properly declare the structs as const as well. Signed-off-by: Daniel Latypov Reviewed-by: David Gow Reviewed-by: Miguel Ojeda --- include/kunit/test.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index e49348bbc6ee..d574c871dd9f 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -477,7 +477,7 @@ void kunit_do_failed_assertion(struct kunit *test, =20 #define _KUNIT_FAILED(test, assert_type, assert_class, assert_format, INIT= IALIZER, fmt, ...) do { \ static const struct kunit_loc __loc =3D KUNIT_CURRENT_LOC; \ - struct assert_class __assertion =3D INITIALIZER; \ + const struct assert_class __assertion =3D INITIALIZER; \ kunit_do_failed_assertion(test, \ &__loc, \ assert_type, \ --=20 2.38.0.rc1.362.ged0d419d3c-goog