From nobody Tue May 5 10:02:26 2026 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 709DFC433F5 for ; Wed, 25 May 2022 15:45:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239642AbiEYPp0 (ORCPT ); Wed, 25 May 2022 11:45:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245255AbiEYPpC (ORCPT ); Wed, 25 May 2022 11:45:02 -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 3C9995D1B7 for ; Wed, 25 May 2022 08:44:59 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id i204-20020a2522d5000000b006524ae5d7d7so4959998ybi.16 for ; Wed, 25 May 2022 08:44:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=tmO2qs2LNYzR+TlpvHKwl7wCARa3+s+fYe2/d+I3X6M=; b=VoR/WphEp0RSbSVgBmUuQrebaa7xCuolaDuEZS9YoRyE+Gtzx8pYu51VVMV2PMSSiR 0kZtTYSSwJMc2bZe5gfTztkBVQ8w4ycXSpM2+scxAFn8TUlxWnAGgIbSSLytcEFKzOtu wSjSL4c3W0RZSzN2EO+Z7de9uNddCaXmVRU/h7SvAIjPZPGYJeFHQN74Nu2JLpQMiirw DLh+4yiN3+XSrKoAgdfQYP2q1GRonpvbYtzZihFLThZohd8FWMjt/EjZNMTcc/dUeRYX ovB0K3ueSvglStOjrUbOI2BwDYDI9WTnDMz69lOfBNDfM3Ma+mfHOsh37nqw3zL1nft4 en/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=tmO2qs2LNYzR+TlpvHKwl7wCARa3+s+fYe2/d+I3X6M=; b=TriMnxn2zg2AsahAkXoOmWySEjUq64b3L+RFQX3DBRGTtWQM2XVZUd+OqL8k5JVOs1 f8agtUGr51PtOtRCbSsRf/eWP195FQjzIuwttdXk7qai6bQ1lCMiMbTvX+MLwsRhjYIs aNdmuJFRM3J71EwsVz4zk7FcZKyFr4WCAOaY4adDhMpULr4E9irdc9UyFdT6dQ2JaVos 2LgLrW2wzvvpAsfzXV1mgxW8OGUfgSWTzNt26vfuyQBubwzUf7Tsr4S7t8s3MTuy6H+i zOk3lweCnI3NEol63MT37zf+aOSY0UN4UBt9kSvh7mZgq6HH9xpQ+3MmCMy7waBpRCQl fzaQ== X-Gm-Message-State: AOAM532cZwQiC5Con0WNkQ12XxvB6L3mlMXz2LZR5KBz2nWo0n4VICAp cSk4mrcYrrUugPkTdIquVgjn13hzwVYduw== X-Google-Smtp-Source: ABdhPJyiRpEeVxByFgMFpyHc5M8y7Z+h709UrDEtJPQkcjqn7TgPXBQJQKs5Xi2YSqWqowEtffWMdPAcId2zzg== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:5bba:b161:2a8a:2ddd]) (user=dlatypov job=sendgmr) by 2002:a25:3857:0:b0:652:3012:bfc2 with SMTP id f84-20020a253857000000b006523012bfc2mr8683767yba.116.1653493498478; Wed, 25 May 2022 08:44:58 -0700 (PDT) Date: Wed, 25 May 2022 08:44:39 -0700 In-Reply-To: <20220525154442.1438081-1-dlatypov@google.com> Message-Id: <20220525154442.1438081-2-dlatypov@google.com> Mime-Version: 1.0 References: <20220525154442.1438081-1-dlatypov@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [RFC PATCH 1/4] rfc: kunit: remove format func from struct kunit_assert, get it to 0 bytes From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: ojeda@kernel.org, linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, 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 --- 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..aab5b1147df9 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 8ffcd7de9607..6730734a2216 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -509,9 +509,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; \ @@ -519,6 +520,7 @@ void kunit_do_failed_assertion(struct kunit *test, &__loc, \ assert_type, \ &__assertion.assert, \ + assert_format, \ fmt, \ ##__VA_ARGS__); \ } \ @@ -530,7 +532,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 @@ -561,6 +564,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, \ @@ -618,8 +622,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, \ @@ -676,8 +680,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, \ @@ -696,6 +700,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 a5053a07409f..3845765e3a80 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -245,7 +245,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 @@ -261,7 +261,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 @@ -285,6 +285,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; @@ -294,7 +295,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.36.1.124.g0e6072fb45-goog From nobody Tue May 5 10:02:26 2026 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 ACD06C433EF for ; Wed, 25 May 2022 15:45:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245230AbiEYPpS (ORCPT ); Wed, 25 May 2022 11:45:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245258AbiEYPpC (ORCPT ); Wed, 25 May 2022 11:45:02 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 472B78DDED for ; Wed, 25 May 2022 08:45:01 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id i204-20020a2522d5000000b006524ae5d7d7so4960065ybi.16 for ; Wed, 25 May 2022 08:45:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=LVwK1TWS2ZAb7Kfk7FPrer/dNWd3IkmkzZFa2JkAqMQ=; b=CuaSdqC5/5PGcYkkjh3I3OulmQD6xBIMr/i67/rl3Sr5cF5QK7OG8SNCHSejBCSiIk nYuKyrdDBQG9TzVOPyZnfHKbYR9ZLYhuVYNx+/hI9J/p96Pm4rG0hwKBSVm7JiSmDaAJ 8oQuniLZV712jAH7Hi5EUL00vQrXjmDGHht6C05HzTKBbO5uPqQGd9JMfZF364rkA4bo HRlCpE905u+xVUd3l+C00OV5QSoY7s3/GKn8e/BkktXg9ZZUceVASl/sjBPF3IbrbGss ff9PXEEjt9LgkVCED7YN2NL50v/EqNmcj0ywVqewWziWGuo7Mqm/EGeV0tujugQwnhBu Ebyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=LVwK1TWS2ZAb7Kfk7FPrer/dNWd3IkmkzZFa2JkAqMQ=; b=5gUvsYs8u3M0KYBBlOawYYf+0S+uYdFlhynT72Cppn//szlNb8ZYsCFEkXOk78vi8d CLqVDmJpxZAXlzLsA745EOZ1suJxv3fwRJI+pypeBFiIScHkPjZ9Sl60q5Swks1IGF02 q1aSE5q3FvZDDFZqUn6CP7t01UdxwmuosMInzxLNGPM89Wm6Z2HuVQQNFD+wmTq9FscH CE/oVXtSMRNETyk6UhxMeYPMnSoaZQ9ejGoX9XgqGAkts5c+/tyeSDeVl0sXytRfytDv K/Pvyq+tXAIsJQPkehthCMZ97EI10GLrfOpf4XRn/WvKsY+IOTyT34Lh1kIqchV1vCzs uEJw== X-Gm-Message-State: AOAM532dXDWn1cCL3axCeEU+bOWeH3Xv87KgT2G1AcrA6KOujQsFb9Fi zCEdHuiG5aB3B3CS83TeXJZzSo5d2/jHHw== X-Google-Smtp-Source: ABdhPJyw+1yei1o/kJT6QO5M4hmlZq8hDTO/YxwP2PZ0T2KZlMVtxI8Ats6BjBJ/yA1EIyPvhA5QvyRZ05/BzQ== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:5bba:b161:2a8a:2ddd]) (user=dlatypov job=sendgmr) by 2002:a05:6902:143:b0:628:7cf1:f2a9 with SMTP id p3-20020a056902014300b006287cf1f2a9mr30129754ybh.51.1653493500535; Wed, 25 May 2022 08:45:00 -0700 (PDT) Date: Wed, 25 May 2022 08:44:40 -0700 In-Reply-To: <20220525154442.1438081-1-dlatypov@google.com> Message-Id: <20220525154442.1438081-3-dlatypov@google.com> Mime-Version: 1.0 References: <20220525154442.1438081-1-dlatypov@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [RFC PATCH 2/4] rfc: kunit: rename base KUNIT_ASSERTION macro to _KUNIT_FAILED From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: ojeda@kernel.org, linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, 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 --- include/kunit/test.h | 119 ++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 58 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 6730734a2216..4a7cb7e72660 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -512,30 +512,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. @@ -560,15 +557,18 @@ 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, \ @@ -618,16 +618,17 @@ 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, \ @@ -676,16 +677,18 @@ 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, \ @@ -696,15 +699,15 @@ 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 (likely(!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.36.1.124.g0e6072fb45-goog From nobody Tue May 5 10:02:26 2026 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 C94F1C433F5 for ; Wed, 25 May 2022 15:45:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245253AbiEYPpe (ORCPT ); Wed, 25 May 2022 11:45:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245269AbiEYPpE (ORCPT ); Wed, 25 May 2022 11:45:04 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D87B31378 for ; Wed, 25 May 2022 08:45:03 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2eb7d137101so181960697b3.12 for ; Wed, 25 May 2022 08:45:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=4eDRE9vOcSvYw9kUli1P4DtZ7qFlBTNAI5FAWRDONxU=; b=LMW44NeuqR1j1pXQTHLqvxCFNDEdiCI2W3fPbAApJUpfne2MXxxcczCTIoIh92KC6T cAP4VCEG2p26KRK99hP2ulco6gkPKJ5lMcNOCWHXLuIaM0XC8J4ScZVI9dIt5c5/lVyH 4N0qZ9JmvRRM4O7B/qRFgq2xKzHC+BGKKIdkRVdtkiXgE2j8Aw5V8yNcK1zZvbGlE8b+ FZlI19SfyleEMUprMmPSF5ExN0jst0IY6CugjgTJYzejHKBxVLT+6tZ68v8xrpZm7eXt xxmWTY3IRWs6kq/QgGtNgtzhq3My5/oXaujGaa7mf4aNAdqkiaO6mPA969anN4pj2zKB Gncg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=4eDRE9vOcSvYw9kUli1P4DtZ7qFlBTNAI5FAWRDONxU=; b=oxuQv6cjG2OIf8WPCpF0b1+tXkxFMZvMrsQceG5BeIpQEbOGYyuO246JvDPoTTdCte uqX//xP5EauYqjZBEYbBsslM9IFoqEbIQ7IehtKxm1cN3IarMC/wXMKZyXqh5ihes7v/ 4aye8XXZT7BK4dSWBp2uHHn26rK2uzV/SLbJjUC01/OyhI8Io8l6HN0AgkHf8tvUuD5h FC/fSdpPymlotzscvr/f08iNSpPE6WyLmSdldEuBf4kQF+5/cy6ZIGelAl2pN7eIktUZ 7qfp9m/JF0NvfOzwjyWBpHoA3G/voJ1wEkZXri781bnKqeqozZe+JjAn3camlzIOkz+V mLwg== X-Gm-Message-State: AOAM533aMiXPUzem+bo9x3+2FUbgtDj+8UgEqu7Xx/5bMMZpbQuYumLs MIG9AU1dZix0+hxSBpZ8chnkLep6n8+JlQ== X-Google-Smtp-Source: ABdhPJy02E3IASmDV51uvh2PP3kPGRgtRVl8uhEsV0zg9rJKXXVuF8UZbRWcT68XWym8eJpclcJy22zDgBDAKg== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:5bba:b161:2a8a:2ddd]) (user=dlatypov job=sendgmr) by 2002:a25:aad0:0:b0:655:8404:34ff with SMTP id t74-20020a25aad0000000b00655840434ffmr5619462ybi.299.1653493502610; Wed, 25 May 2022 08:45:02 -0700 (PDT) Date: Wed, 25 May 2022 08:44:41 -0700 In-Reply-To: <20220525154442.1438081-1-dlatypov@google.com> Message-Id: <20220525154442.1438081-4-dlatypov@google.com> Mime-Version: 1.0 References: <20220525154442.1438081-1-dlatypov@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [RFC PATCH 3/4] rfc: kunit: eliminate KUNIT_INIT_*_ASSERT_STRUCT macros From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: ojeda@kernel.org, linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, 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 } Signed-off-by: Daniel Latypov --- 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 aab5b1147df9..c041cd9fb0d4 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 4a7cb7e72660..e4b54f5d2731 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -551,21 +551,24 @@ 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)) break; \ + 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=3Dexpected_true_), \ fmt, \ ##__VA_ARGS__); \ } while(0) @@ -624,9 +627,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) @@ -684,9 +687,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) @@ -705,7 +708,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.36.1.124.g0e6072fb45-goog From nobody Tue May 5 10:02:26 2026 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 AA682C433EF for ; Wed, 25 May 2022 15:45:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245245AbiEYPpj (ORCPT ); Wed, 25 May 2022 11:45:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245280AbiEYPpF (ORCPT ); Wed, 25 May 2022 11:45:05 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFED18A302 for ; Wed, 25 May 2022 08:45:04 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-2d7eaa730d9so182014427b3.13 for ; Wed, 25 May 2022 08:45:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=8crKctF/FapgbNwtKywxdiRKxJS9KDB6fu8ZFm9leIY=; b=HD0dnRcINZlIc28mvYS69ByWX+qQxZpdVeIizu3L/fs8eMaiizEOW5O0gY2kNm3R0L yaItTi+yrlaV/PsjehHgrVKTSmRgEJO4r018YLG1lc09l74nejdM+P5GAVaqqujGtJ3t ThxEHpfFz4jWZVGaFigAq5VT+n0jZiiFoiEUJcjVwxYh51J2YTMl7v/3EwOJFNsDgupy Cw4cgTSpASYp8AyzW/7xuF1dabTAcwmQSNAW87x3FjCYiLAamLU8Dr0UVMmK1kgDKpBZ aQx17405zIqpx6smduoBe0YD6+OGooQSsZzEC6t73mOAgOEyirzHAL/D7cqEnhaqF0/r 2amA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=8crKctF/FapgbNwtKywxdiRKxJS9KDB6fu8ZFm9leIY=; b=y6lIWus2lx3D5ir68u9/SlVwd5upMTJx8jg/1zeqfAJmlWtO5N1a/rxkE44LZoz8Mb a1cIZZ+0lhYwQwR4OaxpjJxWQ4FOqOfxQYd2jnLDkW1JNzhS+/pOppDh4Vq0tMIWFfjx 62x7o9Du6tqsWIrRae5/Xz5h5cKtBmrxzqI7+3X87wFjhGDti9cTLiGUFzUI8wb1fald xCGypwcHrxdIBGG6EJ9D3PXGQdN9E/u60wWouuX9QyvQI9OfIklbydFYZaxdwJfkkBoH vS2RmAMSZs2+iqtxZH+aNR81GQ5cdx2xdG0sl0LksHdZmrAEarxX8u1PvuWsLNgPJiol 6ogA== X-Gm-Message-State: AOAM5324q4U63mXZ+ZmIodadf8ODRJizy7Qlrj/pOCmiZ1qkwL9BVKQc VZ2TzSGjDYLWXAJYznwt2DyeckdGB7wjiw== X-Google-Smtp-Source: ABdhPJxILJCqLH0vLgrdn2WPeC0lY9gp5yI55a6IR4a66YCH2wlHlcpCY79q19ipw1SsuXvLeiLMV66R9sTbIQ== X-Received: from dlatypov.svl.corp.google.com ([2620:15c:2cd:202:5bba:b161:2a8a:2ddd]) (user=dlatypov job=sendgmr) by 2002:a0d:d503:0:b0:300:d3cb:cee1 with SMTP id x3-20020a0dd503000000b00300d3cbcee1mr1127476ywd.368.1653493504657; Wed, 25 May 2022 08:45:04 -0700 (PDT) Date: Wed, 25 May 2022 08:44:42 -0700 In-Reply-To: <20220525154442.1438081-1-dlatypov@google.com> Message-Id: <20220525154442.1438081-5-dlatypov@google.com> Mime-Version: 1.0 References: <20220525154442.1438081-1-dlatypov@google.com> X-Mailer: git-send-email 2.36.1.124.g0e6072fb45-goog Subject: [RFC PATCH 4/4] kunit: delcare kunit_assert structs as const From: Daniel Latypov To: brendanhiggins@google.com, davidgow@google.com Cc: ojeda@kernel.org, linux-kernel@vger.kernel.org, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, 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 https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git/c= ommit/?h=3Dkunit&id=3D7466886b400b1904ce30fa311904849e314a2cf4 So now let's properly declare the structs as const as well. --- 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 e4b54f5d2731..fa5955c8b412 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -514,7 +514,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.36.1.124.g0e6072fb45-goog