Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: Simon Marchi <simon.marchi@efficios.com>,
	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 13:08:44 +0100	[thread overview]
Message-ID: <82b38316-d9c9-49ae-916a-40f1ced84dcc@suse.de> (raw)
In-Reply-To: <20261019165929.792697-6-simon.marchi@efficios.com>

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);
...

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:
...
$ 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
...

Thanks,
- Tom

  parent reply	other threads:[~2026-11-20 12:09 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 [this message]
2026-11-21  4:12     ` Simon Marchi
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=82b38316-d9c9-49ae-916a-40f1ced84dcc@suse.de \
    --to=tdevries@suse.de \
    --cc=gcc-patches@sourceware.org \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    /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