Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@efficios.com>
To: Tom de Vries <tdevries@suse.de>,
	gcc-patches@sourceware.org, gdb-patches@sourceware.org
Subject: Re: [PATCH 5/6] gdbsupport/selftest: print operand values when a SELF_CHECK fails
Date: Fri, 20 Nov 2026 23:12:59 -0500	[thread overview]
Message-ID: <88fed688-769b-4599-8021-87cd97b809a6@efficios.com> (raw)
In-Reply-To: <82b38316-d9c9-49ae-916a-40f1ced84dcc@suse.de>

On 11/20/26 7:08 AM, Tom de Vries wrote:
> On 10/19/26 6:58 PM, Simon Marchi wrote:
>>   - A top-level && or ||.
>>
>>     Overloading these operators would lose short-circuit evaluation, and
>>     only the leftmost operand could be decomposed anyway (because
>>     comparison operators take precedence).  Options to work around this
>>     are to split the expressions (in the case of &&) or to add an extra
>>     pair of parentheses around the whole expression.  For &&, I would
>>     argue it's a good thing to split expressions, so you get a more
>>     fine-grained message on failure.
> 
> About splitting "&&" expressions (copied from a WIP patch):
> ----------------------------------------------------------------------
> There are two types of self checks using "A && B".
> 
> The first is where A has a value, and B only has a value if A is true:
> ...
> bool
> assign_three_to_b (int *p)
> {
>   if (p == nullptr)
>     return false;
>   *p = 3;
>   return true;
> }
> SELF_CHECK (assign_three_to_b (&b) && b == 3);
> ...
> 
> In this case, the short-circuit aspect of "&&" is relevant.
> 
> In case of failure, it's not clear whether A or B failed.
> 
> Fix this by rewriting this case:
> ...
> SELF_CHECK (assign_three_to_b (&b) && b == 3);
> ...
> into:
> ...
> SELF_CHECK (assign_three_to_b (&b));
> SELF_CHECK (b == 3);
> ...

I think the latter is better indeed, so you can know which side failed.

> The second type is where both A and B have a value:
> ...
> SELF_CHECK (a == 1 && b == 3);
> ...
> 
> In this case, the short-circuit aspect of "&&" is not relevant.
> 
> Again, in case of failure, it's not clear whether A or B failed.
> 
> And rewriting into:
> ...
> SELF_CHECK (a == 1);
> SELF_CHECK (b == 3);
> ...
> means that if the first self-check fails, we don't know whether the second self-check would fail as well.
> ----------------------------------------------------------------------
> 
> The WIP patch contains a macro SELF_CHECK_AND that can be used to do "SELF_CHECK_AND (a == 1, b == 3)" and reports the value of each individual condition in case of failure:

Which patch are you talking about, a patch of yours?

I think this is getting a bit too specialized.  How often would it
really be useful?

> ...
> $ gdb -q -batch -ex "maint selftest <...>"
> Running selftest <...>.
> a == 1: false
> b == 3: true
> Self test failed: self-test &&: a == 1, b == 3 failed at foo.c:123
> ...
> 
> IWBN to have something more c++-like rather than a macro.
> 
> So I wondered, where you do:
> ...
> -    SELF_CHECK (major == 2 && minor == 39);
> +    SELF_CHECK (major == 2);
> +    SELF_CHECK (minor == 39);
> ...
> could you do instead:
> ...
>     SELF_CHECK ((major == 2) & (minor == 39));
> ...
> and get both values reported ?
> 
> I tried this but got an error:
> ...
> error: no match for ‘operator&’ (operand types are ‘selftests::detail::lhs_holder<bool>’ and ‘bool’)
>   702 |     SELF_CHECK ((a == 2) & (b == 2));
>       |                          ^ ~~~~~~~~
>       |                               |
>       |                               bool
> ...

Yeah well you'd have to implement the operator, but you hit the same
problem as stated in the commit message.  By the time an operator& (or
operator&&) would run, the right-hand side would already be evaluated
and the operator would just see a boolean.

Personally I don't think that's a problem, because we don't lose
anything compared to today, there are just some corner cases where the
"magic" can't happen, which is not the end of the world.

Note that I didn't invent anything here, those are the same limitations
as the Catch2 library [1], written by people far smarter than I am (if
it were just me, we'd just import the Catch2 library and use it).

Simon

[1] https://catch2.org/

  reply	other threads:[~2026-11-21  4:13 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-19 16:58 [PATCH 0/6] Make SELF_CHECK print operands of comparisons Simon Marchi
2026-10-19 16:58 ` [PATCH 1/6] gdb/unittests: catch gdb_exception_error in scoped_mmap test Simon Marchi
2026-11-20  8:24   ` Tom de Vries
2026-10-19 16:58 ` [PATCH 2/6] gdbsupport/selftest: print expression when selftest fails and re-format Simon Marchi
2026-11-20  9:02   ` Tom de Vries
2026-10-19 16:58 ` [PATCH 3/6] include/diagnostics.h: add DIAGNOSTIC_IGNORE_PARENTHESES Simon Marchi
2026-11-20  9:09   ` Tom de Vries
2026-10-19 16:58 ` [PATCH 4/6] gdb/gmp-utils: rename gdb_mp{z,q,f}::str to to_string Simon Marchi
2026-11-20  9:58   ` [PATCH 4/6] gdb/gmp-utils: rename gdb_mp{z, q, f}::str " Tom de Vries
2026-10-19 16:58 ` [PATCH 5/6] gdbsupport/selftest: print operand values when a SELF_CHECK fails Simon Marchi
2026-10-27 14:13   ` Tom Tromey
2026-10-27 17:06     ` Simon Marchi
2026-11-20 12:08   ` Tom de Vries
2026-11-21  4:12     ` Simon Marchi [this message]
2026-10-19 16:58 ` [PATCH 6/6] gdb: remove manual printing of values in some selftests Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=88fed688-769b-4599-8021-87cd97b809a6@efficios.com \
    --to=simon.marchi@efficios.com \
    --cc=gcc-patches@sourceware.org \
    --cc=gdb-patches@sourceware.org \
    --cc=tdevries@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox