public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Pedro Alves <pedro@palves.net>, Tom Tromey <tom@tromey.com>,
	Simon Marchi via Gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH 4/4] gdb: make internalvar use a variant
Date: Tue, 15 Mar 2022 22:06:05 -0400	[thread overview]
Message-ID: <81442d2a-055a-961a-c804-ed743bc04c72@polymtl.ca> (raw)
In-Reply-To: <205f0a37-e832-2e25-32b6-266acc33753d@palves.net>

> It requires more C++ expertise than most of other things to know to write such code.  Something that should be
> simple is complicated.  C++ is really lacking proper pattern matching and builtin variant types, to make
> variants easy to use.  But if we use it in a contained way like basically glorified tagged union to get rid
> of some boilerplace we have to write today,  I guess it's OK.

Yes, that was my thought.  The intent is replacing those instances of
"enum kind + union data" with something that can hold non-trivial
objects, calls constructors / destructors on data fields automatically,
validates that we don't access a wrong (inactive) field.

That being said, it's likely that many instances of this pattern are in
structures that aren't even new'ed / delete'd today, so it's not as if
we could use variants in them tomorrow.

> How debuggeable is nonstd::variant?  When printing a tagged union in GDB, it's easy to figure out the
> union's current value.  What does printing a nonstd::variant (not mapped to the std variant) look
> like? 

With nonstd:


$1 = {
  next = 0x60600000ff20,
  name = 0x602000085330 "salut",
  v = {
    data = {
      __data = '\276' <repeats 32 times>,
      __align = {<No data fields>}
    },
    type_index = 0 '\000'
  }
}

With std:


$1 = {
  next = 0x60600000ff20,
  name = 0x602000085350 "salut",
  v = std::variant<internalvar_void, internalvar_value, internalvar_make_value, internalvar_function, internalvar_integer, internalvar_string> [index 0] = {{<No data fields>}}
}

> AFAICT from variant.cpp source file, the variant storage is an untyped buffer:
> 
>     enum { data_align = detail::typelist_max_alignof< variant_types >::value };
> 
>     using aligned_storage_t = typename std::aligned_storage< data_size, data_align >::type;
>     aligned_storage_t data;
> 
> so I guess printing a nonstd::variant results in pretty opaque output.  We'd need a pretty
> printer to fix this.  Or maybe we just assume that people developing/debugging GDB build
> it against a C++17 or higher compiler?  (Not sure that's a great assumption.)

Do you know off-hand if the std::variant pretty printer is supposed to
show the active data field?

> I wonder whether we should do:
> 
>  namespace gdb {
>    using namespace nonstd;
>  }
> 
> and then use gdb::variant throughout, instead of "nonstd::variant".  At least "gdb"
> is three letters, and thus less code/indentation churn when we someday switch to "std".
> Also less churn if we move to some other variant type, though not sure that's likely
> after we start using one.

Good idea.

But now I'm not even convinced myself of the usefulness of having an
std::variant-like in the tree.  The internalvar change is really just an
example, but it doesn't really need changing (and Tom suggeted what's
probably a better way to change it if we wanted to).  I wanted to use a
variant in some TUI patch that I started, but haven't finished.  So, I
guess we have it here as a reference if the need ever comes up again.

Simon

  reply	other threads:[~2022-03-16  2:06 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-01 14:07 [PATCH 0/4] Add variant type Simon Marchi
2022-02-01 14:07 ` [PATCH 1/4] gdb: remove internalvar_funcs::destroy Simon Marchi
2022-03-04 16:15   ` Tom Tromey
2022-03-06 16:33     ` Simon Marchi
2022-02-01 14:07 ` [PATCH 2/4] gdb: constify parameter of value_copy Simon Marchi
2022-03-04 16:16   ` Tom Tromey
2022-03-06 16:33     ` Simon Marchi
2022-02-01 14:07 ` [PATCH 3/4] gdbsupport: add variant-lite header Simon Marchi
2022-02-01 14:07 ` [PATCH 4/4] gdb: make internalvar use a variant Simon Marchi
2022-03-04 16:23   ` Tom Tromey
2022-03-07 12:12     ` Pedro Alves
2022-03-16  2:06       ` Simon Marchi [this message]
2022-03-16 13:26         ` Pedro Alves
2022-03-16 13:28           ` Simon Marchi
2022-02-03  0:02 ` [PATCH 0/4] Add variant type Andrew Burgess
2022-02-03  1:32   ` Simon Marchi
2022-02-04 12:44     ` Andrew Burgess
2022-02-04 13:19       ` 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=81442d2a-055a-961a-c804-ed743bc04c72@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    --cc=tom@tromey.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).