forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kunit: split out part of kunit_assert into a static const
This is per Linus's suggestion in [1]. The issue there is that every KUNIT_EXPECT/KUNIT_ASSERT puts a kunit_assert object onto the stack. Normally we rely on compilers to elide this, but when that doesn't work out, this blows up the stack usage of kunit test functions. We can move some data off the stack by making it static. This change introduces a new `struct kunit_loc` to hold the file and line number and then just passing assert_type (EXPECT or ASSERT) as an argument. In [1], it was suggested to also move out the format string as well, but users could theoretically craft a format string at runtime, so we can't. This change leaves a copy of `assert_type` in kunit_assert for now because cleaning up all the macros to not pass it around is a bit more involved. Here's an example of the expanded code for KUNIT_FAIL(): if (__builtin_expect(!!(!(false)), 0)) { static const struct kunit_loc loc = { .file = ... }; struct kunit_fail_assert __assertion = { .assert = { .type ... }; kunit_do_failed_assertion(test, &loc, KUNIT_EXPECTATION, &__assertion.assert, ...); }; [1] https://groups.google.com/g/kunit-dev/c/i3fZXgvBrfA/m/VULQg1z6BAAJ Signed-off-by: Daniel Latypov <[email protected]> Suggested-by: Linus Torvalds <[email protected]> Reviewed-by: David Gow <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
- Loading branch information
Showing
4 changed files
with
42 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,21 +28,31 @@ enum kunit_assert_type { | |
KUNIT_EXPECTATION, | ||
}; | ||
|
||
/** | ||
* struct kunit_loc - Identifies the source location of a line of code. | ||
* @line: the line number in the file. | ||
* @file: the file name. | ||
*/ | ||
struct kunit_loc { | ||
int line; | ||
const char *file; | ||
}; | ||
|
||
#define KUNIT_CURRENT_LOC { .file = __FILE__, .line = __LINE__ } | ||
|
||
/** | ||
* struct kunit_assert - Data for printing a failed assertion or expectation. | ||
* @type: the type (either an expectation or an assertion) of this kunit_assert. | ||
* @line: the source code line number that the expectation/assertion is at. | ||
* @file: the file path of the source file that the expectation/assertion is in. | ||
* @message: an optional message to provide additional context. | ||
* @format: a function which formats the data in this kunit_assert to a string. | ||
* | ||
* Represents a failed expectation/assertion. Contains all the data necessary to | ||
* format a string to a user reporting the failure. | ||
*/ | ||
struct kunit_assert { | ||
// TODO([email protected]): delete this unused field when we've | ||
// updated all the related KUNIT_INIT_ASSERT* macros. | ||
enum kunit_assert_type type; | ||
int line; | ||
const char *file; | ||
struct va_format message; | ||
void (*format)(const struct kunit_assert *assert, | ||
struct string_stream *stream); | ||
|
@@ -65,14 +75,13 @@ struct kunit_assert { | |
*/ | ||
#define KUNIT_INIT_ASSERT_STRUCT(assert_type, fmt) { \ | ||
.type = assert_type, \ | ||
.file = __FILE__, \ | ||
.line = __LINE__, \ | ||
.message = KUNIT_INIT_VA_FMT_NULL, \ | ||
.format = fmt \ | ||
} | ||
|
||
void kunit_base_assert_format(const struct kunit_assert *assert, | ||
struct string_stream *stream); | ||
void kunit_assert_prologue(const struct kunit_loc *loc, | ||
enum kunit_assert_type type, | ||
struct string_stream *stream); | ||
|
||
void kunit_assert_print_msg(const struct kunit_assert *assert, | ||
struct string_stream *stream); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters